Skip to content

Commit a1c5d83

Browse files
committed
shader: separate shader matrix input cache from ShaderMatSpec
If multiple ShaderMatSpec entries use the same state matrix, this should result in a reduction in the number of times that state matrix is fetched. This is especially so for arrays, which are now fetched once rather than once for every item. This is the first step towards trying to solve panda3d#846.
1 parent 6c66c86 commit a1c5d83

11 files changed

+595
-504
lines changed

panda/src/display/graphicsStateGuardian.cxx

Lines changed: 383 additions & 323 deletions
Large diffs are not rendered by default.

panda/src/display/graphicsStateGuardian.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,12 @@ class EXPCL_PANDA_DISPLAY GraphicsStateGuardian : public GraphicsStateGuardianBa
336336

337337
virtual void clear(DrawableRegion *clearable);
338338

339-
const LMatrix4 *fetch_specified_value(Shader::ShaderMatSpec &spec, int altered);
340-
const LMatrix4 *fetch_specified_part(Shader::ShaderMatInput input, InternalName *name,
341-
LMatrix4 &t, int index);
342-
const LMatrix4 *fetch_specified_member(const NodePath &np, CPT_InternalName member, LMatrix4 &t);
339+
void update_shader_matrix_cache(Shader *shader, LMatrix4 *cache, int altered);
340+
const LMatrix4 *fetch_specified_value(Shader::ShaderMatSpec &spec, const LMatrix4 *cache, int altered);
341+
void fetch_specified_part(Shader::ShaderMatInput input, InternalName *name,
342+
LMatrix4 *into, int count = 1);
343+
void fetch_specified_member(const NodePath &np, CPT_InternalName member,
344+
LMatrix4 &t);
343345
PT(Texture) fetch_specified_texture(Shader::ShaderTexSpec &spec,
344346
SamplerState &sampler, int &view);
345347
const Shader::ShaderPtrData *fetch_ptr_parameter(const Shader::ShaderPtrSpec& spec);

panda/src/dxgsg9/dxShaderContext9.cxx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ DXShaderContext9(Shader *s, GSG *gsg) : ShaderContext(s) {
7474
}
7575
}
7676
#endif
77+
78+
_mat_part_cache = new LMatrix4[s->cp_get_mat_cache_size()];
7779
}
7880

7981
/**
@@ -92,6 +94,8 @@ DXShaderContext9::
9294
delete _vertex_element_array;
9395
_vertex_element_array = nullptr;
9496
}
97+
98+
delete[] _mat_part_cache;
9599
}
96100

97101
/**
@@ -232,15 +236,20 @@ issue_parameters(GSG *gsg, int altered) {
232236
}
233237
}
234238

235-
for (size_t i = 0; i < _shader->_mat_spec.size(); ++i) {
236-
Shader::ShaderMatSpec &spec = _shader->_mat_spec[i];
239+
if (altered & _shader->_mat_deps) {
240+
gsg->update_shader_matrix_cache(_shader, _mat_part_cache, altered);
241+
242+
for (Shader::ShaderMatSpec &spec : _shader->_mat_spec) {
243+
if ((altered & spec._dep) == 0) {
244+
continue;
245+
}
237246

238-
if (altered & (spec._dep[0] | spec._dep[1])) {
239247
CGparameter p = _cg_parameter_map[spec._id._seqno];
240248
if (p == nullptr) {
241249
continue;
242250
}
243-
const LMatrix4 *val = gsg->fetch_specified_value(spec, altered);
251+
252+
const LMatrix4 *val = gsg->fetch_specified_value(spec, _mat_part_cache, altered);
244253
if (val) {
245254
HRESULT hr;
246255
PN_stdfloat v [4];

panda/src/dxgsg9/dxShaderContext9.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class EXPCL_PANDADX DXShaderContext9 : public ShaderContext {
8787
pvector <CGparameter> _cg_parameter_map;
8888
#endif
8989

90+
LMatrix4 *_mat_part_cache = nullptr;
91+
9092
private:
9193
void release_resources(void);
9294

panda/src/glstuff/glCgShaderContext_src.cxx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@ CLP(CgShaderContext)(CLP(GraphicsStateGuardian) *glgsg, Shader *s) : ShaderConte
338338
}
339339
}
340340

341+
_mat_part_cache = new LMatrix4[_shader->cp_get_mat_cache_size()];
342+
341343
_glgsg->report_my_gl_errors();
342344
}
343345

@@ -347,6 +349,7 @@ CLP(CgShaderContext)(CLP(GraphicsStateGuardian) *glgsg, Shader *s) : ShaderConte
347349
CLP(CgShaderContext)::
348350
~CLP(CgShaderContext)() {
349351
// Don't call release_resources; we may not have an active context.
352+
delete[] _mat_part_cache;
350353
}
351354

352355
/**
@@ -690,14 +693,14 @@ issue_parameters(int altered) {
690693
}
691694

692695
if (altered & _shader->_mat_deps) {
693-
for (int i = 0; i < (int)_shader->_mat_spec.size(); ++i) {
694-
Shader::ShaderMatSpec &spec = _shader->_mat_spec[i];
696+
_glgsg->update_shader_matrix_cache(_shader, _mat_part_cache, altered);
695697

696-
if ((altered & (spec._dep[0] | spec._dep[1])) == 0) {
698+
for (Shader::ShaderMatSpec &spec : _shader->_mat_spec) {
699+
if ((altered & spec._dep) == 0) {
697700
continue;
698701
}
699702

700-
const LMatrix4 *val = _glgsg->fetch_specified_value(spec, altered);
703+
const LMatrix4 *val = _glgsg->fetch_specified_value(spec, _mat_part_cache, altered);
701704
if (!val) continue;
702705
const PN_stdfloat *data = val->get_data();
703706

panda/src/glstuff/glCgShaderContext_src.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class EXPCL_GL CLP(CgShaderContext) final : public ShaderContext {
7575
long _transform_table_size;
7676
long _slider_table_size;
7777

78+
LMatrix4 *_mat_part_cache = nullptr;
7879
pvector<CGparameter> _cg_parameter_map;
7980

8081
WCPT(RenderState) _state_rs;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy