tinyGraphicsStateGuardian.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. class TinyTextureContext;
  24. ////////////////////////////////////////////////////////////////////
  25. // Class : TinyGraphicsStateGuardian
  26. // Description : An interface to the TinyGL software rendering code
  27. // within this module.
  28. ////////////////////////////////////////////////////////////////////
  29. class EXPCL_TINYDISPLAY TinyGraphicsStateGuardian : public GraphicsStateGuardian {
  30. public:
  31. TinyGraphicsStateGuardian(GraphicsPipe *pipe,
  32. TinyGraphicsStateGuardian *share_with);
  33. virtual ~TinyGraphicsStateGuardian();
  34. virtual void reset();
  35. virtual void free_pointers();
  36. virtual void close_gsg();
  37. virtual bool depth_offset_decals();
  38. virtual PT(GeomMunger) make_geom_munger(const RenderState *state,
  39. Thread *current_thread);
  40. virtual void clear(DrawableRegion *clearable);
  41. virtual void prepare_display_region(DisplayRegionPipelineReader *dr,
  42. Lens::StereoChannel stereo_channel);
  43. virtual CPT(TransformState) calc_projection_mat(const Lens *lens);
  44. virtual bool prepare_lens();
  45. virtual bool begin_frame(Thread *current_thread);
  46. virtual bool begin_scene();
  47. virtual void end_scene();
  48. virtual void end_frame(Thread *current_thread);
  49. virtual bool begin_draw_primitives(const GeomPipelineReader *geom_reader,
  50. const GeomMunger *munger,
  51. const GeomVertexDataPipelineReader *data_reader,
  52. bool force);
  53. virtual bool draw_triangles(const GeomPrimitivePipelineReader *reader,
  54. bool force);
  55. virtual bool draw_tristrips(const GeomPrimitivePipelineReader *reader,
  56. bool force);
  57. virtual bool draw_lines(const GeomPrimitivePipelineReader *reader,
  58. bool force);
  59. virtual bool draw_points(const GeomPrimitivePipelineReader *reader,
  60. bool force);
  61. virtual void end_draw_primitives();
  62. virtual bool framebuffer_copy_to_texture
  63. (Texture *tex, int z, const DisplayRegion *dr, const RenderBuffer &rb);
  64. virtual bool framebuffer_copy_to_ram
  65. (Texture *tex, int z, const DisplayRegion *dr, const RenderBuffer &rb);
  66. virtual void set_state_and_transform(const RenderState *state,
  67. const TransformState *transform);
  68. virtual TextureContext *prepare_texture(Texture *tex);
  69. virtual bool update_texture(TextureContext *tc, bool force);
  70. virtual void release_texture(TextureContext *tc);
  71. virtual void do_issue_light();
  72. virtual void bind_light(PointLight *light_obj, const NodePath &light,
  73. int light_id);
  74. virtual void bind_light(DirectionalLight *light_obj, const NodePath &light,
  75. int light_id);
  76. virtual void bind_light(Spotlight *light_obj, const NodePath &light,
  77. int light_id);
  78. private:
  79. void do_issue_transform();
  80. void do_issue_render_mode();
  81. void do_issue_cull_face();
  82. void do_issue_rescale_normal();
  83. void do_issue_material();
  84. void do_issue_texture();
  85. void do_issue_scissor();
  86. void set_scissor(float left, float right, float bottom, float top);
  87. bool apply_texture(TextureContext *tc);
  88. bool upload_texture(TinyTextureContext *gtc, bool force);
  89. bool upload_simple_texture(TinyTextureContext *gtc);
  90. bool setup_gltex(GLTexture *gltex, int x_size, int y_size, int num_levels);
  91. int get_tex_shift(int orig_size);
  92. static void copy_lum_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level);
  93. static void copy_alpha_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level);
  94. static void copy_one_channel_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level, int channel);
  95. static void copy_la_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level);
  96. static void copy_rgb_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level);
  97. static void copy_rgba_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level);
  98. void setup_material(GLMaterial *gl_material, const Material *material);
  99. void do_auto_rescale_normal();
  100. static void load_matrix(M4 *matrix, const TransformState *transform);
  101. static int get_color_blend_op(ColorBlendAttrib::Operand operand);
  102. static ZB_lookupTextureFunc get_tex_filter_func(Texture::FilterType filter);
  103. INLINE void clear_light_state();
  104. public:
  105. // Filled in by the Tiny*GraphicsWindow at begin_frame().
  106. ZBuffer *_current_frame_buffer;
  107. private:
  108. // Allocated by prepare_display_region when necessary for a zoomed
  109. // display region.
  110. ZBuffer *_aux_frame_buffer;
  111. GLContext *_c;
  112. enum ColorMaterialFlags {
  113. CMF_ambient = 0x001,
  114. CMF_diffuse = 0x002,
  115. };
  116. int _color_material_flags;
  117. int _texturing_state;
  118. int _texfilter_state;
  119. bool _texture_replace;
  120. bool _filled_flat;
  121. bool _auto_rescale_normal;
  122. CPT(TransformState) _scissor_mat;
  123. // Cache the data necessary to bind each particular light each
  124. // frame, so if we bind a given light multiple times, we only have
  125. // to compute its data once.
  126. typedef pmap<NodePath, GLLight> Lights;
  127. Lights _plights, _dlights, _slights;
  128. // Used during being_draw_primitives() .. end_draw_primitives().
  129. int _min_vertex;
  130. int _max_vertex;
  131. GLVertex *_vertices;
  132. int _vertices_size;
  133. static PStatCollector _vertices_immediate_pcollector;
  134. static PStatCollector _draw_transform_pcollector;
  135. static PStatCollector _pixel_count_white_untextured_pcollector;
  136. static PStatCollector _pixel_count_flat_untextured_pcollector;
  137. static PStatCollector _pixel_count_smooth_untextured_pcollector;
  138. static PStatCollector _pixel_count_white_textured_pcollector;
  139. static PStatCollector _pixel_count_flat_textured_pcollector;
  140. static PStatCollector _pixel_count_smooth_textured_pcollector;
  141. static PStatCollector _pixel_count_white_perspective_pcollector;
  142. static PStatCollector _pixel_count_flat_perspective_pcollector;
  143. static PStatCollector _pixel_count_smooth_perspective_pcollector;
  144. public:
  145. static TypeHandle get_class_type() {
  146. return _type_handle;
  147. }
  148. static void init_type() {
  149. GraphicsStateGuardian::init_type();
  150. register_type(_type_handle, "TinyGraphicsStateGuardian",
  151. GraphicsStateGuardian::get_class_type());
  152. }
  153. virtual TypeHandle get_type() const {
  154. return get_class_type();
  155. }
  156. virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
  157. private:
  158. static TypeHandle _type_handle;
  159. };
  160. #include "tinyGraphicsStateGuardian.I"
  161. #endif