tinyGraphicsStateGuardian.h 8.1 KB

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