Commit | Line | Data |
---|---|---|
b311480e | 1 | // Created on: 2011-09-20 |
2 | // Created by: Sergey ZERCHANINOV | |
1981cb22 | 3 | // Copyright (c) 2011-2013 OPEN CASCADE SAS |
b311480e | 4 | // |
5 | // The content of this file is subject to the Open CASCADE Technology Public | |
6 | // License Version 6.5 (the "License"). You may not use the content of this file | |
7 | // except in compliance with the License. Please obtain a copy of the License | |
8 | // at http://www.opencascade.org and read it completely before using this file. | |
9 | // | |
10 | // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its | |
11 | // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France. | |
12 | // | |
13 | // The Original Code and all software distributed under the License is | |
14 | // distributed on an "AS IS" basis, without warranty of any kind, and the | |
15 | // Initial Developer hereby disclaims all such warranties, including without | |
16 | // limitation, any warranties of merchantability, fitness for a particular | |
17 | // purpose or non-infringement. Please see the License for the specific terms | |
18 | // and conditions governing the rights and limitations under the License. | |
19 | ||
2166f0fa SK |
20 | #ifndef _OpenGl_Workspace_Header |
21 | #define _OpenGl_Workspace_Header | |
22 | ||
e276548b | 23 | #ifdef HAVE_OPENCL |
24 | #include <map> | |
25 | #include <set> | |
26 | ||
27 | #include <OpenGl_Cl.hxx> | |
28 | #endif | |
29 | ||
2166f0fa SK |
30 | #include <Handle_OpenGl_Workspace.hxx> |
31 | #include <OpenGl_Window.hxx> | |
32 | ||
33 | #include <TColStd_Array2OfReal.hxx> | |
34 | #include <Quantity_Color.hxx> | |
bf75be98 | 35 | #include <Graphic3d_CView.hxx> |
2166f0fa | 36 | #include <Graphic3d_TypeOfComposition.hxx> |
bf75be98 | 37 | #include <Graphic3d_TypeOfTexture.hxx> |
38 | #include <Graphic3d_PtrFrameBuffer.hxx> | |
39 | #include <Graphic3d_BufferType.hxx> | |
40 | #include <Handle_Graphic3d_TextureParams.hxx> | |
2166f0fa | 41 | |
2166f0fa SK |
42 | #include <Aspect_CLayer2d.hxx> |
43 | #include <Aspect_Handle.hxx> | |
44 | #include <Aspect_PrintAlgo.hxx> | |
2166f0fa SK |
45 | |
46 | #include <InterfaceGraphic_Graphic3d.hxx> | |
47 | #include <InterfaceGraphic_Visual3d.hxx> | |
48 | ||
1981cb22 | 49 | #include <NCollection_Sequence.hxx> |
50 | ||
2166f0fa SK |
51 | #include <OpenGl_tsm.hxx> |
52 | ||
bf75be98 | 53 | #include <OpenGl_AspectFace.hxx> |
2166f0fa SK |
54 | #include <OpenGl_Display.hxx> |
55 | #include <OpenGl_Matrix.hxx> | |
56 | #include <OpenGl_NamedStatus.hxx> | |
a174a3c5 | 57 | #include <OpenGl_PrinterContext.hxx> |
e276548b | 58 | #ifdef HAVE_OPENCL |
59 | #include <OpenGl_SceneGeometry.hxx> | |
60 | #endif | |
2166f0fa | 61 | #include <OpenGl_TextParam.hxx> |
4269bd1b | 62 | #include <OpenGl_RenderFilter.hxx> |
2166f0fa SK |
63 | |
64 | #include <Handle_OpenGl_View.hxx> | |
bf75be98 | 65 | #include <Handle_OpenGl_Texture.hxx> |
2166f0fa SK |
66 | |
67 | class OpenGl_AspectLine; | |
2166f0fa SK |
68 | class OpenGl_AspectMarker; |
69 | class OpenGl_AspectText; | |
70 | class OpenGl_FrameBuffer; | |
71 | class OpenGl_Structure; | |
4269bd1b | 72 | class OpenGl_Element; |
692613e5 | 73 | class Image_PixMap; |
2166f0fa | 74 | |
e276548b | 75 | //! Represents window with GL context. |
76 | //! Provides methods to render primitives and maintain GL state. | |
2166f0fa SK |
77 | class OpenGl_Workspace : public OpenGl_Window |
78 | { | |
79 | public: | |
80 | ||
81 | //! Main constructor - prepare GL context for specified window. | |
82 | OpenGl_Workspace (const Handle(OpenGl_Display)& theDisplay, | |
83 | const CALL_DEF_WINDOW& theCWindow, | |
5e27df78 | 84 | Aspect_RenderingContext theGContext, |
58655684 | 85 | const Handle(OpenGl_Caps)& theCaps, |
5e27df78 | 86 | const Handle(OpenGl_Context)& theShareCtx); |
2166f0fa SK |
87 | |
88 | //! Destructor | |
89 | virtual ~OpenGl_Workspace(); | |
90 | ||
91 | void SetActiveView (const Handle(OpenGl_View)& theView) { myView = theView; } | |
92 | const Handle(OpenGl_View)& ActiveView () const { return myView; } | |
93 | ||
94 | //! Redraw the window. | |
95 | void Redraw (const Graphic3d_CView& theCView, | |
96 | const Aspect_CLayer2d& theCUnderLayer, | |
97 | const Aspect_CLayer2d& theCOverLayer); | |
98 | ||
298f9ad7 | 99 | //! Deprecated. Simply calls Redraw(). |
2166f0fa SK |
100 | void Update (const Graphic3d_CView& theCView, |
101 | const Aspect_CLayer2d& theCUnderLayer, | |
102 | const Aspect_CLayer2d& theCOverLayer) | |
103 | { | |
298f9ad7 | 104 | Redraw (theCView, theCUnderLayer, theCOverLayer); |
2166f0fa SK |
105 | } |
106 | ||
107 | //! Special method to perform printing. | |
108 | //! System-specific and currently only Win platform implemented. | |
a174a3c5 | 109 | Standard_Boolean Print (const Handle(OpenGl_PrinterContext)& thePrintContext, |
110 | const Graphic3d_CView& theCView, | |
bf75be98 | 111 | const Aspect_CLayer2d& theCUnderLayer, |
2166f0fa SK |
112 | const Aspect_CLayer2d& theCOverLayer, |
113 | const Aspect_Handle theHPrintDC, | |
114 | const Standard_Boolean theToShowBackground, | |
115 | const Standard_CString theFileName, | |
116 | const Aspect_PrintAlgo thePrintAlgorithm, | |
117 | const Standard_Real theScaleFactor); | |
118 | ||
a174a3c5 | 119 | const Handle(OpenGl_PrinterContext)& PrinterContext() const |
120 | { | |
121 | return myPrintContext; | |
122 | } | |
123 | ||
2166f0fa SK |
124 | void DisplayCallback (const Graphic3d_CView& theCView, int theReason); |
125 | ||
1981cb22 | 126 | Standard_Boolean SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer); |
2166f0fa SK |
127 | Standard_Boolean BeginAddMode(); |
128 | void EndAddMode(); | |
129 | void ClearImmediatMode (const Graphic3d_CView& theCView, | |
130 | const Standard_Boolean theToFlush); | |
131 | void RedrawImmediatMode(); | |
132 | Standard_Boolean BeginImmediatMode (const Graphic3d_CView& theCView, | |
133 | const Standard_Boolean theUseDepthTest, | |
134 | const Standard_Boolean theRetainMode); | |
135 | void EndImmediatMode(); | |
2166f0fa | 136 | void DrawStructure (const OpenGl_Structure* theStructure); |
2166f0fa SK |
137 | |
138 | Graphic3d_PtrFrameBuffer FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight); | |
139 | void FBORelease (Graphic3d_PtrFrameBuffer theFBOPtr); | |
692613e5 | 140 | Standard_Boolean BufferDump (OpenGl_FrameBuffer* theFBOPtr, |
141 | Image_PixMap& theImage, | |
142 | const Graphic3d_BufferType& theBufferType); | |
2166f0fa | 143 | |
2166f0fa SK |
144 | void UseTransparency (const Standard_Boolean theFlag); |
145 | Standard_Boolean& UseZBuffer() { return myUseZBuffer; } | |
146 | Standard_Boolean& UseDepthTest() { return myUseDepthTest; } | |
147 | Standard_Boolean& UseGLLight() { return myUseGLLight; } | |
148 | ||
149 | //// RELATED TO STATUS //// | |
150 | ||
151 | Standard_Integer NamedStatus; | |
152 | ||
2166f0fa SK |
153 | const TEL_COLOUR* HighlightColor; |
154 | ||
155 | const OpenGl_Matrix* SetViewMatrix (const OpenGl_Matrix* ); | |
30f0ad28 | 156 | const OpenGl_Matrix* SetStructureMatrix (const OpenGl_Matrix*, bool aRevert = false); |
2166f0fa | 157 | |
0f8c0fb8 | 158 | //! Updates current model-view matrix |
159 | //! replacing it with StructureMatrixT*ViewMatrix from the workspace. | |
160 | const void UpdateModelViewMatrix(); | |
161 | ||
2166f0fa SK |
162 | const OpenGl_AspectLine* SetAspectLine (const OpenGl_AspectLine* theAspect); |
163 | const OpenGl_AspectFace* SetAspectFace (const OpenGl_AspectFace* theAspect); | |
164 | const OpenGl_AspectMarker* SetAspectMarker (const OpenGl_AspectMarker* theAspect); | |
165 | const OpenGl_AspectText* SetAspectText (const OpenGl_AspectText* theAspect); | |
166 | ||
167 | void SetTextParam (const OpenGl_TextParam* theParam) { TextParam_set = theParam; } | |
168 | ||
3946774d | 169 | //// THESE METHODS ARE EXPORTED AS THEY PROVIDE STATE INFO TO USERDRAW |
170 | Standard_EXPORT const OpenGl_AspectLine* AspectLine (const Standard_Boolean theWithApply); | |
171 | Standard_EXPORT const OpenGl_AspectFace* AspectFace (const Standard_Boolean theWithApply); | |
172 | Standard_EXPORT const OpenGl_AspectMarker* AspectMarker (const Standard_Boolean theWithApply); | |
173 | Standard_EXPORT const OpenGl_AspectText* AspectText (const Standard_Boolean theWithApply); | |
a174a3c5 | 174 | inline const OpenGl_TextParam* AspectTextParams() const |
175 | { | |
176 | return TextParam_applied; | |
177 | } | |
2166f0fa | 178 | |
34a44cbd | 179 | //! Clear the applied aspect state. |
180 | void ResetAppliedAspect(); | |
181 | ||
bf75be98 | 182 | Standard_EXPORT Handle(OpenGl_Texture) DisableTexture(); |
183 | Standard_EXPORT Handle(OpenGl_Texture) EnableTexture (const Handle(OpenGl_Texture)& theTexture, | |
184 | const Handle(Graphic3d_TextureParams)& theParams = NULL); | |
185 | ||
4269bd1b | 186 | //! Set filter for restricting rendering of particular elements. |
187 | //! Filter can be applied for rendering passes used by recursive | |
188 | //! rendering algorithms for rendering elements of groups. | |
189 | //! @param theFilter [in] the filter instance. | |
190 | inline void SetRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter) | |
191 | { | |
192 | myRenderFilter = theFilter; | |
193 | } | |
194 | ||
195 | //! Get rendering filter. | |
196 | //! @return filter instance. | |
197 | inline const Handle(OpenGl_RenderFilter)& GetRenderFilter() const | |
198 | { | |
199 | return myRenderFilter; | |
200 | } | |
201 | ||
202 | //! @return applied view matrix. | |
203 | inline const OpenGl_Matrix* ViewMatrix() const { return ViewMatrix_applied; } | |
204 | ||
205 | //! @return applied model structure matrix. | |
206 | inline const OpenGl_Matrix* ModelMatrix() const { return StructureMatrix_applied; } | |
207 | ||
2166f0fa SK |
208 | protected: |
209 | ||
1981cb22 | 210 | void CopyBuffers (const Standard_Boolean theFrontToBack); |
2166f0fa SK |
211 | |
212 | virtual Standard_Boolean Activate(); | |
213 | ||
214 | // TEMPORARY!!! | |
bf75be98 | 215 | void Redraw1 (const Graphic3d_CView& theCView, |
216 | const Aspect_CLayer2d& theCUnderLayer, | |
2166f0fa SK |
217 | const Aspect_CLayer2d& theCOverLayer, |
218 | const int theToSwap); | |
219 | ||
bf75be98 | 220 | void UpdateMaterial (const int flag); |
2166f0fa | 221 | |
bf75be98 | 222 | void setTextureParams (Handle(OpenGl_Texture)& theTexture, |
223 | const Handle(Graphic3d_TextureParams)& theParams); | |
2166f0fa | 224 | |
e276548b | 225 | #ifdef HAVE_OPENCL |
226 | ||
227 | public: | |
228 | ||
229 | //! Returns information about OpenCL device used for computations. | |
230 | Standard_Boolean GetOpenClDeviceInfo ( | |
231 | NCollection_DataMap<TCollection_AsciiString, TCollection_AsciiString>& theInfo) const; | |
232 | ||
233 | protected: | |
234 | ||
235 | //! Describes result of OpenCL initializing. | |
236 | enum OpenClInitStatus | |
237 | { | |
238 | OpenGl_CLIS_NONE, | |
239 | OpenGl_CLIS_INIT, | |
240 | OpenGl_CLIS_FAIL | |
241 | }; | |
242 | ||
243 | protected: //! @name methods related to ray-tracing | |
244 | ||
245 | //! Updates 3D scene geometry for ray-tracing. | |
246 | Standard_Boolean UpdateRaytraceGeometry (Standard_Boolean theCheck); | |
247 | ||
248 | //! Checks to see if the structure is modified. | |
249 | Standard_Boolean CheckRaytraceStructure (const OpenGl_Structure* theStructure); | |
250 | ||
251 | //! Updates 3D scene light sources for ray-tracing. | |
252 | Standard_Boolean UpdateRaytraceLightSources (const GLdouble theInvModelView[16]); | |
253 | ||
254 | //! Updates environment map for ray-tracing. | |
255 | Standard_Boolean UpdateRaytraceEnvironmentMap(); | |
256 | ||
257 | //! Adds OpenGL structure to ray-traced scene geometry. | |
258 | Standard_Boolean AddRaytraceStructure (const OpenGl_Structure* theStruct, | |
259 | const float* theTrans, std::set<const OpenGl_Structure*>& theElements); | |
260 | ||
261 | //! Adds OpenGL primitive array to ray-traced scene geometry. | |
262 | Standard_Boolean AddRaytracePrimitiveArray ( | |
263 | const CALL_DEF_PARRAY* theArray, int theMatID, const float* theTrans); | |
264 | ||
265 | //! Adds vertex indices from OpenGL primitive array to ray-traced scene geometry. | |
266 | Standard_Boolean AddRaytraceVertexIndices (const CALL_DEF_PARRAY* theArray, | |
267 | int theFirstVert, int theVertOffset, int theVertNum, int theMatID); | |
268 | ||
269 | //! Adds OpenGL triangle array to ray-traced scene geometry. | |
270 | Standard_Boolean AddRaytraceTriangleArray (const CALL_DEF_PARRAY* theArray, | |
271 | int theFirstVert, int theVertOffset, int theVertNum, int theMatID); | |
272 | ||
273 | //! Adds OpenGL triangle fan array to ray-traced scene geometry. | |
274 | Standard_Boolean AddRaytraceTriangleFanArray (const CALL_DEF_PARRAY* theArray, | |
275 | int theFirstVert, int theVertOffset, int theVertNum, int theMatID); | |
276 | ||
277 | //! Adds OpenGL triangle fan array to ray-traced scene geometry. | |
278 | Standard_Boolean AddRaytraceTriangleStripArray (const CALL_DEF_PARRAY* theArray, | |
279 | int theFirstVert, int theVertOffset, int theVertNum, int theMatID); | |
280 | ||
281 | //! Adds OpenGL quadrangle array to ray-traced scene geometry. | |
282 | Standard_Boolean AddRaytraceQuadrangleArray (const CALL_DEF_PARRAY* theArray, | |
283 | int theFirstVert, int theVertOffset, int theVertNum, int theMatID); | |
284 | ||
285 | //! Adds OpenGL quadrangle strip array to ray-traced scene geometry. | |
286 | Standard_Boolean AddRaytraceQuadrangleStripArray (const CALL_DEF_PARRAY* theArray, | |
287 | int theFirstVert, int theVertOffset, int theVertNum, int theMatID); | |
288 | ||
289 | //! Adds OpenGL polygon array to ray-traced scene geometry. | |
290 | Standard_Boolean AddRaytracePolygonArray (const CALL_DEF_PARRAY* theArray, | |
291 | int theFirstVert, int theVertOffset, int theVertNum, int theMatID); | |
292 | ||
293 | //! Initializes OpenCL resources. | |
294 | Standard_Boolean InitOpenCL(); | |
295 | ||
296 | //! Releases OpenCL resources. | |
297 | void ReleaseOpenCL(); | |
298 | ||
299 | //! Resizes OpenCL output image. | |
300 | Standard_Boolean ResizeRaytraceOutputBuffer (const cl_int theSizeX, const cl_int theSizeY); | |
301 | ||
302 | //! Writes scene geometry to OpenCl device. | |
303 | Standard_Boolean WriteRaytraceSceneToDevice(); | |
304 | ||
305 | //! Runs OpenCL ray-tracing kernels. | |
306 | Standard_Boolean RunRaytraceOpenCLKernels (const Graphic3d_CView& theCView, | |
307 | const GLfloat theOrigins[16], | |
308 | const GLfloat theDirects[16], | |
309 | const int theSizeX, | |
310 | const int theSizeY); | |
311 | ||
312 | //! Redraws the window using OpenCL ray tracing. | |
313 | Standard_Boolean Raytrace (const Graphic3d_CView& theCView, | |
314 | const int theSizeX, int theSizeY, const Tint theToSwap); | |
315 | ||
316 | protected: //! @name fields related to ray-tracing | |
317 | ||
318 | //! Result of OpenCL initialization. | |
319 | OpenClInitStatus myComputeInitStatus; | |
320 | //! Is ATI/AMD OpenCL platform used? | |
321 | Standard_Boolean myIsAmdComputePlatform; | |
322 | ||
323 | //! Is geometry data valid? | |
324 | Standard_Boolean myIsRaytraceDataValid; | |
325 | //! Is geometry data musty be updated? | |
326 | Standard_Boolean myToUpdateRaytraceData; | |
327 | //! 3D scene geometry data for ray-tracing. | |
328 | OpenGl_RaytraceScene myRaytraceSceneData; | |
329 | ||
330 | //! Radius of bounding sphere of the scene. | |
331 | float myRaytraceSceneRadius; | |
332 | //! Scene epsilon to prevent self-intersections. | |
333 | float myRaytraceSceneEpsilon; | |
334 | ||
335 | //! Binned SAH-based BVH builder. | |
336 | OpenGl_BinnedBVHBuilder myBVHBuilder; | |
337 | ||
338 | //! OpenCL context. | |
339 | cl_context myComputeContext; | |
340 | //! OpenCL command queue. | |
341 | cl_command_queue myRaytraceQueue; | |
342 | //! OpenCL computing program. | |
343 | cl_program myRaytraceProgram; | |
344 | //! OpenCL ray-tracing render kernel. | |
345 | cl_kernel myRaytraceRenderKernel; | |
346 | //! OpenCL adaptive anti-aliasing kernel. | |
347 | cl_kernel myRaytraceSmoothKernel; | |
348 | ||
349 | //! OpenCL image to store environment map. | |
350 | cl_mem myRaytraceEnvironment; | |
351 | //! OpenCL image to store rendering result. | |
352 | cl_mem myRaytraceOutputImage; | |
353 | //! OpenCL image to store anti-aliasing result. | |
354 | cl_mem myRaytraceOutputImageSmooth; | |
355 | ||
356 | //! OpenGL output texture handle. | |
357 | GLuint myRaytraceOutputTexture[2]; | |
358 | ||
359 | //! OpenCL buffer of vertex normals. | |
360 | cl_mem myRaytraceNormalBuffer; | |
361 | //! OpenCL buffer of vertex coordinates. | |
362 | cl_mem myRaytraceVertexBuffer; | |
363 | //! OpenCL buffer of indices of triangle vertices. | |
364 | cl_mem myRaytraceTriangleBuffer; | |
365 | ||
366 | //! OpenCL buffer of minimum points of BVH nodes. | |
367 | cl_mem myRaytraceNodeMinPointBuffer; | |
368 | //! OpenCL buffer of maximum points of BVH nodes. | |
369 | cl_mem myRaytraceNodeMaxPointBuffer; | |
370 | //! OpenCL buffer of data records of BVH nodes. | |
371 | cl_mem myRaytraceNodeDataRcrdBuffer; | |
372 | ||
373 | //! OpenCL buffer of material properties. | |
374 | cl_mem myRaytraceMaterialBuffer; | |
375 | ||
376 | //! OpenCL buffer of light source properties. | |
377 | cl_mem myRaytraceLightSourceBuffer; | |
378 | ||
379 | //! State of OpenGL view. | |
380 | Standard_Size myViewModificationStatus; | |
381 | ||
382 | //! State of OpenGL layer list. | |
383 | Standard_Size myLayersModificationStatus; | |
384 | ||
385 | //! State of OpenGL elements reflected to ray-tracing. | |
386 | std::map<const OpenGl_Structure*, Standard_Size> myStructureStates; | |
387 | ||
388 | #endif // HAVE_OPENCL | |
389 | ||
bf75be98 | 390 | protected: //! @name protected fields |
2166f0fa | 391 | |
a174a3c5 | 392 | Handle(OpenGl_PrinterContext) myPrintContext; |
bf75be98 | 393 | Handle(OpenGl_View) myView; // WSViews - now just one view is supported |
bf75be98 | 394 | Standard_Boolean myIsTransientOpen; // transientOpen |
1981cb22 | 395 | Standard_Boolean myRetainMode; |
396 | Standard_Boolean myTransientDrawToFront; //!< optimization flag for immediate mode (to render directly to the front buffer) | |
397 | ||
398 | NCollection_Sequence<const OpenGl_Structure*> myTransientList; | |
2166f0fa | 399 | |
bf75be98 | 400 | Standard_Boolean myUseTransparency; |
401 | Standard_Boolean myUseZBuffer; | |
402 | Standard_Boolean myUseDepthTest; | |
403 | Standard_Boolean myUseGLLight; | |
404 | Standard_Boolean myBackBufferRestored; | |
2166f0fa | 405 | |
bf75be98 | 406 | protected: //! @name fields related to status |
407 | ||
4269bd1b | 408 | Handle(OpenGl_RenderFilter) myRenderFilter; |
bf75be98 | 409 | Handle(OpenGl_Texture) myTextureBound; //!< currently bound texture (managed by OpenGl_AspectFace and OpenGl_View environment texture) |
2166f0fa SK |
410 | const OpenGl_AspectLine *AspectLine_set, *AspectLine_applied; |
411 | const OpenGl_AspectFace *AspectFace_set, *AspectFace_applied; | |
412 | const OpenGl_AspectMarker *AspectMarker_set, *AspectMarker_applied; | |
413 | const OpenGl_AspectText *AspectText_set, *AspectText_applied; | |
414 | ||
415 | const OpenGl_TextParam *TextParam_set, *TextParam_applied; | |
416 | ||
417 | const OpenGl_Matrix* ViewMatrix_applied; | |
418 | const OpenGl_Matrix* StructureMatrix_applied; | |
419 | ||
0f8c0fb8 | 420 | //! Model matrix with applied structure transformations |
421 | OpenGl_Matrix myModelViewMatrix; | |
422 | ||
2166f0fa SK |
423 | const TEL_POFFSET_PARAM* PolygonOffset_applied; |
424 | ||
bf75be98 | 425 | OpenGl_AspectFace myAspectFaceHl; // Hiddenline aspect |
426 | ||
427 | public: //! @name type definition | |
2166f0fa | 428 | |
bf75be98 | 429 | DEFINE_STANDARD_RTTI(OpenGl_Workspace) |
1c35b92f | 430 | DEFINE_STANDARD_ALLOC |
2166f0fa SK |
431 | |
432 | }; | |
433 | ||
bf75be98 | 434 | #endif // _OpenGl_Workspace_Header |