1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-04-21 10:13:43 +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 // 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! // 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, // but driver will fall back into software rendering,
const bool toForceP2 = !theCtx->IsGlGreaterEqual (3, 0) && !theCtx->arbNPTW; const bool toForceP2 = !theCtx->IsGlGreaterEqual (3, 0) && !theCtx->arbNPTW;
const GLsizei aWidthOut = toForceP2 ? OpenGl_Context::GetPowerOfTwo (aWidth, aMaxSize) : Min (aWidth, aMaxSize); 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 OpenGl_Context;
class Image_PixMap; 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. //! Texture resource.
class OpenGl_Texture : public OpenGl_Resource class OpenGl_Texture : public OpenGl_Resource
{ {
@ -98,6 +225,13 @@ public:
const Image_PixMap& theImage, const Image_PixMap& theImage,
const Graphic3d_TypeOfTexture theType); 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 //! @return true if texture was generated within mipmaps
Standard_EXPORT const Standard_Boolean HasMipmaps() const; Standard_EXPORT const Standard_Boolean HasMipmaps() const;

View File

@ -194,9 +194,6 @@ OpenGl_Workspace::OpenGl_Workspace (const Handle(OpenGl_Display)& theDisplay,
myViewModificationStatus = 0; myViewModificationStatus = 0;
myLayersModificationStatus = 0; myLayersModificationStatus = 0;
myRaytraceOutputTexture[0] = 0;
myRaytraceOutputTexture[1] = 0;
myIsRaytraceDataValid = Standard_False; myIsRaytraceDataValid = Standard_False;
myToUpdateRaytraceData = 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. //! Redraws the window using OpenCL ray tracing.
Standard_Boolean Raytrace (const Graphic3d_CView& theCView, 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 protected: //! @name fields related to ray-tracing
@ -373,10 +373,12 @@ protected: //! @name fields related to ray-tracing
//! OpenCL image to store rendering result. //! OpenCL image to store rendering result.
cl_mem myRaytraceOutputImage; cl_mem myRaytraceOutputImage;
//! OpenCL image to store anti-aliasing result. //! OpenCL image to store anti-aliasing result.
cl_mem myRaytraceOutputImageSmooth; cl_mem myRaytraceOutputImageAA;
//! OpenGL output texture handle. //! OpenGL texture to store rendering result.
GLuint myRaytraceOutputTexture[2]; Handle(OpenGl_Texture) myRaytraceOutputTexture;
//! OpenGL texture to store anti-aliasing result.
Handle(OpenGl_Texture) myRaytraceOutputTextureAA;
//! OpenCL buffer of vertex normals. //! OpenCL buffer of vertex normals.
cl_mem myRaytraceNormalBuffer; cl_mem myRaytraceNormalBuffer;

View File

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