Panda3D
 All Classes Functions Variables Enumerations
tinyGraphicsStateGuardian.h
00001 // Filename: tinyGraphicsStateGuardian.h
00002 // Created by:  drose (24Apr08)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef TINYGRAPHICSSTATEGUARDIAN_H
00016 #define TINYGRAPHICSSTATEGUARDIAN_H
00017 
00018 #include "pandabase.h"
00019 
00020 #include "graphicsStateGuardian.h"
00021 #include "colorBlendAttrib.h"
00022 #include "simpleLru.h"
00023 #include "zmath.h"
00024 #include "zbuffer.h"
00025 #include "zgl.h"
00026 #include "geomVertexReader.h"
00027 
00028 class TinyTextureContext;
00029 
00030 ////////////////////////////////////////////////////////////////////
00031 //       Class : TinyGraphicsStateGuardian
00032 // Description : An interface to the TinyPanda software rendering code
00033 //               within this module.
00034 //
00035 //               TinyPanda takes its name from TinyGL, the
00036 //               public-domain software renderer (see
00037 //               http://fabrice.bellard.free.fr/TinyGL/ ) from which
00038 //               this code originated.  It has since been heavily
00039 //               modified, to integrate it closely with Panda, and to
00040 //               add additional features such as blending, filtering,
00041 //               and multitexturing.
00042 ////////////////////////////////////////////////////////////////////
00043 class EXPCL_TINYDISPLAY TinyGraphicsStateGuardian : public GraphicsStateGuardian {
00044 public:
00045   TinyGraphicsStateGuardian(GraphicsEngine *engine, GraphicsPipe *pipe,
00046                             TinyGraphicsStateGuardian *share_with);
00047 
00048   virtual ~TinyGraphicsStateGuardian();
00049 
00050   virtual void reset();
00051   virtual void free_pointers();
00052   virtual void close_gsg();
00053 
00054   virtual bool depth_offset_decals();
00055 
00056   virtual PT(GeomMunger) make_geom_munger(const RenderState *state,
00057                                           Thread *current_thread);
00058 
00059   virtual void clear(DrawableRegion *clearable);
00060 
00061   virtual void prepare_display_region(DisplayRegionPipelineReader *dr);
00062   virtual CPT(TransformState) calc_projection_mat(const Lens *lens);
00063   virtual bool prepare_lens();
00064 
00065   virtual bool begin_frame(Thread *current_thread);
00066   virtual bool begin_scene();
00067   virtual void end_scene();
00068   virtual void end_frame(Thread *current_thread);
00069 
00070   virtual bool begin_draw_primitives(const GeomPipelineReader *geom_reader,
00071                                      const GeomMunger *munger,
00072                                      const GeomVertexDataPipelineReader *data_reader,
00073                                      bool force);
00074   virtual bool draw_triangles(const GeomPrimitivePipelineReader *reader,
00075                               bool force);
00076   virtual bool draw_tristrips(const GeomPrimitivePipelineReader *reader,
00077                               bool force);
00078   virtual bool draw_lines(const GeomPrimitivePipelineReader *reader,
00079                           bool force);
00080   virtual bool draw_points(const GeomPrimitivePipelineReader *reader,
00081                            bool force);
00082   virtual void end_draw_primitives();
00083 
00084   virtual bool framebuffer_copy_to_texture
00085   (Texture *tex, int z, const DisplayRegion *dr, const RenderBuffer &rb);
00086   virtual bool framebuffer_copy_to_ram
00087   (Texture *tex, int z, const DisplayRegion *dr, const RenderBuffer &rb);
00088 
00089   virtual void set_state_and_transform(const RenderState *state,
00090                                        const TransformState *transform);
00091 
00092   virtual TextureContext *prepare_texture(Texture *tex, int view);
00093   virtual bool update_texture(TextureContext *tc, bool force);
00094   bool update_texture(TextureContext *tc, bool force, int stage_index);
00095   virtual void release_texture(TextureContext *tc);
00096 
00097   virtual void do_issue_light();
00098   virtual void bind_light(PointLight *light_obj, const NodePath &light,
00099                           int light_id);
00100   virtual void bind_light(DirectionalLight *light_obj, const NodePath &light,
00101                           int light_id);
00102   virtual void bind_light(Spotlight *light_obj, const NodePath &light,
00103                           int light_id);
00104 
00105 private:
00106   void do_issue_transform();
00107   void do_issue_render_mode();
00108   void do_issue_cull_face();
00109   void do_issue_rescale_normal();
00110   void do_issue_depth_offset();
00111   void do_issue_material();
00112   void do_issue_texture();
00113   void do_issue_scissor();
00114 
00115   void set_scissor(PN_stdfloat left, PN_stdfloat right, PN_stdfloat bottom, PN_stdfloat top);
00116 
00117   bool apply_texture(TextureContext *tc);
00118   bool upload_texture(TinyTextureContext *gtc, bool force);
00119   bool upload_simple_texture(TinyTextureContext *gtc);
00120   bool setup_gltex(GLTexture *gltex, int x_size, int y_size, int num_levels);
00121   int get_tex_shift(int orig_size);
00122 
00123   static void copy_lum_image(ZTextureLevel *dest, int xsize, int ysize, TinyTextureContext *gtc, int level);
00124   static void copy_alpha_image(ZTextureLevel *dest, int xsize, int ysize, TinyTextureContext *gtc, int level);
00125   static void copy_one_channel_image(ZTextureLevel *dest, int xsize, int ysize, TinyTextureContext *gtc, int level, int channel);
00126   static void copy_la_image(ZTextureLevel *dest, int xsize, int ysize, TinyTextureContext *gtc, int level);
00127   static void copy_rgb_image(ZTextureLevel *dest, int xsize, int ysize, TinyTextureContext *gtc, int level);
00128   static void copy_rgba_image(ZTextureLevel *dest, int xsize, int ysize, TinyTextureContext *gtc, int level);
00129 
00130   void setup_material(GLMaterial *gl_material, const Material *material);
00131   void do_auto_rescale_normal();
00132   static void load_matrix(M4 *matrix, const TransformState *transform);
00133   static int get_color_blend_op(ColorBlendAttrib::Operand operand);
00134   static ZB_lookupTextureFunc get_tex_filter_func(Texture::FilterType filter);
00135   static ZB_texWrapFunc get_tex_wrap_func(Texture::WrapMode wrap_mode);
00136 
00137   INLINE void clear_light_state();
00138 
00139   // Methods used to generate texture coordinates.
00140   class TexCoordData {
00141   public:
00142     GeomVertexReader _r1;
00143     GeomVertexReader _r2;
00144     LMatrix4 _mat;
00145   };
00146   typedef void GenTexcoordFunc(V2 &result, TexCoordData &tcdata);
00147 
00148   static void texgen_null(V2 &result, TexCoordData &tcdata);
00149   static void texgen_simple(V2 &result, TexCoordData &tcdata);
00150   static void texgen_texmat(V2 &result, TexCoordData &tcdata);
00151   static void texgen_sphere_map(V2 &result, TexCoordData &tcdata);
00152 public:
00153   // Filled in by the Tiny*GraphicsWindow at begin_frame().
00154   ZBuffer *_current_frame_buffer;
00155 
00156 private:
00157   // Allocated by prepare_display_region when necessary for a zoomed
00158   // display region.
00159   ZBuffer *_aux_frame_buffer;
00160 
00161   GLContext *_c;
00162 
00163   enum ColorMaterialFlags {
00164     CMF_ambient   = 0x001,
00165     CMF_diffuse   = 0x002,
00166   };
00167   int _color_material_flags;
00168   int _texturing_state;
00169   int _texfilter_state;
00170   bool _texture_replace;
00171   bool _filled_flat;
00172   bool _auto_rescale_normal;
00173 
00174   CPT(TransformState) _scissor_mat;
00175 
00176   // Cache the data necessary to bind each particular light each
00177   // frame, so if we bind a given light multiple times, we only have
00178   // to compute its data once.
00179   typedef pmap<NodePath, GLLight> Lights;
00180   Lights _plights, _dlights, _slights;
00181 
00182   // Used during being_draw_primitives() .. end_draw_primitives().
00183   int _min_vertex;
00184   int _max_vertex;
00185   GLVertex *_vertices;
00186   int _vertices_size;
00187 
00188   static PStatCollector _vertices_immediate_pcollector;
00189   static PStatCollector _draw_transform_pcollector;
00190   static PStatCollector _pixel_count_white_untextured_pcollector;
00191   static PStatCollector _pixel_count_flat_untextured_pcollector;
00192   static PStatCollector _pixel_count_smooth_untextured_pcollector;
00193   static PStatCollector _pixel_count_white_textured_pcollector;
00194   static PStatCollector _pixel_count_flat_textured_pcollector;
00195   static PStatCollector _pixel_count_smooth_textured_pcollector;
00196   static PStatCollector _pixel_count_white_perspective_pcollector;
00197   static PStatCollector _pixel_count_flat_perspective_pcollector;
00198   static PStatCollector _pixel_count_smooth_perspective_pcollector;
00199   static PStatCollector _pixel_count_smooth_multitex2_pcollector;
00200   static PStatCollector _pixel_count_smooth_multitex3_pcollector;
00201 
00202 public:
00203   static TypeHandle get_class_type() {
00204     return _type_handle;
00205   }
00206   static void init_type() {
00207     GraphicsStateGuardian::init_type();
00208     register_type(_type_handle, "TinyGraphicsStateGuardian",
00209                   GraphicsStateGuardian::get_class_type());
00210   }
00211   virtual TypeHandle get_type() const {
00212     return get_class_type();
00213   }
00214   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00215 
00216 private:
00217   static TypeHandle _type_handle;
00218 };
00219 
00220 #include "tinyGraphicsStateGuardian.I"
00221 
00222 #endif
 All Classes Functions Variables Enumerations