1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-04-10 18:51:21 +03:00

0024320: TKOpenGl, Ray Tracing - OpenGL resources created for OpenCL interconnection should be managed in common way

This commit is contained in:
dbp 2014-01-14 13:05:08 +04:00 committed by bugmaster
parent 3b1817a9e2
commit 68333c8f16
5 changed files with 348 additions and 140 deletions

View File

@ -292,7 +292,7 @@ bool OpenGl_Texture::Init (const Handle(OpenGl_Context)& theCtx,
// Notice that formally general NPOT textures are required by OpenGL 2.0 specifications
// however some hardware (NV30 - GeForce FX, RadeOn 9xxx and Xxxx) supports GLSL but not NPOT!
// Trying to create NPOT rextures on such hardware will not fail
// Trying to create NPOT textures on such hardware will not fail
// but driver will fall back into software rendering,
const bool toForceP2 = !theCtx->IsGlGreaterEqual (3, 0) && !theCtx->arbNPTW;
const GLsizei aWidthOut = toForceP2 ? OpenGl_Context::GetPowerOfTwo (aWidth, aMaxSize) : Min (aWidth, aMaxSize);
@ -486,3 +486,84 @@ bool OpenGl_Texture::Init (const Handle(OpenGl_Context)& theCtx,
}
}
}
// =======================================================================
// function : InitRectangle
// purpose :
// =======================================================================
bool OpenGl_Texture::InitRectangle (const Handle(OpenGl_Context)& theCtx,
const Standard_Integer theSizeX,
const Standard_Integer theSizeY,
const OpenGl_TextureFormat& theFormat)
{
if (!Create (theCtx) || !theCtx->IsGlGreaterEqual (3, 0))
{
return false;
}
myTarget = GL_TEXTURE_RECTANGLE;
const GLsizei aSizeX = Min (theCtx->MaxTextureSize(), theSizeX);
const GLsizei aSizeY = Min (theCtx->MaxTextureSize(), theSizeY);
Bind (theCtx);
if (myParams->Filter() == Graphic3d_TOTF_NEAREST)
{
glTexParameteri (myTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (myTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
else
{
glTexParameteri (myTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (myTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
myTextFormat = theFormat.Internal();
glTexImage2D (GL_PROXY_TEXTURE_RECTANGLE,
0,
myTextFormat,
aSizeX,
aSizeY,
0,
theFormat.Format(),
GL_FLOAT,
NULL);
GLint aTestSizeX = 0;
GLint aTestSizeY = 0;
glGetTexLevelParameteriv (
GL_PROXY_TEXTURE_RECTANGLE, 0, GL_TEXTURE_WIDTH, &aTestSizeX);
glGetTexLevelParameteriv (
GL_PROXY_TEXTURE_RECTANGLE, 0, GL_TEXTURE_HEIGHT, &aTestSizeY);
if (aTestSizeX == 0 || aTestSizeY == 0)
{
Unbind (theCtx);
return false;
}
glTexImage2D (myTarget,
0,
myTextFormat,
aSizeX,
aSizeY,
0,
theFormat.Format(),
GL_FLOAT,
NULL);
if (glGetError() != GL_NO_ERROR)
{
Unbind (theCtx);
return false;
}
mySizeX = aSizeX;
mySizeY = aSizeY;
Unbind (theCtx);
return true;
}

View File

@ -25,6 +25,133 @@ class Handle(OpenGl_Context);
class OpenGl_Context;
class Image_PixMap;
//! Selects preferable texture format for specified parameters.
template<class T>
struct OpenGl_TextureFormatSelector
{
// Not implemented
};
template<>
struct OpenGl_TextureFormatSelector<GLubyte>
{
static GLint Internal (GLuint theChannels)
{
switch (theChannels)
{
case 1:
return GL_R8;
case 2:
return GL_RG8;
case 3:
return GL_RGB8;
case 4:
return GL_RGBA8;
default:
return GL_NONE;
}
}
};
template<>
struct OpenGl_TextureFormatSelector<GLushort>
{
static GLint Internal (GLuint theChannels)
{
switch (theChannels)
{
case 1:
return GL_R16;
case 2:
return GL_RG16;
case 3:
return GL_RGB16;
case 4:
return GL_RGBA16;
default:
return GL_NONE;
}
}
};
template<>
struct OpenGl_TextureFormatSelector<GLfloat>
{
static GLint Internal (GLuint theChannels)
{
switch (theChannels)
{
case 1:
return GL_R32F;
case 2:
return GL_RG32F;
case 3:
return GL_RGB32F;
case 4:
return GL_RGBA32F;
default:
return GL_NONE;
}
}
};
//! Stores parameters of OpenGL texture format.
class OpenGl_TextureFormat
{
friend class OpenGl_Texture;
public:
//! Returns OpenGL format of the pixel data.
inline GLenum Format() const
{
switch (myChannels)
{
case 1:
return GL_RED;
case 2:
return GL_RG;
case 3:
return GL_RGB;
case 4:
return GL_RGBA;
default:
return GL_NONE;
}
}
//! Returns OpenGL internal format of the pixel data.
inline GLint Internal() const
{
return myInternal;
}
//! Returns texture format for specified type and number of channels.
template<class T, int N>
static OpenGl_TextureFormat Create()
{
return OpenGl_TextureFormat (N, OpenGl_TextureFormatSelector<T>::Internal (N));
}
private:
//! Creates new texture format.
OpenGl_TextureFormat (const GLint theChannels, const GLint theInternal)
: myChannels (theChannels),
myInternal (theInternal)
{
//
}
private:
//! OpenGL internal format of the pixel data.
GLint myInternal;
//! Number of channels for each pixel (from 1 to 4).
GLint myChannels;
};
//! Texture resource.
class OpenGl_Texture : public OpenGl_Resource
{
@ -98,6 +225,13 @@ public:
const Image_PixMap& theImage,
const Graphic3d_TypeOfTexture theType);
//! Allocates texture rectangle with specified format and size.
//! \note Texture data is not initialized (will contain trash).
Standard_EXPORT bool InitRectangle (const Handle(OpenGl_Context)& theCtx,
const Standard_Integer theSizeX,
const Standard_Integer theSizeY,
const OpenGl_TextureFormat& theFormat);
//! @return true if texture was generated within mipmaps
Standard_EXPORT const Standard_Boolean HasMipmaps() const;

View File

@ -194,9 +194,6 @@ OpenGl_Workspace::OpenGl_Workspace (const Handle(OpenGl_Display)& theDisplay,
myViewModificationStatus = 0;
myLayersModificationStatus = 0;
myRaytraceOutputTexture[0] = 0;
myRaytraceOutputTexture[1] = 0;
myIsRaytraceDataValid = Standard_False;
myToUpdateRaytraceData = Standard_False;

View File

@ -333,7 +333,7 @@ protected: //! @name methods related to ray-tracing
//! Redraws the window using OpenCL ray tracing.
Standard_Boolean Raytrace (const Graphic3d_CView& theCView,
const int theSizeX, int theSizeY, const Tint theToSwap);
const int theSizeX, const int theSizeY, const Tint theToSwap);
protected: //! @name fields related to ray-tracing
@ -373,10 +373,12 @@ protected: //! @name fields related to ray-tracing
//! OpenCL image to store rendering result.
cl_mem myRaytraceOutputImage;
//! OpenCL image to store anti-aliasing result.
cl_mem myRaytraceOutputImageSmooth;
cl_mem myRaytraceOutputImageAA;
//! OpenGL output texture handle.
GLuint myRaytraceOutputTexture[2];
//! OpenGL texture to store rendering result.
Handle(OpenGl_Texture) myRaytraceOutputTexture;
//! OpenGL texture to store anti-aliasing result.
Handle(OpenGl_Texture) myRaytraceOutputTextureAA;
//! OpenCL buffer of vertex normals.
cl_mem myRaytraceNormalBuffer;

View File

@ -87,8 +87,8 @@ Standard_Boolean OpenGl_Workspace::UpdateRaytraceEnvironmentMap()
if (myRaytraceEnvironment != NULL)
clReleaseMemObject (myRaytraceEnvironment);
int aSizeX = 1;
int aSizeY = 1;
Standard_Integer aSizeX = 1;
Standard_Integer aSizeY = 1;
if (!myView->TextureEnv().IsNull() && myView->SurfaceDetail() != Visual3d_TOD_NONE)
{
@ -124,7 +124,7 @@ Standard_Boolean OpenGl_Workspace::UpdateRaytraceEnvironmentMap()
}
else
{
for (int aPixel = 0; aPixel < aSizeX * aSizeY * 4; ++aPixel)
for (Standard_Integer aPixel = 0; aPixel < aSizeX * aSizeY * 4; ++aPixel)
aPixelData[aPixel] = 0.f;
}
@ -195,7 +195,7 @@ Standard_Boolean OpenGl_Workspace::UpdateRaytraceGeometry (Standard_Boolean theC
const OpenGl_ArrayOfStructure& aStructArray = aPriorityList.ArrayOfStructures();
for (int anIndex = 0; anIndex < aStructArray.Length(); ++anIndex)
for (Standard_Integer anIndex = 0; anIndex < aStructArray.Length(); ++anIndex)
{
OpenGl_SequenceOfStructure::Iterator aStructIt;
@ -220,8 +220,8 @@ Standard_Boolean OpenGl_Workspace::UpdateRaytraceGeometry (Standard_Boolean theC
if (aTransform == NULL)
aTransform = new float[16];
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
for (Standard_Integer i = 0; i < 4; ++i)
for (Standard_Integer j = 0; j < 4; ++j)
{
aTransform[j * 4 + i] = aStructure->Transformation()->mat[i][j];
}
@ -385,11 +385,11 @@ Standard_Boolean OpenGl_Workspace::AddRaytraceStructure (const OpenGl_Structure*
}
// Get structure material
int aStructMatID = -1;
Standard_Integer aStructMatID = -1;
if (theStructure->AspectFace() != NULL)
{
aStructMatID = static_cast<int> (myRaytraceSceneData.Materials.size());
aStructMatID = static_cast<Standard_Integer> (myRaytraceSceneData.Materials.size());
OpenGl_RaytraceMaterial aStructMaterial;
CreateMaterial (theStructure->AspectFace()->IntFront(), aStructMaterial);
@ -402,11 +402,11 @@ Standard_Boolean OpenGl_Workspace::AddRaytraceStructure (const OpenGl_Structure*
while (anItg.More())
{
// Get group material
int aGroupMatID = -1;
Standard_Integer aGroupMatID = -1;
if (anItg.Value()->AspectFace() != NULL)
{
aGroupMatID = static_cast<int> (myRaytraceSceneData.Materials.size());
aGroupMatID = static_cast<Standard_Integer> (myRaytraceSceneData.Materials.size());
OpenGl_RaytraceMaterial aGroupMaterial;
CreateMaterial (anItg.Value()->AspectFace()->IntFront(), aGroupMaterial);
@ -414,11 +414,11 @@ Standard_Boolean OpenGl_Workspace::AddRaytraceStructure (const OpenGl_Structure*
myRaytraceSceneData.Materials.push_back (aGroupMaterial);
}
int aMatID = aGroupMatID < 0 ? aStructMatID : aGroupMatID;
Standard_Integer aMatID = aGroupMatID < 0 ? aStructMatID : aGroupMatID;
if (aStructMatID < 0 && aGroupMatID < 0)
{
aMatID = static_cast<int> (myRaytraceSceneData.Materials.size());
aMatID = static_cast<Standard_Integer> (myRaytraceSceneData.Materials.size());
myRaytraceSceneData.Materials.push_back (OpenGl_RaytraceMaterial());
}
@ -432,7 +432,7 @@ Standard_Boolean OpenGl_Workspace::AddRaytraceStructure (const OpenGl_Structure*
if (anAspect != NULL)
{
aMatID = static_cast<int> (myRaytraceSceneData.Materials.size());
aMatID = static_cast<Standard_Integer> (myRaytraceSceneData.Materials.size());
OpenGl_RaytraceMaterial aMaterial;
CreateMaterial (anAspect->IntFront(), aMaterial);
@ -465,8 +465,8 @@ Standard_Boolean OpenGl_Workspace::AddRaytraceStructure (const OpenGl_Structure*
{
float* aTransform = new float[16];
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
for (Standard_Integer i = 0; i < 4; ++i)
for (Standard_Integer j = 0; j < 4; ++j)
{
aTransform[j * 4 + i] =
anIts.Value()->Transformation()->mat[i][j];
@ -491,7 +491,7 @@ Standard_Boolean OpenGl_Workspace::AddRaytraceStructure (const OpenGl_Structure*
// purpose : Adds OpenGL primitive array to ray-traced scene geometry
// =======================================================================
Standard_Boolean OpenGl_Workspace::AddRaytracePrimitiveArray (const CALL_DEF_PARRAY* theArray,
int theMatID,
Standard_Integer theMatID,
const float* theTransform)
{
if (theArray->type != TelPolygonsArrayType &&
@ -530,9 +530,9 @@ Standard_Boolean OpenGl_Workspace::AddRaytracePrimitiveArray (const CALL_DEF_PAR
myRaytraceSceneData.Vertices.reserve (
myRaytraceSceneData.Vertices.size() + theArray->num_vertexs);
const int aFirstVert = static_cast<int> (myRaytraceSceneData.Vertices.size());
const Standard_Integer aFirstVert = static_cast<Standard_Integer> (myRaytraceSceneData.Vertices.size());
for (int aVert = 0; aVert < theArray->num_vertexs; ++aVert)
for (Standard_Integer aVert = 0; aVert < theArray->num_vertexs; ++aVert)
{
OpenGl_RTVec4f aVertex (theArray->vertices[aVert].xyz[0],
theArray->vertices[aVert].xyz[1],
@ -550,7 +550,7 @@ Standard_Boolean OpenGl_Workspace::AddRaytracePrimitiveArray (const CALL_DEF_PAR
myRaytraceSceneData.Normals.reserve (
myRaytraceSceneData.Normals.size() + theArray->num_vertexs);
for (int aNorm = 0; aNorm < theArray->num_vertexs; ++aNorm)
for (Standard_Integer aNorm = 0; aNorm < theArray->num_vertexs; ++aNorm)
{
OpenGl_RTVec4f aNormal;
@ -577,11 +577,11 @@ Standard_Boolean OpenGl_Workspace::AddRaytracePrimitiveArray (const CALL_DEF_PAR
std::cout << "\tNumber of bounds = " << theArray->num_bounds << std::endl;
#endif
int aVertOffset = 0;
Standard_Integer aVertOffset = 0;
for (int aBound = 0; aBound < theArray->num_bounds; ++aBound)
for (Standard_Integer aBound = 0; aBound < theArray->num_bounds; ++aBound)
{
const int aVertNum = theArray->bounds[aBound];
const Standard_Integer aVertNum = theArray->bounds[aBound];
#ifdef RAY_TRACE_PRINT_INFO
std::cout << "\tAdd indices from bound " << aBound << ": " <<
@ -598,7 +598,7 @@ Standard_Boolean OpenGl_Workspace::AddRaytracePrimitiveArray (const CALL_DEF_PAR
}
else
{
const int aVertNum = theArray->num_edges > 0 ? theArray->num_edges : theArray->num_vertexs;
const Standard_Integer aVertNum = theArray->num_edges > 0 ? theArray->num_edges : theArray->num_vertexs;
#ifdef RAY_TRACE_PRINT_INFO
std::cout << "\tAdd indices: " << aVertNum << std::endl;
@ -615,10 +615,10 @@ Standard_Boolean OpenGl_Workspace::AddRaytracePrimitiveArray (const CALL_DEF_PAR
// purpose : Adds vertex indices to ray-traced scene geometry
// =======================================================================
Standard_Boolean OpenGl_Workspace::AddRaytraceVertexIndices (const CALL_DEF_PARRAY* theArray,
int theFirstVert,
int theVertOffset,
int theVertNum,
int theMatID)
Standard_Integer theFirstVert,
Standard_Integer theVertOffset,
Standard_Integer theVertNum,
Standard_Integer theMatID)
{
myRaytraceSceneData.Triangles.reserve (myRaytraceSceneData.Triangles.size() + theVertNum);
switch (theArray->type)
@ -638,17 +638,17 @@ Standard_Boolean OpenGl_Workspace::AddRaytraceVertexIndices (const CALL_DEF_PARR
// purpose : Adds OpenGL triangle array to ray-traced scene geometry
// =======================================================================
Standard_Boolean OpenGl_Workspace::AddRaytraceTriangleArray (const CALL_DEF_PARRAY* theArray,
int theFirstVert,
int theVertOffset,
int theVertNum,
int theMatID)
Standard_Integer theFirstVert,
Standard_Integer theVertOffset,
Standard_Integer theVertNum,
Standard_Integer theMatID)
{
if (theVertNum < 3)
return Standard_True;
if (theArray->num_edges > 0)
{
for (int aVert = theVertOffset; aVert < theVertOffset + theVertNum - 2; aVert += 3)
for (Standard_Integer aVert = theVertOffset; aVert < theVertOffset + theVertNum - 2; aVert += 3)
{
myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + theArray->edges[aVert + 0],
theFirstVert + theArray->edges[aVert + 1],
@ -658,7 +658,7 @@ Standard_Boolean OpenGl_Workspace::AddRaytraceTriangleArray (const CALL_DEF_PARR
}
else
{
for (int aVert = theVertOffset; aVert < theVertOffset + theVertNum - 2; aVert += 3)
for (Standard_Integer aVert = theVertOffset; aVert < theVertOffset + theVertNum - 2; aVert += 3)
{
myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + aVert + 0,
theFirstVert + aVert + 1,
@ -675,17 +675,17 @@ Standard_Boolean OpenGl_Workspace::AddRaytraceTriangleArray (const CALL_DEF_PARR
// purpose : Adds OpenGL triangle fan array to ray-traced scene geometry
// =======================================================================
Standard_Boolean OpenGl_Workspace::AddRaytraceTriangleFanArray (const CALL_DEF_PARRAY* theArray,
int theFirstVert,
int theVertOffset,
int theVertNum,
int theMatID)
Standard_Integer theFirstVert,
Standard_Integer theVertOffset,
Standard_Integer theVertNum,
Standard_Integer theMatID)
{
if (theVertNum < 3)
return Standard_True;
if (theArray->num_edges > 0)
{
for (int aVert = theVertOffset; aVert < theVertOffset + theVertNum - 2; ++aVert)
for (Standard_Integer aVert = theVertOffset; aVert < theVertOffset + theVertNum - 2; ++aVert)
{
myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + theArray->edges[theVertOffset],
theFirstVert + theArray->edges[aVert + 1],
@ -695,7 +695,7 @@ Standard_Boolean OpenGl_Workspace::AddRaytraceTriangleFanArray (const CALL_DEF_P
}
else
{
for (int aVert = theVertOffset; aVert < theVertOffset + theVertNum - 2; ++aVert)
for (Standard_Integer aVert = theVertOffset; aVert < theVertOffset + theVertNum - 2; ++aVert)
{
myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + theVertOffset,
theFirstVert + aVert + 1,
@ -712,10 +712,10 @@ Standard_Boolean OpenGl_Workspace::AddRaytraceTriangleFanArray (const CALL_DEF_P
// purpose : Adds OpenGL triangle strip array to ray-traced scene geometry
// =======================================================================
Standard_Boolean OpenGl_Workspace::AddRaytraceTriangleStripArray (const CALL_DEF_PARRAY* theArray,
int theFirstVert,
int theVertOffset,
int theVertNum,
int theMatID)
Standard_Integer theFirstVert,
Standard_Integer theVertOffset,
Standard_Integer theVertNum,
Standard_Integer theMatID)
{
if (theVertNum < 3)
return Standard_True;
@ -728,7 +728,7 @@ Standard_Boolean OpenGl_Workspace::AddRaytraceTriangleStripArray (const CALL_DEF
theFirstVert + theArray->edges[theVertOffset + 2],
theMatID));
for (int aVert = theVertOffset + 1, aTriNum = 1; aVert < theVertOffset + theVertNum - 2; ++aVert, ++aTriNum)
for (Standard_Integer aVert = theVertOffset + 1, aTriNum = 1; aVert < theVertOffset + theVertNum - 2; ++aVert, ++aTriNum)
{
myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (
theFirstVert + theArray->edges[aVert + (aTriNum % 2) ? 1 : 0],
@ -744,7 +744,7 @@ Standard_Boolean OpenGl_Workspace::AddRaytraceTriangleStripArray (const CALL_DEF
theFirstVert + theVertOffset + 2,
theMatID));
for (int aVert = theVertOffset + 1, aTriNum = 1; aVert < theVertOffset + theVertNum - 2; ++aVert, ++aTriNum)
for (Standard_Integer aVert = theVertOffset + 1, aTriNum = 1; aVert < theVertOffset + theVertNum - 2; ++aVert, ++aTriNum)
{
myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + aVert + ( aTriNum % 2 ) ? 1 : 0,
theFirstVert + aVert + ( aTriNum % 2 ) ? 0 : 1,
@ -761,17 +761,17 @@ Standard_Boolean OpenGl_Workspace::AddRaytraceTriangleStripArray (const CALL_DEF
// purpose : Adds OpenGL quad array to ray-traced scene geometry
// =======================================================================
Standard_Boolean OpenGl_Workspace::AddRaytraceQuadrangleArray (const CALL_DEF_PARRAY* theArray,
int theFirstVert,
int theVertOffset,
int theVertNum,
int theMatID)
Standard_Integer theFirstVert,
Standard_Integer theVertOffset,
Standard_Integer theVertNum,
Standard_Integer theMatID)
{
if (theVertNum < 4)
return Standard_True;
if (theArray->num_edges > 0)
{
for (int aVert = theVertOffset; aVert < theVertOffset + theVertNum - 3; aVert += 4)
for (Standard_Integer aVert = theVertOffset; aVert < theVertOffset + theVertNum - 3; aVert += 4)
{
myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + theArray->edges[aVert + 0],
theFirstVert + theArray->edges[aVert + 1],
@ -786,7 +786,7 @@ Standard_Boolean OpenGl_Workspace::AddRaytraceQuadrangleArray (const CALL_DEF_PA
}
else
{
for (int aVert = theVertOffset; aVert < theVertOffset + theVertNum - 3; aVert += 4)
for (Standard_Integer aVert = theVertOffset; aVert < theVertOffset + theVertNum - 3; aVert += 4)
{
myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + aVert + 0,
theFirstVert + aVert + 1,
@ -808,10 +808,10 @@ Standard_Boolean OpenGl_Workspace::AddRaytraceQuadrangleArray (const CALL_DEF_PA
// purpose : Adds OpenGL quad strip array to ray-traced scene geometry
// =======================================================================
Standard_Boolean OpenGl_Workspace::AddRaytraceQuadrangleStripArray (const CALL_DEF_PARRAY* theArray,
int theFirstVert,
int theVertOffset,
int theVertNum,
int theMatID)
Standard_Integer theFirstVert,
Standard_Integer theVertOffset,
Standard_Integer theVertNum,
Standard_Integer theMatID)
{
if (theVertNum < 4)
return Standard_True;
@ -830,7 +830,7 @@ Standard_Boolean OpenGl_Workspace::AddRaytraceQuadrangleStripArray (const CALL_D
theFirstVert + theArray->edges[theVertOffset + 2],
theMatID));
for (int aVert = theVertOffset + 2; aVert < theVertOffset + theVertNum - 3; aVert += 2)
for (Standard_Integer aVert = theVertOffset + 2; aVert < theVertOffset + theVertNum - 3; aVert += 2)
{
myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (
theFirstVert + theArray->edges[aVert + 0],
@ -857,7 +857,7 @@ Standard_Boolean OpenGl_Workspace::AddRaytraceQuadrangleStripArray (const CALL_D
theFirstVert + 2,
theMatID));
for (int aVert = theVertOffset + 2; aVert < theVertOffset + theVertNum - 3; aVert += 2)
for (Standard_Integer aVert = theVertOffset + 2; aVert < theVertOffset + theVertNum - 3; aVert += 2)
{
myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + aVert + 0,
theFirstVert + aVert + 1,
@ -879,17 +879,17 @@ Standard_Boolean OpenGl_Workspace::AddRaytraceQuadrangleStripArray (const CALL_D
// purpose : Adds OpenGL polygon array to ray-traced scene geometry
// =======================================================================
Standard_Boolean OpenGl_Workspace::AddRaytracePolygonArray (const CALL_DEF_PARRAY* theArray,
int theFirstVert,
int theVertOffset,
int theVertNum,
int theMatID)
Standard_Integer theFirstVert,
Standard_Integer theVertOffset,
Standard_Integer theVertNum,
Standard_Integer theMatID)
{
if (theArray->num_vertexs < 3)
return Standard_True;
if (theArray->edges != NULL)
{
for (int aVert = theVertOffset; aVert < theVertOffset + theVertNum - 2; ++aVert)
for (Standard_Integer aVert = theVertOffset; aVert < theVertOffset + theVertNum - 2; ++aVert)
{
myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + theArray->edges[theVertOffset],
theFirstVert + theArray->edges[aVert + 1],
@ -899,7 +899,7 @@ Standard_Boolean OpenGl_Workspace::AddRaytracePolygonArray (const CALL_DEF_PARRA
}
else
{
for (int aVert = theVertOffset; aVert < theVertOffset + theVertNum - 2; ++aVert)
for (Standard_Integer aVert = theVertOffset; aVert < theVertOffset + theVertNum - 2; ++aVert)
{
myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + theVertOffset,
theFirstVert + aVert + 1,
@ -1298,7 +1298,7 @@ void OpenGl_Workspace::ReleaseOpenCL()
clReleaseMemObject (myRaytraceOutputImage);
clReleaseMemObject (myRaytraceEnvironment);
clReleaseMemObject (myRaytraceOutputImageSmooth);
clReleaseMemObject (myRaytraceOutputImageAA);
clReleaseMemObject (myRaytraceVertexBuffer);
clReleaseMemObject (myRaytraceNormalBuffer);
@ -1313,8 +1313,16 @@ void OpenGl_Workspace::ReleaseOpenCL()
clReleaseContext (myComputeContext);
if (glIsTexture (*myRaytraceOutputTexture))
glDeleteTextures (2, myRaytraceOutputTexture);
if (!myGlContext.IsNull())
{
if (!myRaytraceOutputTexture.IsNull())
myGlContext->DelayedRelease (myRaytraceOutputTexture);
myRaytraceOutputTexture.Nullify();
if (!myRaytraceOutputTextureAA.IsNull())
myGlContext->DelayedRelease (myRaytraceOutputTextureAA);
myRaytraceOutputTextureAA.Nullify();
}
}
// =======================================================================
@ -1329,63 +1337,46 @@ Standard_Boolean OpenGl_Workspace::ResizeRaytraceOutputBuffer (const cl_int theS
return Standard_False;
}
bool toResize = true;
GLint aSizeX = -1;
GLint aSizeY = -1;
if (*myRaytraceOutputTexture != 0)
if (!myRaytraceOutputTexture.IsNull())
{
if (!myGlContext->IsGlGreaterEqual (2, 1))
Standard_Boolean toResize = myRaytraceOutputTexture->SizeX() != theSizeX ||
myRaytraceOutputTexture->SizeY() != theSizeY;
if (!toResize)
return Standard_True;
if (!myGlContext.IsNull())
{
return Standard_False;
}
glBindTexture (GL_TEXTURE_RECTANGLE, *myRaytraceOutputTexture);
glGetTexLevelParameteriv (GL_TEXTURE_RECTANGLE, 0, GL_TEXTURE_WIDTH, &aSizeX);
glGetTexLevelParameteriv (GL_TEXTURE_RECTANGLE, 0, GL_TEXTURE_HEIGHT, &aSizeY);
toResize = (aSizeX != theSizeX) || (aSizeY != theSizeY);
if (toResize)
{
glDeleteTextures (2, myRaytraceOutputTexture);
if (!myRaytraceOutputTexture.IsNull())
myGlContext->DelayedRelease (myRaytraceOutputTexture);
if (!myRaytraceOutputTextureAA.IsNull())
myGlContext->DelayedRelease (myRaytraceOutputTextureAA);
}
}
if (!toResize)
{
return Standard_True;
}
myRaytraceOutputTexture = new OpenGl_Texture();
myRaytraceOutputTexture->Create (myGlContext);
myRaytraceOutputTexture->InitRectangle (myGlContext,
theSizeX, theSizeY, OpenGl_TextureFormat::Create<GLfloat, 4>());
myRaytraceOutputTextureAA = new OpenGl_Texture();
myRaytraceOutputTextureAA->Create (myGlContext);
myRaytraceOutputTextureAA->InitRectangle (myGlContext,
theSizeX, theSizeY, OpenGl_TextureFormat::Create<GLfloat, 4>());
glGenTextures (2, myRaytraceOutputTexture);
for (int aTexIter = 0; aTexIter < 2; ++aTexIter)
{
glBindTexture (GL_TEXTURE_RECTANGLE, myRaytraceOutputTexture[aTexIter]);
if (myRaytraceOutputImage != NULL)
clReleaseMemObject (myRaytraceOutputImage);
glTexParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_R, GL_CLAMP);
glTexParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D (GL_TEXTURE_RECTANGLE, 0, GL_RGBA32F,
theSizeX, theSizeY, 0,
GL_RGBA, GL_FLOAT, NULL);
}
if (myRaytraceOutputImageAA != NULL)
clReleaseMemObject (myRaytraceOutputImageAA);
cl_int anError = CL_SUCCESS;
if (myRaytraceOutputImage != NULL)
{
clReleaseMemObject (myRaytraceOutputImage);
}
if (myRaytraceOutputImageSmooth != NULL)
{
clReleaseMemObject (myRaytraceOutputImageSmooth);
}
myRaytraceOutputImage = clCreateFromGLTexture2D (myComputeContext,
CL_MEM_READ_WRITE, GL_TEXTURE_RECTANGLE, 0, myRaytraceOutputTexture->TextureId(), &anError);
myRaytraceOutputImage = clCreateFromGLTexture2D (myComputeContext, CL_MEM_READ_WRITE,
GL_TEXTURE_RECTANGLE, 0,
myRaytraceOutputTexture[0], &anError);
if (anError != CL_SUCCESS)
{
#ifdef RAY_TRACE_PRINT_INFO
@ -1394,9 +1385,9 @@ Standard_Boolean OpenGl_Workspace::ResizeRaytraceOutputBuffer (const cl_int theS
return Standard_False;
}
myRaytraceOutputImageSmooth = clCreateFromGLTexture2D (myComputeContext, CL_MEM_READ_WRITE,
GL_TEXTURE_RECTANGLE, 0,
myRaytraceOutputTexture[1], &anError);
myRaytraceOutputImageAA = clCreateFromGLTexture2D (myComputeContext,
CL_MEM_READ_WRITE, GL_TEXTURE_RECTANGLE, 0, myRaytraceOutputTextureAA->TextureId(), &anError);
if (anError != CL_SUCCESS)
{
#ifdef RAY_TRACE_PRINT_INFO
@ -1630,8 +1621,8 @@ Standard_Boolean OpenGl_Workspace::WriteRaytraceSceneToDevice()
Standard_Boolean OpenGl_Workspace::RunRaytraceOpenCLKernels (const Graphic3d_CView& theCView,
const GLfloat theOrigins[16],
const GLfloat theDirects[16],
const int theSizeX,
const int theSizeY)
const Standard_Integer theSizeX,
const Standard_Integer theSizeY)
{
if (myRaytraceRenderKernel == NULL || myRaytraceQueue == NULL)
return Standard_False;
@ -1702,7 +1693,7 @@ Standard_Boolean OpenGl_Workspace::RunRaytraceOpenCLKernels (const Graphic3d_CVi
anError = clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
sizeof(cl_mem), &myRaytraceOutputImage);
anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
sizeof(cl_mem), &myRaytraceOutputImageSmooth);
sizeof(cl_mem), &myRaytraceOutputImageAA);
anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
sizeof(cl_mem), &myRaytraceEnvironment);
anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
@ -1941,7 +1932,7 @@ void ComputeInverseMatrix (const T m[16], T inv[16])
det = T (1.0) / det;
for (int i = 0; i < 16; ++i)
for (Standard_Integer i = 0; i < 16; ++i)
inv[i] *= det;
}
@ -1950,15 +1941,15 @@ void ComputeInverseMatrix (const T m[16], T inv[16])
// purpose : Generates primary rays for corners of screen quad
// =======================================================================
void GenerateCornerRays (const GLdouble theInvModelProj[16],
float theOrigins[16],
float theDirects[16])
cl_float theOrigins[16],
cl_float theDirects[16])
{
int aOriginIndex = 0;
int aDirectIndex = 0;
Standard_Integer aOriginIndex = 0;
Standard_Integer aDirectIndex = 0;
for (int y = -1; y <= 1; y += 2)
for (Standard_Integer y = -1; y <= 1; y += 2)
{
for (int x = -1; x <= 1; x += 2)
for (Standard_Integer x = -1; x <= 1; x += 2)
{
OpenGl_RTVec4f aOrigin (float(x),
float(y),
@ -1996,8 +1987,8 @@ void GenerateCornerRays (const GLdouble theInvModelProj[16],
// purpose : Redraws the window using OpenCL ray tracing
// =======================================================================
Standard_Boolean OpenGl_Workspace::Raytrace (const Graphic3d_CView& theCView,
const int theSizeX,
int theSizeY,
const Standard_Integer theSizeX,
const Standard_Integer theSizeY,
const Tint theToSwap)
{
if (!InitOpenCL())
@ -2022,8 +2013,8 @@ Standard_Boolean OpenGl_Workspace::Raytrace (const Graphic3d_CView& theCView,
GLdouble aViewMappingMatrix[16];
GLdouble aOrientationInvers[16];
for (int j = 0; j < 4; ++j)
for (int i = 0; i < 4; ++i)
for (Standard_Integer j = 0; j < 4; ++j)
for (Standard_Integer i = 0; i < 4; ++i)
{
aOrientationMatrix [4 * j + i] = theOrientation (i, j);
aViewMappingMatrix [4 * j + i] = theViewMapping (i, j);
@ -2055,7 +2046,7 @@ Standard_Boolean OpenGl_Workspace::Raytrace (const Graphic3d_CView& theCView,
aDirects);
// Compute ray-traced image using OpenCL kernel
cl_mem anImages[] = { myRaytraceOutputImage, myRaytraceOutputImageSmooth };
cl_mem anImages[] = { myRaytraceOutputImage, myRaytraceOutputImageAA };
cl_int anError = clEnqueueAcquireGLObjects (myRaytraceQueue,
2, anImages,
0, NULL, NULL);
@ -2118,7 +2109,10 @@ Standard_Boolean OpenGl_Workspace::Raytrace (const Graphic3d_CView& theCView,
glColor3f (1.0f, 1.0f, 1.0f);
glBindTexture (GL_TEXTURE_RECTANGLE, myRaytraceOutputTexture[theCView.IsAntialiasingEnabled ? 1 : 0]);
if (!theCView.IsAntialiasingEnabled)
myRaytraceOutputTexture->Bind (myGlContext);
else
myRaytraceOutputTextureAA->Bind (myGlContext);
if (myIsRaytraceDataValid)
{