lighting optimization

This commit is contained in:
David Rose 2008-11-14 00:23:40 +00:00
parent c6ec6b12be
commit d653bfc559
3 changed files with 167 additions and 116 deletions

View File

@ -12,3 +12,23 @@
// //
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// Function: TinyGraphicsStateGuardian::clear_light_state
// Access: Private
// Description: Removes the current list of active lights from the
// current state context.
////////////////////////////////////////////////////////////////////
INLINE void TinyGraphicsStateGuardian::
clear_light_state() {
_c->lighting_enabled = false;
#ifndef NDEBUG
GLLight *gl_light = _c->first_light;
while (gl_light != (GLLight *)NULL) {
GLLight *next = gl_light->next;
gl_light->next = NULL;
gl_light = next;
}
#endif // NDEBUG
_c->first_light = NULL;
}

View File

@ -438,6 +438,13 @@ end_scene() {
} }
_c->zb = _current_frame_buffer; _c->zb = _current_frame_buffer;
} }
// Clear the lighting state.
clear_light_state();
_plights.clear();
_dlights.clear();
_slights.clear();
GraphicsStateGuardian::end_scene(); GraphicsStateGuardian::end_scene();
} }
@ -1571,15 +1578,7 @@ do_issue_light() {
} }
// First, release all of the previously-assigned lights. // First, release all of the previously-assigned lights.
_c->lighting_enabled = false; clear_light_state();
GLLight *gl_light = _c->first_light;
while (gl_light != (GLLight *)NULL) {
GLLight *next = gl_light->next;
gl_light->next = NULL;
gl_light = next;
}
_c->first_light = NULL;
// Now, assign new lights. // Now, assign new lights.
if (_target._light != (LightAttrib *)NULL) { if (_target._light != (LightAttrib *)NULL) {
@ -1604,21 +1603,18 @@ do_issue_light() {
} else { } else {
// Other kinds of lights each get their own GLLight object. // Other kinds of lights each get their own GLLight object.
nassertv(num_enabled < MAX_LIGHTS); light_obj->bind(this, light, num_enabled);
GLLight *gl_light = &_c->lights[num_enabled]; num_enabled++;
memset(gl_light, 0, sizeof(GLLight));
gl_light->next = _c->first_light;
_c->first_light = gl_light;
// Handle the diffuse color here, since all lights have this
// property.
GLLight *gl_light = _c->first_light;
nassertv(gl_light != NULL);
const Colorf &diffuse = light_obj->get_color(); const Colorf &diffuse = light_obj->get_color();
gl_light->diffuse.v[0] = diffuse[0]; gl_light->diffuse.v[0] = diffuse[0];
gl_light->diffuse.v[1] = diffuse[1]; gl_light->diffuse.v[1] = diffuse[1];
gl_light->diffuse.v[2] = diffuse[2]; gl_light->diffuse.v[2] = diffuse[2];
gl_light->diffuse.v[3] = diffuse[3]; gl_light->diffuse.v[3] = diffuse[3];
light_obj->bind(this, light, num_enabled);
num_enabled++;
} }
} }
} }
@ -1643,8 +1639,11 @@ do_issue_light() {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
void TinyGraphicsStateGuardian:: void TinyGraphicsStateGuardian::
bind_light(PointLight *light_obj, const NodePath &light, int light_id) { bind_light(PointLight *light_obj, const NodePath &light, int light_id) {
GLLight *gl_light = _c->first_light; pair<Lights::iterator, bool> lookup = _plights.insert(Lights::value_type(light, GLLight()));
nassertv(gl_light != (GLLight *)NULL); GLLight *gl_light = &(*lookup.first).second;
if (lookup.second) {
// It's a brand new light. Define it.
memset(gl_light, 0, sizeof(GLLight));
const Colorf &specular = light_obj->get_specular_color(); const Colorf &specular = light_obj->get_specular_color();
gl_light->specular.v[0] = specular[0]; gl_light->specular.v[0] = specular[0];
@ -1678,6 +1677,13 @@ bind_light(PointLight *light_obj, const NodePath &light, int light_id) {
gl_light->attenuation[2] = att[2]; gl_light->attenuation[2] = att[2];
} }
nassertv(gl_light->next == NULL);
// Add it to the linked list of active lights.
gl_light->next = _c->first_light;
_c->first_light = gl_light;
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: TinyGraphicsStateGuardian::bind_light // Function: TinyGraphicsStateGuardian::bind_light
// Access: Public, Virtual // Access: Public, Virtual
@ -1688,8 +1694,11 @@ bind_light(PointLight *light_obj, const NodePath &light, int light_id) {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
void TinyGraphicsStateGuardian:: void TinyGraphicsStateGuardian::
bind_light(DirectionalLight *light_obj, const NodePath &light, int light_id) { bind_light(DirectionalLight *light_obj, const NodePath &light, int light_id) {
GLLight *gl_light = _c->first_light; pair<Lights::iterator, bool> lookup = _dlights.insert(Lights::value_type(light, GLLight()));
nassertv(gl_light != (GLLight *)NULL); GLLight *gl_light = &(*lookup.first).second;
if (lookup.second) {
// It's a brand new light. Define it.
memset(gl_light, 0, sizeof(GLLight));
const Colorf &specular = light_obj->get_specular_color(); const Colorf &specular = light_obj->get_specular_color();
gl_light->specular.v[0] = specular[0]; gl_light->specular.v[0] = specular[0];
@ -1730,6 +1739,13 @@ bind_light(DirectionalLight *light_obj, const NodePath &light, int light_id) {
gl_light->attenuation[2] = 0.0f; gl_light->attenuation[2] = 0.0f;
} }
nassertv(gl_light->next == NULL);
// Add it to the linked list of active lights.
gl_light->next = _c->first_light;
_c->first_light = gl_light;
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: TinyGraphicsStateGuardian::bind_light // Function: TinyGraphicsStateGuardian::bind_light
// Access: Public, Virtual // Access: Public, Virtual
@ -1740,8 +1756,11 @@ bind_light(DirectionalLight *light_obj, const NodePath &light, int light_id) {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
void TinyGraphicsStateGuardian:: void TinyGraphicsStateGuardian::
bind_light(Spotlight *light_obj, const NodePath &light, int light_id) { bind_light(Spotlight *light_obj, const NodePath &light, int light_id) {
GLLight *gl_light = _c->first_light; pair<Lights::iterator, bool> lookup = _plights.insert(Lights::value_type(light, GLLight()));
nassertv(gl_light != (GLLight *)NULL); GLLight *gl_light = &(*lookup.first).second;
if (lookup.second) {
// It's a brand new light. Define it.
memset(gl_light, 0, sizeof(GLLight));
const Colorf &specular = light_obj->get_specular_color(); const Colorf &specular = light_obj->get_specular_color();
gl_light->specular.v[0] = specular[0]; gl_light->specular.v[0] = specular[0];
@ -1788,6 +1807,13 @@ bind_light(Spotlight *light_obj, const NodePath &light, int light_id) {
gl_light->attenuation[2] = att[2]; gl_light->attenuation[2] = att[2];
} }
nassertv(gl_light->next == NULL);
// Add it to the linked list of active lights.
gl_light->next = _c->first_light;
_c->first_light = gl_light;
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: TinyGraphicsStateGuardian::do_issue_transform // Function: TinyGraphicsStateGuardian::do_issue_transform
// Access: Protected // Access: Protected

View File

@ -22,12 +22,9 @@
#include "simpleLru.h" #include "simpleLru.h"
#include "zmath.h" #include "zmath.h"
#include "zbuffer.h" #include "zbuffer.h"
#include "zgl.h"
class TinyTextureContext; class TinyTextureContext;
struct GLContext;
struct GLVertex;
struct GLMaterial;
struct GLTexture;
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Class : TinyGraphicsStateGuardian // Class : TinyGraphicsStateGuardian
@ -126,6 +123,8 @@ private:
static int get_color_blend_op(ColorBlendAttrib::Operand operand); static int get_color_blend_op(ColorBlendAttrib::Operand operand);
static ZB_lookupTextureFunc get_tex_filter_func(Texture::FilterType filter); static ZB_lookupTextureFunc get_tex_filter_func(Texture::FilterType filter);
INLINE void clear_light_state();
public: public:
// Filled in by the Tiny*GraphicsWindow at begin_frame(). // Filled in by the Tiny*GraphicsWindow at begin_frame().
ZBuffer *_current_frame_buffer; ZBuffer *_current_frame_buffer;
@ -150,6 +149,12 @@ private:
CPT(TransformState) _scissor_mat; CPT(TransformState) _scissor_mat;
// Cache the data necessary to bind each particular light each
// frame, so if we bind a given light multiple times, we only have
// to compute its data once.
typedef pmap<NodePath, GLLight> Lights;
Lights _plights, _dlights, _slights;
// Used during being_draw_primitives() .. end_draw_primitives(). // Used during being_draw_primitives() .. end_draw_primitives().
int _min_vertex; int _min_vertex;
int _max_vertex; int _max_vertex;