fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / Contrib / Techniques / OSGClusterShadingStage.h
blob147d798f1314a313137b4f38a0aadb4a73a4dc7b
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2013 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, carsten_neumann@gmx.net *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 #ifndef _OSGCLUSTERSHADINGSTAGE_H_
40 #define _OSGCLUSTERSHADINGSTAGE_H_
41 #ifdef __sgi
42 #pragma once
43 #endif
45 #include "OSGClusterShadingStageBase.h"
46 #include "OSGAction.h"
47 #include "OSGClusterShadingStageDataFields.h"
49 #include "OSGPlane.h"
50 #include "OSGImageFields.h"
51 #include "OSGTextureObjChunkFields.h"
52 #include "OSGTextureImageChunkFields.h"
53 #include "OSGShaderStorageBufferObjStdLayoutChunkFields.h"
54 #include "OSGUniformBufferObjStd140ChunkFields.h"
55 #include "OSGAlgorithmComputeElementFields.h"
57 OSG_BEGIN_NAMESPACE
59 class Camera;
60 class DrawEnv;
61 class RenderAction;
63 /*! \brief ClusterShadingStage class. See \ref
64 PageContribTechniquesClusterShadingStage for a description.
67 class OSG_CONTRIBTECHNIQUES_DLLMAPPING ClusterShadingStage : public ClusterShadingStageBase
69 protected:
71 /*========================== PUBLIC =================================*/
73 public:
75 typedef ClusterShadingStageBase Inherited;
76 typedef ClusterShadingStage Self;
78 /*---------------------------------------------------------------------*/
79 /*! \name Sync */
80 /*! \{ */
82 virtual void changed(ConstFieldMaskArg whichField,
83 UInt32 origin,
84 BitVector details );
86 /*! \} */
87 /*---------------------------------------------------------------------*/
88 /*! \name Output */
89 /*! \{ */
91 virtual void dump( UInt32 uiIndent = 0,
92 const BitVector bvFlags = 0) const;
94 /*! \} */
95 /*---------------------------------------------------------------------*/
96 /*! \name Data */
97 /*! \{ */
99 void initData (RenderAction *pAction,
100 Int32 iVPLeft,
101 Int32 iVPBottom,
102 Int32 iVPWidth,
103 Int32 iVPHeight);
105 void updateData(RenderAction *pAction,
106 Int32 iVPLeft,
107 Int32 iVPBottom,
108 Int32 iVPWidth,
109 Int32 iVPHeight);
111 /*! \} */
113 /*---------------------------------------------------------------------*/
114 /*! \name Shader Program Snippets */
115 /*! \{ */
117 std::string getFragmentProgramSnippet () const;
119 std::string getClusteringProgSnippet (bool is_frag_shader = true) const;
120 std::string getFrustumProgSnippet () const;
121 std::string getLightIndexProgSnippet () const;
123 /*! \} */
124 /*========================= PROTECTED ===============================*/
126 protected:
128 /*---------------------------------------------------------------------*/
129 /*! \name Shader Program Snippets */
130 /*! \{ */
132 std::string getDispatchProgSnippet () const;
134 /*! \} */
136 /*---------------------------------------------------------------------*/
137 /*! \name Helper Types */
138 /*! \{ */
141 // simple frustum with only left, right, top and bottom planes.
142 // Front and back planes are calculated differently.
144 struct Frustum
146 Plane planes[4];
150 // data that are used in the compute shader code
152 struct DispatchData
154 DispatchData() : matTransf(), viewport(), numTiles() {}
156 Matrix matTransf;
157 Vec4u viewport;
158 Vec2i numTiles;
162 // The clustering data are used in the light culling and in the
163 // fragment shader for the cluster key and z-coord calculation.
164 // Addditional some helper flags are added to this struct.
166 struct ClusteringData
168 ClusteringData()
169 : zNear (0.f), zFar(0.f) //, D(0.f)
170 , nD (0.f), lg_nD(0.f)
171 , a (0.f), b(0.f) //, c(0)
172 , c_1 (0), p_v (0,0) //, n_c(0,0,0)
173 , enabled(true)
176 Real32 zNear; // positive near plane distance from eye zNear > 0
177 Real32 zFar; // positive far plane distance from eye zFar > zNear > 0
178 //Real32 D; // positive near plane offset D >= 0, used only for testing
179 Real32 nD; // zNear + D : shader optimization
180 Real32 lg_nD; // log2(nD) : shader optimization
181 Real32 a; // precalculated factor (c-1)/log2(f/(n+D))
182 Real32 b; // precalculated factor log2(f/(n+D))/(c-1)
183 //Int32 c; // number of cluster planes used only for testing
184 Int32 c_1; // number of cluster planes minus one : shader optimization
185 Vec2i p_v; // viewport corner points
186 //Vec3i n_c; // number of clusters, used only for testing
187 bool enabled;// enable cluster shading
190 struct LightEyeSpaceData
192 Pnt3f position;
193 Vec3f direction;
195 LightEyeSpaceData(void) :
196 position (0.f, 0.f, 0.f),
197 direction(0.f, 0.f, 0.f)
202 typedef std::vector<Frustum> VecFrustumsT;
203 typedef std::pair<UInt32, UInt32> ImageDataT;
204 typedef std::vector<ImageDataT> VecImageDataT;
205 typedef std::vector<UInt32> VecLightIndexT;
206 typedef std::vector<LightEyeSpaceData> VecLightEyeSpaceDataT;
208 struct Sphere
210 Pnt3f c; // Center point.
211 Real32 r; // Radius.
213 Sphere(void) :
214 c(0.f, 0.f, 0.f),
215 r(0.f )
220 struct Cone
222 Pnt3f T; // Cone tip.
223 Real32 h; // Height of the cone.
224 Vec3f d; // Direction of the cone.
225 Real32 r; // bottom radius of the cone.
227 Cone(void) :
228 T(0.f, 0.f, 0.f),
229 h(0.f ),
230 d(0.f, 0.f, 0.f),
231 r(0.f )
236 /*! \} */
238 // Variables should all be in ClusterShadingStageBase.
240 /*---------------------------------------------------------------------*/
241 /*! \name Constructors */
242 /*! \{ */
244 ClusterShadingStage(void);
245 ClusterShadingStage(const ClusterShadingStage &source);
247 /*! \} */
248 /*---------------------------------------------------------------------*/
249 /*! \name Destructors */
250 /*! \{ */
252 virtual ~ClusterShadingStage(void);
254 /*! \} */
255 /*---------------------------------------------------------------------*/
256 /*! \name Draw */
257 /*! \{ */
259 Action::ResultE renderEnter(Action *action);
260 Action::ResultE renderLeave(Action *action);
262 /*! \} */
263 /*---------------------------------------------------------------------*/
264 /*! \name Init */
265 /*! \{ */
267 static void initMethod(InitPhase ePhase);
269 /*! \} */
270 /*---------------------------------------------------------------------*/
271 /*! \name StageData */
272 /*! \{ */
274 void setupStageData (ClusterShadingStageData* pData,
275 Camera* pCam,
276 Int32 iPixelLeft,
277 Int32 iPixelBottom,
278 Int32 iPixelWidth,
279 Int32 iPixelHeight);
281 void updateStageData (ClusterShadingStageData* pData,
282 Camera* pCam,
283 Int32 iPixelLeft,
284 Int32 iPixelBottom,
285 Int32 iPixelWidth,
286 Int32 iPixelHeight);
288 /*! \} */
289 /*---------------------------------------------------------------------*/
290 /*! \name StageData Details */
291 /*! \{ */
292 /*! \} */
294 /*---------------------------------------------------------------------*/
295 /*! \name Frustum Details */
296 /*! \{ */
297 void resize_frustums
298 (ClusterShadingStageData* pData,
299 std::size_t size);
301 Frustum getFrustum
302 (ClusterShadingStageData* pData,
303 const UInt32 idx) const;
305 void setFrustum
306 (ClusterShadingStageData* pData,
307 const UInt32 idx,
308 const Frustum& frustum);
310 std::size_t calc_frustum_buffer_size
311 (const VecFrustumsT& vFrustums);
313 std::vector<UInt8> create_frustum_buffer
314 (const VecFrustumsT& vFrustums);
316 ShaderStorageBufferObjStdLayoutChunkTransitPtr
317 create_frustum_state
318 (const VecFrustumsT& vFrustums);
320 void update_frustum_state
321 (ShaderStorageBufferObjStdLayoutChunk* ssbo,
322 const VecFrustumsT& vFrustums);
324 std::size_t calc_frustum_buffer_size
325 (ClusterShadingStageData* pData);
327 std::vector<UInt8> create_frustum_buffer
328 (ClusterShadingStageData* pData);
330 ShaderStorageBufferObjStdLayoutChunkTransitPtr
331 create_frustum_state
332 (ClusterShadingStageData* pData);
334 void update_frustum_state
335 (ShaderStorageBufferObjStdLayoutChunk* ssbo,
336 ClusterShadingStageData* pData);
338 void update_frustum_state
339 (ClusterShadingStageData* pData,
340 const VecFrustumsT& vFrustums);
342 bool check_frustum_state
343 (ClusterShadingStageData* pData,
344 const Vec4u& viewport);
346 Pnt4f NdcFromScreen
347 (const Pnt3f& p_w,
348 const Vec4u& viewport,
349 Real32 zNear,
350 Real32 zFar);
352 Pnt4f NdcFromScreen
353 (const Pnt3f& p_w, // with z-coord already
354 const Vec4u& viewport); // given in ndc coords!
356 Pnt3f EyeFromNdc
357 (const Pnt4f& p_n,
358 const Matrix& matInvProjection);
360 bool isOrthographicCamera
361 (Camera* cam,
362 UInt32 width,
363 UInt32 height);
365 void calc_ortho_frustums_cpu
366 (const Vec4u& viewport,
367 const Matrix& matInvProjection,
368 VecFrustumsT& frustums);
370 void calc_persp_frustums_cpu
371 (const Vec4u& viewport,
372 const Matrix& matInvProjection,
373 VecFrustumsT& frustums);
374 /*! \} */
375 /*---------------------------------------------------------------------*/
376 /*! \name Image Details */
377 /*! \{ */
378 ImageTransitPtr create_image
379 (UInt32 width,
380 UInt32 height,
381 UInt32 depth,
382 bool allocate_memory);
384 void update_image
385 (Image* image,
386 UInt32 width,
387 UInt32 height,
388 UInt32 depth,
389 bool allocate_memory);
391 void update_image_data
392 (Image* image,
393 const VecImageDataT& imageData);
395 void write_image_data
396 (UInt32 i,
397 UInt32 j,
398 UInt32 k,
399 const Vec3u& dimensions,
400 const ImageDataT& data,
401 VecImageDataT& imageData);
403 TextureObjChunkTransitPtr
404 create_texture_state
405 (Image* image);
407 void update_texture_state
408 (TextureObjChunk* texObjChunk);
410 TextureImageChunkTransitPtr
411 create_texture_image_state
412 (TextureObjChunk* texObjChunk,
413 const GLenum access);
415 void update_light_grid_image
416 (const Vec4u& viewport,
417 Image* image,
418 bool allocate_memory);
420 /*! \} */
421 /*---------------------------------------------------------------------*/
422 /*! \name Light Index Details */
423 /*! \{ */
424 std::size_t calc_light_index_buffer_size
425 (const VecLightIndexT& vIndexList);
427 std::vector<UInt8> create_light_index_buffer
428 (const VecLightIndexT& vIndexList);
430 ShaderStorageBufferObjStdLayoutChunkTransitPtr
431 create_index_state
432 (const VecLightIndexT& vIndexList);
434 ShaderStorageBufferObjStdLayoutChunkTransitPtr
435 create_index_state
436 (std::size_t sz);
438 void update_index_state
439 (ShaderStorageBufferObjStdLayoutChunk* ssbo,
440 const VecLightIndexT& vIndexList);
442 void clear_index_state
443 (ShaderStorageBufferObjStdLayoutChunk* ssbo,
444 std::size_t sz);
445 /*! \} */
446 /*---------------------------------------------------------------------*/
447 /*! \name Light Index Counter Details */
448 /*! \{ */
449 ShaderStorageBufferObjStdLayoutChunkTransitPtr
450 create_light_index_counter_state();
452 void clear_light_index_counter_state
453 (ShaderStorageBufferObjStdLayoutChunk* ssbo);
454 /*! \} */
455 /*---------------------------------------------------------------------*/
456 /*! \name Dispatch Data Details */
457 /*! \{ */
458 std::size_t calc_dispatch_data_buffer_size
459 (const DispatchData& data);
461 std::vector<UInt8> create_dispatch_data_buffer
462 (const DispatchData& data);
464 UniformBufferObjStd140ChunkTransitPtr
465 create_dispatch_data
466 (const DispatchData& data);
468 void update_dispatch_data
469 (UniformBufferObjStd140Chunk* ubo,
470 const DispatchData& data);
471 /*! \} */
472 /*---------------------------------------------------------------------*/
473 /*! \name Clustering Data Details */
474 /*! \{ */
475 std::size_t calc_clustering_data_buffer_size
476 (const ClusteringData& data);
478 std::vector<UInt8> create_clustering_data_buffer
479 (const ClusteringData& data);
481 UniformBufferObjStd140ChunkTransitPtr
482 create_clustering_data
483 (const ClusteringData& data);
485 void update_clustering_data
486 (UniformBufferObjStd140Chunk* ubo,
487 const ClusteringData& data);
488 /*! \} */
489 /*---------------------------------------------------------------------*/
490 /*! \name Plane and Frustums Test */
491 /*! \{ */
492 bool PointInsidePlane (const Pnt3f& p,
493 const Plane& plane);
495 bool SphereInsidePlane (const Sphere& sphere,
496 const Plane& plane);
498 bool SphereInsideFrustum (const Sphere& sphere,
499 const Frustum& frustum,
500 Real32 zNear,
501 Real32 zFar);
503 bool ConeInsidePlane (const Cone& cone,
504 const Plane& plane);
506 bool ConeInsideFrustum (const Cone& cone,
507 const Frustum& frustum,
508 Real32 zNear,
509 Real32 zFar);
510 /*! \} */
511 /*---------------------------------------------------------------------*/
512 /*! \name Cluster Access */
513 /*! \{ */
514 UInt32 cluster_k (Real32 z_e,
515 Real32 n,
516 Real32 f,
517 Real32 D,
518 UInt32 c);
520 Real32 cluster_z (UInt32 k,
521 Real32 n,
522 Real32 f,
523 Real32 D,
524 UInt32 c);
526 UInt32 cluster_k (Real32 z_e,
527 Real32 nD,
528 Real32 lg_nD,
529 Real32 f,
530 Real32 a,
531 UInt32 c_1);
533 Real32 cluster_z (UInt32 k,
534 Real32 n,
535 Real32 f,
536 Real32 nD,
537 Real32 b,
538 UInt32 c_1);
539 /*! \} */
540 /*---------------------------------------------------------------------*/
541 /*! \name Compute Shader Programs */
542 /*! \{ */
544 std::string get_persp_frustum_cp_program();
545 std::string get_ortho_frustum_cp_program();
546 std::string get_light_culling_cp_program();
547 std::string get_fragment_cp_program();
549 /*! \} */
550 /*---------------------------------------------------------------------*/
551 /*! \name Computation */
552 /*! \{ */
554 void create_computation(ClusterShadingStageData* pData);
555 void update_computation(ClusterShadingStageData* pData);
557 /*! \} */
558 /*---------------------------------------------------------------------*/
559 /*! \name Frustum Computation */
560 /*! \{ */
562 void create_frustum_computation(ClusterShadingStageData* pData);
564 /*! \} */
565 /*---------------------------------------------------------------------*/
566 /*! \name Light Culling Computation */
567 /*! \{ */
569 void create_light_culling_computation(ClusterShadingStageData* pData);
571 /*! \} */
572 /*---------------------------------------------------------------------*/
573 /*! \name Fragment Shader */
574 /*! \{ */
576 void create_fragment_shader(ClusterShadingStageData* pData);
578 /*! \} */
579 /*---------------------------------------------------------------------*/
580 /*! \name Update Dispatch Configurations */
581 /*! \{ */
583 void update_dispatch_config_frustums
584 (ClusterShadingStageData* pData,
585 const Vec4u& viewport,
586 const Matrix& matInvProjection);
588 void update_dispatch_config_cull_lights
589 (ClusterShadingStageData* pData,
590 const Vec4u& viewport,
591 const Matrix& matViewing);
593 /*! \} */
594 /*---------------------------------------------------------------------*/
595 /*! \name Calculate Affected Lights */
596 /*! \{ */
598 void calc_affected_lights (const Matrix& matEyeFromWorld,
599 FrustumVolume volViewFrustum,
600 VecLightIndexT& vecAffectedLights,
601 VecLightEyeSpaceDataT& vecEyeSpaceData);
603 void transform_to_eye_space (Matrix matEyeFromWorld,
604 Node* beacon,
605 const Pnt3f& position_bs,
606 const Vec3f& direction_bs,
607 Pnt3f& position_es,
608 Vec3f& direction_es);
610 bool point_light_inside_frustum
611 (const Pnt3f& position_es,
612 Real32 range,
613 const Frustum& frustum,
614 Real32 n,
615 Real32 f);
617 bool spot_light_inside_frustum
618 (const Pnt3f& position_es,
619 const Vec3f& direction_es,
620 Real32 range,
621 Real32 spotlightAngle,
622 const Frustum& frustum,
623 Real32 n,
624 Real32 f);
626 /*! \} */
627 /*---------------------------------------------------------------------*/
628 /*! \name CPU Light Culling */
629 /*! \{ */
631 std::size_t frustum_accessor
632 (const UInt32 i,
633 const UInt32 j,
634 const Vec3u& dimensions);
636 void cull_lights
637 (ClusterShadingStageData* pData,
638 const VecLightIndexT& vecAffectedLights,
639 const VecLightEyeSpaceDataT& vecEyeSpaceData,
640 const Vec3u& dimensions,
641 const Matrix& matEyeFromWorld,
642 Real32 zNear,
643 Real32 zFar,
644 Real32 D,
645 VecImageDataT& gridData,
646 VecLightIndexT& lightIndexList);
648 /*! \} */
649 /*========================== PRIVATE ================================*/
651 private:
653 bool _bSetupStage;
655 friend class FieldContainer;
656 friend class ClusterShadingStageBase;
658 // prohibit default functions (move to 'public' if you need one)
659 void operator =(const ClusterShadingStage &source);
662 typedef ClusterShadingStage *ClusterShadingStageP;
664 OSG_END_NAMESPACE
666 #include "OSGClusterShadingStageBase.inl"
667 #include "OSGClusterShadingStage.inl"
669 #endif /* _OSGCLUSTERSHADINGSTAGE_H_ */