From ab6081054cb07c3a6c249af80fc63d16b01dd008 Mon Sep 17 00:00:00 2001 From: David Rose Date: Fri, 6 Feb 2009 01:45:25 +0000 Subject: [PATCH] set_flash_texture --- panda/src/display/graphicsStateGuardian.cxx | 59 ++++++++++++++++- panda/src/display/graphicsStateGuardian.h | 10 +++ .../glstuff/glGraphicsStateGuardian_src.cxx | 63 ++++++++++++++----- .../src/glstuff/glGraphicsStateGuardian_src.h | 2 +- 4 files changed, 115 insertions(+), 19 deletions(-) diff --git a/panda/src/display/graphicsStateGuardian.cxx b/panda/src/display/graphicsStateGuardian.cxx index 49c0a703e7..fd6f3dd546 100644 --- a/panda/src/display/graphicsStateGuardian.cxx +++ b/panda/src/display/graphicsStateGuardian.cxx @@ -434,6 +434,62 @@ traverse_prepared_textures(GraphicsStateGuardian::TextureCallback *func, } } +#ifndef NDEBUG +//////////////////////////////////////////////////////////////////// +// Function: GraphicsStateGuardian::set_flash_texture +// Access: Published +// Description: Sets the "flash texture". This is a debug feature; +// when enabled, the specified texture will begin +// flashing in the scene, helping you to find it +// visually. +// +// The texture also flashes with a color code: blue for +// mipmap level 0, yellow for mipmap level 1, and red +// for mipmap level 2 or higher (even for textures that +// don't have mipmaps). This gives you an idea of the +// choice of the texture size. If it is blue, the +// texture is being drawn the proper size or magnified; +// if it is yellow, it is being minified a little bit; +// and if it red, it is being minified considerably. If +// you see a red texture when you are right in front of +// it, you should consider reducing the size of the +// texture to avoid wasting texture memory. +// +// Not all rendering backends support the flash_texture +// feature. Presently, it is only supported by OpenGL. +//////////////////////////////////////////////////////////////////// +void GraphicsStateGuardian:: +set_flash_texture(Texture *tex) { + _flash_texture = tex; +} +#endif // NDEBUG + +#ifndef NDEBUG +//////////////////////////////////////////////////////////////////// +// Function: GraphicsStateGuardian::clear_flash_texture +// Access: Published +// Description: Resets the "flash texture", so that no textures will +// flash. See set_flash_texture(). +//////////////////////////////////////////////////////////////////// +void GraphicsStateGuardian:: +clear_flash_texture() { + _flash_texture = NULL; +} +#endif // NDEBUG + +#ifndef NDEBUG +//////////////////////////////////////////////////////////////////// +// Function: GraphicsStateGuardian::get_flash_texture +// Access: Published +// Description: Returns the current "flash texture", if any, or NULL +// if none. See set_flash_texture(). +//////////////////////////////////////////////////////////////////// +Texture *GraphicsStateGuardian:: +get_flash_texture() const { + return _flash_texture; +} +#endif // NDEBUG + //////////////////////////////////////////////////////////////////// // Function: GraphicsStateGuardian::set_scene // Access: Public @@ -1925,7 +1981,7 @@ bind_light(Spotlight *light_obj, const NodePath &light, int light_id) { #ifdef DO_PSTATS //////////////////////////////////////////////////////////////////// // Function: GraphicsStateGuardian::init_frame_pstats -// Access: Pubilc, Static +// Access: Public, Static // Description: Initializes the relevant PStats data at the beginning // of the frame. //////////////////////////////////////////////////////////////////// @@ -1953,6 +2009,7 @@ init_frame_pstats() { } #endif // DO_PSTATS + //////////////////////////////////////////////////////////////////// // Function: GraphicsStateGuardian::create_gamma_table // Access: Public, Static diff --git a/panda/src/display/graphicsStateGuardian.h b/panda/src/display/graphicsStateGuardian.h index 7c3e9f289c..9117817126 100644 --- a/panda/src/display/graphicsStateGuardian.h +++ b/panda/src/display/graphicsStateGuardian.h @@ -175,6 +175,12 @@ PUBLISHED: typedef bool TextureCallback(TextureContext *tc, void *callback_arg); void traverse_prepared_textures(TextureCallback *func, void *callback_arg); +#ifndef NDEBUG + void set_flash_texture(Texture *tex); + void clear_flash_texture(); + Texture *get_flash_texture() const; +#endif + public: bool set_scene(SceneSetup *scene_setup); virtual SceneSetup *get_scene() const; @@ -460,6 +466,10 @@ protected: float _gamma; Texture::QualityLevel _texture_quality_override; + +#ifndef NDEBUG + PT(Texture) _flash_texture; +#endif public: // Statistics diff --git a/panda/src/glstuff/glGraphicsStateGuardian_src.cxx b/panda/src/glstuff/glGraphicsStateGuardian_src.cxx index ef3d908c3c..e4c0bff8b6 100644 --- a/panda/src/glstuff/glGraphicsStateGuardian_src.cxx +++ b/panda/src/glstuff/glGraphicsStateGuardian_src.cxx @@ -6424,13 +6424,39 @@ void CLP(GraphicsStateGuardian):: update_standard_texture_bindings() { #ifndef NDEBUG if (_show_texture_usage) { - update_show_usage_texture_bindings(); + update_show_usage_texture_bindings(-1); return; } #endif // NDEBUG int num_stages = _target_texture->get_num_on_ff_stages(); +#ifndef NDEBUG + // Also check the _flash_texture. If it is non-NULL, we need to + // check to see if our flash_texture is in the texture stack here. + // If so, then we need to call the special show_texture method + // instead of the normal texture stack. + if (_flash_texture != (Texture *)NULL) { + double now = ClockObject::get_global_clock()->get_frame_time(); + int this_second = (int)floor(now); + if (this_second & 1) { + int show_stage_index = -1; + for (int i = 0; i < num_stages && show_stage_index < 0; ++i) { + TextureStage *stage = _target_texture->get_on_ff_stage(i); + Texture *texture = _target_texture->get_on_texture(stage); + if (texture == _flash_texture) { + show_stage_index = i; + } + } + + if (show_stage_index >= 0) { + update_show_usage_texture_bindings(show_stage_index); + return; + } + } + } +#endif // NDEBUG + nassertv(num_stages <= _max_texture_stages && _num_active_texture_stages <= _max_texture_stages); @@ -6625,9 +6651,14 @@ update_standard_texture_bindings() { // Description: This is a special function that loads the usage // textures in gl-show-texture-usage mode, instead of // loading the actual used textures. +// +// If the indicated stage_index is >= 0, then it is the +// particular texture that is shown. Otherwise, the +// textures are rotated through based on +// show_texture_usage_index. //////////////////////////////////////////////////////////////////// void CLP(GraphicsStateGuardian):: -update_show_usage_texture_bindings() { +update_show_usage_texture_bindings(int show_stage_index) { int num_stages = _target_texture->get_num_on_ff_stages(); nassertv(num_stages <= _max_texture_stages && @@ -6671,7 +6702,11 @@ update_show_usage_texture_bindings() { if (num_stages > 0) { // Now, pick just one texture stage to apply. - i = _show_texture_usage_index % num_stages; + if (show_stage_index >= 0 && show_stage_index < num_stages) { + i = show_stage_index; + } else { + i = _show_texture_usage_index % num_stages; + } TextureStage *stage = _target_texture->get_on_ff_stage(i); Texture *texture = _target_texture->get_on_texture(stage); @@ -6708,30 +6743,24 @@ update_show_usage_texture_bindings() { // Access: Protected // Description: Uploads a special "usage" texture intended to be // applied only in gl-show-texture-usage mode, to reveal -// where texure memory is being spent. +// where texture memory is being spent. //////////////////////////////////////////////////////////////////// void CLP(GraphicsStateGuardian):: upload_usage_texture(int width, int height) { GLP(TexParameteri)(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); GLP(TexParameteri)(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - GLP(TexParameteri)(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - GLP(TexParameteri)(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - // Get the color and scale appropriate to this level. - int max_size = _show_texture_usage_max_size; - float scale = (double)(width * height) / (max_size * max_size); - scale = csqrt(min(scale, 1.0f)) * 0.8f + 0.2f; + GLP(TexParameteri)(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); + GLP(TexParameteri)(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); if (GLCAT.is_debug()) { GLCAT.debug() - << "upload_usage_texture(" << width << ", " << height - << "), scale = " << scale << "\n"; + << "upload_usage_texture(" << width << ", " << height << ")\n"; } static Colorf colors[3] = { - Colorf(0.0f, 0.0f, 1.0f, 1.0f), // mipmap 0: blue - Colorf(1.0f, 1.0f, 1.0f, 1.0f), // mipmap 1: white - Colorf(1.0f, 0.0f, 0.0f, 1.0f), // mipmap 2 and higher: red + Colorf(0.4f, 0.5f, 0.8f, 1.0f), // mipmap 0: blue + Colorf(1.0f, 1.0f, 0.0f, 1.0f), // mipmap 1: yellow + Colorf(0.8f, 0.3f, 0.3f, 1.0f), // mipmap 2 and higher: red }; @@ -6742,7 +6771,7 @@ upload_usage_texture(int width, int height) { int n = 0; while (true) { // Choose the color for the nth mipmap. - Colorf c = colors[min(n, 2)] * scale; + Colorf c = colors[min(n, 2)]; // A simple union to store the colors values bytewise, and get the // answer wordwise, independently of machine byte-ordernig. diff --git a/panda/src/glstuff/glGraphicsStateGuardian_src.h b/panda/src/glstuff/glGraphicsStateGuardian_src.h index 3ff8bb5eb0..998bc83b33 100644 --- a/panda/src/glstuff/glGraphicsStateGuardian_src.h +++ b/panda/src/glstuff/glGraphicsStateGuardian_src.h @@ -324,7 +324,7 @@ protected: void update_standard_texture_bindings(); #ifndef NDEBUG - void update_show_usage_texture_bindings(); + void update_show_usage_texture_bindings(int show_stage_index); void upload_usage_texture(int width, int height); #endif // NDEBUG