Panda3D
 All Classes Functions Variables Enumerations
dxGraphicsStateGuardian9.h
1 // Filename: dxGraphicsStateGuardian9.h
2 // Created by: mike (02Feb99)
3 // Updated by: fperazzi, PandaSE (05May10) (added get_supports_cg_profile)
4 //
5 ////////////////////////////////////////////////////////////////////
6 //
7 // PANDA 3D SOFTWARE
8 // Copyright (c) Carnegie Mellon University. All rights reserved.
9 //
10 // All use of this software is subject to the terms of the revised BSD
11 // license. You should have received a copy of this license along
12 // with this source code in a file named "LICENSE."
13 //
14 ////////////////////////////////////////////////////////////////////
15 
16 #ifndef DXGRAPHICSSTATEGUARDIAN9_H
17 #define DXGRAPHICSSTATEGUARDIAN9_H
18 
19 #include "dxgsg9base.h"
20 #include "dxTextureContext9.h"
21 #include "config_dxgsg9.h"
22 
23 #include "graphicsStateGuardian.h"
24 #include "texture.h"
25 #include "samplerState.h"
26 #include "displayRegion.h"
27 #include "material.h"
28 #include "depthTestAttrib.h"
29 #include "cullFaceAttrib.h"
30 #include "renderModeAttrib.h"
31 #include "colorBlendAttrib.h"
32 #include "fog.h"
33 #include "pointerToArray.h"
34 
35 #include "vertexElementArray.h"
36 #include "dxShaderContext9.h"
37 
38 enum GsgPageType {
39  GPT_Texture,
40  GPT_VertexBuffer,
41  GPT_IndexBuffer,
42 
43  GPT_TotalPageTypes
44 };
45 
46 class Light;
47 
48 class DXTextureContext9;
49 class DXVertexBufferContext9;
51 
52 class wdxGraphicsBuffer9;
53 
54 ////////////////////////////////////////////////////////////////////
55 // Class : DXGraphicsStateGuardian9
56 // Description : A GraphicsStateGuardian for rendering into DirectX9
57 // contexts.
58 ////////////////////////////////////////////////////////////////////
59 class EXPCL_PANDADX DXGraphicsStateGuardian9 : public GraphicsStateGuardian {
60 public:
63 
65  calc_fb_properties(DWORD cformat, DWORD dformat,
66  DWORD multisampletype, DWORD multisamplequality);
67 
68  virtual TextureContext *prepare_texture(Texture *tex, int view);
69  void apply_texture(int i, TextureContext *tc, const SamplerState &sampler);
70  virtual bool update_texture(TextureContext *tc, bool force);
71  bool upload_texture(DXTextureContext9 *dtc, bool force);
72  virtual void release_texture(TextureContext *tc);
73  virtual bool extract_texture_data(Texture *tex);
74 
76  void release_shader(ShaderContext *sc);
77 
79  bool apply_vertex_buffer(VertexBufferContext *vbc,
80  const GeomVertexArrayDataHandle *reader,
81  bool force);
82  virtual void release_vertex_buffer(VertexBufferContext *vbc);
83 
84  bool setup_array_data(CLP(VertexBufferContext)*& vbc,
85  const GeomVertexArrayDataHandle* data,
86  bool force);
87 
89  bool apply_index_buffer(IndexBufferContext *ibc,
90  const GeomPrimitivePipelineReader *reader, bool force);
91  virtual void release_index_buffer(IndexBufferContext *ibc);
92 
93  virtual void begin_occlusion_query();
94  virtual PT(OcclusionQueryContext) end_occlusion_query();
95 
96  virtual PT(GeomMunger) make_geom_munger(const RenderState *state,
97  Thread *current_thread);
98 
99  virtual void clear(DrawableRegion *clearable);
100 
102  virtual CPT(TransformState) calc_projection_mat(const Lens *lens);
103  virtual bool prepare_lens();
104 
105  virtual bool begin_frame(Thread *current_thread);
106  virtual bool begin_scene();
107  virtual void end_scene();
108  virtual void end_frame(Thread *current_thread);
109 
110  virtual bool begin_draw_primitives(const GeomPipelineReader *geom_reader,
111  const GeomMunger *munger,
112  const GeomVertexDataPipelineReader *data_reader,
113  bool force);
114  virtual bool draw_triangles(const GeomPrimitivePipelineReader *reader,
115  bool force);
116  virtual bool draw_tristrips(const GeomPrimitivePipelineReader *reader,
117  bool force);
118  virtual bool draw_trifans(const GeomPrimitivePipelineReader *reader,
119  bool force);
120  virtual bool draw_lines(const GeomPrimitivePipelineReader *reader,
121  bool force);
122  virtual bool draw_linestrips(const GeomPrimitivePipelineReader *reader,
123  bool force);
124  virtual bool draw_points(const GeomPrimitivePipelineReader *reader,
125  bool force);
126  virtual void end_draw_primitives();
127 
128  virtual bool framebuffer_copy_to_texture(Texture *tex, int view, int z,
129  const DisplayRegion *dr,
130  const RenderBuffer &rb);
131  virtual bool framebuffer_copy_to_ram(Texture *tex, int view, int z,
132  const DisplayRegion *dr,
133  const RenderBuffer &rb);
134  bool do_framebuffer_copy_to_ram(Texture *tex, int view, int z,
135  const DisplayRegion *dr,
136  const RenderBuffer &rb,
137  bool inverted);
138 
139  void reset_render_states (void);
140  virtual void reset();
141 
142  virtual void apply_fog(Fog *fog);
143 
144  virtual void bind_light(PointLight *light_obj, const NodePath &light,
145  int light_id);
146  virtual void bind_light(DirectionalLight *light_obj, const NodePath &light,
147  int light_id);
148  virtual void bind_light(Spotlight *light_obj, const NodePath &light,
149  int light_id);
150 
151  static D3DFORMAT get_index_type(Geom::NumericType numeric_type);
152  INLINE static DWORD LColor_to_D3DCOLOR(const LColor &cLColor);
153 
154  virtual void set_state_and_transform(const RenderState *state,
155  const TransformState *transform);
156 
157  bool check_dx_allocation (HRESULT result, int allocation_size, int attempts);
158 
159  INLINE HRESULT set_render_state (D3DRENDERSTATETYPE state, DWORD value);
160  INLINE HRESULT set_texture_stage_state (DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value);
161  INLINE HRESULT set_sampler_state (DWORD sampler, D3DSAMPLERSTATETYPE type, DWORD value);
162 
163  INLINE bool get_supports_render_texture() const;
164 
165  static bool get_gamma_table(void);
166  static bool static_set_gamma(bool restore, PN_stdfloat gamma);
167  bool set_gamma(PN_stdfloat gamma);
168  void restore_gamma();
169  static void atexit_function(void);
170 
171  static void set_cg_device(LPDIRECT3DDEVICE9 cg_device);
172  virtual bool get_supports_cg_profile(const string &name) const;
173 
174 
175 protected:
176  void do_issue_transform();
177  void do_issue_alpha_test();
178  void do_issue_shader();
179  void do_issue_render_mode();
180  void do_issue_rescale_normal();
181  void do_issue_color_write();
182  void do_issue_depth_test();
183  void do_issue_depth_write();
184  void do_issue_cull_face();
185  void do_issue_fog();
186  void do_issue_depth_offset();
187  void do_issue_tex_gen();
188  void do_issue_shade_model();
189  void do_issue_material();
190  void do_issue_texture();
191  void do_issue_blending();
192  void do_issue_stencil();
193  void do_issue_scissor();
194 
195  virtual void reissue_transforms();
196 
197  virtual void enable_lighting(bool enable);
198  virtual void set_ambient_light(const LColor &color);
199  virtual void enable_light(int light_id, bool enable);
200 
201  virtual void enable_clip_plane(int plane_id, bool enable);
202  virtual void bind_clip_plane(const NodePath &plane, int plane_id);
203 
204  virtual void close_gsg();
205  void free_nondx_resources();
206  void free_d3d_device();
207 
208  void set_draw_buffer(const RenderBuffer &rb);
209  void set_read_buffer(const RenderBuffer &rb);
210 
211  void do_auto_rescale_normal();
212 
213  void disable_standard_vertex_arrays();
214  bool update_standard_vertex_arrays(bool force);
215  void disable_standard_texture_bindings();
216  void update_standard_texture_bindings();
217 
218 protected:
219  INLINE static D3DTEXTUREADDRESS get_texture_wrap_mode(SamplerState::WrapMode wm);
220  INLINE static D3DFOGMODE get_fog_mode_type(Fog::Mode m);
221  const D3DCOLORVALUE &get_light_color(Light *light) const;
222  INLINE static D3DTRANSFORMSTATETYPE get_tex_mat_sym(int stage_index);
223 
224  static D3DBLEND get_blend_func(ColorBlendAttrib::Operand operand);
225  void report_texmgr_stats();
226 
227  void set_context(DXScreenData *new_context);
228  void set_render_target();
229 
230  void set_texture_blend_mode(int i, const TextureStage *stage);
231 
232  void dx_cleanup();
233  HRESULT reset_d3d_device(D3DPRESENT_PARAMETERS *p_presentation_params,
234  DXScreenData **screen = NULL);
235 
236  bool check_cooperative_level();
237 
238  void show_frame();
239 
240  bool create_swap_chain (DXScreenData *new_context);
241  bool release_swap_chain (DXScreenData *new_context);
242  void copy_pres_reset(DXScreenData *new_context);
243 
244  static D3DTEXTUREFILTERTYPE get_d3d_min_type(SamplerState::FilterType filter_type);
245  static D3DTEXTUREFILTERTYPE get_d3d_mip_type(SamplerState::FilterType filter_type);
246  static D3DTEXTUREOP get_texture_operation(TextureStage::CombineMode mode, int scale);
247  DWORD get_texture_argument(TextureStage::CombineSource source,
248  TextureStage::CombineOperand operand) const;
249  static DWORD get_texture_argument_modifier(TextureStage::CombineOperand operand);
250 
251  void draw_primitive_up(D3DPRIMITIVETYPE primitive_type,
252  unsigned int primitive_count,
253  unsigned int first_vertex,
254  unsigned int num_vertices,
255  const unsigned char *buffer, size_t stride);
256  void draw_indexed_primitive_up(D3DPRIMITIVETYPE primitive_type,
257  unsigned int min_index, unsigned int max_index,
258  unsigned int num_primitives,
259  const unsigned char *index_data,
260  D3DFORMAT index_type,
261  const unsigned char *buffer, size_t stride);
262 
263  INLINE static unsigned char *get_safe_buffer_start();
264 
265 public:
266  DXScreenData *_screen;
267 
268 protected:
269  LPDIRECT3DDEVICE9 _d3d_device; // same as _screen->_d3d_device, cached for spd
270  IDirect3DSwapChain9 *_swap_chain;
271  D3DPRESENT_PARAMETERS _presentation_reset; // This is built during reset device
272 
273  bool _dx_is_ready;
274  HRESULT _last_testcooplevel_result;
275 
276  bool _vertex_blending_enabled;
277  bool _supports_render_texture;
278 
279  RenderBuffer::Type _cur_read_pixel_buffer; // source for copy_pixel_buffer operation
280  bool _auto_rescale_normal;
281 
282  PN_stdfloat _material_ambient;
283  PN_stdfloat _material_diffuse;
284  PN_stdfloat _material_specular;
285  PN_stdfloat _material_shininess;
286  PN_stdfloat _material_emission;
287 
288  enum DxgsgFogType {
289  None,
290  PerVertexFog=D3DRS_FOGVERTEXMODE,
291  PerPixelFog=D3DRS_FOGTABLEMODE
292  };
293  DxgsgFogType _do_fog_type;
294 
295  D3DVIEWPORT9 _current_viewport;
296  bool _supports_depth_bias;
297 
298  DWORD _clip_plane_bits;
299  CullFaceAttrib::Mode _cull_face_mode;
300  RenderModeAttrib::Mode _current_fill_mode; //point/wireframe/solid
301 
302  PT(Shader) _current_shader;
303  CLP(ShaderContext) *_current_shader_context;
304  PT(Shader) _vertex_array_shader;
305  CLP(ShaderContext) *_vertex_array_shader_context;
306  PT(Shader) _texture_binding_shader;
307  CLP(ShaderContext) *_texture_binding_shader_context;
308 
309  const DXIndexBufferContext9 *_active_ibuffer;
310 
311  bool _overlay_windows_supported;
312  bool _tex_stats_retrieval_impossible;
313  bool _supports_texture_constant_color;
314  DWORD _constant_color_operand;
315 
316  static D3DMATRIX _d3d_ident_mat;
317 
318  static unsigned char *_temp_buffer;
319  static unsigned char *_safe_buffer_start;
320 
321  int _gsg_managed_textures;
322  int _gsg_managed_vertex_buffers;
323  int _gsg_managed_index_buffers;
324  UINT _available_texture_memory;
325 
326  DWORD _last_fvf;
327  int _num_bound_streams;
328 
329  // Cache the data necessary to bind each particular light each
330  // frame, so if we bind a given light multiple times, we only have
331  // to compute its data once.
333  DirectionalLights _dlights;
334 
335  #define MAXIMUM_TEXTURES 16
336 
337 
338  // from D3DRENDERSTATETYPE + pad
339  #define MAXIMUM_RENDER_STATES 256
340 
341  // from D3DTEXTURESTAGESTATETYPE + pad
342  #define MAXIMUM_TEXTURE_STAGE_STATES 40
343  typedef struct {
344  DWORD state_array [MAXIMUM_TEXTURE_STAGE_STATES];
345  }
346  TextureStageStates;
347 
348  // from D3DSAMPLERSTATETYPE + pad
349  #define MAXIMUM_TEXTURE_RENDER_STATES 16
350  typedef struct {
351  DWORD state_array [MAXIMUM_TEXTURE_RENDER_STATES];
352  }
353  TextureRenderStates;
354 
355  // from D3DRENDERSTATETYPE
356  DWORD _render_state_array [MAXIMUM_RENDER_STATES];
357  TextureStageStates _texture_stage_states_array [D3D_MAXTEXTURESTAGES];
358  TextureRenderStates _texture_render_states_array [MAXIMUM_TEXTURES];
359 
360  int _num_active_texture_stages;
361 
362  int _vertex_shader_version_major;
363  int _vertex_shader_version_minor;
364  int _pixel_shader_version_major;
365  int _pixel_shader_version_minor;
366 
367  char *_vertex_shader_profile;
368  char *_pixel_shader_profile;
369 
370  int _vertex_shader_maximum_constants;
371 
372  bool _supports_stream_offset;
373 
374  list <wdxGraphicsBuffer9 **> _graphics_buffer_list;
375 
376  int _supports_gamma_calibration;
377 
378 #ifdef HAVE_CG
379  CGcontext _cg_context;
380  static LPDIRECT3DDEVICE9 _cg_device;
381 #endif
382 
383 public:
384  virtual TypeHandle get_type() const {
385  return get_class_type();
386  }
387  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
388 
389  static TypeHandle get_class_type() {
390  return _type_handle;
391  }
392 
393 public:
394  static void init_type() {
395  GraphicsStateGuardian::init_type();
396  register_type(_type_handle, "DXGraphicsStateGuardian9",
397  GraphicsStateGuardian::get_class_type());
398  }
399 
400 private:
401  static TypeHandle _type_handle;
402 
403  friend class wdxGraphicsWindow9;
404  friend class wdxGraphicsPipe9;
405  friend class wdxGraphicsWindowGroup9;
406  friend class DXTextureContext9;
407  friend class wdxGraphicsBuffer9;
408  friend class DXVertexBufferContext9;
409  friend class DXIndexBufferContext9;
410  friend class DXShaderContext9;
411 };
412 
413 #include "dxGraphicsStateGuardian9.I"
414 
415 #endif
A GraphicsStateGuardian for rendering into DirectX9 contexts.
A light shining from infinitely far away in a particular direction, like sunlight.
The abstract interface to all kinds of lights.
Definition: light.h:42
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 void release_shader(ShaderContext *sc)
Releases the resources allocated by prepare_shader.
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 release_index_buffer(IndexBufferContext *ibc)
Frees the resources previously allocated via a call to prepare_data(), including deleting the IndexBu...
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
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.
virtual void end_draw_primitives()
Called after a sequence of draw_primitive() functions are called, this should do whatever cleanup is ...
virtual bool get_supports_cg_profile(const string &name) const
Returns true if this particular GSG supports the specified Cg Shader Profile.
Objects of this class are used to convert vertex data from a Geom into a format suitable for passing ...
Definition: geomMunger.h:57
This is a special class object that holds all the information returned by a particular GSG to indicat...
virtual bool set_gamma(PN_stdfloat gamma)
Set gamma.
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 an abstract base class for a family of classes that represent the fundamental geometry primit...
Definition: geomPrimitive.h:63
Definition: shader.h:50
This is a special class object that holds all the information returned by a particular GSG to indicat...
virtual void restore_gamma()
Restore original gamma setting.
Caches a GeomPrimitive in the DirectX device as an index buffer.
virtual void release_vertex_buffer(VertexBufferContext *vbc)
Frees the resources previously allocated via a call to prepare_data(), including deleting the VertexB...
This data object is returned by GeomVertexArrayData::get_handle() or modify_handle().
Encapsulates the data from a Geom, pre-fetched for one stage of the pipeline.
Definition: geom.h:401
The ShaderContext is meant to contain the compiled version of a shader string.
Definition: shaderContext.h:35
virtual bool draw_tristrips(const GeomPrimitivePipelineReader *reader, bool force)
Draws a series of triangle strips.
virtual void set_state_and_transform(const RenderState *state, const TransformState *transform)
Simultaneously resets the render state and the transform state.
An offscreen render buffer.
Specifies how atmospheric fog effects are applied to geometry.
Definition: fog.h:46
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 begin_occlusion_query()
Begins a new occlusion query.
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 ShaderContext * prepare_shader(Shader *shader)
Compile a vertex/fragment shader body.
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:53
Represents a set of settings that indicate how a texture is sampled.
Definition: samplerState.h:39
A single graphics window for rendering DirectX under Microsoft Windows.
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
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...
virtual bool draw_trifans(const GeomPrimitivePipelineReader *reader, bool force)
Draws a series of triangle fans.
A thread; that is, a lightweight process.
Definition: thread.h:51
This is a special class object that holds all the information returned by a particular GSG to indicat...
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 &quot;scene&quot; (usual...
virtual bool draw_linestrips(const GeomPrimitivePipelineReader *reader, bool force)
Draws a series of line strips.
virtual VertexBufferContext * prepare_vertex_buffer(GeomVertexArrayData *data)
Prepares the indicated buffer for retained-mode rendering.
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 graphics pipe represents the interface for creating DirectX9 graphics windows.
Returned from a GSG in response to begin_occlusion_query() .
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
A container for the various kinds of properties we might ask to have on a graphics frameBuffer before...
virtual IndexBufferContext * prepare_index_buffer(GeomPrimitive *data)
Prepares the indicated buffer for retained-mode rendering.
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...
Defines the properties of a named stage of the multitexture pipeline.
Definition: textureStage.h:38
A RenderBuffer is an arbitrary subset of the various layers (depth buffer, color buffer, etc.) of a drawing region.
Definition: renderBuffer.h:30
virtual bool extract_texture_data(Texture *tex)
This method should only be called by the GraphicsEngine.
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165
A light originating from a single point in space, and shining in all directions.
Definition: pointLight.h:27
This is the data for one array of a GeomVertexData structure.
virtual bool begin_scene()
Called between begin_frame() and end_frame() to mark the beginning of drawing commands for a &quot;scene&quot; ...
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&#39;s properties.