Panda3D
tinyGraphicsStateGuardian.h
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 
15 #ifndef TINYGRAPHICSSTATEGUARDIAN_H
16 #define TINYGRAPHICSSTATEGUARDIAN_H
17 
18 #include "pandabase.h"
19 
20 #include "graphicsStateGuardian.h"
21 #include "colorBlendAttrib.h"
22 #include "simpleLru.h"
23 #include "zmath.h"
24 #include "zbuffer.h"
25 #include "zgl.h"
26 #include "geomVertexReader.h"
27 
28 class TinyTextureContext;
29 
30 ////////////////////////////////////////////////////////////////////
31 // Class : TinyGraphicsStateGuardian
32 // Description : An interface to the TinyPanda software rendering code
33 // within this module.
34 //
35 // TinyPanda takes its name from TinyGL, the
36 // public-domain software renderer (see
37 // http://fabrice.bellard.free.fr/TinyGL/ ) from which
38 // this code originated. It has since been heavily
39 // modified, to integrate it closely with Panda, and to
40 // add additional features such as blending, filtering,
41 // and multitexturing.
42 ////////////////////////////////////////////////////////////////////
43 class EXPCL_TINYDISPLAY TinyGraphicsStateGuardian : public GraphicsStateGuardian {
44 public:
46  TinyGraphicsStateGuardian *share_with);
47 
48  virtual ~TinyGraphicsStateGuardian();
49 
50  virtual void reset();
51  virtual void free_pointers();
52  virtual void close_gsg();
53 
54  virtual bool depth_offset_decals();
55 
56  virtual PT(GeomMunger) make_geom_munger(const RenderState *state,
57  Thread *current_thread);
58 
59  virtual void clear(DrawableRegion *clearable);
60 
62  virtual CPT(TransformState) calc_projection_mat(const Lens *lens);
63  virtual bool prepare_lens();
64 
65  virtual bool begin_frame(Thread *current_thread);
66  virtual bool begin_scene();
67  virtual void end_scene();
68  virtual void end_frame(Thread *current_thread);
69 
70  virtual bool begin_draw_primitives(const GeomPipelineReader *geom_reader,
71  const GeomMunger *munger,
72  const GeomVertexDataPipelineReader *data_reader,
73  bool force);
74  virtual bool draw_triangles(const GeomPrimitivePipelineReader *reader,
75  bool force);
76  virtual bool draw_tristrips(const GeomPrimitivePipelineReader *reader,
77  bool force);
78  virtual bool draw_lines(const GeomPrimitivePipelineReader *reader,
79  bool force);
80  virtual bool draw_points(const GeomPrimitivePipelineReader *reader,
81  bool force);
82  virtual void end_draw_primitives();
83 
84  virtual bool framebuffer_copy_to_texture
85  (Texture *tex, int view, int z, const DisplayRegion *dr, const RenderBuffer &rb);
86  virtual bool framebuffer_copy_to_ram
87  (Texture *tex, int view, int z, const DisplayRegion *dr, const RenderBuffer &rb);
88 
89  virtual void set_state_and_transform(const RenderState *state,
90  const TransformState *transform);
91 
92  virtual TextureContext *prepare_texture(Texture *tex, int view);
93  virtual bool update_texture(TextureContext *tc, bool force);
94  bool update_texture(TextureContext *tc, bool force, int stage_index, bool uses_mipmaps);
95  virtual void release_texture(TextureContext *tc);
96 
97  virtual void do_issue_light();
98  virtual void bind_light(PointLight *light_obj, const NodePath &light,
99  int light_id);
100  virtual void bind_light(DirectionalLight *light_obj, const NodePath &light,
101  int light_id);
102  virtual void bind_light(Spotlight *light_obj, const NodePath &light,
103  int light_id);
104 
105 private:
106  void do_issue_transform();
107  void do_issue_render_mode();
108  void do_issue_cull_face();
109  void do_issue_rescale_normal();
110  void do_issue_depth_offset();
111  void do_issue_material();
112  void do_issue_texture();
113  void do_issue_scissor();
114 
115  void set_scissor(PN_stdfloat left, PN_stdfloat right, PN_stdfloat bottom, PN_stdfloat top);
116 
117  bool apply_texture(TextureContext *tc);
118  bool upload_texture(TinyTextureContext *gtc, bool force, bool uses_mipmaps);
119  bool upload_simple_texture(TinyTextureContext *gtc);
120  bool setup_gltex(GLTexture *gltex, int x_size, int y_size, int num_levels);
121  int get_tex_shift(int orig_size);
122 
123  static void copy_lum_image(ZTextureLevel *dest, int xsize, int ysize, TinyTextureContext *gtc, int level);
124  static void copy_alpha_image(ZTextureLevel *dest, int xsize, int ysize, TinyTextureContext *gtc, int level);
125  static void copy_one_channel_image(ZTextureLevel *dest, int xsize, int ysize, TinyTextureContext *gtc, int level, int channel);
126  static void copy_la_image(ZTextureLevel *dest, int xsize, int ysize, TinyTextureContext *gtc, int level);
127  static void copy_rgb_image(ZTextureLevel *dest, int xsize, int ysize, TinyTextureContext *gtc, int level);
128  static void copy_rgba_image(ZTextureLevel *dest, int xsize, int ysize, TinyTextureContext *gtc, int level);
129 
130  void setup_material(GLMaterial *gl_material, const Material *material);
131  void do_auto_rescale_normal();
132  static void load_matrix(M4 *matrix, const TransformState *transform);
133  static int get_color_blend_op(ColorBlendAttrib::Operand operand);
134  static ZB_lookupTextureFunc get_tex_filter_func(SamplerState::FilterType filter);
135  static ZB_texWrapFunc get_tex_wrap_func(SamplerState::WrapMode wrap_mode);
136 
137  INLINE void clear_light_state();
138 
139  // Methods used to generate texture coordinates.
140  class TexCoordData {
141  public:
142  GeomVertexReader _r1;
143  GeomVertexReader _r2;
144  LMatrix4 _mat;
145  };
146  typedef void GenTexcoordFunc(V2 &result, TexCoordData &tcdata);
147 
148  static void texgen_null(V2 &result, TexCoordData &tcdata);
149  static void texgen_simple(V2 &result, TexCoordData &tcdata);
150  static void texgen_texmat(V2 &result, TexCoordData &tcdata);
151  static void texgen_sphere_map(V2 &result, TexCoordData &tcdata);
152 public:
153  // Filled in by the Tiny*GraphicsWindow at begin_frame().
154  ZBuffer *_current_frame_buffer;
155 
156 private:
157  // Allocated by prepare_display_region when necessary for a zoomed
158  // display region.
159  ZBuffer *_aux_frame_buffer;
160 
161  GLContext *_c;
162 
163  enum ColorMaterialFlags {
164  CMF_ambient = 0x001,
165  CMF_diffuse = 0x002,
166  };
167  int _color_material_flags;
168  int _texturing_state;
169  int _texfilter_state;
170  bool _texture_replace;
171  bool _filled_flat;
172  bool _auto_rescale_normal;
173 
174  CPT(TransformState) _scissor_mat;
175 
176  // Cache the data necessary to bind each particular light each
177  // frame, so if we bind a given light multiple times, we only have
178  // to compute its data once.
180  Lights _plights, _dlights, _slights;
181 
182  // Used during being_draw_primitives() .. end_draw_primitives().
183  int _min_vertex;
184  int _max_vertex;
185  GLVertex *_vertices;
186  int _vertices_size;
187 
188  static PStatCollector _vertices_immediate_pcollector;
189  static PStatCollector _draw_transform_pcollector;
190  static PStatCollector _pixel_count_white_untextured_pcollector;
191  static PStatCollector _pixel_count_flat_untextured_pcollector;
192  static PStatCollector _pixel_count_smooth_untextured_pcollector;
193  static PStatCollector _pixel_count_white_textured_pcollector;
194  static PStatCollector _pixel_count_flat_textured_pcollector;
195  static PStatCollector _pixel_count_smooth_textured_pcollector;
196  static PStatCollector _pixel_count_white_perspective_pcollector;
197  static PStatCollector _pixel_count_flat_perspective_pcollector;
198  static PStatCollector _pixel_count_smooth_perspective_pcollector;
199  static PStatCollector _pixel_count_smooth_multitex2_pcollector;
200  static PStatCollector _pixel_count_smooth_multitex3_pcollector;
201 
202 public:
203  static TypeHandle get_class_type() {
204  return _type_handle;
205  }
206  static void init_type() {
207  GraphicsStateGuardian::init_type();
208  register_type(_type_handle, "TinyGraphicsStateGuardian",
209  GraphicsStateGuardian::get_class_type());
210  }
211  virtual TypeHandle get_type() const {
212  return get_class_type();
213  }
214  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
215 
216 private:
217  static TypeHandle _type_handle;
218 };
219 
220 #include "tinyGraphicsStateGuardian.I"
221 
222 #endif
A light shining from infinitely far away in a particular direction, like sunlight.
virtual bool draw_points(const GeomPrimitivePipelineReader *reader, bool force)
Draws a series of disconnected points.
virtual bool framebuffer_copy_to_ram(Texture *tex, int view, int z, const DisplayRegion *dr, const RenderBuffer &rb)
Copy the pixels within the indicated display region from the framebuffer into system memory...
virtual void end_frame(Thread *current_thread)
Called after each frame is rendered, to allow the GSG a chance to do any internal cleanup after rende...
Encapsulates the data from a DisplayRegion, pre-fetched for one stage of the pipeline.
virtual bool depth_offset_decals()
Returns true if this GSG can implement decals using a DepthOffsetAttrib, or false if that is unreliab...
virtual TextureContext * prepare_texture(Texture *tex)
Creates whatever structures the GSG requires to represent the texture internally, and returns a newly...
This is a base class for GraphicsWindow (actually, GraphicsOutput) and DisplayRegion, both of which are conceptually rectangular regions into which drawing commands may be issued.
virtual void clear(DrawableRegion *clearable)
Clears the framebuffer within the current DisplayRegion, according to the flags indicated by the give...
A base class for any number of different kinds of lenses, linear and otherwise.
Definition: lens.h:45
Definition: zmath.h:18
Definition: zmath.h:6
virtual bool update_texture(TextureContext *tc, bool force)
Ensures that the current Texture data is refreshed onto the GSG.
virtual void prepare_display_region(DisplayRegionPipelineReader *dr)
Makes the specified DisplayRegion current.
Definition: zgl.h:141
virtual void end_draw_primitives()
Called after a sequence of draw_primitive() functions are called, this should do whatever cleanup is ...
Objects of this class are used to convert vertex data from a Geom into a format suitable for passing ...
Definition: geomMunger.h:57
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition: texture.h:75
This is a special class object that holds all the information returned by a particular GSG to indicat...
Encapsulates the data from a Geom, pre-fetched for one stage of the pipeline.
Definition: geom.h:401
virtual bool draw_tristrips(const GeomPrimitivePipelineReader *reader, bool force)
Draws a series of triangle strips.
A lightweight class that represents a single element that may be timed and/or counted via stats...
virtual void set_state_and_transform(const RenderState *state, const TransformState *transform)
Simultaneously resets the render state and the transform state.
Definition: zgl.h:106
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
Definition: zgl.h:123
An object to create GraphicsOutputs that share a particular 3-D API.
Definition: graphicsPipe.h:58
virtual bool draw_triangles(const GeomPrimitivePipelineReader *reader, bool force)
Draws a series of disconnected triangles.
virtual void release_texture(TextureContext *tc)
Frees the resources previously allocated via a call to prepare_texture(), including deleting the Text...
virtual bool draw_lines(const GeomPrimitivePipelineReader *reader, bool force)
Draws a series of disconnected line segments.
A light originating from a single point in space, and shining in a particular direction, with a cone-shaped falloff.
Definition: spotlight.h:37
virtual void do_issue_light()
This implementation of do_issue_light() assumes we have a limited number of hardware lights available...
Defines the way an object appears in the presence of lighting.
Definition: material.h:34
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:53
virtual bool begin_draw_primitives(const GeomPipelineReader *geom_reader, const GeomMunger *munger, const GeomVertexDataPipelineReader *data_reader, bool force)
Called before a sequence of draw_primitive() functions are called, this should prepare the vertex dat...
Definition: zgl.h:64
A thread; that is, a lightweight process.
Definition: thread.h:51
This object provides a high-level interface for quickly reading a sequence of numeric values from a v...
Encapsulates all the communication with a particular instance of a given rendering backend...
virtual void end_scene()
Called between begin_frame() and end_frame() to mark the end of drawing commands for a "scene" (usual...
A rectangular subregion within a window for rendering into.
Definition: displayRegion.h:61
Encapsulates the data from a GeomVertexData, pre-fetched for one stage of the pipeline.
This class is the main interface to controlling the render process.
virtual void reset()
Resets all internal state as if the gsg were newly created.
virtual bool framebuffer_copy_to_texture(Texture *tex, int view, int z, const DisplayRegion *dr, const RenderBuffer &rb)
Copy the pixels within the indicated display region from the framebuffer into texture memory...
virtual bool begin_frame(Thread *current_thread)
Called before each frame is rendered, to allow the GSG a chance to do any internal cleanup before beg...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
Encapsulates the data from a GeomPrimitive, pre-fetched for one stage of the pipeline.
virtual bool prepare_lens()
Makes the current lens (whichever lens was most recently specified with set_scene()) active...
A RenderBuffer is an arbitrary subset of the various layers (depth buffer, color buffer, etc.) of a drawing region.
Definition: renderBuffer.h:30
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165
An interface to the TinyPanda software rendering code within this module.
A light originating from a single point in space, and shining in all directions.
Definition: pointLight.h:27
virtual bool begin_scene()
Called between begin_frame() and end_frame() to mark the beginning of drawing commands for a "scene" ...
virtual void bind_light(PointLight *light_obj, const NodePath &light, int light_id)
Called the first time a particular light has been bound to a given id within a frame, this should set up the associated hardware light with the light's properties.