Skip to content

Commit 511b0c1

Browse files
committed
ShaderGenerator: don't generate a shader per Material
1 parent 40c6f41 commit 511b0c1

File tree

6 files changed

+99
-73
lines changed

6 files changed

+99
-73
lines changed

panda/src/gobj/material.h

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,7 @@ class EXPCL_PANDA_GOBJ Material : public TypedWritableReferenceCount, public Nam
127127
MAKE_PROPERTY(local, get_local, set_local);
128128
MAKE_PROPERTY(twoside, get_twoside, set_twoside);
129129

130-
private:
131-
LColor _base_color;
132-
LColor _ambient;
133-
LColor _diffuse;
134-
LColor _specular;
135-
LColor _emission;
136-
PN_stdfloat _shininess;
137-
PN_stdfloat _roughness;
138-
PN_stdfloat _metallic;
139-
PN_stdfloat _refractive_index;
140-
141-
static PT(Material) _default;
142-
130+
public:
143131
enum Flags {
144132
F_ambient = 0x001,
145133
F_diffuse = 0x002,
@@ -153,8 +141,24 @@ class EXPCL_PANDA_GOBJ Material : public TypedWritableReferenceCount, public Nam
153141
F_base_color = 0x200,
154142
F_refractive_index = 0x400,
155143
};
144+
145+
private:
146+
LColor _base_color;
147+
LColor _ambient;
148+
LColor _diffuse;
149+
LColor _specular;
150+
LColor _emission;
151+
PN_stdfloat _shininess;
152+
PN_stdfloat _roughness;
153+
PN_stdfloat _metallic;
154+
PN_stdfloat _refractive_index;
155+
156+
static PT(Material) _default;
157+
156158
int _flags;
157159

160+
friend class MaterialAttrib;
161+
158162
public:
159163
static void register_with_read_factory();
160164
virtual void write_datagram(BamWriter *manager, Datagram &me);

