Skip to content
Permalink
Browse files

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 #846.
  • Loading branch information
rdb committed Jan 25, 2020
1 parent 6c66c86 commit 89f1a0e63c7c0c01562f58e05712dfe393d21b66

Large diffs are not rendered by default.

@@ -336,10 +336,12 @@ class EXPCL_PANDA_DISPLAY GraphicsStateGuardian : public GraphicsStateGuardianBa

virtual void clear(DrawableRegion *clearable);

const LMatrix4 *fetch_specified_value(Shader::ShaderMatSpec &spec, int altered);
const LMatrix4 *fetch_specified_part(Shader::ShaderMatInput input, InternalName *name,
LMatrix4 &t, int index);
const LMatrix4 *fetch_specified_member(const NodePath &np, CPT_InternalName member, LMatrix4 &t);
void update_shader_matrix_cache(Shader *shader, LMatrix4 *cache, int altered);
const LMatrix4 *fetch_specified_value(Shader::ShaderMatSpec &spec, const LMatrix4 *cache, int altered);
void fetch_specified_part(Shader::ShaderMatInput input, InternalName *name,
LMatrix4 *into, int count = 1);
void fetch_specified_member(const NodePath &np, CPT_InternalName member,
LMatrix4 &t);
PT(Texture) fetch_specified_texture(Shader::ShaderTexSpec &spec,
SamplerState &sampler, int &view);
const Shader::ShaderPtrData *fetch_ptr_parameter(const Shader::ShaderPtrSpec& spec);
@@ -74,6 +74,8 @@ DXShaderContext9(Shader *s, GSG *gsg) : ShaderContext(s) {
}
}
#endif

_mat_part_cache = new LMatrix4[s->cp_get_mat_cache_size()];
}

/**
@@ -92,6 +94,8 @@ DXShaderContext9::
delete _vertex_element_array;
_vertex_element_array = nullptr;
}

delete[] _mat_part_cache;
}

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

for (size_t i = 0; i < _shader->_mat_spec.size(); ++i) {
Shader::ShaderMatSpec &spec = _shader->_mat_spec[i];
if (altered & _shader->_mat_deps) {
gsg->update_shader_matrix_cache(_shader, _mat_part_cache, altered);

for (Shader::ShaderMatSpec &spec : _shader->_mat_spec) {
if ((altered & spec._dep) == 0) {
continue;
}

if (altered & (spec._dep[0] | spec._dep[1])) {
CGparameter p = _cg_parameter_map[spec._id._seqno];
if (p == nullptr) {
continue;
}
const LMatrix4 *val = gsg->fetch_specified_value(spec, altered);

const LMatrix4 *val = gsg->fetch_specified_value(spec, _mat_part_cache, altered);
if (val) {
HRESULT hr;
PN_stdfloat v [4];
@@ -87,6 +87,8 @@ class EXPCL_PANDADX DXShaderContext9 : public ShaderContext {
pvector <CGparameter> _cg_parameter_map;
#endif

LMatrix4 *_mat_part_cache = nullptr;

private:
void release_resources(void);

@@ -338,6 +338,8 @@ CLP(CgShaderContext)(CLP(GraphicsStateGuardian) *glgsg, Shader *s) : ShaderConte
}
}

_mat_part_cache = new LMatrix4[_shader->cp_get_mat_cache_size()];

_glgsg->report_my_gl_errors();
}

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

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

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

if ((altered & (spec._dep[0] | spec._dep[1])) == 0) {
for (Shader::ShaderMatSpec &spec : _shader->_mat_spec) {
if ((altered & spec._dep) == 0) {
continue;
}

const LMatrix4 *val = _glgsg->fetch_specified_value(spec, altered);
const LMatrix4 *val = _glgsg->fetch_specified_value(spec, _mat_part_cache, altered);
if (!val) continue;
const PN_stdfloat *data = val->get_data();

@@ -75,6 +75,7 @@ class EXPCL_GL CLP(CgShaderContext) final : public ShaderContext {
long _transform_table_size;
long _slider_table_size;

LMatrix4 *_mat_part_cache = nullptr;
pvector<CGparameter> _cg_parameter_map;

WCPT(RenderState) _state_rs;

0 comments on commit 89f1a0e

Please sign in to comment.
You can’t perform that action at this time.