tinyGraphicsStateGuardian.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Filename: tinyGraphicsStateGuardian.h
  2. // Created by: drose (24Apr08)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. #ifndef TINYGRAPHICSSTATEGUARDIAN_H
  15. #define TINYGRAPHICSSTATEGUARDIAN_H
  16. #include "pandabase.h"
  17. #include "graphicsStateGuardian.h"
  18. #include "colorBlendAttrib.h"
  19. #include "simpleLru.h"
  20. #include "zmath.h"
  21. #include "zbuffer.h"
  22. #include "zgl.h"
  23. #include "geomVertexReader.h"
  24. class TinyTextureContext;
  25. ////////////////////////////////////////////////////////////////////
  26. // Class : TinyGraphicsStateGuardian
  27. // Description : An interface to the TinyPanda software rendering code
  28. // within this module.
  29. //
  30. // TinyPanda takes its name from TinyGL, the
  31. // public-domain software renderer (see
  32. // http://fabrice.bellard.free.fr/TinyGL/ ) from which
  33. // this code originated. It has since been heavily
  34. // modified, to integrate it closely with Panda, and to
  35. // add additional features such as blending, filtering,
  36. // and multitexturing.
  37. ////////////////////////////////////////////////////////////////////
  38. class EXPCL_TINYDISPLAY TinyGraphicsStateGuardian : public GraphicsStateGuardian {
  39. public:
  40. TinyGraphicsStateGuardian(GraphicsEngine *engine, GraphicsPipe *pipe,
  41. TinyGraphicsStateGuardian *share_with);
  42. virtual ~TinyGraphicsStateGuardian();
  43. virtual void reset();
  44. virtual void free_pointers();
  45. virtual void close_gsg();
  46. virtual bool depth_offset_decals();
  47. virtual PT(GeomMunger) make_geom_munger(const RenderState *state,
  48. Thread *current_thread);
  49. virtual void clear(DrawableRegion *clearable);
  50. virtual void prepare_display_region(DisplayRegionPipelineReader *dr,
  51. Lens::StereoChannel stereo_channel);
  52. virtual CPT(TransformState) calc_projection_mat(const Lens *lens);
  53. virtual bool prepare_lens();
  54. virtual bool begin_frame(Thread *current_thread);
  55. virtual bool begin_scene();
  56. virtual void end_scene();
  57. virtual void end_frame(Thread *current_thread);
  58. virtual bool begin_draw_primitives(const GeomPipelineReader *geom_reader,
  59. const GeomMunger *munger,
  60. const GeomVertexDataPipelineReader *data_reader,
  61. bool force);
  62. virtual bool draw_triangles(const GeomPrimitivePipelineReader *reader,
  63. bool force);
  64. virtual bool draw_tristrips(const GeomPrimitivePipelineReader *reader,
  65. bool force);
  66. virtual bool draw_lines(const GeomPrimitivePipelineReader *reader,
  67. bool force);
  68. virtual bool draw_points(const GeomPrimitivePipelineReader *reader,
  69. bool force);
  70. virtual void end_draw_primitives();
  71. virtual bool framebuffer_copy_to_texture
  72. (Texture *tex, int z, const DisplayRegion *dr, const RenderBuffer &rb);
  73. virtual bool framebuffer_copy_to_ram
  74. (Texture *tex, int z, const DisplayRegion *dr, const RenderBuffer &rb);
  75. virtual void set_state_and_transform(const RenderState *state,
  76. const TransformState *transform);
  77. virtual TextureContext *prepare_texture(Texture *tex);
  78. virtual bool update_texture(TextureContext *tc, bool force);
  79. bool update_texture(TextureContext *tc, bool force, int stage_index);
  80. virtual void release_texture(TextureContext *tc);
  81. virtual void do_issue_light();
  82. virtual void bind_light(PointLight *light_obj, const NodePath &light,
  83. int light_id);
  84. virtual void bind_light(DirectionalLight *light_obj, const NodePath &light,
  85. int light_id);
  86. virtual void bind_light(Spotlight *light_obj, const NodePath &light,
  87. int light_id);
  88. private:
  89. void do_issue_transform();
  90. void do_issue_render_mode();
  91. void do_issue_cull_face();
  92. void do_issue_rescale_normal();
  93. void do_issue_depth_offset();
  94. void do_issue_material();
  95. void do_issue_texture();
  96. void do_issue_scissor();
  97. void set_scissor(float left, float right, float bottom, float top);
  98. bool apply_texture(TextureContext *tc);
  99. bool upload_texture(TinyTextureContext *gtc, bool force);
  100. bool upload_simple_texture(TinyTextureContext *gtc);
  101. bool setup_gltex(GLTexture *gltex, int x_size, int y_size, int num_levels);
  102. int get_tex_shift(int orig_size);
  103. static void copy_lum_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level);
  104. static void copy_alpha_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level);
  105. static void copy_one_channel_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level, int channel);
  106. static void copy_la_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level);
  107. static void copy_rgb_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level);
  108. static void copy_rgba_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level);
  109. void setup_material(GLMaterial *gl_material, const Material *material);
  110. void do_auto_rescale_normal();
  111. static void load_matrix(M4 *matrix, const TransformState *transform);
  112. static int get_color_blend_op(ColorBlendAttrib::Operand operand);
  113. static ZB_lookupTextureFunc get_tex_filter_func(Texture::FilterType filter);
  114. static ZB_texWrapFunc get_tex_wrap_func(Texture::WrapMode wrap_mode);
  115. INLINE void clear_light_state();
  116. // Methods used to generate texture coordinates.
  117. class TexCoordData {
  118. public:
  119. GeomVertexReader _r1;
  120. GeomVertexReader _r2;
  121. LMatrix4f _mat;
  122. };
  123. typedef void GenTexcoordFunc(V2 &result, TexCoordData &tcdata);
  124. static void texgen_null(V2 &result, TexCoordData &tcdata);
  125. static void texgen_simple(V2 &result, TexCoordData &tcdata);
  126. static void texgen_texmat(V2 &result, TexCoordData &tcdata);
  127. static void texgen_sphere_map(V2 &result, TexCoordData &tcdata);
  128. public:
  129. // Filled in by the Tiny*GraphicsWindow at begin_frame().
  130. ZBuffer *_current_frame_buffer;
  131. private:
  132. // Allocated by prepare_display_region when necessary for a zoomed
  133. // display region.
  134. ZBuffer *_aux_frame_buffer;
  135. GLContext *_c;
  136. enum ColorMaterialFlags {
  137. CMF_ambient = 0x001,
  138. CMF_diffuse = 0x002,
  139. };
  140. int _color_material_flags;
  141. int _texturing_state;
  142. int _texfilter_state;
  143. bool _texture_replace;
  144. bool _filled_flat;
  145. bool _auto_rescale_normal;
  146. RenderState::SlotMask _inv_state_mask;
  147. CPT(TransformState) _scissor_mat;
  148. // Cache the data necessary to bind each particular light each
  149. // frame, so if we bind a given light multiple times, we only have
  150. // to compute its data once.
  151. typedef pmap<NodePath, GLLight> Lights;
  152. Lights _plights, _dlights, _slights;
  153. // Used during being_draw_primitives() .. end_draw_primitives().
  154. int _min_vertex;
  155. int _max_vertex;
  156. GLVertex *_vertices;
  157. int _vertices_size;
  158. static PStatCollector _vertices_immediate_pcollector;
  159. static PStatCollector _draw_transform_pcollector;
  160. static PStatCollector _pixel_count_white_untextured_pcollector;
  161. static PStatCollector _pixel_count_flat_untextured_pcollector;
  162. static PStatCollector _pixel_count_smooth_untextured_pcollector;
  163. static PStatCollector _pixel_count_white_textured_pcollector;
  164. static PStatCollector _pixel_count_flat_textured_pcollector;
  165. static PStatCollector _pixel_count_smooth_textured_pcollector;
  166. static PStatCollector _pixel_count_white_perspective_pcollector;
  167. static PStatCollector _pixel_count_flat_perspective_pcollector;
  168. static PStatCollector _pixel_count_smooth_perspective_pcollector;
  169. static PStatCollector _pixel_count_smooth_multitex2_pcollector;
  170. static PStatCollector _pixel_count_smooth_multitex3_pcollector;
  171. public:
  172. static TypeHandle get_class_type() {
  173. return _type_handle;
  174. }
  175. static void init_type() {
  176. GraphicsStateGuardian::init_type();
  177. register_type(_type_handle, "TinyGraphicsStateGuardian",
  178. GraphicsStateGuardian::get_class_type());
  179. }
  180. virtual TypeHandle get_type() const {
  181. return get_class_type();
  182. }
  183. virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
  184. private:
  185. static TypeHandle _type_handle;
  186. };
  187. #include "tinyGraphicsStateGuardian.I"
  188. #endif