Panda3D

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                                       Lens::StereoChannel stereo_channel);
00063   virtual CPT(TransformState) calc_projection_mat(const Lens *lens);
00064   virtual bool prepare_lens();
00065 
00066   virtual bool begin_frame(Thread *current_thread);
00067   virtual bool begin_scene();
00068   virtual void end_scene();
00069   virtual void end_frame(Thread *current_thread);
00070 
00071   virtual bool begin_draw_primitives(const GeomPipelineReader *geom_reader,
00072                                      const GeomMunger *munger,
00073                                      const GeomVertexDataPipelineReader *data_reader,
00074                                      bool force);
00075   virtual bool draw_triangles(const GeomPrimitivePipelineReader *reader,
00076                               bool force);
00077   virtual bool draw_tristrips(const GeomPrimitivePipelineReader *reader,
00078                               bool force);
00079   virtual bool draw_lines(const GeomPrimitivePipelineReader *reader,
00080                           bool force);
00081   virtual bool draw_points(const GeomPrimitivePipelineReader *reader,
00082                            bool force);
00083   virtual void end_draw_primitives();
00084 
00085   virtual bool framebuffer_copy_to_texture
00086   (Texture *tex, int z, const DisplayRegion *dr, const RenderBuffer &rb);
00087   virtual bool framebuffer_copy_to_ram
00088   (Texture *tex, int z, const DisplayRegion *dr, const RenderBuffer &rb);
00089 
00090   virtual void set_state_and_transform(const RenderState *state,
00091                                        const TransformState *transform);
00092 
00093   virtual TextureContext *prepare_texture(Texture *tex);
00094   virtual bool update_texture(TextureContext *tc, bool force);
00095   bool update_texture(TextureContext *tc, bool force, int stage_index);
00096   virtual void release_texture(TextureContext *tc);
00097 
00098   virtual void do_issue_light();
00099   virtual void bind_light(PointLight *light_obj, const NodePath &light,
00100                           int light_id);
00101   virtual void bind_light(DirectionalLight *light_obj, const NodePath &light,
00102                           int light_id);
00103   virtual void bind_light(Spotlight *light_obj, const NodePath &light,
00104                           int light_id);
00105 
00106 private:
00107   void do_issue_transform();
00108   void do_issue_render_mode();
00109   void do_issue_cull_face();
00110   void do_issue_rescale_normal();
00111   void do_issue_depth_offset();
00112   void do_issue_material();
00113   void do_issue_texture();
00114   void do_issue_scissor();
00115 
00116   void set_scissor(float left, float right, float bottom, float top);
00117 
00118   bool apply_texture(TextureContext *tc);
00119   bool upload_texture(TinyTextureContext *gtc, bool force);
00120   bool upload_simple_texture(TinyTextureContext *gtc);
00121   bool setup_gltex(GLTexture *gltex, int x_size, int y_size, int num_levels);
00122   int get_tex_shift(int orig_size);
00123 
00124   static void copy_lum_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level);
00125   static void copy_alpha_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level);
00126   static void copy_one_channel_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level, int channel);
00127   static void copy_la_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level);
00128   static void copy_rgb_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level);
00129   static void copy_rgba_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level);
00130 
00131   void setup_material(GLMaterial *gl_material, const Material *material);
00132   void do_auto_rescale_normal();
00133   static void load_matrix(M4 *matrix, const TransformState *transform);
00134   static int get_color_blend_op(ColorBlendAttrib::Operand operand);
00135   static ZB_lookupTextureFunc get_tex_filter_func(Texture::FilterType filter);
00136   static ZB_texWrapFunc get_tex_wrap_func(Texture::WrapMode wrap_mode);
00137 
00138   INLINE void clear_light_state();
00139 
00140   // Methods used to generate texture coordinates.
00141   class TexCoordData {
00142   public:
00143     GeomVertexReader _r1;
00144     GeomVertexReader _r2;
00145     LMatrix4f _mat;
00146   };
00147   typedef void GenTexcoordFunc(V2 &result, TexCoordData &tcdata);
00148 
00149   static void texgen_null(V2 &result, TexCoordData &tcdata);
00150   static void texgen_simple(V2 &result, TexCoordData &tcdata);
00151   static void texgen_texmat(V2 &result, TexCoordData &tcdata);
00152   static void texgen_sphere_map(V2 &result, TexCoordData &tcdata);
00153 public:
00154   // Filled in by the Tiny*GraphicsWindow at begin_frame().
00155   ZBuffer *_current_frame_buffer;
00156 
00157 private:
00158   // Allocated by prepare_display_region when necessary for a zoomed
00159   // display region.
00160   ZBuffer *_aux_frame_buffer;
00161 
00162   GLContext *_c;
00163 
00164   enum ColorMaterialFlags {
00165     CMF_ambient   = 0x001,
00166     CMF_diffuse   = 0x002,
00167   };
00168   int _color_material_flags;
00169   int _texturing_state;
00170   int _texfilter_state;
00171   bool _texture_replace;
00172   bool _filled_flat;
00173   bool _auto_rescale_normal;
00174   RenderState::SlotMask _inv_state_mask;
00175 
00176   CPT(TransformState) _scissor_mat;
00177 
00178   // Cache the data necessary to bind each particular light each
00179   // frame, so if we bind a given light multiple times, we only have
00180   // to compute its data once.
00181   typedef pmap<NodePath, GLLight> Lights;
00182   Lights _plights, _dlights, _slights;
00183 
00184   // Used during being_draw_primitives() .. end_draw_primitives().
00185   int _min_vertex;
00186   int _max_vertex;
00187   GLVertex *_vertices;
00188   int _vertices_size;
00189 
00190   static PStatCollector _vertices_immediate_pcollector;
00191   static PStatCollector _draw_transform_pcollector;
00192   static PStatCollector _pixel_count_white_untextured_pcollector;
00193   static PStatCollector _pixel_count_flat_untextured_pcollector;
00194   static PStatCollector _pixel_count_smooth_untextured_pcollector;
00195   static PStatCollector _pixel_count_white_textured_pcollector;
00196   static PStatCollector _pixel_count_flat_textured_pcollector;
00197   static PStatCollector _pixel_count_smooth_textured_pcollector;
00198   static PStatCollector _pixel_count_white_perspective_pcollector;
00199   static PStatCollector _pixel_count_flat_perspective_pcollector;
00200   static PStatCollector _pixel_count_smooth_perspective_pcollector;
00201   static PStatCollector _pixel_count_smooth_multitex2_pcollector;
00202   static PStatCollector _pixel_count_smooth_multitex3_pcollector;
00203 
00204 public:
00205   static TypeHandle get_class_type() {
00206     return _type_handle;
00207   }
00208   static void init_type() {
00209     GraphicsStateGuardian::init_type();
00210     register_type(_type_handle, "TinyGraphicsStateGuardian",
00211                   GraphicsStateGuardian::get_class_type());
00212   }
00213   virtual TypeHandle get_type() const {
00214     return get_class_type();
00215   }
00216   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00217 
00218 private:
00219   static TypeHandle _type_handle;
00220 };
00221 
00222 #include "tinyGraphicsStateGuardian.I"
00223 
00224 #endif
 All Classes Functions Variables Enumerations