panda/src/pgraph/materialAttrib.I

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Use MaterialAttrib::make() to construct a new MaterialAttrib object.
1616
*/
1717
INLINE MaterialAttrib::
18-
MaterialAttrib() {
18+
MaterialAttrib() : _flags(0) {
1919
}
2020

2121
/**
@@ -24,7 +24,7 @@ MaterialAttrib() {
2424
*/
2525
INLINE bool MaterialAttrib::
2626
is_off() const {
27-
return _material == (const Material *)NULL;
27+
return _flags == 0;
2828
}
2929

3030
/**
@@ -35,3 +35,11 @@ INLINE Material *MaterialAttrib::
3535
get_material() const {
3636
return _material;
3737
}
38+
39+
/**
40+
* Equivalent to get_material()->get_flags().
41+
*/
42+
INLINE int MaterialAttrib::
43+
get_material_flags() const {
44+
return _flags;
45+
}

panda/src/pgraph/materialAttrib.cxx

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ make(Material *material) {
3030
MaterialAttrib *attrib = new MaterialAttrib;
3131
attrib->_material = material;
3232
material->set_attrib_lock();
33+
attrib->_flags = material->_flags;
34+
nassertr(attrib->_flags & Material::F_attrib_lock, nullptr);
3335
return return_new(attrib);
3436
}
3537

@@ -58,10 +60,34 @@ make_default() {
5860
void MaterialAttrib::
5961
output(ostream &out) const {
6062
out << get_type() << ":";
61-
if (is_off()) {
63+
if (_material != nullptr) {
64+
out << *_material;
65+
} else if (is_off()) {
6266
out << "(off)";
6367
} else {
64-
out << *_material;
68+
// This is a state returned from get_auto_shader_attrib().
69+
out << "(on";
70+
#ifndef NDEBUG
71+
if (_flags & Material::F_ambient) {
72+
out << " amb";
73+
}
74+
if (_flags & Material::F_diffuse) {
75+
out << " diff";
76+
}
77+
if (_flags & Material::F_specular) {
78+
out << " spec";
79+
}
80+
if (_flags & Material::F_emission) {
81+
out << " emit";
82+
}
83+
if (_flags & Material::F_local) {
84+
out << " local";
85+
}
86+
if (_flags & Material::F_twoside) {
87+
out << " twoside";
88+
}
89+
#endif
90+
out << ")";
6591
}
6692
}
6793

@@ -87,7 +113,7 @@ compare_to_impl(const RenderAttrib *other) const {
87113
if (_material != ta->_material) {
88114
return _material < ta->_material ? -1 : 1;
89115
}
90-
return 0;
116+
return _flags < ta->_flags;
91117
}
92118

93119
/**
@@ -100,6 +126,7 @@ size_t MaterialAttrib::
100126
get_hash_impl() const {
101127
size_t hash = 0;
102128
hash = pointer_hash::add_hash(hash, _material);
129+
hash = int_hash::add_hash(hash, _flags);
103130
return hash;
104131
}
105132

@@ -108,7 +135,15 @@ get_hash_impl() const {
108135
*/
109136
CPT(RenderAttrib) MaterialAttrib::
110137
get_auto_shader_attrib_impl(const RenderState *state) const {
111-
return this;
138+
if (_material == nullptr) {
139+
return this;
140+
} else {
141+
// Make a copy, but only with the flags, not with the material itself.
142+
MaterialAttrib *attrib = new MaterialAttrib();
143+
attrib->_material = nullptr;
144+
attrib->_flags = _flags;
145+
return return_new(attrib);
146+
}
112147
}
113148

114149
/**
@@ -139,8 +174,12 @@ complete_pointers(TypedWritable **p_list, BamReader *manager) {
139174
int pi = RenderAttrib::complete_pointers(p_list, manager);
140175

141176
TypedWritable *material = p_list[pi++];
142-
if (material != (TypedWritable *)NULL) {
177+
if (material != nullptr) {
143178
_material = DCAST(Material, material);
179+
_flags = _material->_flags | Material::F_attrib_lock;
180+
} else {
181+
_material = nullptr;
182+
_flags = 0;
144183
}
145184

146185
return pi;

panda/src/pgraph/materialAttrib.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class EXPCL_PANDA_PGRAPH MaterialAttrib : public RenderAttrib {
3636
INLINE bool is_off() const;
3737
INLINE Material *get_material() const;
3838

39+
public:
40+
INLINE int get_material_flags() const;
41+
3942
PUBLISHED:
4043
MAKE_PROPERTY(material, get_material);
4144

@@ -49,6 +52,7 @@ class EXPCL_PANDA_PGRAPH MaterialAttrib : public RenderAttrib {
4952

5053
private:
5154
PT(Material) _material;
55+
int _flags;
5256

5357
PUBLISHED:
5458
static int get_class_slot() {

panda/src/pgraphnodes/shaderGenerator.cxx

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,7 @@ analyze_renderstate(const RenderState *rs) {
265265

266266
const MaterialAttrib *material;
267267
rs->get_attrib_def(material);
268-
269-
if (!material->is_off()) {
270-
_material = material->get_material();
271-
} else {
272-
_material = Material::get_default();
273-
}
268+
_material_flags = material->get_material_flags();
274269

275270
// Break out the lights by type.
276271

@@ -285,14 +280,7 @@ analyze_renderstate(const RenderState *rs) {
285280
nassertv(light_obj != (PandaNode *)NULL);
286281

287282
if (light_obj->is_ambient_light()) {
288-
if (_material->has_ambient()) {
289-
LColor a = _material->get_ambient();
290-
if ((a[0]!=0.0)||(a[1]!=0.0)||(a[2]!=0.0)) {
291-
_have_ambient = true;
292-
}
293-
} else {
294-
_have_ambient = true;
295-
}
283+
_have_ambient = true;
296284
_lighting = true;
297285

298286
} else if (light_obj->is_of_type(LightLensNode::get_class_type())) {
@@ -369,29 +357,16 @@ analyze_renderstate(const RenderState *rs) {
369357
// Decide which material modes need to be calculated.
370358

371359
if (_lighting) {
372-
if (_material->has_diffuse()) {
373-
LColor d = _material->get_diffuse();
374-
if ((d[0]!=0.0)||(d[1]!=0.0)||(d[2]!=0.0)) {
375-
_have_diffuse = true;
376-
}
377-
} else {
378-
_have_diffuse = true;
379-
}
360+
_have_diffuse = true;
380361
}
381362

382-
if (_lighting && (_material->has_emission())) {
383-
LColor e = _material->get_emission();
384-
if ((e[0]!=0.0)||(e[1]!=0.0)||(e[2]!=0.0)) {
385-
_have_emission = true;
386-
}
363+
if (_lighting && (_material_flags & Material::F_emission) != 0) {
364+
_have_emission = true;
387365
}
388366

389367
if (_lighting) {
390-
if (_material->has_specular()) {
391-
LColor s = _material->get_specular();
392-
if ((s[0]!=0.0)||(s[1]!=0.0)||(s[2]!=0.0)) {
393-
_have_specular = true;
394-
}
368+
if (_material_flags & Material::F_specular) {
369+
_have_specular = true;
395370
} else if (_map_index_gloss >= 0) {
396371
_have_specular = true;
397372
}
@@ -402,14 +377,10 @@ analyze_renderstate(const RenderState *rs) {
402377
// Decide whether to separate ambient and diffuse calculations.
403378

404379
if (_have_ambient && _have_diffuse) {
405-
if (_material->has_ambient()) {
406-
if (_material->has_diffuse()) {
407-
_separate_ambient_diffuse = _material->get_ambient() != _material->get_diffuse();
408-
} else {
409-
_separate_ambient_diffuse = true;
410-
}
380+
if (_material_flags & Material::F_ambient) {
381+
_separate_ambient_diffuse = true;
411382
} else {
412-
if (_material->has_diffuse()) {
383+
if (_material_flags & Material::F_diffuse) {
413384
_separate_ambient_diffuse = true;
414385
} else {
415386
_separate_ambient_diffuse = false;
@@ -431,10 +402,10 @@ analyze_renderstate(const RenderState *rs) {
431402
// Does the shader need material properties as input?
432403

433404
_need_material_props =
434-
(_have_ambient && (_material->has_ambient()))||
435-
(_have_diffuse && (_material->has_diffuse()))||
436-
(_have_emission && (_material->has_emission()))||
437-
(_have_specular && (_material->has_specular()));
405+
(_have_ambient && (_material_flags & Material::F_ambient) != 0) ||
406+
(_have_diffuse && (_material_flags & Material::F_diffuse) != 0) ||
407+
(_have_emission && (_material_flags & Material::F_emission) != 0) ||
408+
(_have_specular && (_material_flags & Material::F_specular) != 0);
438409

439410
// Check for clip planes.
440411

@@ -494,7 +465,7 @@ clear_analysis() {
494465
_out_aux_normal = false;
495466
_out_aux_glow = false;
496467
_out_aux_any = false;
497-
_material = (Material*)NULL;
468+
_material_flags = 0;
498469
_need_material_props = false;
499470
_need_world_position = false;
500471
_need_world_normal = false;
@@ -867,7 +838,7 @@ synthesize_shader(const RenderState *rs, const GeomVertexAnimationSpec &anim) {
867838
text << "\t uniform float4x4 attr_material,\n";
868839
}
869840
if (_have_specular) {
870-
if (_material->get_local()) {
841+
if (_material_flags & Material::F_local) {
871842
text << "\t uniform float4 mspos_view,\n";
872843
} else {
873844
text << "\t uniform float4 row1_view_to_model,\n";
@@ -1041,7 +1012,7 @@ synthesize_shader(const RenderState *rs, const GeomVertexAnimationSpec &anim) {
10411012
}
10421013
if (_have_specular) {
10431014
text << "\t float4 tot_specular = float4(0,0,0,0);\n";
1044-
if (_material->has_specular()) {
1015+
if (_material_flags & Material::F_specular) {
10451016
text << "\t float shininess = attr_material[3].w;\n";
10461017
} else {
10471018
text << "\t float shininess = 50; // no shininess specified, using default\n";
@@ -1073,7 +1044,7 @@ synthesize_shader(const RenderState *rs, const GeomVertexAnimationSpec &anim) {
10731044
text << "\t tot_diffuse += lcolor;\n";
10741045
}
10751046
if (_have_specular) {
1076-
if (_material->get_local()) {
1047+
if (_material_flags & Material::F_local) {
10771048
text << "\t lhalf = normalize(lvec - normalize(l_eye_position.xyz));\n";
10781049
} else {
10791050
text << "\t lhalf = dlight_light" << i << "_rel_view[3].xyz;\n";
@@ -1103,7 +1074,7 @@ synthesize_shader(const RenderState *rs, const GeomVertexAnimationSpec &anim) {
11031074
text << "\t tot_diffuse += lcolor;\n";
11041075
}
11051076
if (_have_specular) {
1106-
if (_material->get_local()) {
1077+
if (_material_flags & Material::F_local) {
11071078
text << "\t lhalf = normalize(lvec - normalize(l_eye_position.xyz));\n";
11081079
} else {
11091080
text << "\t lhalf = normalize(lvec - float3(0, 1, 0));\n";
@@ -1141,7 +1112,7 @@ synthesize_shader(const RenderState *rs, const GeomVertexAnimationSpec &anim) {
11411112
text << "\t tot_diffuse += lcolor;\n";
11421113
}
11431114
if (_have_specular) {
1144-
if (_material->get_local()) {
1115+
if (_material_flags & Material::F_local) {
11451116
text << "\t lhalf = normalize(lvec - normalize(l_eye_position.xyz));\n";
11461117
} else {
11471118
text << "\t lhalf = normalize(lvec - float3(0,1,0));\n";
@@ -1199,7 +1170,7 @@ synthesize_shader(const RenderState *rs, const GeomVertexAnimationSpec &anim) {
11991170
}
12001171
}
12011172
if ((_have_ambient)&&(_separate_ambient_diffuse)) {
1202-
if (_material->has_ambient()) {
1173+
if (_material_flags & Material::F_ambient) {
12031174
text << "\t result += tot_ambient * attr_material[0];\n";
12041175
} else if (_vertex_colors) {
12051176
text << "\t result += tot_ambient * l_color;\n";
@@ -1210,7 +1181,7 @@ synthesize_shader(const RenderState *rs, const GeomVertexAnimationSpec &anim) {
12101181
}
12111182
}
12121183
if (_have_diffuse) {
1213-
if (_material->has_diffuse()) {
1184+
if (_material_flags & Material::F_diffuse) {
12141185
text << "\t result += tot_diffuse * attr_material[1];\n";
12151186
} else if (_vertex_colors) {
12161187
text << "\t result += tot_diffuse * l_color;\n";
@@ -1389,7 +1360,7 @@ synthesize_shader(const RenderState *rs, const GeomVertexAnimationSpec &anim) {
13891360

13901361
if (_lighting) {
13911362
if (_have_specular) {
1392-
if (_material->has_specular()) {
1363+
if (_material_flags & Material::F_specular) {
13931364
text << "\t tot_specular *= attr_material[3];\n";
13941365
}
13951366
if (_map_index_gloss >= 0 && _auto_gloss_on) {

panda/src/pgraphnodes/shaderGenerator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class EXPCL_PANDA_PGRAPHNODES ShaderGenerator : public TypedReferenceCount {
8787
// RenderState analysis information. Created by analyze_renderstate:
8888

8989
CPT(RenderState) _state;
90-
Material *_material;
90+
int _material_flags;
9191
int _num_textures;
9292

9393
pvector<LightLensNode *> _lights;

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