From 1220d98e7ad7320bc4ce575e9d26a11d3d70da74 Mon Sep 17 00:00:00 2001 From: kgv Date: Thu, 6 May 2021 20:03:24 +0300 Subject: [PATCH] 0032349: Visualization, TKOpenGl - move base buffer interface out from OpenGl_VertexBuffer class to OpenGl_Buffer OpenGl_Buffer - added new class as a base for OpenGl_VertexBuffer, OpenGl_IndexBuffer, OpenGl_TextureBuffer. OpenGl_TextureBufferArb has been renamed to OpenGl_TextureBuffer. OpenGl_FrameBuffer - added initializers taking vec2i instead of (int,int) for dimensions. --- src/D3DHost/D3DHost_FrameBuffer.cxx | 9 +- src/D3DHost/D3DHost_FrameBuffer.hxx | 4 +- src/D3DHost/D3DHost_View.cxx | 1 + src/OpenGl/FILES | 10 +- src/OpenGl/OpenGl_BackgroundArray.hxx | 2 +- src/OpenGl/OpenGl_Buffer.cxx | 446 ++++++++++++++++++ src/OpenGl/OpenGl_Buffer.hxx | 293 ++++++++++++ src/OpenGl/OpenGl_BufferCompatT.hxx | 262 ++++++++++ src/OpenGl/OpenGl_DepthPeeling.cxx | 5 +- src/OpenGl/OpenGl_DepthPeeling.hxx | 1 + src/OpenGl/OpenGl_FrameBuffer.cxx | 115 +++-- src/OpenGl/OpenGl_FrameBuffer.hxx | 249 +++++----- src/OpenGl/OpenGl_FrameStats.cxx | 2 + src/OpenGl/OpenGl_FrameStatsPrs.cxx | 2 + src/OpenGl/OpenGl_GraphicDriver.cxx | 1 + src/OpenGl/OpenGl_IndexBuffer.cxx | 9 +- src/OpenGl/OpenGl_IndexBuffer.hxx | 13 +- src/OpenGl/OpenGl_PBREnvironment.cxx | 1 + src/OpenGl/OpenGl_PrimitiveArray.cxx | 2 +- src/OpenGl/OpenGl_PrimitiveArray.hxx | 12 +- src/OpenGl/OpenGl_Sampler.cxx | 11 +- src/OpenGl/OpenGl_Sampler.hxx | 22 +- src/OpenGl/OpenGl_SceneGeometry.cxx | 10 +- src/OpenGl/OpenGl_SceneGeometry.hxx | 1 - src/OpenGl/OpenGl_ShaderManager.hxx | 7 +- src/OpenGl/OpenGl_Texture.cxx | 8 +- src/OpenGl/OpenGl_Texture.hxx | 67 +-- ...BufferArb.cxx => OpenGl_TextureBuffer.cxx} | 85 ++-- src/OpenGl/OpenGl_TextureBuffer.hxx | 116 +++++ src/OpenGl/OpenGl_TextureBufferArb.hxx | 99 +--- src/OpenGl/OpenGl_UniformBuffer.cxx | 35 ++ src/OpenGl/OpenGl_UniformBuffer.hxx | 37 ++ src/OpenGl/OpenGl_VertexBuffer.cxx | 322 +++++-------- src/OpenGl/OpenGl_VertexBuffer.hxx | 348 +------------- src/OpenGl/OpenGl_VertexBuffer.lxx | 136 ------ src/OpenGl/OpenGl_VertexBufferCompat.cxx | 197 -------- src/OpenGl/OpenGl_VertexBufferCompat.hxx | 87 +--- src/OpenGl/OpenGl_VertexBufferEditor.hxx | 27 +- src/OpenGl/OpenGl_View.cxx | 127 ++--- src/OpenGl/OpenGl_View.hxx | 49 +- src/OpenGl/OpenGl_View_Raytrace.cxx | 33 +- src/OpenGl/OpenGl_Window.cxx | 2 +- src/OpenGl/OpenGl_Window_1.mm | 2 +- src/OpenGl/OpenGl_Workspace.cxx | 3 +- src/OpenGl/OpenGl_Workspace.hxx | 12 +- 45 files changed, 1829 insertions(+), 1453 deletions(-) create mode 100644 src/OpenGl/OpenGl_Buffer.cxx create mode 100644 src/OpenGl/OpenGl_Buffer.hxx create mode 100644 src/OpenGl/OpenGl_BufferCompatT.hxx rename src/OpenGl/{OpenGl_TextureBufferArb.cxx => OpenGl_TextureBuffer.cxx} (70%) create mode 100644 src/OpenGl/OpenGl_TextureBuffer.hxx create mode 100644 src/OpenGl/OpenGl_UniformBuffer.cxx create mode 100644 src/OpenGl/OpenGl_UniformBuffer.hxx delete mode 100644 src/OpenGl/OpenGl_VertexBuffer.lxx delete mode 100644 src/OpenGl/OpenGl_VertexBufferCompat.cxx diff --git a/src/D3DHost/D3DHost_FrameBuffer.cxx b/src/D3DHost/D3DHost_FrameBuffer.cxx index 35120490a1..a077d9718c 100644 --- a/src/D3DHost/D3DHost_FrameBuffer.cxx +++ b/src/D3DHost/D3DHost_FrameBuffer.cxx @@ -19,8 +19,9 @@ #include #include +#include +#include #include -#include IMPLEMENT_STANDARD_RTTIEXT(D3DHost_FrameBuffer,OpenGl_FrameBuffer) @@ -114,9 +115,9 @@ Standard_Boolean D3DHost_FrameBuffer::InitD3dFallback (const Handle(OpenGl_Conte const Standard_Boolean theIsD3dEx, const Standard_Integer theSizeX, const Standard_Integer theSizeY, - const GLint theDepthFormat) + const Standard_Integer theDepthFormat) { - const Standard_Boolean isGlInit = OpenGl_FrameBuffer::Init (theCtx, theSizeX, theSizeY, GL_RGBA8, theDepthFormat, 0); + const Standard_Boolean isGlInit = OpenGl_FrameBuffer::Init (theCtx, Graphic3d_Vec2i (theSizeX, theSizeY), GL_RGBA8, theDepthFormat, 0); myD3dFallback = Standard_True; const Standard_Integer aSizeX = theSizeX > 0 ? theSizeX : 2; @@ -142,7 +143,7 @@ Standard_Boolean D3DHost_FrameBuffer::InitD3dInterop (const Handle(OpenGl_Contex const Standard_Boolean theIsD3dEx, const Standard_Integer theSizeX, const Standard_Integer theSizeY, - const GLint theDepthFormat) + const Standard_Integer theDepthFormat) { Release (theCtx.operator->()); #if !defined(GL_ES_VERSION_2_0) diff --git a/src/D3DHost/D3DHost_FrameBuffer.hxx b/src/D3DHost/D3DHost_FrameBuffer.hxx index 4f4f78021c..2f6c4172e0 100644 --- a/src/D3DHost/D3DHost_FrameBuffer.hxx +++ b/src/D3DHost/D3DHost_FrameBuffer.hxx @@ -63,7 +63,7 @@ public: const Standard_Boolean theIsD3dEx, const Standard_Integer theSizeX, const Standard_Integer theSizeY, - const GLint theDepthFormat); + const Standard_Integer theDepthFormat); //! Initializes OpenGL FBO + Direct3D surface for copying memory using fallback. //! Color pixel format is always GL_RGBA8/D3DFMT_X8R8G8B8, no MSAA. @@ -79,7 +79,7 @@ public: const Standard_Boolean theIsD3dEx, const Standard_Integer theSizeX, const Standard_Integer theSizeY, - const GLint theDepthFormat); + const Standard_Integer theDepthFormat); //! Binds Direct3D color buffer to OpenGL texture. Standard_EXPORT Standard_Boolean registerD3dBuffer (const Handle(OpenGl_Context)& theCtx); diff --git a/src/D3DHost/D3DHost_View.cxx b/src/D3DHost/D3DHost_View.cxx index a699f8d45a..6e38676919 100644 --- a/src/D3DHost/D3DHost_View.cxx +++ b/src/D3DHost/D3DHost_View.cxx @@ -19,6 +19,7 @@ #include #include +#include #include diff --git a/src/OpenGl/FILES b/src/OpenGl/FILES index ba46db0118..e5b28a4b3c 100755 --- a/src/OpenGl/FILES +++ b/src/OpenGl/FILES @@ -14,6 +14,9 @@ OpenGl_AspectsSprite.cxx OpenGl_AspectsSprite.hxx OpenGl_AspectsTextureSet.cxx OpenGl_AspectsTextureSet.hxx +OpenGl_Buffer.cxx +OpenGl_Buffer.hxx +OpenGl_BufferCompatT.hxx OpenGl_FrameStats.cxx OpenGl_FrameStats.hxx OpenGl_FrameStatsPrs.cxx @@ -134,13 +137,14 @@ OpenGl_StencilTest.cxx OpenGl_StencilTest.hxx OpenGl_TileSampler.hxx OpenGl_TileSampler.cxx -OpenGl_TextureBufferArb.cxx +OpenGl_TextureBuffer.cxx +OpenGl_TextureBuffer.hxx OpenGl_TextureBufferArb.hxx +OpenGl_UniformBuffer.cxx +OpenGl_UniformBuffer.hxx OpenGl_Vec.hxx OpenGl_VertexBuffer.cxx OpenGl_VertexBuffer.hxx -OpenGl_VertexBuffer.lxx -OpenGl_VertexBufferCompat.cxx OpenGl_VertexBufferCompat.hxx OpenGl_VertexBufferEditor.hxx OpenGl_TextBuilder.hxx diff --git a/src/OpenGl/OpenGl_BackgroundArray.hxx b/src/OpenGl/OpenGl_BackgroundArray.hxx index 795f4aa05c..1b679af91d 100644 --- a/src/OpenGl/OpenGl_BackgroundArray.hxx +++ b/src/OpenGl/OpenGl_BackgroundArray.hxx @@ -18,11 +18,11 @@ #include #include +#include #include #include #include #include -#include //! Tool class for generating reusable data for //! gradient or texture background rendering. diff --git a/src/OpenGl/OpenGl_Buffer.cxx b/src/OpenGl/OpenGl_Buffer.cxx new file mode 100644 index 0000000000..4428ce10f3 --- /dev/null +++ b/src/OpenGl/OpenGl_Buffer.cxx @@ -0,0 +1,446 @@ +// Created by: Kirill GAVRILOV +// Copyright (c) 2013-2014 OPEN CASCADE SAS +// +// This file is part of Open CASCADE Technology software library. +// +// This library is free software; you can redistribute it and/or modify it under +// the terms of the GNU Lesser General Public License version 2.1 as published +// by the Free Software Foundation, with special exception defined in the file +// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT +// distribution for complete text of the license and disclaimer of any warranty. +// +// Alternatively, this file may be used under the terms of Open CASCADE +// commercial license or contractual agreement. + +#include + +#include +#include +#include +#include + +IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Buffer, OpenGl_Resource) + +// ======================================================================= +// function : sizeOfGlType +// purpose : +// ======================================================================= +size_t OpenGl_Buffer::sizeOfGlType (unsigned int theType) +{ + switch (theType) + { + case GL_BYTE: + case GL_UNSIGNED_BYTE: return sizeof(Standard_Byte); + case GL_SHORT: + case GL_UNSIGNED_SHORT: return sizeof(unsigned short); + #ifdef GL_INT + case GL_INT: + #endif + case GL_UNSIGNED_INT: return sizeof(unsigned int); + case GL_FLOAT: return sizeof(float); + #ifdef GL_DOUBLE + case GL_DOUBLE: return sizeof(double); + #endif + default: return 0; + } +} + +// ======================================================================= +// function : FormatTarget +// purpose : +// ======================================================================= +TCollection_AsciiString OpenGl_Buffer::FormatTarget (unsigned int theTarget) +{ + switch (theTarget) + { + case GL_ARRAY_BUFFER: return "GL_ARRAY_BUFFER"; + case GL_ELEMENT_ARRAY_BUFFER: return "GL_ELEMENT_ARRAY_BUFFER"; + case GL_PIXEL_UNPACK_BUFFER: return "GL_PIXEL_UNPACK_BUFFER"; + case GL_PIXEL_PACK_BUFFER: return "GL_PIXEL_PACK_BUFFER"; + case GL_UNIFORM_BUFFER: return "GL_UNIFORM_BUFFER"; + case GL_TEXTURE_BUFFER: return "GL_TEXTURE_BUFFER"; + case GL_COPY_READ_BUFFER: return "GL_COPY_READ_BUFFER"; + case GL_COPY_WRITE_BUFFER: return "GL_COPY_WRITE_BUFFER"; + case GL_TRANSFORM_FEEDBACK_BUFFER: return "GL_TRANSFORM_FEEDBACK_BUFFER"; + #ifdef GL_QUERY_BUFFER + case GL_QUERY_BUFFER: return "GL_QUERY_BUFFER"; + case GL_DRAW_INDIRECT_BUFFER: return "GL_DRAW_INDIRECT_BUFFER"; + case GL_ATOMIC_COUNTER_BUFFER: return "GL_ATOMIC_COUNTER_BUFFER"; + case GL_DISPATCH_INDIRECT_BUFFER: return "GL_DISPATCH_INDIRECT_BUFFER"; + case GL_SHADER_STORAGE_BUFFER: return "GL_SHADER_STORAGE_BUFFER"; + #endif + } + return OpenGl_Context::FormatGlEnumHex (theTarget); +} + +// ======================================================================= +// function : OpenGl_Buffer +// purpose : +// ======================================================================= +OpenGl_Buffer::OpenGl_Buffer() +: OpenGl_Resource(), + myOffset (NULL), + myBufferId (NO_BUFFER), + myComponentsNb (4), + myElemsNb (0), + myDataType (GL_FLOAT) +{ + // +} + +// ======================================================================= +// function : ~OpenGl_Buffer +// purpose : +// ======================================================================= +OpenGl_Buffer::~OpenGl_Buffer() +{ + Release (NULL); +} + +// ======================================================================= +// function : Create +// purpose : +// ======================================================================= +bool OpenGl_Buffer::Create (const Handle(OpenGl_Context)& theGlCtx) +{ + if (myBufferId == NO_BUFFER && theGlCtx->core15fwd != NULL) + { + theGlCtx->core15fwd->glGenBuffers (1, &myBufferId); + } + return myBufferId != NO_BUFFER; +} + +// ======================================================================= +// function : Release +// purpose : +// ======================================================================= +void OpenGl_Buffer::Release (OpenGl_Context* theGlCtx) +{ + if (myBufferId == NO_BUFFER) + { + return; + } + + // application can not handle this case by exception - this is bug in code + Standard_ASSERT_RETURN (theGlCtx != NULL, + "OpenGl_Buffer destroyed without GL context! Possible GPU memory leakage...",); + + if (theGlCtx->IsValid()) + { + theGlCtx->core15fwd->glDeleteBuffers (1, &myBufferId); + } + myOffset = NULL; + myBufferId = NO_BUFFER; +} + +// ======================================================================= +// function : Bind +// purpose : +// ======================================================================= +void OpenGl_Buffer::Bind (const Handle(OpenGl_Context)& theGlCtx) const +{ + theGlCtx->core15fwd->glBindBuffer (GetTarget(), myBufferId); +} + +// ======================================================================= +// function : Unbind +// purpose : +// ======================================================================= +void OpenGl_Buffer::Unbind (const Handle(OpenGl_Context)& theGlCtx) const +{ + theGlCtx->core15fwd->glBindBuffer (GetTarget(), NO_BUFFER); +} + +// ======================================================================= +// function : BindBufferBase +// purpose : +// ======================================================================= +void OpenGl_Buffer::BindBufferBase (const Handle(OpenGl_Context)& theGlCtx, + unsigned int theIndex) +{ + theGlCtx->core30->glBindBufferBase (GetTarget(), theIndex, myBufferId); +} + +// ======================================================================= +// function : UnbindBufferBase +// purpose : +// ======================================================================= +void OpenGl_Buffer::UnbindBufferBase (const Handle(OpenGl_Context)& theGlCtx, + unsigned int theIndex) +{ + theGlCtx->core30->glBindBufferBase (GetTarget(), theIndex, NO_BUFFER); +} + +// ======================================================================= +// function : BindBufferRange +// purpose : +// ======================================================================= +void OpenGl_Buffer::BindBufferRange (const Handle(OpenGl_Context)& theGlCtx, + unsigned int theIndex, + const intptr_t theOffset, + const size_t theSize) +{ + theGlCtx->core30->glBindBufferRange (GetTarget(), theIndex, myBufferId, (GLintptr )theOffset, (GLsizeiptr )theSize); +} + +// ======================================================================= +// function : Init +// purpose : +// ======================================================================= +bool OpenGl_Buffer::Init (const Handle(OpenGl_Context)& theGlCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const float* theData) +{ + return init (theGlCtx, theComponentsNb, theElemsNb, theData, GL_FLOAT); +} + +// ======================================================================= +// function : Init +// purpose : +// ======================================================================= +bool OpenGl_Buffer::Init (const Handle(OpenGl_Context)& theGlCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const unsigned int* theData) +{ + return init (theGlCtx, theComponentsNb, theElemsNb, theData, GL_UNSIGNED_INT); +} + +// ======================================================================= +// function : Init +// purpose : +// ======================================================================= +bool OpenGl_Buffer::Init (const Handle(OpenGl_Context)& theGlCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const unsigned short* theData) +{ + return init (theGlCtx, theComponentsNb, theElemsNb, theData, GL_UNSIGNED_SHORT); +} + +// ======================================================================= +// function : Init +// purpose : +// ======================================================================= +bool OpenGl_Buffer::Init (const Handle(OpenGl_Context)& theGlCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const Standard_Byte* theData) +{ + return init (theGlCtx, theComponentsNb, theElemsNb, theData, GL_UNSIGNED_BYTE); +} + +// ======================================================================= +// function : init +// purpose : +// ======================================================================= +bool OpenGl_Buffer::init (const Handle(OpenGl_Context)& theGlCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const void* theData, + const unsigned int theDataType, + const Standard_Integer theStride) +{ + if (!Create (theGlCtx)) + { + return false; + } + + Bind (theGlCtx); + myDataType = theDataType; + myComponentsNb = theComponentsNb; + myElemsNb = theElemsNb; + theGlCtx->core15fwd->glBufferData (GetTarget(), GLsizeiptr(myElemsNb) * theStride, theData, GL_STATIC_DRAW); + const int anErr = theGlCtx->core15fwd->glGetError(); + if (anErr != GL_NO_ERROR + && anErr != GL_OUT_OF_MEMORY) // pass-through out-of-memory error, but log unexpected errors + { + theGlCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, + TCollection_AsciiString ("Error: glBufferData (") + + FormatTarget (GetTarget()) + "," + + OpenGl_Context::FormatSize (GLsizeiptr(myElemsNb) * theStride) + "," + + OpenGl_Context::FormatPointer (theData) + ") Id: " + (int )myBufferId + + " failed with " + OpenGl_Context::FormatGlError (anErr)); + } + Unbind (theGlCtx); + return anErr == GL_NO_ERROR; +} + +// ======================================================================= +// function : SubData +// purpose : +// ======================================================================= +bool OpenGl_Buffer::SubData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + const float* theData) +{ + return subData (theGlCtx, theElemFrom, theElemsNb, theData, GL_FLOAT); +} + +// ======================================================================= +// function : SubData +// purpose : +// ======================================================================= +bool OpenGl_Buffer::SubData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + const unsigned int* theData) +{ + return subData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_INT); +} + +// ======================================================================= +// function : SubData +// purpose : +// ======================================================================= +bool OpenGl_Buffer::SubData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + const unsigned short* theData) +{ + return subData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_SHORT); +} + +// ======================================================================= +// function : SubData +// purpose : +// ======================================================================= +bool OpenGl_Buffer::SubData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + const Standard_Byte* theData) +{ + return subData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_BYTE); +} + +// ======================================================================= +// function : subData +// purpose : +// ======================================================================= +bool OpenGl_Buffer::subData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + const void* theData, + const unsigned int theDataType) +{ + if (!IsValid() || myDataType != theDataType || + theElemFrom < 0 || ((theElemFrom + theElemsNb) > myElemsNb)) + { + return false; + } + + Bind (theGlCtx); + const size_t aDataSize = sizeOfGlType (theDataType); + theGlCtx->core15fwd->glBufferSubData (GetTarget(), + GLintptr(theElemFrom) * GLintptr (myComponentsNb) * aDataSize, // offset in bytes + GLsizeiptr(theElemsNb) * GLsizeiptr(myComponentsNb) * aDataSize, // size in bytes + theData); + const int anErr = theGlCtx->core15fwd->glGetError(); + if (anErr != GL_NO_ERROR) + { + theGlCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, + TCollection_AsciiString ("Error: glBufferSubData (") + + FormatTarget (GetTarget()) + "," + + OpenGl_Context::FormatSize (GLintptr(theElemFrom) * GLintptr (myComponentsNb) * aDataSize) + "," + + OpenGl_Context::FormatSize (GLsizeiptr(theElemsNb) * GLsizeiptr(myComponentsNb) * aDataSize) + "," + + OpenGl_Context::FormatPointer (theData) + ") Id: " + (int )myBufferId + + " failed with " + OpenGl_Context::FormatGlError (anErr)); + } + Unbind (theGlCtx); + return anErr == GL_NO_ERROR; +} + +// ======================================================================= +// function : subData +// purpose : +// ======================================================================= +bool OpenGl_Buffer::GetSubData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + float* theData) +{ + return getSubData (theGlCtx, theElemFrom, theElemsNb, theData, GL_FLOAT); +} + +// ======================================================================= +// function : GetSubData +// purpose : +// ======================================================================= +bool OpenGl_Buffer::GetSubData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + unsigned short* theData) +{ + return getSubData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_SHORT); +} + +// ======================================================================= +// function : GetSubData +// purpose : +// ======================================================================= +bool OpenGl_Buffer::GetSubData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + unsigned int* theData) +{ + return getSubData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_INT); +} + +// ======================================================================= +// function : GetSubData +// purpose : +// ======================================================================= +bool OpenGl_Buffer::GetSubData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + Standard_Byte* theData) +{ + return getSubData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_BYTE); +} + +// ======================================================================= +// function : getSubData +// purpose : +// ======================================================================= +bool OpenGl_Buffer::getSubData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + void* theData, + const unsigned int theDataType) +{ + if (!IsValid() || myDataType != theDataType + || theElemFrom < 0 || ((theElemFrom + theElemsNb) > myElemsNb) + || !theGlCtx->hasGetBufferData) + { + return false; + } + + Bind (theGlCtx); + const size_t aDataSize = sizeOfGlType (theDataType); + const GLintptr anOffset = GLintptr (theElemFrom) * GLintptr (myComponentsNb) * aDataSize; + const GLsizeiptr aSize = GLsizeiptr(theElemsNb) * GLsizeiptr(myComponentsNb) * aDataSize; + bool isDone = theGlCtx->GetBufferSubData (GetTarget(), anOffset, aSize, theData); + isDone = isDone && (theGlCtx->core15fwd->glGetError() == GL_NO_ERROR); + Unbind (theGlCtx); + return isDone; +} + +// ======================================================================= +// function : DumpJson +// purpose : +// ======================================================================= +void OpenGl_Buffer::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const +{ + OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream) + OCCT_DUMP_BASE_CLASS (theOStream, theDepth, OpenGl_Resource) + + OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, GetTarget()) + OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, myOffset) + + OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myBufferId) + OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myComponentsNb) + OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myElemsNb) + OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myDataType) +} diff --git a/src/OpenGl/OpenGl_Buffer.hxx b/src/OpenGl/OpenGl_Buffer.hxx new file mode 100644 index 0000000000..e18ce4b198 --- /dev/null +++ b/src/OpenGl/OpenGl_Buffer.hxx @@ -0,0 +1,293 @@ +// Created by: Kirill GAVRILOV +// Copyright (c) 2013-2014 OPEN CASCADE SAS +// +// This file is part of Open CASCADE Technology software library. +// +// This library is free software; you can redistribute it and/or modify it under +// the terms of the GNU Lesser General Public License version 2.1 as published +// by the Free Software Foundation, with special exception defined in the file +// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT +// distribution for complete text of the license and disclaimer of any warranty. +// +// Alternatively, this file may be used under the terms of Open CASCADE +// commercial license or contractual agreement. + +#ifndef _OpenGl_Buffer_H__ +#define _OpenGl_Buffer_H__ + +#include +#include + +//! Buffer Object - is a general storage object for arbitrary data (see sub-classes). +class OpenGl_Buffer : public OpenGl_Resource +{ + DEFINE_STANDARD_RTTIEXT(OpenGl_Buffer, OpenGl_Resource) +public: + + //! Helpful constants + static const unsigned int NO_BUFFER = 0; + + //! Format VBO target enumeration value. + Standard_EXPORT static TCollection_AsciiString FormatTarget (unsigned int theTarget); + +public: + + //! Create uninitialized buffer. + Standard_EXPORT OpenGl_Buffer(); + + //! Destroy object. + Standard_EXPORT virtual ~OpenGl_Buffer(); + + //! Return buffer target. + virtual unsigned int GetTarget() const = 0; + + //! Return TRUE if this is a virtual (for backward compatibility) VBO object. + virtual bool IsVirtual() const { return false; } + + //! @return true if current object was initialized + bool IsValid() const { return myBufferId != NO_BUFFER; } + + //! @return the number of components per generic vertex attribute. + unsigned int GetComponentsNb() const { return myComponentsNb; } + + //! @return number of vertex attributes / number of vertices specified within ::Init() + Standard_Integer GetElemsNb() const { return myElemsNb; } + + //! Overrides the number of vertex attributes / number of vertexes. + //! It is up to user specifying this number correct (e.g. below initial value)! + void SetElemsNb (Standard_Integer theNbElems) { myElemsNb = theNbElems; } + + //! @return data type of each component in the array. + unsigned int GetDataType() const { return myDataType; } + + //! @return offset to data, NULL by default + Standard_Byte* GetDataOffset() const { return myOffset; } + + //! Creates buffer object name (id) if not yet generated. + //! Data should be initialized by another method. + Standard_EXPORT virtual bool Create (const Handle(OpenGl_Context)& theGlCtx); + + //! Destroy object - will release GPU memory if any. + Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE; + + //! Bind this buffer object. + Standard_EXPORT virtual void Bind (const Handle(OpenGl_Context)& theGlCtx) const; + + //! Unbind this buffer object. + Standard_EXPORT virtual void Unbind (const Handle(OpenGl_Context)& theGlCtx) const; + + //! Notice that buffer object will be unbound after this call. + //! @param theComponentsNb [in] specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4; + //! @param theElemsNb [in] elements count; + //! @param theData [in] pointer to float data (vertices/normals etc.). + Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const float* theData); + + //! Notice that buffer object will be unbound after this call. + //! @param theComponentsNb [in] specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4; + //! @param theElemsNb [in] elements count; + //! @param theData [in] pointer to unsigned int data (indices etc.). + Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const unsigned int* theData); + + //! Notice that buffer object will be unbound after this call. + //! @param theComponentsNb [in] specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4; + //! @param theElemsNb [in] elements count; + //! @param theData [in] pointer to unsigned short data (indices etc.). + Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const unsigned short* theData); + + //! Notice that buffer object will be unbound after this call. + //! @param theComponentsNb [in] specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4; + //! @param theElemsNb [in] elements count; + //! @param theData [in] pointer to Standard_Byte data (indices/colors etc.). + Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const Standard_Byte* theData); + + //! Notice that buffer object will be unbound after this call. + //! Function replaces portion of data within this buffer object using glBufferSubData(). + //! The buffer object should be initialized before call. + //! @param theElemFrom [in] element id from which replace buffer data (>=0); + //! @param theElemsNb [in] elements count (theElemFrom + theElemsNb <= GetElemsNb()); + //! @param theData [in] pointer to float data. + Standard_EXPORT bool SubData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + const float* theData); + + //! Read back buffer sub-range. + //! Notice that buffer object will be unbound after this call. + //! Function reads portion of data from this buffer object using glGetBufferSubData(). + //! @param theElemFrom [in] element id from which replace buffer data (>=0); + //! @param theElemsNb [in] elements count (theElemFrom + theElemsNb <= GetElemsNb()); + //! @param theData [out] destination pointer to float data. + Standard_EXPORT bool GetSubData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + float* theData); + + //! Notice that buffer object will be unbound after this call. + //! Function replaces portion of data within this buffer object using glBufferSubData(). + //! The buffer object should be initialized before call. + //! @param theElemFrom [in] element id from which replace buffer data (>=0); + //! @param theElemsNb [in] elements count (theElemFrom + theElemsNb <= GetElemsNb()); + //! @param theData [in] pointer to unsigned int data. + Standard_EXPORT bool SubData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + const unsigned int* theData); + + //! Read back buffer sub-range. + //! Notice that buffer object will be unbound after this call. + //! Function reads portion of data from this buffer object using glGetBufferSubData(). + //! @param theElemFrom [in] element id from which replace buffer data (>=0); + //! @param theElemsNb [in] elements count (theElemFrom + theElemsNb <= GetElemsNb()); + //! @param theData [out] destination pointer to unsigned int data. + Standard_EXPORT bool GetSubData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + unsigned int* theData); + + //! Notice that buffer object will be unbound after this call. + //! Function replaces portion of data within this buffer object using glBufferSubData(). + //! The buffer object should be initialized before call. + //! @param theElemFrom [in] element id from which replace buffer data (>=0); + //! @param theElemsNb [in] elements count (theElemFrom + theElemsNb <= GetElemsNb()); + //! @param theData [in] pointer to unsigned short data. + Standard_EXPORT bool SubData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + const unsigned short* theData); + + //! Read back buffer sub-range. + //! Notice that buffer object will be unbound after this call. + //! Function reads portion of data from this buffer object using glGetBufferSubData(). + //! @param theElemFrom [in] element id from which replace buffer data (>=0); + //! @param theElemsNb [in] elements count (theElemFrom + theElemsNb <= GetElemsNb()); + //! @param theData [out] destination pointer to unsigned short data. + Standard_EXPORT bool GetSubData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + unsigned short* theData); + + //! Notice that buffer object will be unbound after this call. + //! Function replaces portion of data within this buffer object using glBufferSubData(). + //! The buffer object should be initialized before call. + //! @param theElemFrom [in] element id from which replace buffer data (>=0); + //! @param theElemsNb [in] elements count (theElemFrom + theElemsNb <= GetElemsNb()); + //! @param theData [in] pointer to Standard_Byte data. + Standard_EXPORT bool SubData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + const Standard_Byte* theData); + + //! Read back buffer sub-range. + //! Notice that buffer object will be unbound after this call. + //! Function reads portion of data from this buffer object using glGetBufferSubData(). + //! @param theElemFrom [in] element id from which replace buffer data (>=0); + //! @param theElemsNb [in] elements count (theElemFrom + theElemsNb <= GetElemsNb()); + //! @param theData [out] destination pointer to Standard_Byte data. + Standard_EXPORT bool GetSubData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + Standard_Byte* theData); + +public: //! @name advanced methods + + //! Returns estimated GPU memory usage for holding data without considering overheads and allocation alignment rules. + virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE + { + return IsValid() + ? sizeOfGlType (myDataType) * myComponentsNb * myElemsNb + : 0; + } + + //! @return size of specified GL type + Standard_EXPORT static size_t sizeOfGlType (unsigned int theType); + + //! Initialize buffer with new data. + Standard_EXPORT virtual bool init (const Handle(OpenGl_Context)& theGlCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const void* theData, + const unsigned int theDataType, + const Standard_Integer theStride); + + //! Initialize buffer with new data. + bool init (const Handle(OpenGl_Context)& theGlCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const void* theData, + const unsigned int theDataType) + { + return init (theGlCtx, theComponentsNb, theElemsNb, theData, theDataType, + Standard_Integer(theComponentsNb) * Standard_Integer(sizeOfGlType (theDataType))); + } + + //! Update part of the buffer with new data. + Standard_EXPORT virtual bool subData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + const void* theData, + const unsigned int theDataType); + + //! Read back buffer sub-range. + Standard_EXPORT virtual bool getSubData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + void* theData, + const unsigned int theDataType); + +public: + + //! Dumps the content of me into the stream + Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const Standard_OVERRIDE; + +protected: + + //! Binds a buffer object to an indexed buffer target. + //! Wrapper for glBindBufferBase(). + //! @param theGlCtx [in] active OpenGL context + //! @param theIndex [in] index to bind + Standard_EXPORT void BindBufferBase (const Handle(OpenGl_Context)& theGlCtx, + unsigned int theIndex); + + //! Unbinds a buffer object from an indexed buffer target. + //! Wrapper for glBindBufferBase(). + //! @param theGlCtx [in] active OpenGL context + //! @param theIndex [in] index to bind + Standard_EXPORT void UnbindBufferBase (const Handle(OpenGl_Context)& theGlCtx, + unsigned int theIndex); + + //! Binds a buffer object to an indexed buffer target with specified offset and size. + //! Wrapper for glBindBufferRange(). + //! @param theGlCtx [in] active OpenGL context + //! @param theIndex [in] index to bind (@sa GL_MAX_UNIFORM_BUFFER_BINDINGS in case of uniform buffer) + //! @param theOffset [in] offset within the buffer (@sa GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT in case of uniform buffer) + //! @param theSize [in] sub-section length starting from offset + Standard_EXPORT void BindBufferRange (const Handle(OpenGl_Context)& theGlCtx, + unsigned int theIndex, + const intptr_t theOffset, + const size_t theSize); + +protected: + + Standard_Byte* myOffset; //!< offset to data + unsigned int myBufferId; //!< VBO name (index) + unsigned int myComponentsNb; //!< Number of components per generic vertex attribute, must be 1, 2, 3, or 4 + Standard_Integer myElemsNb; //!< Number of vertex attributes / number of vertices + unsigned int myDataType; //!< Data type (GL_FLOAT, GL_UNSIGNED_INT, GL_UNSIGNED_BYTE etc.) + +}; + +DEFINE_STANDARD_HANDLE(OpenGl_Buffer, OpenGl_Resource) + +#endif // _OpenGl_Buffer_H__ diff --git a/src/OpenGl/OpenGl_BufferCompatT.hxx b/src/OpenGl/OpenGl_BufferCompatT.hxx new file mode 100644 index 0000000000..990ae8376a --- /dev/null +++ b/src/OpenGl/OpenGl_BufferCompatT.hxx @@ -0,0 +1,262 @@ +// Created by: Kirill GAVRILOV +// Copyright (c) 2014 OPEN CASCADE SAS +// +// This file is part of Open CASCADE Technology software library. +// +// This library is free software; you can redistribute it and/or modify it under +// the terms of the GNU Lesser General Public License version 2.1 as published +// by the Free Software Foundation, with special exception defined in the file +// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT +// distribution for complete text of the license and disclaimer of any warranty. +// +// Alternatively, this file may be used under the terms of Open CASCADE +// commercial license or contractual agreement. + +#ifndef _OpenGl_BufferCompatT_HeaderFile +#define _OpenGl_BufferCompatT_HeaderFile + +#include +#include + +//! Compatibility layer for old OpenGL without VBO. +//! Make sure to pass pointer from GetDataOffset() instead of NULL. +//! Method GetDataOffset() returns pointer to real data in this class +//! (while base class OpenGl_VertexBuffer always return NULL). +//! +//! Methods Bind()/Unbind() do nothing (do not affect OpenGL state) +//! and ::GetTarget() is never used. +//! For this reason there is no analog for OpenGl_IndexBuffer. +//! Just pass GetDataOffset() to glDrawElements() directly as last argument. +//! +//! Class overrides methods init() and subData() to copy data into own memory buffer. +//! Extra method initLink() might be used to pass existing buffer through handle without copying the data. +//! +//! Method Create() creates dummy identifier for this object which should NOT be passed to OpenGL functions. +template +class OpenGl_BufferCompatT : public BaseBufferT +{ + +public: + + //! Create uninitialized VBO. + OpenGl_BufferCompatT() + { + // + } + + //! Destroy object. + virtual ~OpenGl_BufferCompatT() + { + Release (NULL); + } + + //! Return TRUE. + virtual bool IsVirtual() const Standard_OVERRIDE { return true; } + + //! Creates VBO name (id) if not yet generated. + //! Data should be initialized by another method. + inline bool Create (const Handle(OpenGl_Context)& theGlCtx) Standard_OVERRIDE; + + //! Destroy object - will release memory if any. + inline virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE; + + //! Bind this VBO. + virtual void Bind (const Handle(OpenGl_Context)& ) const Standard_OVERRIDE + { + // + } + + //! Unbind this VBO. + virtual void Unbind (const Handle(OpenGl_Context)& ) const Standard_OVERRIDE + { + // + } + +public: //! @name advanced methods + + //! Initialize buffer with existing data. + //! Data will NOT be copied by this method! + inline bool initLink (const Handle(NCollection_Buffer)& theData, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const unsigned int theDataType); + + //! Initialize buffer with new data (data will be copied). + inline virtual bool init (const Handle(OpenGl_Context)& theGlCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const void* theData, + const unsigned int theDataType, + const Standard_Integer theStride) Standard_OVERRIDE; + + //! Update part of the buffer with new data. + inline virtual bool subData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + const void* theData, + const unsigned int theDataType) Standard_OVERRIDE; + + //! Read back buffer sub-range. + inline virtual bool getSubData (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + void* theData, + const unsigned int theDataType) Standard_OVERRIDE; + +protected: + + Handle(NCollection_Buffer) myData; //!< buffer data + +}; + +// ======================================================================= +// function : Create +// purpose : +// ======================================================================= +template +bool OpenGl_BufferCompatT::Create (const Handle(OpenGl_Context)& ) +{ + if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER) + { + BaseBufferT::myBufferId = (unsigned int )-1; // dummy identifier... + myData = new NCollection_Buffer (Graphic3d_Buffer::DefaultAllocator()); + } + return BaseBufferT::myBufferId != OpenGl_Buffer::NO_BUFFER; +} + +// ======================================================================= +// function : Release +// purpose : +// ======================================================================= +template +void OpenGl_BufferCompatT::Release (OpenGl_Context* ) +{ + if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER) + { + return; + } + + BaseBufferT::myOffset = NULL; + BaseBufferT::myBufferId = OpenGl_Buffer::NO_BUFFER; + myData.Nullify(); +} + +// ======================================================================= +// function : initLink +// purpose : +// ======================================================================= +template +bool OpenGl_BufferCompatT::initLink (const Handle(NCollection_Buffer)& theData, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const unsigned int theDataType) +{ + if (theData.IsNull()) + { + BaseBufferT::myOffset = NULL; + return false; + } + + if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER) + { + BaseBufferT::myBufferId = (unsigned int )-1; // dummy identifier... + } + myData = theData; + BaseBufferT::myDataType = theDataType; + BaseBufferT::myComponentsNb = theComponentsNb; + BaseBufferT::myElemsNb = theElemsNb; + BaseBufferT::myOffset = myData->ChangeData(); + return true; +} + +// ======================================================================= +// function : init +// purpose : +// ======================================================================= +template +bool OpenGl_BufferCompatT::init (const Handle(OpenGl_Context)& theCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const void* theData, + const unsigned int theDataType, + const Standard_Integer theStride) +{ + if (!Create (theCtx)) + { + BaseBufferT::myOffset = NULL; + return false; + } + + BaseBufferT::myDataType = theDataType; + BaseBufferT::myComponentsNb = theComponentsNb; + BaseBufferT::myElemsNb = theElemsNb; + + const size_t aNbBytes = size_t(BaseBufferT::myElemsNb) * theStride; + if (!myData->Allocate (aNbBytes)) + { + BaseBufferT::myOffset = NULL; + return false; + } + + BaseBufferT::myOffset = myData->ChangeData(); + if (theData != NULL) + { + memcpy (myData->ChangeData(), theData, aNbBytes); + } + return true; +} + +// ======================================================================= +// function : subData +// purpose : +// ======================================================================= +template +bool OpenGl_BufferCompatT::subData (const Handle(OpenGl_Context)& , + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + const void* theData, + const unsigned int theDataType) +{ + if (!BaseBufferT::IsValid() || BaseBufferT::myDataType != theDataType + || theElemFrom < 0 || ((theElemFrom + theElemsNb) > BaseBufferT::myElemsNb)) + { + return false; + } + else if (theData == NULL) + { + return true; + } + + const size_t aDataSize = BaseBufferT::sizeOfGlType (theDataType); + const size_t anOffset = size_t(theElemFrom) * size_t(BaseBufferT::myComponentsNb) * aDataSize; + const size_t aNbBytes = size_t(theElemsNb) * size_t(BaseBufferT::myComponentsNb) * aDataSize; + memcpy (myData->ChangeData() + anOffset, theData, aNbBytes); + return true; +} + +// ======================================================================= +// function : getSubData +// purpose : +// ======================================================================= +template +bool OpenGl_BufferCompatT::getSubData (const Handle(OpenGl_Context)& , + const Standard_Integer theElemFrom, + const Standard_Integer theElemsNb, + void* theData, + const unsigned int theDataType) +{ + if (!BaseBufferT::IsValid() || BaseBufferT::myDataType != theDataType + || theElemFrom < 0 || ((theElemFrom + theElemsNb) > BaseBufferT::myElemsNb) + || theData == NULL) + { + return false; + } + + const size_t aDataSize = BaseBufferT::sizeOfGlType (theDataType); + const size_t anOffset = size_t(theElemFrom) * size_t(BaseBufferT::myComponentsNb) * aDataSize; + const size_t aNbBytes = size_t(theElemsNb) * size_t(BaseBufferT::myComponentsNb) * aDataSize; + memcpy (theData, myData->Data() + anOffset, aNbBytes); + return true; +} + +#endif // _OpenGl_VertexBufferCompat_HeaderFile diff --git a/src/OpenGl/OpenGl_DepthPeeling.cxx b/src/OpenGl/OpenGl_DepthPeeling.cxx index af2e3adc6b..f89caa2976 100644 --- a/src/OpenGl/OpenGl_DepthPeeling.cxx +++ b/src/OpenGl/OpenGl_DepthPeeling.cxx @@ -12,9 +12,12 @@ // Alternatively, this file may be used under the terms of Open CASCADE // commercial license or contractual agreement. -#include #include +#include +#include +#include + IMPLEMENT_STANDARD_RTTIEXT(OpenGl_DepthPeeling, OpenGl_NamedResource) //======================================================================= diff --git a/src/OpenGl/OpenGl_DepthPeeling.hxx b/src/OpenGl/OpenGl_DepthPeeling.hxx index b26a958c18..7345cd9c4c 100644 --- a/src/OpenGl/OpenGl_DepthPeeling.hxx +++ b/src/OpenGl/OpenGl_DepthPeeling.hxx @@ -16,6 +16,7 @@ #define _OpenGl_DepthPeeling_HeaderFile #include +#include //! Class provides FBOs for dual depth peeling. class OpenGl_DepthPeeling : public OpenGl_NamedResource diff --git a/src/OpenGl/OpenGl_FrameBuffer.cxx b/src/OpenGl/OpenGl_FrameBuffer.cxx index 44b94e0903..628016deda 100644 --- a/src/OpenGl/OpenGl_FrameBuffer.cxx +++ b/src/OpenGl/OpenGl_FrameBuffer.cxx @@ -13,7 +13,10 @@ // commercial license or contractual agreement. #include + #include +#include +#include #include #include @@ -91,6 +94,24 @@ OpenGl_FrameBuffer::~OpenGl_FrameBuffer() Release (NULL); } +// ======================================================================= +// function : GetSizeX +// purpose : +// ======================================================================= +Standard_Integer OpenGl_FrameBuffer::GetSizeX() const +{ + return !myColorTextures.IsEmpty() ? myColorTextures.First()->SizeX() : 0; +} + +// ======================================================================= +// function : GetSizeY +// purpose : +// ======================================================================= +Standard_Integer OpenGl_FrameBuffer::GetSizeY() const +{ + return !myColorTextures.IsEmpty() ? myColorTextures.First()->SizeY() : 0; +} + // ======================================================================= // function : InitWrapper // purpose : @@ -164,18 +185,17 @@ Standard_Boolean OpenGl_FrameBuffer::InitWrapper (const Handle(OpenGl_Context)& // purpose : // ======================================================================= Standard_Boolean OpenGl_FrameBuffer::Init (const Handle(OpenGl_Context)& theGlContext, - const GLsizei theSizeX, - const GLsizei theSizeY, - const GLint theColorFormat, - const GLint theDepthFormat, - const GLsizei theNbSamples) + const Graphic3d_Vec2i& theSize, + const Standard_Integer theColorFormat, + const Standard_Integer theDepthFormat, + const Standard_Integer theNbSamples) { OpenGl_ColorFormats aColorFormats; if (theColorFormat != 0) { aColorFormats.Append (theColorFormat); } - return Init (theGlContext, theSizeX, theSizeY, aColorFormats, theDepthFormat, theNbSamples); + return Init (theGlContext, theSize, aColorFormats, theDepthFormat, theNbSamples); } // ======================================================================= @@ -183,11 +203,10 @@ Standard_Boolean OpenGl_FrameBuffer::Init (const Handle(OpenGl_Context)& theGlCo // purpose : // ======================================================================= Standard_Boolean OpenGl_FrameBuffer::Init (const Handle(OpenGl_Context)& theGlContext, - const GLsizei theSizeX, - const GLsizei theSizeY, + const Graphic3d_Vec2i& theSize, const OpenGl_ColorFormats& theColorFormats, const Handle(OpenGl_Texture)& theDepthStencilTexture, - const GLsizei theNbSamples) + const Standard_Integer theNbSamples) { myColorFormats = theColorFormats; @@ -226,10 +245,10 @@ Standard_Boolean OpenGl_FrameBuffer::Init (const Handle(OpenGl_Context)& theGlCo myIsOwnBuffer = true; // setup viewport sizes as is - myVPSizeX = theSizeX; - myVPSizeY = theSizeY; - const Standard_Integer aSizeX = theSizeX > 0 ? theSizeX : 2; - const Standard_Integer aSizeY = theSizeY > 0 ? theSizeY : 2; + myVPSizeX = theSize.x(); + myVPSizeY = theSize.y(); + const Standard_Integer aSizeX = theSize.x() > 0 ? theSize.x() : 2; + const Standard_Integer aSizeY = theSize.y() > 0 ? theSize.y() : 2; // Create the textures (will be used as color buffer and depth-stencil buffer) if (theNbSamples != 0) @@ -306,11 +325,10 @@ Standard_Boolean OpenGl_FrameBuffer::Init (const Handle(OpenGl_Context)& theGlCo // purpose : // ======================================================================= Standard_Boolean OpenGl_FrameBuffer::Init (const Handle(OpenGl_Context)& theGlContext, - const GLsizei theSizeX, - const GLsizei theSizeY, + const Graphic3d_Vec2i& theSize, const OpenGl_ColorFormats& theColorFormats, - const GLint theDepthFormat, - const GLsizei theNbSamples) + const Standard_Integer theDepthFormat, + const Standard_Integer theNbSamples) { myColorFormats = theColorFormats; @@ -330,8 +348,8 @@ Standard_Boolean OpenGl_FrameBuffer::Init (const Handle(OpenGl_Context)& theGlCo myDepthFormat = theDepthFormat; myNbSamples = theNbSamples; - myInitVPSizeX = theSizeX; - myInitVPSizeY = theSizeY; + myInitVPSizeX = theSize.x(); + myInitVPSizeY = theSize.y(); if (theGlContext->arbFBO == NULL) { return Standard_False; @@ -350,10 +368,10 @@ Standard_Boolean OpenGl_FrameBuffer::Init (const Handle(OpenGl_Context)& theGlCo myIsOwnDepth = true; // setup viewport sizes as is - myVPSizeX = theSizeX; - myVPSizeY = theSizeY; - const Standard_Integer aSizeX = theSizeX > 0 ? theSizeX : 2; - const Standard_Integer aSizeY = theSizeY > 0 ? theSizeY : 2; + myVPSizeX = theSize.x(); + myVPSizeY = theSize.y(); + const Standard_Integer aSizeX = theSize.x() > 0 ? theSize.x() : 2; + const Standard_Integer aSizeY = theSize.y() > 0 ? theSize.y() : 2; bool hasStencilRB = false; // Create the textures (will be used as color buffer and depth-stencil buffer) @@ -475,17 +493,14 @@ Standard_Boolean OpenGl_FrameBuffer::Init (const Handle(OpenGl_Context)& theGlCo // purpose : // ======================================================================= Standard_Boolean OpenGl_FrameBuffer::InitLazy (const Handle(OpenGl_Context)& theGlContext, - const GLsizei theViewportSizeX, - const GLsizei theViewportSizeY, - const GLint theColorFormat, - const GLint theDepthFormat, - const GLsizei theNbSamples) + const Graphic3d_Vec2i& theViewportSize, + const Standard_Integer theColorFormat, + const Standard_Integer theDepthFormat, + const Standard_Integer theNbSamples) { OpenGl_ColorFormats aColorFormats; - aColorFormats.Append (theColorFormat); - - return InitLazy (theGlContext, theViewportSizeX, theViewportSizeY, aColorFormats, theDepthFormat, theNbSamples); + return InitLazy (theGlContext, theViewportSize, aColorFormats, theDepthFormat, theNbSamples); } // ======================================================================= @@ -493,14 +508,13 @@ Standard_Boolean OpenGl_FrameBuffer::InitLazy (const Handle(OpenGl_Context)& the // purpose : // ======================================================================= Standard_Boolean OpenGl_FrameBuffer::InitLazy (const Handle(OpenGl_Context)& theGlContext, - const GLsizei theViewportSizeX, - const GLsizei theViewportSizeY, + const Graphic3d_Vec2i& theViewportSize, const OpenGl_ColorFormats& theColorFormats, - const GLint theDepthFormat, - const GLsizei theNbSamples) + const Standard_Integer theDepthFormat, + const Standard_Integer theNbSamples) { - if (myVPSizeX == theViewportSizeX - && myVPSizeY == theViewportSizeY + if (myVPSizeX == theViewportSize.x() + && myVPSizeY == theViewportSize.y() && myColorFormats == theColorFormats && myDepthFormat == theDepthFormat && myNbSamples == theNbSamples) @@ -508,7 +522,7 @@ Standard_Boolean OpenGl_FrameBuffer::InitLazy (const Handle(OpenGl_Context)& the return IsValid(); } - return Init (theGlContext, theViewportSizeX, theViewportSizeY, theColorFormats, theDepthFormat, theNbSamples); + return Init (theGlContext, theViewportSize, theColorFormats, theDepthFormat, theNbSamples); } // ======================================================================= @@ -516,11 +530,10 @@ Standard_Boolean OpenGl_FrameBuffer::InitLazy (const Handle(OpenGl_Context)& the // purpose : // ======================================================================= Standard_Boolean OpenGl_FrameBuffer::InitWithRB (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theSizeX, - const GLsizei theSizeY, - const GLint theColorFormat, - const GLint theDepthFormat, - const GLuint theColorRBufferFromWindow) + const Graphic3d_Vec2i& theSize, + const Standard_Integer theColorFormat, + const Standard_Integer theDepthFormat, + const unsigned int theColorRBufferFromWindow) { myColorFormats.Clear(); myColorFormats.Append (theColorFormat); @@ -537,8 +550,8 @@ Standard_Boolean OpenGl_FrameBuffer::InitWithRB (const Handle(OpenGl_Context)& t myDepthFormat = theDepthFormat; myNbSamples = 0; - myInitVPSizeX = theSizeX; - myInitVPSizeY = theSizeY; + myInitVPSizeX = theSize.x(); + myInitVPSizeY = theSize.y(); if (theGlCtx->arbFBO == NULL) { return Standard_False; @@ -552,10 +565,10 @@ Standard_Boolean OpenGl_FrameBuffer::InitWithRB (const Handle(OpenGl_Context)& t myIsOwnDepth = true; // setup viewport sizes as is - myVPSizeX = theSizeX; - myVPSizeY = theSizeY; - const Standard_Integer aSizeX = theSizeX > 0 ? theSizeX : 2; - const Standard_Integer aSizeY = theSizeY > 0 ? theSizeY : 2; + myVPSizeX = theSize.x(); + myVPSizeY = theSize.y(); + const Standard_Integer aSizeX = theSize.x() > 0 ? theSize.x() : 2; + const Standard_Integer aSizeY = theSize.y() > 0 ? theSize.y() : 2; // Create the render-buffers if (theColorRBufferFromWindow != NO_RENDERBUFFER) @@ -754,8 +767,8 @@ void OpenGl_FrameBuffer::SetupViewport (const Handle(OpenGl_Context)& theGlCtx) // function : ChangeViewport // purpose : // ======================================================================= -void OpenGl_FrameBuffer::ChangeViewport (const GLsizei theVPSizeX, - const GLsizei theVPSizeY) +void OpenGl_FrameBuffer::ChangeViewport (const Standard_Integer theVPSizeX, + const Standard_Integer theVPSizeY) { myVPSizeX = theVPSizeX; myVPSizeY = theVPSizeY; diff --git a/src/OpenGl/OpenGl_FrameBuffer.hxx b/src/OpenGl/OpenGl_FrameBuffer.hxx index bc204e1732..6e8a813ba5 100644 --- a/src/OpenGl/OpenGl_FrameBuffer.hxx +++ b/src/OpenGl/OpenGl_FrameBuffer.hxx @@ -15,18 +15,20 @@ #ifndef OPENGL_FRAME_BUFFER_H #define OPENGL_FRAME_BUFFER_H -#include #include -#include #include +#include #include +#include + +class Image_PixMap; +class OpenGl_Texture; -class OpenGl_FrameBuffer; DEFINE_STANDARD_HANDLE(OpenGl_FrameBuffer, OpenGl_Resource) //! Short declaration of useful collection types. -typedef NCollection_Vector OpenGl_ColorFormats; +typedef NCollection_Vector OpenGl_ColorFormats; //! Class implements FrameBuffer Object (FBO) resource //! intended for off-screen rendering. @@ -36,8 +38,8 @@ class OpenGl_FrameBuffer : public OpenGl_Resource public: //! Helpful constants - static const GLuint NO_FRAMEBUFFER = 0; - static const GLuint NO_RENDERBUFFER = 0; + static const unsigned int NO_FRAMEBUFFER = 0; + static const unsigned int NO_RENDERBUFFER = 0; public: @@ -64,64 +66,43 @@ public: Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE; //! Number of multisampling samples. - GLsizei NbSamples() const - { - return myNbSamples; - } + Standard_Integer NbSamples() const { return myNbSamples; } //! Number of color buffers. - GLsizei NbColorBuffers() const - { - return myColorTextures.Length(); - } + Standard_Integer NbColorBuffers() const { return myColorTextures.Length(); } //! Return true if FBO has been created with color attachment. - bool HasColor() const - { - return !myColorFormats.IsEmpty(); - } + bool HasColor() const { return !myColorFormats.IsEmpty(); } //! Return true if FBO has been created with depth attachment. - bool HasDepth() const - { - return myDepthFormat != 0; - } + bool HasDepth() const { return myDepthFormat != 0; } + + //! Return textures width x height. + Graphic3d_Vec2i GetSize() const { return Graphic3d_Vec2i (GetSizeX(), GetSizeY()); } //! Textures width. - GLsizei GetSizeX() const - { - return myColorTextures (0)->SizeX(); - } + Standard_EXPORT Standard_Integer GetSizeX() const; //! Textures height. - GLsizei GetSizeY() const - { - return myColorTextures (0)->SizeY(); - } + Standard_EXPORT Standard_Integer GetSizeY() const; + + //! Return viewport width x height. + Graphic3d_Vec2i GetVPSize() const { return Graphic3d_Vec2i (myVPSizeX, myVPSizeY); } //! Viewport width. - GLsizei GetVPSizeX() const - { - return myVPSizeX; - } + Standard_Integer GetVPSizeX() const { return myVPSizeX; } //! Viewport height. - GLsizei GetVPSizeY() const - { - return myVPSizeY; - } + Standard_Integer GetVPSizeY() const { return myVPSizeY; } + + //! Return viewport width x height. + Graphic3d_Vec2i GetInitVPSize() const { return Graphic3d_Vec2i (myInitVPSizeX, myInitVPSizeY); } //! Viewport width. - GLsizei GetInitVPSizeX() const - { - return myInitVPSizeX; - } + Standard_Integer GetInitVPSizeX() const { return myInitVPSizeX; } //! Viewport height. - GLsizei GetInitVPSizeY() const - { - return myInitVPSizeY; - } + Standard_Integer GetInitVPSizeY() const { return myInitVPSizeY; } //! Returns true if current object was initialized Standard_Boolean IsValid() const @@ -131,86 +112,76 @@ public: //! Initialize FBO for rendering into single/multiple color buffer and depth textures. //! @param theGlCtx currently bound OpenGL context - //! @param theSizeX texture width - //! @param theSizeY texture height + //! @param theSize texture width x height //! @param theColorFormats list of color texture sized format (0 means no color attachment), e.g. GL_RGBA8 //! @param theDepthStencilTexture depth-stencil texture //! @param theNbSamples MSAA number of samples (0 means normal texture) //! @return true on success Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theSizeX, - const GLsizei theSizeY, + const Graphic3d_Vec2i& theSize, const OpenGl_ColorFormats& theColorFormats, const Handle(OpenGl_Texture)& theDepthStencilTexture, - const GLsizei theNbSamples = 0); + const Standard_Integer theNbSamples = 0); //! Initialize FBO for rendering into textures. //! @param theGlCtx currently bound OpenGL context - //! @param theSizeX texture width - //! @param theSizeY texture height + //! @param theSize texture width x height //! @param theColorFormat color texture sized format (0 means no color attachment), e.g. GL_RGBA8 //! @param theDepthFormat depth-stencil texture sized format (0 means no depth attachment), e.g. GL_DEPTH24_STENCIL8 //! @param theNbSamples MSAA number of samples (0 means normal texture) //! @return true on success Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theSizeX, - const GLsizei theSizeY, - const GLint theColorFormat, - const GLint theDepthFormat, - const GLsizei theNbSamples = 0); + const Graphic3d_Vec2i& theSize, + const Standard_Integer theColorFormat, + const Standard_Integer theDepthFormat, + const Standard_Integer theNbSamples = 0); //! Initialize FBO for rendering into single/multiple color buffer and depth textures. //! @param theGlCtx currently bound OpenGL context - //! @param theSizeX texture width - //! @param theSizeY texture height + //! @param theSize texture width x height //! @param theColorFormats list of color texture sized format (0 means no color attachment), e.g. GL_RGBA8 //! @param theDepthFormat depth-stencil texture sized format (0 means no depth attachment), e.g. GL_DEPTH24_STENCIL8 //! @param theNbSamples MSAA number of samples (0 means normal texture) //! @return true on success Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theSizeX, - const GLsizei theSizeY, + const Graphic3d_Vec2i& theSize, const OpenGl_ColorFormats& theColorFormats, - const GLint theDepthFormat, - const GLsizei theNbSamples = 0); + const Standard_Integer theDepthFormat, + const Standard_Integer theNbSamples = 0); //! (Re-)initialize FBO with specified dimensions. Standard_EXPORT Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theViewportSizeX, - const GLsizei theViewportSizeY, - const GLint theColorFormat, - const GLint theDepthFormat, - const GLsizei theNbSamples = 0); + const Graphic3d_Vec2i& theViewportSize, + const Standard_Integer theColorFormat, + const Standard_Integer theDepthFormat, + const Standard_Integer theNbSamples = 0); //! (Re-)initialize FBO with specified dimensions. Standard_EXPORT Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theViewportSizeX, - const GLsizei theViewportSizeY, + const Graphic3d_Vec2i& theViewportSize, const OpenGl_ColorFormats& theColorFormats, - const GLint theDepthFormat, - const GLsizei theNbSamples = 0); + const Standard_Integer theDepthFormat, + const Standard_Integer theNbSamples = 0); //! (Re-)initialize FBO with properties taken from another FBO. Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx, const OpenGl_FrameBuffer& theFbo) { - return InitLazy (theGlCtx, theFbo.myVPSizeX, theFbo.myVPSizeY, theFbo.myColorFormats, theFbo.myDepthFormat, theFbo.myNbSamples); + return InitLazy (theGlCtx, Graphic3d_Vec2i (theFbo.myVPSizeX, theFbo.myVPSizeY), theFbo.myColorFormats, theFbo.myDepthFormat, theFbo.myNbSamples); } //! (Re-)initialize FBO with specified dimensions. //! The Render Buffer Objects will be used for Color, Depth and Stencil attachments (as opposite to textures). //! @param theGlCtx currently bound OpenGL context - //! @param theSizeX render buffer width - //! @param theSizeY render buffer height + //! @param theSize render buffer width x height //! @param theColorFormat color render buffer sized format, e.g. GL_RGBA8 //! @param theDepthFormat depth-stencil render buffer sized format, e.g. GL_DEPTH24_STENCIL8 //! @param theColorRBufferFromWindow when specified - should be ID of already initialized RB object, which will be released within this class Standard_EXPORT Standard_Boolean InitWithRB (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theSizeX, - const GLsizei theSizeY, - const GLint theColorFormat, - const GLint theDepthFormat, - const GLuint theColorRBufferFromWindow = 0); + const Graphic3d_Vec2i& theSize, + const Standard_Integer theColorFormat, + const Standard_Integer theDepthFormat, + const unsigned int theColorRBufferFromWindow = 0); //! Initialize class from currently bound FBO. //! Retrieved OpenGL objects will not be destroyed on Release. @@ -225,8 +196,8 @@ public: Standard_EXPORT void SetupViewport (const Handle(OpenGl_Context)& theGlCtx); //! Override viewport settings - Standard_EXPORT void ChangeViewport (const GLsizei theVPSizeX, - const GLsizei theVPSizeY); + Standard_EXPORT void ChangeViewport (const Standard_Integer theVPSizeX, + const Standard_Integer theVPSizeY); //! Bind frame buffer for drawing and reading (to render into the texture). Standard_EXPORT virtual void BindBuffer (const Handle(OpenGl_Context)& theGlCtx); @@ -241,32 +212,98 @@ public: Standard_EXPORT virtual void UnbindBuffer (const Handle(OpenGl_Context)& theGlCtx); //! Returns the color texture for the given color buffer index. - const Handle(OpenGl_Texture)& ColorTexture (const GLint theColorBufferIndex = 0) const + const Handle(OpenGl_Texture)& ColorTexture (const Standard_Integer theColorBufferIndex = 0) const { - return myColorTextures (theColorBufferIndex); + return myColorTextures.Value (theColorBufferIndex); } //! Returns the depth-stencil texture. - const Handle(OpenGl_Texture)& DepthStencilTexture() const - { - return myDepthStencilTexture; - } + const Handle(OpenGl_Texture)& DepthStencilTexture() const { return myDepthStencilTexture; } //! Returns the color Render Buffer. - GLuint ColorRenderBuffer() const - { - return myGlColorRBufferId; - } + unsigned int ColorRenderBuffer() const { return myGlColorRBufferId; } //! Returns the depth Render Buffer. - GLuint DepthStencilRenderBuffer() const - { - return myGlDepthRBufferId; - } + unsigned int DepthStencilRenderBuffer() const { return myGlDepthRBufferId; } //! Returns estimated GPU memory usage for holding data without considering overheads and allocation alignment rules. Standard_EXPORT virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE; +public: + + //! Initialize FBO for rendering into single/multiple color buffer and depth textures. + Standard_DEPRECATED("Obsolete method, use Init() taking Graphic3d_Vec2i") + bool Init (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theSizeX, + const Standard_Integer theSizeY, + const OpenGl_ColorFormats& theColorFormats, + const Handle(OpenGl_Texture)& theDepthStencilTexture, + const Standard_Integer theNbSamples = 0) + { + return Init (theGlCtx, Graphic3d_Vec2i (theSizeX, theSizeY), theColorFormats, theDepthStencilTexture, theNbSamples); + } + + //! Initialize FBO for rendering into textures. + Standard_DEPRECATED("Obsolete method, use Init() taking Graphic3d_Vec2i") + bool Init (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theSizeX, + const Standard_Integer theSizeY, + const Standard_Integer theColorFormat, + const Standard_Integer theDepthFormat, + const Standard_Integer theNbSamples = 0) + { + return Init (theGlCtx, Graphic3d_Vec2i (theSizeX, theSizeY), theColorFormat, theDepthFormat, theNbSamples); + } + + //! Initialize FBO for rendering into single/multiple color buffer and depth textures. + Standard_DEPRECATED("Obsolete method, use Init() taking Graphic3d_Vec2i") + bool Init (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theSizeX, + const Standard_Integer theSizeY, + const OpenGl_ColorFormats& theColorFormats, + const Standard_Integer theDepthFormat, + const Standard_Integer theNbSamples = 0) + { + return Init (theGlCtx, Graphic3d_Vec2i (theSizeX, theSizeY), theColorFormats, theDepthFormat, theNbSamples); + } + + //! (Re-)initialize FBO with specified dimensions. + Standard_DEPRECATED("Obsolete method, use InitLazy() taking Graphic3d_Vec2i") + bool InitLazy (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theViewportSizeX, + const Standard_Integer theViewportSizeY, + const Standard_Integer theColorFormat, + const Standard_Integer theDepthFormat, + const Standard_Integer theNbSamples = 0) + { + return InitLazy (theGlCtx, Graphic3d_Vec2i (theViewportSizeX, theViewportSizeY), theColorFormat, theDepthFormat, theNbSamples); + } + + //! (Re-)initialize FBO with specified dimensions. + Standard_DEPRECATED("Obsolete method, use InitLazy() taking Graphic3d_Vec2i") + bool InitLazy (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theViewportSizeX, + const Standard_Integer theViewportSizeY, + const OpenGl_ColorFormats& theColorFormats, + const Standard_Integer theDepthFormat, + const Standard_Integer theNbSamples = 0) + { + return InitLazy (theGlCtx, Graphic3d_Vec2i (theViewportSizeX, theViewportSizeY), theColorFormats, theDepthFormat, theNbSamples); + } + + //! (Re-)initialize FBO with specified dimensions. + //! The Render Buffer Objects will be used for Color, Depth and Stencil attachments (as opposite to textures). + Standard_DEPRECATED("Obsolete method, use InitWithRB() taking Graphic3d_Vec2i") + bool InitWithRB (const Handle(OpenGl_Context)& theGlCtx, + const Standard_Integer theSizeX, + const Standard_Integer theSizeY, + const Standard_Integer theColorFormat, + const Standard_Integer theDepthFormat, + const unsigned int theColorRBufferFromWindow = 0) + { + return InitWithRB (theGlCtx, Graphic3d_Vec2i (theSizeX, theSizeY), theColorFormat, theDepthFormat, theColorRBufferFromWindow); + } + protected: Standard_Boolean isValidFrameBuffer() const @@ -280,16 +317,16 @@ protected: protected: - GLsizei myInitVPSizeX; //!< viewport width specified during initialization (kept even on failure) - GLsizei myInitVPSizeY; //!< viewport height specified during initialization (kept even on failure) - GLsizei myVPSizeX; //!< viewport width (should be <= texture width) - GLsizei myVPSizeY; //!< viewport height (should be <= texture height) - GLsizei myNbSamples; //!< number of MSAA samples + Standard_Integer myInitVPSizeX; //!< viewport width specified during initialization (kept even on failure) + Standard_Integer myInitVPSizeY; //!< viewport height specified during initialization (kept even on failure) + Standard_Integer myVPSizeX; //!< viewport width (should be <= texture width) + Standard_Integer myVPSizeY; //!< viewport height (should be <= texture height) + Standard_Integer myNbSamples; //!< number of MSAA samples OpenGl_ColorFormats myColorFormats; //!< sized format for color texture, GL_RGBA8 by default - GLint myDepthFormat; //!< sized format for depth-stencil texture, GL_DEPTH24_STENCIL8 by default - GLuint myGlFBufferId; //!< FBO object ID - GLuint myGlColorRBufferId; //!< color Render Buffer object (alternative to myColorTexture) - GLuint myGlDepthRBufferId; //!< depth-stencil Render Buffer object (alternative to myDepthStencilTexture) + Standard_Integer myDepthFormat; //!< sized format for depth-stencil texture, GL_DEPTH24_STENCIL8 by default + unsigned int myGlFBufferId; //!< FBO object ID + unsigned int myGlColorRBufferId; //!< color Render Buffer object (alternative to myColorTexture) + unsigned int myGlDepthRBufferId; //!< depth-stencil Render Buffer object (alternative to myDepthStencilTexture) bool myIsOwnBuffer; //!< flag indicating that FBO should be deallocated by this class bool myIsOwnColor; //!< flag indicating that color textures should be deallocated by this class bool myIsOwnDepth; //!< flag indicating that depth texture should be deallocated by this class diff --git a/src/OpenGl/OpenGl_FrameStats.cxx b/src/OpenGl/OpenGl_FrameStats.cxx index 568c3c06b4..b0a96f2afd 100644 --- a/src/OpenGl/OpenGl_FrameStats.cxx +++ b/src/OpenGl/OpenGl_FrameStats.cxx @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include IMPLEMENT_STANDARD_RTTIEXT(OpenGl_FrameStats, Graphic3d_FrameStats) diff --git a/src/OpenGl/OpenGl_FrameStatsPrs.cxx b/src/OpenGl/OpenGl_FrameStatsPrs.cxx index 664317b87f..deafb1c0b4 100644 --- a/src/OpenGl/OpenGl_FrameStatsPrs.cxx +++ b/src/OpenGl/OpenGl_FrameStatsPrs.cxx @@ -14,6 +14,8 @@ #include #include +#include +#include #include #include diff --git a/src/OpenGl/OpenGl_GraphicDriver.cxx b/src/OpenGl/OpenGl_GraphicDriver.cxx index a23c5826c0..b98795d318 100644 --- a/src/OpenGl/OpenGl_GraphicDriver.cxx +++ b/src/OpenGl/OpenGl_GraphicDriver.cxx @@ -25,6 +25,7 @@ #include #include #include +#include #include #include diff --git a/src/OpenGl/OpenGl_IndexBuffer.cxx b/src/OpenGl/OpenGl_IndexBuffer.cxx index 1fa4af997d..0dcf7e7d9d 100644 --- a/src/OpenGl/OpenGl_IndexBuffer.cxx +++ b/src/OpenGl/OpenGl_IndexBuffer.cxx @@ -17,21 +17,20 @@ #include #include - -IMPLEMENT_STANDARD_RTTIEXT(OpenGl_IndexBuffer,OpenGl_VertexBuffer) +IMPLEMENT_STANDARD_RTTIEXT(OpenGl_IndexBuffer, OpenGl_Buffer) // ======================================================================= // function : OpenGl_IndexBuffer // purpose : // ======================================================================= OpenGl_IndexBuffer::OpenGl_IndexBuffer() -: OpenGl_VertexBuffer() {} +: OpenGl_Buffer() {} // ======================================================================= // function : GetTarget // purpose : // ======================================================================= -GLenum OpenGl_IndexBuffer::GetTarget() const +unsigned int OpenGl_IndexBuffer::GetTarget() const { return GL_ELEMENT_ARRAY_BUFFER; } @@ -43,5 +42,5 @@ GLenum OpenGl_IndexBuffer::GetTarget() const void OpenGl_IndexBuffer::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const { OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream) - OCCT_DUMP_BASE_CLASS (theOStream, theDepth, OpenGl_VertexBuffer) + OCCT_DUMP_BASE_CLASS (theOStream, theDepth, OpenGl_Buffer) } diff --git a/src/OpenGl/OpenGl_IndexBuffer.hxx b/src/OpenGl/OpenGl_IndexBuffer.hxx index 8747047426..15882a70cd 100644 --- a/src/OpenGl/OpenGl_IndexBuffer.hxx +++ b/src/OpenGl/OpenGl_IndexBuffer.hxx @@ -15,25 +15,28 @@ #ifndef _OpenGl_IndexBuffer_H__ #define _OpenGl_IndexBuffer_H__ -#include +#include //! Index buffer is just a VBO with special target (GL_ELEMENT_ARRAY_BUFFER). -class OpenGl_IndexBuffer : public OpenGl_VertexBuffer +class OpenGl_IndexBuffer : public OpenGl_Buffer { public: + //! Empty constructor. Standard_EXPORT OpenGl_IndexBuffer(); - Standard_EXPORT virtual GLenum GetTarget() const Standard_OVERRIDE; + + //! Return buffer object target (GL_ELEMENT_ARRAY_BUFFER). + Standard_EXPORT virtual unsigned int GetTarget() const Standard_OVERRIDE; //! Dumps the content of me into the stream Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const Standard_OVERRIDE; public: - DEFINE_STANDARD_RTTIEXT(OpenGl_IndexBuffer,OpenGl_VertexBuffer) // Type definition + DEFINE_STANDARD_RTTIEXT(OpenGl_IndexBuffer, OpenGl_Buffer) }; -DEFINE_STANDARD_HANDLE(OpenGl_IndexBuffer, OpenGl_VertexBuffer) +DEFINE_STANDARD_HANDLE(OpenGl_IndexBuffer, OpenGl_Buffer) #endif // _OpenGl_IndexBuffer_H__ diff --git a/src/OpenGl/OpenGl_PBREnvironment.cxx b/src/OpenGl/OpenGl_PBREnvironment.cxx index 65719aff4c..b6e83efd9c 100644 --- a/src/OpenGl/OpenGl_PBREnvironment.cxx +++ b/src/OpenGl/OpenGl_PBREnvironment.cxx @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include diff --git a/src/OpenGl/OpenGl_PrimitiveArray.cxx b/src/OpenGl/OpenGl_PrimitiveArray.cxx index 0f78f4eb94..6a75ca8808 100644 --- a/src/OpenGl/OpenGl_PrimitiveArray.cxx +++ b/src/OpenGl/OpenGl_PrimitiveArray.cxx @@ -331,7 +331,7 @@ Standard_Boolean OpenGl_PrimitiveArray::buildVBO (const Handle(OpenGl_Context)& aVboAttribs->initLink (myAttribs, 0, myAttribs->NbElements, GL_NONE); if (!myIndices.IsNull()) { - Handle(OpenGl_VertexBufferCompat) aVboIndices = new OpenGl_VertexBufferCompat(); + Handle(OpenGl_IndexBufferCompat) aVboIndices = new OpenGl_IndexBufferCompat(); switch (myIndices->Stride) { case 2: diff --git a/src/OpenGl/OpenGl_PrimitiveArray.hxx b/src/OpenGl/OpenGl_PrimitiveArray.hxx index 7f98265604..8cdd676ad4 100644 --- a/src/OpenGl/OpenGl_PrimitiveArray.hxx +++ b/src/OpenGl/OpenGl_PrimitiveArray.hxx @@ -16,8 +16,6 @@ #ifndef OpenGl_PrimitiveArray_Header #define OpenGl_PrimitiveArray_Header -#include - #include #include #include @@ -26,6 +24,8 @@ #include +class OpenGl_IndexBuffer; +class OpenGl_VertexBuffer; class OpenGl_GraphicDriver; //! Class for rendering of arbitrary primitive array. @@ -74,7 +74,7 @@ public: void Invalidate() const { myIsVboInit = Standard_False; } //! @return primitive type (GL_LINES, GL_TRIANGLES and others) - GLint DrawMode() const { return myDrawMode; } + Standard_Integer DrawMode() const { return myDrawMode; } //! Return TRUE if primitive type generates shaded triangulation. virtual Standard_Boolean IsFillDrawMode() const Standard_OVERRIDE { return myIsFillType; } @@ -101,7 +101,7 @@ public: public: //! Returns index VBO. - const Handle(OpenGl_VertexBuffer)& IndexVbo() const { return myVboIndices; } + const Handle(OpenGl_IndexBuffer)& IndexVbo() const { return myVboIndices; } //! Returns attributes VBO. const Handle(OpenGl_VertexBuffer)& AttributesVbo() const { return myVboAttribs; } @@ -148,13 +148,13 @@ private: protected: - mutable Handle(OpenGl_VertexBuffer) myVboIndices; + mutable Handle(OpenGl_IndexBuffer) myVboIndices; mutable Handle(OpenGl_VertexBuffer) myVboAttribs; mutable Handle(Graphic3d_IndexBuffer) myIndices; mutable Handle(Graphic3d_Buffer) myAttribs; mutable Handle(Graphic3d_BoundBuffer) myBounds; - GLshort myDrawMode; + short myDrawMode; mutable Standard_Boolean myIsFillType; mutable Standard_Boolean myIsVboInit; diff --git a/src/OpenGl/OpenGl_Sampler.cxx b/src/OpenGl/OpenGl_Sampler.cxx index ce4a4ef240..a793dda89d 100644 --- a/src/OpenGl/OpenGl_Sampler.cxx +++ b/src/OpenGl/OpenGl_Sampler.cxx @@ -16,6 +16,7 @@ #include #include +#include #include #include @@ -151,10 +152,10 @@ void OpenGl_Sampler::Unbind (const Handle(OpenGl_Context)& theCtx, // purpose : // ======================================================================= void OpenGl_Sampler::setParameter (const Handle(OpenGl_Context)& theCtx, - OpenGl_Sampler* theSampler, - GLenum theTarget, - GLenum theParam, - GLint theValue) + OpenGl_Sampler* theSampler, + unsigned int theTarget, + unsigned int theParam, + Standard_Integer theValue) { if (theSampler != NULL && theSampler->isValidSampler()) { @@ -186,7 +187,7 @@ void OpenGl_Sampler::SetParameters (const Handle(Graphic3d_TextureParams)& thePa void OpenGl_Sampler::applySamplerParams (const Handle(OpenGl_Context)& theCtx, const Handle(Graphic3d_TextureParams)& theParams, OpenGl_Sampler* theSampler, - const GLenum theTarget, + const unsigned int theTarget, const Standard_Integer theMaxMipLevels) { if (theSampler != NULL && theSampler->Parameters() == theParams) diff --git a/src/OpenGl/OpenGl_Sampler.hxx b/src/OpenGl/OpenGl_Sampler.hxx index 8a2b91e08e..bca6e4416e 100644 --- a/src/OpenGl/OpenGl_Sampler.hxx +++ b/src/OpenGl/OpenGl_Sampler.hxx @@ -16,7 +16,7 @@ #ifndef _OpenGl_Sampler_Header #define _OpenGl_Sampler_Header -#include +#include #include class OpenGl_Texture; @@ -31,7 +31,7 @@ class OpenGl_Sampler : public OpenGl_Resource public: //! Helpful constant defining invalid sampler identifier - static const GLuint NO_SAMPLER = 0; + static const unsigned int NO_SAMPLER = 0; public: @@ -84,15 +84,15 @@ public: //! Sets specific sampler parameter. void SetParameter (const Handle(OpenGl_Context)& theCtx, - GLenum theTarget, - GLenum theParam, - GLint theValue) + unsigned int theTarget, + unsigned int theParam, + Standard_Integer theValue) { setParameter (theCtx, this, theTarget, theParam, theValue); } //! Returns OpenGL sampler ID. - GLuint SamplerID() const + unsigned int SamplerID() const { return mySamplerID; } @@ -124,9 +124,9 @@ protected: //! Sets specific sampler parameter. Standard_EXPORT static void setParameter (const Handle(OpenGl_Context)& theContext, OpenGl_Sampler* theSampler, - GLenum theTarget, - GLenum theParam, - GLint theValue); + unsigned int theTarget, + unsigned int theParam, + Standard_Integer theValue); //! Apply sampler parameters. //! @param theCtx [in] active OpenGL context @@ -138,7 +138,7 @@ protected: Standard_EXPORT static void applySamplerParams (const Handle(OpenGl_Context)& theCtx, const Handle(Graphic3d_TextureParams)& theParams, OpenGl_Sampler* theSampler, - const GLenum theTarget, + const unsigned int theTarget, const Standard_Integer theMaxMipLevel); //! Apply global texture state for deprecated OpenGL functionality. @@ -155,7 +155,7 @@ protected: Handle(Graphic3d_TextureParams) myParams; //!< texture parameters unsigned int mySamplerRevision; //!< modification counter of parameters related to sampler state - GLuint mySamplerID; //!< OpenGL sampler object ID + unsigned int mySamplerID; //!< OpenGL sampler object ID bool myIsImmutable; //!< immutable flag preventing further modifications of sampler parameters, FALSE by default }; diff --git a/src/OpenGl/OpenGl_SceneGeometry.cxx b/src/OpenGl/OpenGl_SceneGeometry.cxx index 87ec8c5795..070f6ae8ae 100644 --- a/src/OpenGl/OpenGl_SceneGeometry.cxx +++ b/src/OpenGl/OpenGl_SceneGeometry.cxx @@ -13,16 +13,18 @@ // Alternatively, this file may be used under the terms of Open CASCADE // commercial license or contractual agreement. -#include +#include + +#include #include +#include #include #include #include -#include #include -#include +#include -//! Use this macro to output BVH profiling info +// Use this macro to output BVH profiling info // #define RAY_TRACE_PRINT_INFO namespace diff --git a/src/OpenGl/OpenGl_SceneGeometry.hxx b/src/OpenGl/OpenGl_SceneGeometry.hxx index e9ab551c4a..f5192be53a 100644 --- a/src/OpenGl/OpenGl_SceneGeometry.hxx +++ b/src/OpenGl/OpenGl_SceneGeometry.hxx @@ -20,7 +20,6 @@ #include #include #include -#include #include #include diff --git a/src/OpenGl/OpenGl_ShaderManager.hxx b/src/OpenGl/OpenGl_ShaderManager.hxx index b1653f9f55..bb4b5717a0 100644 --- a/src/OpenGl/OpenGl_ShaderManager.hxx +++ b/src/OpenGl/OpenGl_ShaderManager.hxx @@ -18,12 +18,15 @@ #include #include +#include +#include +#include #include #include +#include #include -#include -#include #include +#include class OpenGl_View; class OpenGl_VertexBuffer; diff --git a/src/OpenGl/OpenGl_Texture.cxx b/src/OpenGl/OpenGl_Texture.cxx index 68e44560bc..16a4f20f48 100644 --- a/src/OpenGl/OpenGl_Texture.cxx +++ b/src/OpenGl/OpenGl_Texture.cxx @@ -707,10 +707,10 @@ bool OpenGl_Texture::InitCompressed (const Handle(OpenGl_Context)& theCtx, // purpose : // ======================================================================= bool OpenGl_Texture::Init2DMultisample (const Handle(OpenGl_Context)& theCtx, - const GLsizei theNbSamples, - const GLint theTextFormat, - const GLsizei theSizeX, - const GLsizei theSizeY) + const Standard_Integer theNbSamples, + const Standard_Integer theTextFormat, + const Standard_Integer theSizeX, + const Standard_Integer theSizeY) { if (!Create (theCtx) || theNbSamples > theCtx->MaxMsaaSamples() diff --git a/src/OpenGl/OpenGl_Texture.hxx b/src/OpenGl/OpenGl_Texture.hxx index 03f57d959e..468ce13581 100644 --- a/src/OpenGl/OpenGl_Texture.hxx +++ b/src/OpenGl/OpenGl_Texture.hxx @@ -16,6 +16,7 @@ #define _OpenGl_Texture_H__ #include +#include #include #include #include @@ -32,7 +33,7 @@ class OpenGl_Texture : public OpenGl_NamedResource public: //! Helpful constants - static const GLuint NO_TEXTURE = 0; + static const unsigned int NO_TEXTURE = 0; //! Return pixel size of pixel format in bytes. //! Note that this method considers that OpenGL natively supports this pixel format, @@ -52,7 +53,7 @@ public: virtual bool IsValid() const { return myTextureId != NO_TEXTURE; } //! @return target to which the texture is bound (GL_TEXTURE_1D, GL_TEXTURE_2D) - GLenum GetTarget() const { return myTarget; } + unsigned int GetTarget() const { return myTarget; } //! @return texture width (0 LOD) GLsizei SizeX() const { return mySizeX; } @@ -61,13 +62,13 @@ public: GLsizei SizeY() const { return mySizeY; } //! @return texture ID - GLuint TextureId() const { return myTextureId; } + unsigned int TextureId() const { return myTextureId; } //! @return texture format (not sized) - GLenum GetFormat() const { return myTextFormat; } + unsigned int GetFormat() const { return myTextFormat; } //! @return texture format (sized) - GLint SizedFormat() const { return mySizedFormat; } + Standard_Integer SizedFormat() const { return mySizedFormat; } //! Return true for GL_RED and GL_ALPHA formats. bool IsAlpha() const { return myIsAlpha; } @@ -158,10 +159,10 @@ public: //! Initialize the 2D multisampling texture using glTexImage2DMultisample(). Standard_EXPORT bool Init2DMultisample (const Handle(OpenGl_Context)& theCtx, - const GLsizei theNbSamples, - const GLint theTextFormat, - const GLsizei theSizeX, - const GLsizei theSizeY); + const Standard_Integer theNbSamples, + const Standard_Integer theTextFormat, + const Standard_Integer theSizeX, + const Standard_Integer theSizeY); //! Allocates texture rectangle with specified format and size. //! \note Texture data is not initialized (will contain trash). @@ -208,10 +209,10 @@ public: Standard_DEPRECATED("Deprecated method, OpenGl_TextureFormat::FindFormat() should be used instead") static bool GetDataFormat (const Handle(OpenGl_Context)& theCtx, - const Image_Format theFormat, - GLint& theTextFormat, - GLenum& thePixelFormat, - GLenum& theDataType) + const Image_Format theFormat, + Standard_Integer& theTextFormat, + unsigned int& thePixelFormat, + unsigned int& theDataType) { OpenGl_TextureFormat aFormat = OpenGl_TextureFormat::FindFormat (theCtx, theFormat, false); theTextFormat = aFormat.InternalFormat(); @@ -222,10 +223,10 @@ public: Standard_DEPRECATED("Deprecated method, OpenGl_TextureFormat::FindFormat() should be used instead") static bool GetDataFormat (const Handle(OpenGl_Context)& theCtx, - const Image_PixMap& theData, - GLint& theTextFormat, - GLenum& thePixelFormat, - GLenum& theDataType) + const Image_PixMap& theData, + Standard_Integer& theTextFormat, + unsigned int& thePixelFormat, + unsigned int& theDataType) { OpenGl_TextureFormat aFormat = OpenGl_TextureFormat::FindFormat (theCtx, theData.Format(), false); theTextFormat = aFormat.InternalFormat(); @@ -236,11 +237,11 @@ public: Standard_DEPRECATED("Deprecated method, OpenGl_TextureFormat should be passed instead of separate parameters") bool Init (const Handle(OpenGl_Context)& theCtx, - const GLint theTextFormat, - const GLenum thePixelFormat, - const GLenum theDataType, - const GLsizei theSizeX, - const GLsizei theSizeY, + const Standard_Integer theTextFormat, + const unsigned int thePixelFormat, + const unsigned int theDataType, + const Standard_Integer theSizeX, + const Standard_Integer theSizeY, const Graphic3d_TypeOfTexture theType, const Image_PixMap* theImage = NULL) { @@ -261,9 +262,9 @@ public: Standard_DEPRECATED("Deprecated method, OpenGl_TextureFormat should be passed instead of separate parameters") bool Init3D (const Handle(OpenGl_Context)& theCtx, - const GLint theTextFormat, - const GLenum thePixelFormat, - const GLenum theDataType, + const Standard_Integer theTextFormat, + const unsigned int thePixelFormat, + const unsigned int theDataType, const Standard_Integer theSizeX, const Standard_Integer theSizeY, const Standard_Integer theSizeZ, @@ -278,7 +279,7 @@ public: //! Initializes 6 sides of cubemap. //! If theCubeMap is not NULL then size and format will be taken from it and corresponding arguments will be ignored. - //! Otherwise this parametres will be taken from arguments. + //! Otherwise this parameters will be taken from arguments. //! @param theCtx [in] active OpenGL context //! @param theCubeMap [in] cubemap definition, can be NULL //! @param theSize [in] cubemap dimensions @@ -301,13 +302,13 @@ protected: Handle(OpenGl_Sampler) mySampler; //!< texture sampler Standard_Size myRevision; //!< revision of associated data source - GLuint myTextureId; //!< GL resource ID - GLenum myTarget; //!< GL_TEXTURE_1D/GL_TEXTURE_2D/GL_TEXTURE_3D - GLsizei mySizeX; //!< texture width - GLsizei mySizeY; //!< texture height - GLsizei mySizeZ; //!< texture depth - GLenum myTextFormat; //!< texture format - GL_RGB, GL_RGBA,... - GLint mySizedFormat;//!< internal (sized) texture format + unsigned int myTextureId; //!< GL resource ID + unsigned int myTarget; //!< GL_TEXTURE_1D/GL_TEXTURE_2D/GL_TEXTURE_3D + Standard_Integer mySizeX; //!< texture width + Standard_Integer mySizeY; //!< texture height + Standard_Integer mySizeZ; //!< texture depth + unsigned int myTextFormat; //!< texture format - GL_RGB, GL_RGBA,... + Standard_Integer mySizedFormat;//!< internal (sized) texture format Standard_Integer myNbSamples; //!< number of MSAA samples Standard_Integer myMaxMipLevel;//!< upper mipmap level index (0 means no mipmaps) bool myIsAlpha; //!< indicates alpha format diff --git a/src/OpenGl/OpenGl_TextureBufferArb.cxx b/src/OpenGl/OpenGl_TextureBuffer.cxx similarity index 70% rename from src/OpenGl/OpenGl_TextureBufferArb.cxx rename to src/OpenGl/OpenGl_TextureBuffer.cxx index 38a14d1aef..1c23297467 100644 --- a/src/OpenGl/OpenGl_TextureBufferArb.cxx +++ b/src/OpenGl/OpenGl_TextureBuffer.cxx @@ -12,20 +12,21 @@ // Alternatively, this file may be used under the terms of Open CASCADE // commercial license or contractual agreement. -#include +#include +#include +#include #include #include - -IMPLEMENT_STANDARD_RTTIEXT(OpenGl_TextureBufferArb,OpenGl_VertexBuffer) +IMPLEMENT_STANDARD_RTTIEXT(OpenGl_TextureBuffer, OpenGl_Buffer) // ======================================================================= -// function : OpenGl_TextureBufferArb +// function : OpenGl_TextureBuffer // purpose : // ======================================================================= -OpenGl_TextureBufferArb::OpenGl_TextureBufferArb() -: OpenGl_VertexBuffer(), +OpenGl_TextureBuffer::OpenGl_TextureBuffer() +: OpenGl_Buffer(), myTextureId (NO_TEXTURE), myTexFormat (GL_RGBA32F) { @@ -33,10 +34,10 @@ OpenGl_TextureBufferArb::OpenGl_TextureBufferArb() } // ======================================================================= -// function : ~OpenGl_TextureBufferArb +// function : ~OpenGl_TextureBuffer // purpose : // ======================================================================= -OpenGl_TextureBufferArb::~OpenGl_TextureBufferArb() +OpenGl_TextureBuffer::~OpenGl_TextureBuffer() { Release (NULL); } @@ -45,7 +46,7 @@ OpenGl_TextureBufferArb::~OpenGl_TextureBufferArb() // function : GetTarget // purpose : // ======================================================================= -GLenum OpenGl_TextureBufferArb::GetTarget() const +unsigned int OpenGl_TextureBuffer::GetTarget() const { return GL_TEXTURE_BUFFER; // GL_TEXTURE_BUFFER for OpenGL 3.1+, OpenGL ES 3.2 } @@ -54,37 +55,37 @@ GLenum OpenGl_TextureBufferArb::GetTarget() const // function : Release // purpose : // ======================================================================= -void OpenGl_TextureBufferArb::Release (OpenGl_Context* theGlCtx) +void OpenGl_TextureBuffer::Release (OpenGl_Context* theGlCtx) { if (myTextureId != NO_TEXTURE) { // application can not handle this case by exception - this is bug in code Standard_ASSERT_RETURN (theGlCtx != NULL, - "OpenGl_TextureBufferExt destroyed without GL context! Possible GPU memory leakage...",); + "OpenGl_TextureBuffer destroyed without GL context! Possible GPU memory leakage...",); if (theGlCtx->IsValid()) { - glDeleteTextures (1, &myTextureId); + theGlCtx->core20fwd->glDeleteTextures (1, &myTextureId); } myTextureId = NO_TEXTURE; } - OpenGl_VertexBuffer::Release (theGlCtx); + base_type::Release (theGlCtx); } // ======================================================================= // function : Create // purpose : // ======================================================================= -bool OpenGl_TextureBufferArb::Create (const Handle(OpenGl_Context)& theGlCtx) +bool OpenGl_TextureBuffer::Create (const Handle(OpenGl_Context)& theGlCtx) { - if (!OpenGl_VertexBuffer::Create (theGlCtx)) + if (!base_type::Create (theGlCtx)) { return false; } if (myTextureId == NO_TEXTURE) { - glGenTextures (1, &myTextureId); + theGlCtx->core20fwd->glGenTextures (1, &myTextureId); } return myTextureId != NO_TEXTURE; } @@ -93,10 +94,10 @@ bool OpenGl_TextureBufferArb::Create (const Handle(OpenGl_Context)& theGlCtx) // function : Init // purpose : // ======================================================================= -bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx, - const GLuint theComponentsNb, - const GLsizei theElemsNb, - const GLfloat* theData) +bool OpenGl_TextureBuffer::Init (const Handle(OpenGl_Context)& theGlCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const float* theData) { if (theGlCtx->arbTBO == NULL) { @@ -114,7 +115,7 @@ bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx, return false; } else if (!Create (theGlCtx) - || !OpenGl_VertexBuffer::Init (theGlCtx, theComponentsNb, theElemsNb, theData)) + || !base_type::Init (theGlCtx, theComponentsNb, theElemsNb, theData)) { return false; } @@ -139,10 +140,10 @@ bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx, // function : Init // purpose : // ======================================================================= -bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx, - const GLuint theComponentsNb, - const GLsizei theElemsNb, - const GLuint* theData) +bool OpenGl_TextureBuffer::Init (const Handle(OpenGl_Context)& theGlCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const unsigned int* theData) { if (theGlCtx->arbTBO == NULL) { @@ -160,7 +161,7 @@ bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx, return false; } else if (!Create (theGlCtx) - || !OpenGl_VertexBuffer::Init (theGlCtx, theComponentsNb, theElemsNb, theData)) + || !base_type::Init (theGlCtx, theComponentsNb, theElemsNb, theData)) { return false; } @@ -185,10 +186,10 @@ bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx, // function : Init // purpose : // ======================================================================= -bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx, - const GLuint theComponentsNb, - const GLsizei theElemsNb, - const GLushort* theData) +bool OpenGl_TextureBuffer::Init (const Handle(OpenGl_Context)& theGlCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const unsigned short* theData) { if (theGlCtx->arbTBO == NULL) { @@ -201,7 +202,7 @@ bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx, return false; } else if (!Create (theGlCtx) - || !OpenGl_VertexBuffer::Init (theGlCtx, theComponentsNb, theElemsNb, theData)) + || !base_type::Init (theGlCtx, theComponentsNb, theElemsNb, theData)) { return false; } @@ -226,10 +227,10 @@ bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx, // function : Init // purpose : // ======================================================================= -bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx, - const GLuint theComponentsNb, - const GLsizei theElemsNb, - const GLubyte* theData) +bool OpenGl_TextureBuffer::Init (const Handle(OpenGl_Context)& theGlCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const Standard_Byte* theData) { if (theGlCtx->arbTBO == NULL) { @@ -242,7 +243,7 @@ bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx, return false; } else if (!Create (theGlCtx) - || !OpenGl_VertexBuffer::Init (theGlCtx, theComponentsNb, theElemsNb, theData)) + || !base_type::Init (theGlCtx, theComponentsNb, theElemsNb, theData)) { return false; } @@ -267,20 +268,20 @@ bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx, // function : BindTexture // purpose : // ======================================================================= -void OpenGl_TextureBufferArb::BindTexture (const Handle(OpenGl_Context)& theGlCtx, - const Graphic3d_TextureUnit theTextureUnit) const +void OpenGl_TextureBuffer::BindTexture (const Handle(OpenGl_Context)& theGlCtx, + const Graphic3d_TextureUnit theTextureUnit) const { theGlCtx->core20fwd->glActiveTexture (GL_TEXTURE0 + theTextureUnit); - glBindTexture (GetTarget(), myTextureId); + theGlCtx->core20fwd->glBindTexture (GetTarget(), myTextureId); } // ======================================================================= // function : UnbindTexture // purpose : // ======================================================================= -void OpenGl_TextureBufferArb::UnbindTexture (const Handle(OpenGl_Context)& theGlCtx, - const Graphic3d_TextureUnit theTextureUnit) const +void OpenGl_TextureBuffer::UnbindTexture (const Handle(OpenGl_Context)& theGlCtx, + const Graphic3d_TextureUnit theTextureUnit) const { theGlCtx->core20fwd->glActiveTexture (GL_TEXTURE0 + theTextureUnit); - glBindTexture (GetTarget(), NO_TEXTURE); + theGlCtx->core20fwd->glBindTexture (GetTarget(), NO_TEXTURE); } diff --git a/src/OpenGl/OpenGl_TextureBuffer.hxx b/src/OpenGl/OpenGl_TextureBuffer.hxx new file mode 100644 index 0000000000..555b070e10 --- /dev/null +++ b/src/OpenGl/OpenGl_TextureBuffer.hxx @@ -0,0 +1,116 @@ +// Created by: Kirill GAVRILOV +// Copyright (c) 2013-2014 OPEN CASCADE SAS +// +// This file is part of Open CASCADE Technology software library. +// +// This library is free software; you can redistribute it and/or modify it under +// the terms of the GNU Lesser General Public License version 2.1 as published +// by the Free Software Foundation, with special exception defined in the file +// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT +// distribution for complete text of the license and disclaimer of any warranty. +// +// Alternatively, this file may be used under the terms of Open CASCADE +// commercial license or contractual agreement. + +#ifndef _OpenGl_TextureBuffer_H__ +#define _OpenGl_TextureBuffer_H__ + +#include +#include + +//! Texture Buffer Object. +//! This is a special 1D texture that VBO-style initialized. +//! The main differences from general 1D texture: +//! - no interpolation between field; +//! - greater sizes; +//! - special sampler object in GLSL shader to access data by index. +//! +//! Notice that though TBO is inherited from VBO this is to unify design +//! user shouldn't cast it to base class and all really useful methods +//! are declared in this class. +class OpenGl_TextureBuffer : public OpenGl_Buffer +{ + DEFINE_STANDARD_RTTIEXT(OpenGl_TextureBuffer, OpenGl_Buffer) +public: + + //! Helpful constants + static const unsigned int NO_TEXTURE = 0; + +public: + + //! Create uninitialized TBO. + Standard_EXPORT OpenGl_TextureBuffer(); + + //! Destroy object, will throw exception if GPU memory not released with Release() before. + Standard_EXPORT virtual ~OpenGl_TextureBuffer(); + + //! Override VBO target + Standard_EXPORT virtual unsigned int GetTarget() const Standard_OVERRIDE; + + //! Returns true if TBO is valid. + //! Notice that no any real GL call is performed! + bool IsValid() const + { + return OpenGl_Buffer::IsValid() + && myTextureId != NO_TEXTURE; + } + + //! Destroy object - will release GPU memory if any. + Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE; + + //! Creates VBO and Texture names (ids) if not yet generated. + //! Data should be initialized by another method. + Standard_EXPORT bool Create (const Handle(OpenGl_Context)& theGlCtx) Standard_OVERRIDE; + + //! Perform TBO initialization with specified data. + //! Existing data will be deleted. + Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const float* theData); + + //! Perform TBO initialization with specified data. + //! Existing data will be deleted. + Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const unsigned int* theData); + + //! Perform TBO initialization with specified data. + //! Existing data will be deleted. + Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const unsigned short* theData); + + //! Perform TBO initialization with specified data. + //! Existing data will be deleted. + Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx, + const unsigned int theComponentsNb, + const Standard_Integer theElemsNb, + const Standard_Byte* theData); + + //! Bind TBO to specified Texture Unit. + Standard_EXPORT void BindTexture (const Handle(OpenGl_Context)& theGlCtx, + const Graphic3d_TextureUnit theTextureUnit) const; + + //! Unbind TBO. + Standard_EXPORT void UnbindTexture (const Handle(OpenGl_Context)& theGlCtx, + const Graphic3d_TextureUnit theTextureUnit) const; + + //! Returns name of TBO. + unsigned int TextureId() const { return myTextureId; } + + //! Returns internal texture format. + unsigned int TextureFormat() const { return myTexFormat; } + +protected: + + unsigned int myTextureId; //!< texture id + unsigned int myTexFormat; //!< internal texture format + +}; + +DEFINE_STANDARD_HANDLE(OpenGl_TextureBuffer, OpenGl_Buffer) + +#endif // _OpenGl_TextureBuffer_H__ diff --git a/src/OpenGl/OpenGl_TextureBufferArb.hxx b/src/OpenGl/OpenGl_TextureBufferArb.hxx index fe8bf81097..ba15e21ab9 100644 --- a/src/OpenGl/OpenGl_TextureBufferArb.hxx +++ b/src/OpenGl/OpenGl_TextureBufferArb.hxx @@ -15,102 +15,9 @@ #ifndef _OpenGl_TextureBufferArb_H__ #define _OpenGl_TextureBufferArb_H__ -#include -#include +#include -//! Texture Buffer Object. -//! This is a special 1D texture that VBO-style initialized. -//! The main differences from general 1D texture: -//! - no interpolation between field; -//! - greater sizes; -//! - special sampler object in GLSL shader to access data by index. -//! -//! Notice that though TBO is inherited from VBO this is to unify design -//! user shouldn't cast it to base class and all really useful methods -//! are declared in this class. -class OpenGl_TextureBufferArb : public OpenGl_VertexBuffer -{ - DEFINE_STANDARD_RTTIEXT(OpenGl_TextureBufferArb, OpenGl_VertexBuffer) -public: - - //! Helpful constants - static const GLuint NO_TEXTURE = 0; - -public: - - //! Create uninitialized TBO. - Standard_EXPORT OpenGl_TextureBufferArb(); - - //! Destroy object, will throw exception if GPU memory not released with Release() before. - Standard_EXPORT virtual ~OpenGl_TextureBufferArb(); - - //! Override VBO target - Standard_EXPORT virtual GLenum GetTarget() const Standard_OVERRIDE; - - //! Returns true if TBO is valid. - //! Notice that no any real GL call is performed! - bool IsValid() const - { - return OpenGl_VertexBuffer::IsValid() - && myTextureId != NO_TEXTURE; - } - - //! Destroy object - will release GPU memory if any. - Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE; - - //! Creates VBO and Texture names (ids) if not yet generated. - //! Data should be initialized by another method. - Standard_EXPORT bool Create (const Handle(OpenGl_Context)& theGlCtx) Standard_OVERRIDE; - - //! Perform TBO initialization with specified data. - //! Existing data will be deleted. - Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx, - const GLuint theComponentsNb, - const GLsizei theElemsNb, - const GLfloat* theData); - - //! Perform TBO initialization with specified data. - //! Existing data will be deleted. - Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx, - const GLuint theComponentsNb, - const GLsizei theElemsNb, - const GLuint* theData); - - //! Perform TBO initialization with specified data. - //! Existing data will be deleted. - Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx, - const GLuint theComponentsNb, - const GLsizei theElemsNb, - const GLushort* theData); - - //! Perform TBO initialization with specified data. - //! Existing data will be deleted. - Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx, - const GLuint theComponentsNb, - const GLsizei theElemsNb, - const GLubyte* theData); - - //! Bind TBO to specified Texture Unit. - Standard_EXPORT void BindTexture (const Handle(OpenGl_Context)& theGlCtx, - const Graphic3d_TextureUnit theTextureUnit) const; - - //! Unbind TBO. - Standard_EXPORT void UnbindTexture (const Handle(OpenGl_Context)& theGlCtx, - const Graphic3d_TextureUnit theTextureUnit) const; - - //! Returns name of TBO. - GLuint TextureId() const { return myTextureId; } - - //! Returns internal texture format. - GLenum TextureFormat() const { return myTexFormat; } - -protected: - - GLuint myTextureId; //!< texture id - GLenum myTexFormat; //!< internal texture format - -}; - -DEFINE_STANDARD_HANDLE(OpenGl_TextureBufferArb, OpenGl_VertexBuffer) +Standard_DEPRECATED("Obsolete alias - OpenGl_TextureBuffer should be used instead") +typedef OpenGl_TextureBuffer OpenGl_TextureBufferArb; #endif // _OpenGl_TextureBufferArb_H__ diff --git a/src/OpenGl/OpenGl_UniformBuffer.cxx b/src/OpenGl/OpenGl_UniformBuffer.cxx new file mode 100644 index 0000000000..af7fa0bfb2 --- /dev/null +++ b/src/OpenGl/OpenGl_UniformBuffer.cxx @@ -0,0 +1,35 @@ +// Copyright (c) 2021 OPEN CASCADE SAS +// +// This file is part of Open CASCADE Technology software library. +// +// This library is free software; you can redistribute it and/or modify it under +// the terms of the GNU Lesser General Public License version 2.1 as published +// by the Free Software Foundation, with special exception defined in the file +// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT +// distribution for complete text of the license and disclaimer of any warranty. +// +// Alternatively, this file may be used under the terms of Open CASCADE +// commercial license or contractual agreement. + +#include + +#include +#include + +IMPLEMENT_STANDARD_RTTIEXT(OpenGl_UniformBuffer, OpenGl_Buffer) + +// ======================================================================= +// function : OpenGl_UniformBuffer +// purpose : +// ======================================================================= +OpenGl_UniformBuffer::OpenGl_UniformBuffer() +: OpenGl_Buffer() {} + +// ======================================================================= +// function : GetTarget +// purpose : +// ======================================================================= +unsigned int OpenGl_UniformBuffer::GetTarget() const +{ + return GL_UNIFORM_BUFFER; +} diff --git a/src/OpenGl/OpenGl_UniformBuffer.hxx b/src/OpenGl/OpenGl_UniformBuffer.hxx new file mode 100644 index 0000000000..4c7509417e --- /dev/null +++ b/src/OpenGl/OpenGl_UniformBuffer.hxx @@ -0,0 +1,37 @@ +// Copyright (c) 2021 OPEN CASCADE SAS +// +// This file is part of Open CASCADE Technology software library. +// +// This library is free software; you can redistribute it and/or modify it under +// the terms of the GNU Lesser General Public License version 2.1 as published +// by the Free Software Foundation, with special exception defined in the file +// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT +// distribution for complete text of the license and disclaimer of any warranty. +// +// Alternatively, this file may be used under the terms of Open CASCADE +// commercial license or contractual agreement. + +#ifndef _OpenGl_UniformBuffer_H__ +#define _OpenGl_UniformBuffer_H__ + +#include + +//! Uniform buffer object. +class OpenGl_UniformBuffer : public OpenGl_Buffer +{ + DEFINE_STANDARD_RTTIEXT(OpenGl_UniformBuffer, OpenGl_Buffer) +public: + + //! Empty constructor. + Standard_EXPORT OpenGl_UniformBuffer(); + + //! Return buffer object target (GL_UNIFORM_BUFFER). + Standard_EXPORT virtual unsigned int GetTarget() const Standard_OVERRIDE; + + using OpenGl_Buffer::BindBufferBase; + using OpenGl_Buffer::UnbindBufferBase; + using OpenGl_Buffer::BindBufferRange; + +}; + +#endif // _OpenGl_UniformBuffer_H__ diff --git a/src/OpenGl/OpenGl_VertexBuffer.cxx b/src/OpenGl/OpenGl_VertexBuffer.cxx index 882912b6fc..da052caaa4 100644 --- a/src/OpenGl/OpenGl_VertexBuffer.cxx +++ b/src/OpenGl/OpenGl_VertexBuffer.cxx @@ -18,34 +18,13 @@ #include #include -IMPLEMENT_STANDARD_RTTIEXT(OpenGl_VertexBuffer,OpenGl_Resource) - -// ======================================================================= -// function : FormatTarget -// purpose : -// ======================================================================= -TCollection_AsciiString OpenGl_VertexBuffer::FormatTarget (GLenum theTarget) -{ - switch (theTarget) - { - case GL_ARRAY_BUFFER: return "GL_ARRAY_BUFFER"; - case GL_ELEMENT_ARRAY_BUFFER: return "GL_ELEMENT_ARRAY_BUFFER"; - case GL_TEXTURE_BUFFER: return "GL_TEXTURE_BUFFER"; - } - return OpenGl_Context::FormatGlEnumHex (theTarget); -} +IMPLEMENT_STANDARD_RTTIEXT(OpenGl_VertexBuffer, OpenGl_Buffer) // ======================================================================= // function : OpenGl_VertexBuffer // purpose : // ======================================================================= OpenGl_VertexBuffer::OpenGl_VertexBuffer() -: OpenGl_Resource(), - myOffset (NULL), - myBufferId (NO_BUFFER), - myComponentsNb (4), - myElemsNb (0), - myDataType (GL_FLOAT) { // } @@ -56,178 +35,24 @@ OpenGl_VertexBuffer::OpenGl_VertexBuffer() // ======================================================================= OpenGl_VertexBuffer::~OpenGl_VertexBuffer() { - Release (NULL); + // } // ======================================================================= // function : GetTarget // purpose : // ======================================================================= -GLenum OpenGl_VertexBuffer::GetTarget() const +unsigned int OpenGl_VertexBuffer::GetTarget() const { return GL_ARRAY_BUFFER; } -// ======================================================================= -// function : Create -// purpose : -// ======================================================================= -bool OpenGl_VertexBuffer::Create (const Handle(OpenGl_Context)& theGlCtx) -{ - if (myBufferId == NO_BUFFER && theGlCtx->core15fwd != NULL) - { - theGlCtx->core15fwd->glGenBuffers (1, &myBufferId); - } - return myBufferId != NO_BUFFER; -} - -// ======================================================================= -// function : Release -// purpose : -// ======================================================================= -void OpenGl_VertexBuffer::Release (OpenGl_Context* theGlCtx) -{ - if (myBufferId == NO_BUFFER) - { - return; - } - - // application can not handle this case by exception - this is bug in code - Standard_ASSERT_RETURN (theGlCtx != NULL, - "OpenGl_VertexBuffer destroyed without GL context! Possible GPU memory leakage...",); - - if (theGlCtx->IsValid()) - { - theGlCtx->core15fwd->glDeleteBuffers (1, &myBufferId); - } - myOffset = NULL; - myBufferId = NO_BUFFER; -} - -// ======================================================================= -// function : Bind -// purpose : -// ======================================================================= -void OpenGl_VertexBuffer::Bind (const Handle(OpenGl_Context)& theGlCtx) const -{ - theGlCtx->core15fwd->glBindBuffer (GetTarget(), myBufferId); -} - -// ======================================================================= -// function : Unbind -// purpose : -// ======================================================================= -void OpenGl_VertexBuffer::Unbind (const Handle(OpenGl_Context)& theGlCtx) const -{ - theGlCtx->core15fwd->glBindBuffer (GetTarget(), NO_BUFFER); -} - -// ======================================================================= -// function : init -// purpose : -// ======================================================================= -bool OpenGl_VertexBuffer::init (const Handle(OpenGl_Context)& theGlCtx, - const GLuint theComponentsNb, - const GLsizei theElemsNb, - const void* theData, - const GLenum theDataType, - const GLsizei theStride) -{ - if (!Create (theGlCtx)) - { - return false; - } - - Bind (theGlCtx); - myDataType = theDataType; - myComponentsNb = theComponentsNb; - myElemsNb = theElemsNb; - theGlCtx->core15fwd->glBufferData (GetTarget(), GLsizeiptr(myElemsNb) * theStride, theData, GL_STATIC_DRAW); - const int anErr = theGlCtx->core15fwd->glGetError(); - if (anErr != GL_NO_ERROR - && anErr != GL_OUT_OF_MEMORY) // pass-through out-of-memory error, but log unexpected errors - { - theGlCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, - TCollection_AsciiString ("Error: glBufferData (") - + FormatTarget (GetTarget()) + "," - + OpenGl_Context::FormatSize (GLsizeiptr(myElemsNb) * theStride) + "," - + OpenGl_Context::FormatPointer (theData) + ") Id: " + (int )myBufferId - + " failed with " + OpenGl_Context::FormatGlError (anErr)); - } - Unbind (theGlCtx); - return anErr == GL_NO_ERROR; -} - -// ======================================================================= -// function : subData -// purpose : -// ======================================================================= -bool OpenGl_VertexBuffer::subData (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theElemFrom, - const GLsizei theElemsNb, - const void* theData, - const GLenum theDataType) -{ - if (!IsValid() || myDataType != theDataType || - theElemFrom < 0 || ((theElemFrom + theElemsNb) > myElemsNb)) - { - return false; - } - - Bind (theGlCtx); - const size_t aDataSize = sizeOfGlType (theDataType); - theGlCtx->core15fwd->glBufferSubData (GetTarget(), - GLintptr(theElemFrom) * GLintptr (myComponentsNb) * aDataSize, // offset in bytes - GLsizeiptr(theElemsNb) * GLsizeiptr(myComponentsNb) * aDataSize, // size in bytes - theData); - const int anErr = theGlCtx->core15fwd->glGetError(); - if (anErr != GL_NO_ERROR) - { - theGlCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, - TCollection_AsciiString ("Error: glBufferSubData (") - + FormatTarget (GetTarget()) + "," - + OpenGl_Context::FormatSize (GLintptr(theElemFrom) * GLintptr (myComponentsNb) * aDataSize) + "," - + OpenGl_Context::FormatSize (GLsizeiptr(theElemsNb) * GLsizeiptr(myComponentsNb) * aDataSize) + "," - + OpenGl_Context::FormatPointer (theData) + ") Id: " + (int )myBufferId - + " failed with " + OpenGl_Context::FormatGlError (anErr)); - } - Unbind (theGlCtx); - return anErr == GL_NO_ERROR; -} - -// ======================================================================= -// function : getSubData -// purpose : -// ======================================================================= -bool OpenGl_VertexBuffer::getSubData (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theElemFrom, - const GLsizei theElemsNb, - void* theData, - const GLenum theDataType) -{ - if (!IsValid() || myDataType != theDataType - || theElemFrom < 0 || ((theElemFrom + theElemsNb) > myElemsNb) - || !theGlCtx->hasGetBufferData) - { - return false; - } - - Bind (theGlCtx); - const size_t aDataSize = sizeOfGlType (theDataType); - const GLintptr anOffset = GLintptr (theElemFrom) * GLintptr (myComponentsNb) * aDataSize; - const GLsizeiptr aSize = GLsizeiptr(theElemsNb) * GLsizeiptr(myComponentsNb) * aDataSize; - bool isDone = theGlCtx->GetBufferSubData (GetTarget(), anOffset, aSize, theData); - isDone = isDone && (glGetError() == GL_NO_ERROR); - Unbind (theGlCtx); - return isDone; -} - // ======================================================================= // function : BindVertexAttrib // purpose : // ======================================================================= void OpenGl_VertexBuffer::BindVertexAttrib (const Handle(OpenGl_Context)& theGlCtx, - const GLuint theAttribLoc) const + const unsigned int theAttribLoc) const { if (!IsValid() || theAttribLoc == GLuint (-1)) { @@ -243,7 +68,7 @@ void OpenGl_VertexBuffer::BindVertexAttrib (const Handle(OpenGl_Context)& theGlC // purpose : // ======================================================================= void OpenGl_VertexBuffer::UnbindVertexAttrib (const Handle(OpenGl_Context)& theGlCtx, - const GLuint theAttribLoc) const + const unsigned int theAttribLoc) const { if (!IsValid() || theAttribLoc == GLuint (-1)) { @@ -298,7 +123,126 @@ bool OpenGl_VertexBuffer::HasNormalAttribute() const return false; } +// ======================================================================= +// function : bindAttribute +// purpose : +// ======================================================================= +void OpenGl_VertexBuffer::bindAttribute (const Handle(OpenGl_Context)& theCtx, + const Graphic3d_TypeOfAttribute theAttribute, + const Standard_Integer theNbComp, + const unsigned int theDataType, + const Standard_Integer theStride, + const void* theOffset) +{ + if (theCtx->ActiveProgram().IsNull()) + { + #if !defined(GL_ES_VERSION_2_0) + if (theCtx->core11ffp != NULL) + { + bindFixed (theCtx, theAttribute, theNbComp, theDataType, theStride, theOffset); + } + else + #endif + { + // OpenGL handles vertex attribute setup independently from active GLSL program, + // but OCCT historically requires program to be bound beforehand (this check could be removed in future). + Message::SendFail ("Error: OpenGl_VertexBuffer::bindAttribute() does nothing without active GLSL program"); + } + return; + } + + theCtx->core20fwd->glEnableVertexAttribArray (theAttribute); + theCtx->core20fwd->glVertexAttribPointer (theAttribute, theNbComp, theDataType, theDataType != GL_FLOAT, theStride, theOffset); +} + +// ======================================================================= +// function : unbindAttribute +// purpose : +// ======================================================================= +void OpenGl_VertexBuffer::unbindAttribute (const Handle(OpenGl_Context)& theCtx, + const Graphic3d_TypeOfAttribute theAttribute) +{ + if (theCtx->ActiveProgram().IsNull()) + { + #if !defined(GL_ES_VERSION_2_0) + if (theCtx->core11ffp != NULL) + { + unbindFixed (theCtx, theAttribute); + } + #endif + return; + } + + theCtx->core20fwd->glDisableVertexAttribArray (theAttribute); +} + #if !defined(GL_ES_VERSION_2_0) +// ======================================================================= +// function : bindFixed +// purpose : +// ======================================================================= +void OpenGl_VertexBuffer::bindFixed (const Handle(OpenGl_Context)& theCtx, + const Graphic3d_TypeOfAttribute theMode, + const Standard_Integer theNbComp, + const unsigned int theDataType, + const Standard_Integer theStride, + const void* theOffset) +{ + switch (theMode) + { + case Graphic3d_TOA_POS: + { + theCtx->core11ffp->glEnableClientState (GL_VERTEX_ARRAY); + theCtx->core11ffp->glVertexPointer (theNbComp, theDataType, theStride, theOffset); + return; + } + case Graphic3d_TOA_NORM: + { + theCtx->core11ffp->glEnableClientState (GL_NORMAL_ARRAY); + theCtx->core11ffp->glNormalPointer (theDataType, theStride, theOffset); + return; + } + case Graphic3d_TOA_UV: + { + theCtx->core11ffp->glEnableClientState (GL_TEXTURE_COORD_ARRAY); + theCtx->core11ffp->glTexCoordPointer (theNbComp, theDataType, theStride, theOffset); + return; + } + case Graphic3d_TOA_COLOR: + { + theCtx->core11ffp->glEnableClientState (GL_COLOR_ARRAY); + theCtx->core11ffp->glColorPointer (theNbComp, theDataType, theStride, theOffset); + theCtx->core11ffp->glColorMaterial (GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE); + theCtx->core11fwd->glEnable (GL_COLOR_MATERIAL); + return; + } + case Graphic3d_TOA_CUSTOM: + { + return; + } + } +} + +// ======================================================================= +// function : unbindFixed +// purpose : +// ======================================================================= +void OpenGl_VertexBuffer::unbindFixed (const Handle(OpenGl_Context)& theCtx, + const Graphic3d_TypeOfAttribute theMode) +{ + switch (theMode) + { + case Graphic3d_TOA_POS: theCtx->core11ffp->glDisableClientState (GL_VERTEX_ARRAY); return; + case Graphic3d_TOA_NORM: theCtx->core11ffp->glDisableClientState (GL_NORMAL_ARRAY); return; + case Graphic3d_TOA_UV: theCtx->core11ffp->glDisableClientState (GL_TEXTURE_COORD_ARRAY); return; + case Graphic3d_TOA_COLOR: unbindFixedColor (theCtx); return; + case Graphic3d_TOA_CUSTOM: + { + return; + } + } +} + // ======================================================================= // function : unbindFixedColor // purpose : @@ -312,21 +256,3 @@ void OpenGl_VertexBuffer::unbindFixedColor (const Handle(OpenGl_Context)& theCtx theCtx->ShaderManager()->UpdateMaterialState(); } #endif - -// ======================================================================= -// function : DumpJson -// purpose : -// ======================================================================= -void OpenGl_VertexBuffer::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const -{ - OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream) - OCCT_DUMP_BASE_CLASS (theOStream, theDepth, OpenGl_Resource) - - OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, GetTarget()) - OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, myOffset) - - OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myBufferId) - OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myComponentsNb) - OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myElemsNb) - OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myDataType) -} diff --git a/src/OpenGl/OpenGl_VertexBuffer.hxx b/src/OpenGl/OpenGl_VertexBuffer.hxx index ee3eec7997..4d3b7b7fe4 100644 --- a/src/OpenGl/OpenGl_VertexBuffer.hxx +++ b/src/OpenGl/OpenGl_VertexBuffer.hxx @@ -15,26 +15,13 @@ #ifndef _OpenGl_VertexBuffer_H__ #define _OpenGl_VertexBuffer_H__ -#include -#include -#include -#include - -#include +#include +#include //! Vertex Buffer Object - is a general storage object for vertex attributes (position, normal, color). //! Notice that you should use OpenGl_IndexBuffer specialization for array of indices. -class OpenGl_VertexBuffer : public OpenGl_Resource +class OpenGl_VertexBuffer : public OpenGl_Buffer { - -public: - - //! Helpful constants - static const GLuint NO_BUFFER = 0; - - //! Format VBO target enumeration value. - Standard_EXPORT static TCollection_AsciiString FormatTarget (GLenum theTarget); - public: //! Create uninitialized VBO. @@ -43,225 +30,16 @@ public: //! Destroy object. Standard_EXPORT virtual ~OpenGl_VertexBuffer(); - Standard_EXPORT virtual GLenum GetTarget() const; - - //! Return TRUE if this is a virtual (for backward compatibility) VBO object. - virtual bool IsVirtual() const { return false; } - - //! @return true if current object was initialized - inline bool IsValid() const - { - return myBufferId != NO_BUFFER; - } - - //! @return the number of components per generic vertex attribute. - inline GLuint GetComponentsNb() const - { - return myComponentsNb; - } - - //! @return number of vertex attributes / number of vertices specified within ::Init() - inline GLsizei GetElemsNb() const - { - return myElemsNb; - } - - //! Overrides the number of vertex attributes / number of vertexes. - //! It is up to user specifying this number correct (e.g. below initial value)! - void SetElemsNb (GLsizei theNbElems) { myElemsNb = theNbElems; } - - //! @return data type of each component in the array. - inline GLenum GetDataType() const - { - return myDataType; - } - - //! @return offset to data, NULL by default - inline GLubyte* GetDataOffset() const - { - return myOffset; - } - - //! Creates VBO name (id) if not yet generated. - //! Data should be initialized by another method. - Standard_EXPORT virtual bool Create (const Handle(OpenGl_Context)& theGlCtx); - - //! Destroy object - will release GPU memory if any. - Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE; - - //! Bind this VBO. - Standard_EXPORT virtual void Bind (const Handle(OpenGl_Context)& theGlCtx) const; - - //! Unbind this VBO. - Standard_EXPORT virtual void Unbind (const Handle(OpenGl_Context)& theGlCtx) const; - - //! Notice that VBO will be unbound after this call. - //! @param theComponentsNb - specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4; - //! @param theElemsNb - elements count; - //! @param theData - pointer to GLfloat data (vertices/normals etc.). - bool Init (const Handle(OpenGl_Context)& theGlCtx, - const GLuint theComponentsNb, - const GLsizei theElemsNb, - const GLfloat* theData) - { - return init (theGlCtx, theComponentsNb, theElemsNb, theData, GL_FLOAT); - } - - //! Notice that VBO will be unbound after this call. - //! @param theComponentsNb - specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4; - //! @param theElemsNb - elements count; - //! @param theData - pointer to GLuint data (indices etc.). - bool Init (const Handle(OpenGl_Context)& theGlCtx, - const GLuint theComponentsNb, - const GLsizei theElemsNb, - const GLuint* theData) - { - return init (theGlCtx, theComponentsNb, theElemsNb, theData, GL_UNSIGNED_INT); - } - - //! Notice that VBO will be unbound after this call. - //! @param theComponentsNb - specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4; - //! @param theElemsNb - elements count; - //! @param theData - pointer to GLushort data (indices etc.). - bool Init (const Handle(OpenGl_Context)& theGlCtx, - const GLuint theComponentsNb, - const GLsizei theElemsNb, - const GLushort* theData) - { - return init (theGlCtx, theComponentsNb, theElemsNb, theData, GL_UNSIGNED_SHORT); - } - - //! Notice that VBO will be unbound after this call. - //! @param theComponentsNb - specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4; - //! @param theElemsNb - elements count; - //! @param theData - pointer to GLubyte data (indices/colors etc.). - bool Init (const Handle(OpenGl_Context)& theGlCtx, - const GLuint theComponentsNb, - const GLsizei theElemsNb, - const GLubyte* theData) - { - return init (theGlCtx, theComponentsNb, theElemsNb, theData, GL_UNSIGNED_BYTE); - } - - //! Notice that VBO will be unbound after this call. - //! Function replaces portion of data within this VBO using glBufferSubData(). - //! The VBO should be initialized before call. - //! @param theElemFrom - element id from which replace buffer data (>=0); - //! @param theElemsNb - elements count (theElemFrom + theElemsNb <= GetElemsNb()); - //! @param theData - pointer to GLfloat data. - bool SubData (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theElemFrom, - const GLsizei theElemsNb, - const GLfloat* theData) - { - return subData (theGlCtx, theElemFrom, theElemsNb, theData, GL_FLOAT); - } - - //! Read back buffer sub-range. - //! Notice that VBO will be unbound after this call. - //! Function reads portion of data from this VBO using glGetBufferSubData(). - //! @param theElemFrom [in] element id from which replace buffer data (>=0); - //! @param theElemsNb [in] elements count (theElemFrom + theElemsNb <= GetElemsNb()); - //! @param theData [out] destination pointer to GLfloat data. - bool GetSubData (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theElemFrom, - const GLsizei theElemsNb, - GLfloat* theData) - { - return getSubData (theGlCtx, theElemFrom, theElemsNb, theData, GL_FLOAT); - } - - //! Notice that VBO will be unbound after this call. - //! Function replaces portion of data within this VBO using glBufferSubData(). - //! The VBO should be initialized before call. - //! @param theElemFrom element id from which replace buffer data (>=0); - //! @param theElemsNb elements count (theElemFrom + theElemsNb <= GetElemsNb()); - //! @param theData pointer to GLuint data. - bool SubData (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theElemFrom, - const GLsizei theElemsNb, - const GLuint* theData) - { - return subData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_INT); - } - - //! Read back buffer sub-range. - //! Notice that VBO will be unbound after this call. - //! Function reads portion of data from this VBO using glGetBufferSubData(). - //! @param theElemFrom [in] element id from which replace buffer data (>=0); - //! @param theElemsNb [in] elements count (theElemFrom + theElemsNb <= GetElemsNb()); - //! @param theData [out] destination pointer to GLuint data. - bool GetSubData (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theElemFrom, - const GLsizei theElemsNb, - GLuint* theData) - { - return getSubData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_INT); - } - - //! Notice that VBO will be unbound after this call. - //! Function replaces portion of data within this VBO using glBufferSubData(). - //! The VBO should be initialized before call. - //! @param theElemFrom element id from which replace buffer data (>=0); - //! @param theElemsNb elements count (theElemFrom + theElemsNb <= GetElemsNb()); - //! @param theData pointer to GLushort data. - bool SubData (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theElemFrom, - const GLsizei theElemsNb, - const GLushort* theData) - { - return subData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_SHORT); - } - - //! Read back buffer sub-range. - //! Notice that VBO will be unbound after this call. - //! Function reads portion of data from this VBO using glGetBufferSubData(). - //! @param theElemFrom [in] element id from which replace buffer data (>=0); - //! @param theElemsNb [in] elements count (theElemFrom + theElemsNb <= GetElemsNb()); - //! @param theData [out] destination pointer to GLushort data. - bool GetSubData (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theElemFrom, - const GLsizei theElemsNb, - GLushort* theData) - { - return getSubData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_SHORT); - } - - //! Notice that VBO will be unbound after this call. - //! Function replaces portion of data within this VBO using glBufferSubData(). - //! The VBO should be initialized before call. - //! @param theElemFrom element id from which replace buffer data (>=0); - //! @param theElemsNb elements count (theElemFrom + theElemsNb <= GetElemsNb()); - //! @param theData pointer to GLubyte data. - bool SubData (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theElemFrom, - const GLsizei theElemsNb, - const GLubyte* theData) - { - return subData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_BYTE); - } - - //! Read back buffer sub-range. - //! Notice that VBO will be unbound after this call. - //! Function reads portion of data from this VBO using glGetBufferSubData(). - //! @param theElemFrom [in] element id from which replace buffer data (>=0); - //! @param theElemsNb [in] elements count (theElemFrom + theElemsNb <= GetElemsNb()); - //! @param theData [out] destination pointer to GLubyte data. - bool GetSubData (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theElemFrom, - const GLsizei theElemsNb, - GLubyte* theData) - { - return getSubData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_BYTE); - } + //! Return buffer target GL_ARRAY_BUFFER. + Standard_EXPORT virtual unsigned int GetTarget() const Standard_OVERRIDE; //! Bind this VBO to active GLSL program. Standard_EXPORT void BindVertexAttrib (const Handle(OpenGl_Context)& theGlCtx, - const GLuint theAttribLoc) const; + const unsigned int theAttribLoc) const; //! Unbind any VBO from active GLSL program. Standard_EXPORT void UnbindVertexAttrib (const Handle(OpenGl_Context)& theGlCtx, - const GLuint theAttribLoc) const; + const unsigned int theAttribLoc) const; //! Bind this VBO and enable specified attribute in OpenGl_Context::ActiveProgram() or FFP. //! @param theGlCtx - handle to bound GL context; @@ -272,7 +50,7 @@ public: if (IsValid()) { Bind (theCtx); - bindAttribute (theCtx, theMode, static_cast (myComponentsNb), myDataType, 0, myOffset); + bindAttribute (theCtx, theMode, static_cast (myComponentsNb), myDataType, 0, myOffset); } } @@ -291,94 +69,33 @@ public: public: //! @name advanced methods - //! Returns estimated GPU memory usage for holding data without considering overheads and allocation alignment rules. - virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE - { - return IsValid() - ? sizeOfGlType (myDataType) * myComponentsNb * myElemsNb - : 0; - } - - //! @return size of specified GL type - static size_t sizeOfGlType (const GLenum theType) - { - switch (theType) - { - case GL_BYTE: - case GL_UNSIGNED_BYTE: return sizeof(GLubyte); - case GL_SHORT: - case GL_UNSIGNED_SHORT: return sizeof(GLushort); - #ifdef GL_INT - case GL_INT: - #endif - case GL_UNSIGNED_INT: return sizeof(GLuint); - case GL_FLOAT: return sizeof(GLfloat); - #ifdef GL_DOUBLE - case GL_DOUBLE: return sizeof(GLdouble); - #endif - default: return 0; - } - } - - //! Initialize buffer with new data. - Standard_EXPORT virtual bool init (const Handle(OpenGl_Context)& theGlCtx, - const GLuint theComponentsNb, - const GLsizei theElemsNb, - const void* theData, - const GLenum theDataType, - const GLsizei theStride); - - //! Initialize buffer with new data. - bool init (const Handle(OpenGl_Context)& theGlCtx, - const GLuint theComponentsNb, - const GLsizei theElemsNb, - const void* theData, - const GLenum theDataType) - { - return init (theGlCtx, theComponentsNb, theElemsNb, theData, theDataType, GLsizei(theComponentsNb) * GLsizei(sizeOfGlType (theDataType))); - } - - //! Update part of the buffer with new data. - Standard_EXPORT virtual bool subData (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theElemFrom, - const GLsizei theElemsNb, - const void* theData, - const GLenum theDataType); - - //! Read back buffer sub-range. - Standard_EXPORT virtual bool getSubData (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theElemFrom, - const GLsizei theElemsNb, - void* theData, - const GLenum theDataType); - //! Setup array pointer - either for active GLSL program OpenGl_Context::ActiveProgram() //! or for FFP using bindFixed() when no program bound. - static void bindAttribute (const Handle(OpenGl_Context)& theGlCtx, - const Graphic3d_TypeOfAttribute theMode, - const GLint theNbComp, - const GLenum theDataType, - const GLsizei theStride, - const GLvoid* theOffset); + Standard_EXPORT static void bindAttribute (const Handle(OpenGl_Context)& theGlCtx, + const Graphic3d_TypeOfAttribute theMode, + const Standard_Integer theNbComp, + const unsigned int theDataType, + const Standard_Integer theStride, + const void* theOffset); //! Disable GLSL array pointer - either for active GLSL program OpenGl_Context::ActiveProgram() //! or for FFP using unbindFixed() when no program bound. - static void unbindAttribute (const Handle(OpenGl_Context)& theGlCtx, - const Graphic3d_TypeOfAttribute theMode); + Standard_EXPORT static void unbindAttribute (const Handle(OpenGl_Context)& theGlCtx, + const Graphic3d_TypeOfAttribute theMode); private: #if !defined(GL_ES_VERSION_2_0) //! Setup FFP array pointer. - static void bindFixed (const Handle(OpenGl_Context)& theGlCtx, - const Graphic3d_TypeOfAttribute theMode, - const GLint theNbComp, - const GLenum theDataType, - const GLsizei theStride, - const GLvoid* theOffset); + Standard_EXPORT static void bindFixed (const Handle(OpenGl_Context)& theGlCtx, + const Graphic3d_TypeOfAttribute theMode, + const Standard_Integer theNbComp, + const unsigned int theDataType, + const Standard_Integer theStride, + const void* theOffset); //! Disable FFP array pointer. - static void unbindFixed (const Handle(OpenGl_Context)& theGlCtx, - const Graphic3d_TypeOfAttribute theMode); + Standard_EXPORT static void unbindFixed (const Handle(OpenGl_Context)& theGlCtx, + const Graphic3d_TypeOfAttribute theMode); //! Disable FFP color array pointer. Standard_EXPORT static void unbindFixedColor (const Handle(OpenGl_Context)& theCtx); @@ -402,25 +119,12 @@ public: //! @name methods for interleaved attributes array //! Unbind all vertex attributes. Default implementation does nothing. Standard_EXPORT virtual void UnbindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const; - //! Dumps the content of me into the stream - Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const Standard_OVERRIDE; - -protected: - - GLubyte* myOffset; //!< offset to data - GLuint myBufferId; //!< VBO name (index) - GLuint myComponentsNb; //!< Number of components per generic vertex attribute, must be 1, 2, 3, or 4 - GLsizei myElemsNb; //!< Number of vertex attributes / number of vertices - GLenum myDataType; //!< Data type (GL_FLOAT, GL_UNSIGNED_INT, GL_UNSIGNED_BYTE etc.) - public: - DEFINE_STANDARD_RTTIEXT(OpenGl_VertexBuffer,OpenGl_Resource) // Type definition + DEFINE_STANDARD_RTTIEXT(OpenGl_VertexBuffer, OpenGl_Buffer) }; -DEFINE_STANDARD_HANDLE(OpenGl_VertexBuffer, OpenGl_Resource) - -#include +DEFINE_STANDARD_HANDLE(OpenGl_VertexBuffer, OpenGl_Buffer) #endif // _OpenGl_VertexBuffer_H__ diff --git a/src/OpenGl/OpenGl_VertexBuffer.lxx b/src/OpenGl/OpenGl_VertexBuffer.lxx deleted file mode 100644 index 48f5fe6369..0000000000 --- a/src/OpenGl/OpenGl_VertexBuffer.lxx +++ /dev/null @@ -1,136 +0,0 @@ -// Created by: Kirill GAVRILOV -// Copyright (c) 2014 OPEN CASCADE SAS -// -// This file is part of Open CASCADE Technology software library. -// -// This library is free software; you can redistribute it and/or modify it under -// the terms of the GNU Lesser General Public License version 2.1 as published -// by the Free Software Foundation, with special exception defined in the file -// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT -// distribution for complete text of the license and disclaimer of any warranty. -// -// Alternatively, this file may be used under the terms of Open CASCADE -// commercial license or contractual agreement. - -// ======================================================================= -// function : bindAttribute -// purpose : -// ======================================================================= -inline void OpenGl_VertexBuffer::bindAttribute (const Handle(OpenGl_Context)& theCtx, - const Graphic3d_TypeOfAttribute theAttribute, - const GLint theNbComp, - const GLenum theDataType, - const GLsizei theStride, - const GLvoid* theOffset) -{ - if (theCtx->ActiveProgram().IsNull()) - { - #if !defined(GL_ES_VERSION_2_0) - if (theCtx->core11ffp != NULL) - { - bindFixed (theCtx, theAttribute, theNbComp, theDataType, theStride, theOffset); - } - else - #endif - { - // OpenGL handles vertex attribute setup independently from active GLSL program, - // but OCCT historically requires program to be bound beforehand (this check could be removed in future). - Message::SendFail ("Error: OpenGl_VertexBuffer::bindAttribute() does nothing without active GLSL program"); - } - return; - } - - theCtx->core20fwd->glEnableVertexAttribArray (theAttribute); - theCtx->core20fwd->glVertexAttribPointer (theAttribute, theNbComp, theDataType, theDataType != GL_FLOAT, theStride, theOffset); -} - -#if !defined(GL_ES_VERSION_2_0) -// ======================================================================= -// function : bindFixed -// purpose : -// ======================================================================= -inline void OpenGl_VertexBuffer::bindFixed (const Handle(OpenGl_Context)& theCtx, - const Graphic3d_TypeOfAttribute theMode, - const GLint theNbComp, - const GLenum theDataType, - const GLsizei theStride, - const GLvoid* theOffset) -{ - switch (theMode) - { - case Graphic3d_TOA_POS: - { - theCtx->core11ffp->glEnableClientState (GL_VERTEX_ARRAY); - theCtx->core11ffp->glVertexPointer (theNbComp, theDataType, theStride, theOffset); - return; - } - case Graphic3d_TOA_NORM: - { - theCtx->core11ffp->glEnableClientState (GL_NORMAL_ARRAY); - theCtx->core11ffp->glNormalPointer (theDataType, theStride, theOffset); - return; - } - case Graphic3d_TOA_UV: - { - theCtx->core11ffp->glEnableClientState (GL_TEXTURE_COORD_ARRAY); - theCtx->core11ffp->glTexCoordPointer (theNbComp, theDataType, theStride, theOffset); - return; - } - case Graphic3d_TOA_COLOR: - { - theCtx->core11ffp->glEnableClientState (GL_COLOR_ARRAY); - theCtx->core11ffp->glColorPointer (theNbComp, theDataType, theStride, theOffset); - theCtx->core11ffp->glColorMaterial (GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE); - theCtx->core11fwd->glEnable (GL_COLOR_MATERIAL); - return; - } - case Graphic3d_TOA_CUSTOM: - { - return; - } - } -} -#endif - -// ======================================================================= -// function : unbindAttribute -// purpose : -// ======================================================================= -inline void OpenGl_VertexBuffer::unbindAttribute (const Handle(OpenGl_Context)& theCtx, - const Graphic3d_TypeOfAttribute theAttribute) -{ - if (theCtx->ActiveProgram().IsNull()) - { - #if !defined(GL_ES_VERSION_2_0) - if (theCtx->core11ffp != NULL) - { - unbindFixed (theCtx, theAttribute); - } - #endif - return; - } - - theCtx->core20fwd->glDisableVertexAttribArray (theAttribute); -} - -#if !defined(GL_ES_VERSION_2_0) -// ======================================================================= -// function : unbindFixed -// purpose : -// ======================================================================= -inline void OpenGl_VertexBuffer::unbindFixed (const Handle(OpenGl_Context)& theCtx, - const Graphic3d_TypeOfAttribute theMode) -{ - switch (theMode) - { - case Graphic3d_TOA_POS: theCtx->core11ffp->glDisableClientState (GL_VERTEX_ARRAY); return; - case Graphic3d_TOA_NORM: theCtx->core11ffp->glDisableClientState (GL_NORMAL_ARRAY); return; - case Graphic3d_TOA_UV: theCtx->core11ffp->glDisableClientState (GL_TEXTURE_COORD_ARRAY); return; - case Graphic3d_TOA_COLOR: unbindFixedColor (theCtx); return; - case Graphic3d_TOA_CUSTOM: - { - return; - } - } -} -#endif diff --git a/src/OpenGl/OpenGl_VertexBufferCompat.cxx b/src/OpenGl/OpenGl_VertexBufferCompat.cxx deleted file mode 100644 index 2add516459..0000000000 --- a/src/OpenGl/OpenGl_VertexBufferCompat.cxx +++ /dev/null @@ -1,197 +0,0 @@ -// Created by: Kirill GAVRILOV -// Copyright (c) 2013-2014 OPEN CASCADE SAS -// -// This file is part of Open CASCADE Technology software library. -// -// This library is free software; you can redistribute it and/or modify it under -// the terms of the GNU Lesser General Public License version 2.1 as published -// by the Free Software Foundation, with special exception defined in the file -// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT -// distribution for complete text of the license and disclaimer of any warranty. -// -// Alternatively, this file may be used under the terms of Open CASCADE -// commercial license or contractual agreement. - -#include - -IMPLEMENT_STANDARD_RTTIEXT(OpenGl_VertexBufferCompat,OpenGl_VertexBuffer) - -// ======================================================================= -// function : OpenGl_VertexBufferCompat -// purpose : -// ======================================================================= -OpenGl_VertexBufferCompat::OpenGl_VertexBufferCompat() -{ - // -} - -// ======================================================================= -// function : ~OpenGl_VertexBufferCompat -// purpose : -// ======================================================================= -OpenGl_VertexBufferCompat::~OpenGl_VertexBufferCompat() -{ - Release (NULL); -} - -// ======================================================================= -// function : Create -// purpose : -// ======================================================================= -bool OpenGl_VertexBufferCompat::Create (const Handle(OpenGl_Context)& ) -{ - if (myBufferId == NO_BUFFER) - { - myBufferId = (GLuint )-1; // dummy identifier... - myData = new NCollection_Buffer (Graphic3d_Buffer::DefaultAllocator()); - } - return myBufferId != NO_BUFFER; -} - -// ======================================================================= -// function : Release -// purpose : -// ======================================================================= -void OpenGl_VertexBufferCompat::Release (OpenGl_Context* ) -{ - if (myBufferId == NO_BUFFER) - { - return; - } - - myOffset = NULL; - myBufferId = NO_BUFFER; - myData.Nullify(); -} - -// ======================================================================= -// function : Bind -// purpose : -// ======================================================================= -void OpenGl_VertexBufferCompat::Bind (const Handle(OpenGl_Context)& ) const -{ - // -} - -// ======================================================================= -// function : Unbind -// purpose : -// ======================================================================= -void OpenGl_VertexBufferCompat::Unbind (const Handle(OpenGl_Context)& ) const -{ - // -} - -// ======================================================================= -// function : initLink -// purpose : -// ======================================================================= -bool OpenGl_VertexBufferCompat::initLink (const Handle(NCollection_Buffer)& theData, - const GLuint theComponentsNb, - const GLsizei theElemsNb, - const GLenum theDataType) -{ - if (theData.IsNull()) - { - myOffset = NULL; - return false; - } - - if (myBufferId == NO_BUFFER) - { - myBufferId = (GLuint )-1; // dummy identifier... - } - myData = theData; - myDataType = theDataType; - myComponentsNb = theComponentsNb; - myElemsNb = theElemsNb; - myOffset = myData->ChangeData(); - return true; -} - -// ======================================================================= -// function : init -// purpose : -// ======================================================================= -bool OpenGl_VertexBufferCompat::init (const Handle(OpenGl_Context)& theCtx, - const GLuint theComponentsNb, - const GLsizei theElemsNb, - const void* theData, - const GLenum theDataType, - const GLsizei theStride) -{ - if (!Create (theCtx)) - { - myOffset = NULL; - return false; - } - - myDataType = theDataType; - myComponentsNb = theComponentsNb; - myElemsNb = theElemsNb; - - const size_t aNbBytes = size_t(myElemsNb) * theStride; - if (!myData->Allocate (aNbBytes)) - { - myOffset = NULL; - return false; - } - - myOffset = myData->ChangeData(); - if (theData != NULL) - { - memcpy (myData->ChangeData(), theData, aNbBytes); - } - return true; -} - -// ======================================================================= -// function : subData -// purpose : -// ======================================================================= -bool OpenGl_VertexBufferCompat::subData (const Handle(OpenGl_Context)& , - const GLsizei theElemFrom, - const GLsizei theElemsNb, - const void* theData, - const GLenum theDataType) -{ - if (!IsValid() || myDataType != theDataType || - theElemFrom < 0 || ((theElemFrom + theElemsNb) > myElemsNb)) - { - return false; - } - else if (theData == NULL) - { - return true; - } - - const size_t aDataSize = sizeOfGlType (theDataType); - const size_t anOffset = size_t(theElemFrom) * size_t(myComponentsNb) * aDataSize; - const size_t aNbBytes = size_t(theElemsNb) * size_t(myComponentsNb) * aDataSize; - memcpy (myData->ChangeData() + anOffset, theData, aNbBytes); - return true; -} - -// ======================================================================= -// function : getSubData -// purpose : -// ======================================================================= -bool OpenGl_VertexBufferCompat::getSubData (const Handle(OpenGl_Context)& , - const GLsizei theElemFrom, - const GLsizei theElemsNb, - void* theData, - const GLenum theDataType) -{ - if (!IsValid() || myDataType != theDataType - || theElemFrom < 0 || ((theElemFrom + theElemsNb) > myElemsNb) - || theData == NULL) - { - return false; - } - - const size_t aDataSize = sizeOfGlType (theDataType); - const size_t anOffset = size_t(theElemFrom) * size_t(myComponentsNb) * aDataSize; - const size_t aNbBytes = size_t(theElemsNb) * size_t(myComponentsNb) * aDataSize; - memcpy (theData, myData->Data() + anOffset, aNbBytes); - return true; -} diff --git a/src/OpenGl/OpenGl_VertexBufferCompat.hxx b/src/OpenGl/OpenGl_VertexBufferCompat.hxx index f7c395702b..1c05a31ab3 100644 --- a/src/OpenGl/OpenGl_VertexBufferCompat.hxx +++ b/src/OpenGl/OpenGl_VertexBufferCompat.hxx @@ -15,90 +15,11 @@ #ifndef _OpenGl_VertexBufferCompat_HeaderFile #define _OpenGl_VertexBufferCompat_HeaderFile +#include #include +#include -//! Compatibility layer for old OpenGL without VBO. -//! Make sure to pass pointer from GetDataOffset() instead of NULL. -//! Method GetDataOffset() returns pointer to real data in this class -//! (while base class OpenGl_VertexBuffer always return NULL). -//! -//! Methods Bind()/Unbind() do nothing (do not affect OpenGL state) -//! and ::GetTarget() is never used. -//! For this reason there is no analog for OpenGl_IndexBuffer. -//! Just pass GetDataOffset() to glDrawElements() directly as last argument. -//! -//! Class overrides methods init() and subData() to copy data into own memory buffer. -//! Extra method initLink() might be used to pass existing buffer through handle without copying the data. -//! -//! Method Create() creates dummy identifier for this object which should NOT be passed to OpenGL functions. -class OpenGl_VertexBufferCompat : public OpenGl_VertexBuffer -{ - -public: - - //! Create uninitialized VBO. - Standard_EXPORT OpenGl_VertexBufferCompat(); - - //! Destroy object. - Standard_EXPORT virtual ~OpenGl_VertexBufferCompat(); - - //! Return TRUE. - virtual bool IsVirtual() const Standard_OVERRIDE { return true; } - - //! Creates VBO name (id) if not yet generated. - //! Data should be initialized by another method. - Standard_EXPORT bool Create (const Handle(OpenGl_Context)& theGlCtx) Standard_OVERRIDE; - - //! Destroy object - will release memory if any. - Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE; - - //! Bind this VBO. - Standard_EXPORT virtual void Bind (const Handle(OpenGl_Context)& theGlCtx) const Standard_OVERRIDE; - - //! Unbind this VBO. - Standard_EXPORT virtual void Unbind (const Handle(OpenGl_Context)& theGlCtx) const Standard_OVERRIDE; - -public: //! @name advanced methods - - //! Initialize buffer with existing data. - //! Data will NOT be copied by this method! - Standard_EXPORT bool initLink (const Handle(NCollection_Buffer)& theData, - const GLuint theComponentsNb, - const GLsizei theElemsNb, - const GLenum theDataType); - - //! Initialize buffer with new data (data will be copied). - Standard_EXPORT virtual bool init (const Handle(OpenGl_Context)& theGlCtx, - const GLuint theComponentsNb, - const GLsizei theElemsNb, - const void* theData, - const GLenum theDataType, - const GLsizei theStride) Standard_OVERRIDE; - - //! Update part of the buffer with new data. - Standard_EXPORT virtual bool subData (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theElemFrom, - const GLsizei theElemsNb, - const void* theData, - const GLenum theDataType) Standard_OVERRIDE; - - //! Read back buffer sub-range. - Standard_EXPORT virtual bool getSubData (const Handle(OpenGl_Context)& theGlCtx, - const GLsizei theElemFrom, - const GLsizei theElemsNb, - void* theData, - const GLenum theDataType) Standard_OVERRIDE; - -protected: - - Handle(NCollection_Buffer) myData; //!< buffer data - -public: - - DEFINE_STANDARD_RTTIEXT(OpenGl_VertexBufferCompat,OpenGl_VertexBuffer) // Type definition - -}; - -DEFINE_STANDARD_HANDLE(OpenGl_VertexBufferCompat, OpenGl_VertexBuffer) +typedef OpenGl_BufferCompatT OpenGl_VertexBufferCompat; +typedef OpenGl_BufferCompatT OpenGl_IndexBufferCompat; #endif // _OpenGl_VertexBufferCompat_HeaderFile diff --git a/src/OpenGl/OpenGl_VertexBufferEditor.hxx b/src/OpenGl/OpenGl_VertexBufferEditor.hxx index 22366d8c5d..e46fba30d5 100644 --- a/src/OpenGl/OpenGl_VertexBufferEditor.hxx +++ b/src/OpenGl/OpenGl_VertexBufferEditor.hxx @@ -15,7 +15,7 @@ #ifndef _OpenGl_VertexBufferEditor_H__ #define _OpenGl_VertexBufferEditor_H__ -#include +#include #include #include @@ -39,26 +39,26 @@ class OpenGl_VertexBufferEditor public: //! Creates empty editor - //! theTmpBufferLength - temporary buffer length - OpenGl_VertexBufferEditor (const Standard_Integer theTmpBufferLength = 0) + //! theTmpBufferLength [in] temporary buffer length + explicit OpenGl_VertexBufferEditor (const Standard_Integer theTmpBufferLength = 0) : myElemFrom (0), myElemsNb (0), myTmpBuffer (0, theTmpBufferLength > 0 ? (theTmpBufferLength - 1) : 2047) {} //! Creates empty editor - //! theTmpBuffer - pointer to temporary buffer - //! theTmpBufferLength - temporary buffer length + //! theTmpBuffer [in] pointer to temporary buffer + //! theTmpBufferLength [in] temporary buffer length OpenGl_VertexBufferEditor (theVec_t* theTmpBuffer, const Standard_Integer theTmpBufferLength) : myElemFrom (0), myElemsNb (0), myTmpBuffer (theTmpBuffer[0], 0, theTmpBufferLength - 1) {} - //! Initialize editor for specified VBO. - //! theGlCtx - bound OpenGL context to edit VBO - //! theVbo - VBO to edit - Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx, - const Handle(OpenGl_VertexBuffer)& theVbo) + //! Initialize editor for specified buffer object. + //! theGlCtx [in] bound OpenGL context to edit buffer object + //! theVbo [in] buffer to edit + Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx, + const Handle(OpenGl_Buffer)& theVbo) { myGlCtx = theGlCtx; myVbo = theVbo; @@ -108,15 +108,12 @@ public: } //! @return assigned VBO - inline const Handle(OpenGl_VertexBuffer)& GetVBO() const - { - return myVbo; - } + const Handle(OpenGl_Buffer)& GetVBO() const { return myVbo; } private: Handle(OpenGl_Context) myGlCtx; //!< handle to current OpenGL context - Handle(OpenGl_VertexBuffer) myVbo; //!< edited VBO + Handle(OpenGl_Buffer) myVbo; //!< edited VBO Standard_Integer myElemFrom; //!< element in VBO to upload from Standard_Integer myElemsNb; //!< current element in temporary buffer NCollection_Array1 myTmpBuffer; //!< temporary array diff --git a/src/OpenGl/OpenGl_View.cxx b/src/OpenGl/OpenGl_View.cxx index 2900289ea5..498e46015c 100644 --- a/src/OpenGl/OpenGl_View.cxx +++ b/src/OpenGl/OpenGl_View.cxx @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -67,6 +68,30 @@ namespace { return checkWasFailedFbo (theFboToCheck, theFboRef->GetVPSizeX(), theFboRef->GetVPSizeY(), theFboRef->NbSamples()); } + + //! Chooses compatible internal color format for OIT frame buffer. + static bool chooseOitColorConfiguration (const Handle(OpenGl_Context)& theGlContext, + const Standard_Integer theConfigIndex, + OpenGl_ColorFormats& theFormats) + { + theFormats.Clear(); + switch (theConfigIndex) + { + case 0: // choose best applicable color format combination + { + theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F); + theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_R16F : GL_R32F); + return true; + } + case 1: // choose non-optimal applicable color format combination + { + theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F); + theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F); + return true; + } + } + return false; // color combination does not exist + } } IMPLEMENT_STANDARD_RTTIEXT(OpenGl_View,Graphic3d_CView) @@ -336,6 +361,15 @@ Standard_Boolean OpenGl_View::SetImmediateModeDrawToFront (const Standard_Boolea return aPrevMode; } +// ======================================================================= +// function : Window +// purpose : +// ======================================================================= +Handle(Aspect_Window) OpenGl_View::Window() const +{ + return myWindow->PlatformWindow(); +} + // ======================================================================= // function : SetWindow // purpose : @@ -998,12 +1032,12 @@ bool OpenGl_View::prepareFrameBuffers (Graphic3d_Camera::Projection& theProj) aSizeY = myWindow->Height(); } - const Standard_Integer aRendSizeX = Standard_Integer(myRenderParams.RenderResolutionScale * aSizeX + 0.5f); - const Standard_Integer aRendSizeY = Standard_Integer(myRenderParams.RenderResolutionScale * aSizeY + 0.5f); + const Graphic3d_Vec2i aRendSize (Standard_Integer(myRenderParams.RenderResolutionScale * aSizeX + 0.5f), + Standard_Integer(myRenderParams.RenderResolutionScale * aSizeY + 0.5f)); if (aSizeX < 1 || aSizeY < 1 - || aRendSizeX < 1 - || aRendSizeY < 1) + || aRendSize.x() < 1 + || aRendSize.y() < 1) { myBackBufferRestored = Standard_False; myIsImmediateDrawn = Standard_False; @@ -1011,7 +1045,7 @@ bool OpenGl_View::prepareFrameBuffers (Graphic3d_Camera::Projection& theProj) } // determine multisampling parameters - Standard_Integer aNbSamples = !myToDisableMSAA && aSizeX == aRendSizeX + Standard_Integer aNbSamples = !myToDisableMSAA && aSizeX == aRendSize.x() ? Max (Min (myRenderParams.NbMsaaSamples, aCtx->MaxMsaaSamples()), 0) : 0; if (aNbSamples != 0) @@ -1037,11 +1071,10 @@ bool OpenGl_View::prepareFrameBuffers (Graphic3d_Camera::Projection& theProj) || theProj == Graphic3d_Camera::Projection_Stereo || aNbSamples != 0 || toUseOit - || aSizeX != aRendSizeX)) + || aSizeX != aRendSize.x())) { - if (myMainSceneFbos[0]->GetVPSizeX() != aRendSizeX - || myMainSceneFbos[0]->GetVPSizeY() != aRendSizeY - || myMainSceneFbos[0]->NbSamples() != aNbSamples) + if (myMainSceneFbos[0]->GetVPSize() != aRendSize + || myMainSceneFbos[0]->NbSamples() != aNbSamples) { if (!myTransientDrawToFront) { @@ -1055,8 +1088,8 @@ bool OpenGl_View::prepareFrameBuffers (Graphic3d_Camera::Projection& theProj) // for further blitting and rendering immediate presentations on top if (aCtx->core20fwd != NULL) { - const bool wasFailedMain0 = checkWasFailedFbo (myMainSceneFbos[0], aRendSizeX, aRendSizeY, aNbSamples); - if (!myMainSceneFbos[0]->Init (aCtx, aRendSizeX, aRendSizeY, myFboColorFormat, myFboDepthFormat, aNbSamples) + const bool wasFailedMain0 = checkWasFailedFbo (myMainSceneFbos[0], aRendSize.x(), aRendSize.y(), aNbSamples); + if (!myMainSceneFbos[0]->Init (aCtx, aRendSize, myFboColorFormat, myFboDepthFormat, aNbSamples) && !wasFailedMain0) { TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Main FBO " @@ -1098,9 +1131,9 @@ bool OpenGl_View::prepareFrameBuffers (Graphic3d_Camera::Projection& theProj) && myMainSceneFbos[0]->IsValid()) { if (aNbSamples != 0 - || aSizeX != aRendSizeX) + || aSizeX != aRendSize.x()) { - hasXRBlitFbo = myXrSceneFbo->InitLazy (aCtx, aSizeX, aSizeY, myFboColorFormat, myFboDepthFormat, 0); + hasXRBlitFbo = myXrSceneFbo->InitLazy (aCtx, Graphic3d_Vec2i (aSizeX, aSizeY), myFboColorFormat, myFboDepthFormat, 0); if (!hasXRBlitFbo) { TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! VR FBO " @@ -1273,10 +1306,9 @@ bool OpenGl_View::prepareFrameBuffers (Graphic3d_Camera::Projection& theProj) if (toUseOit && myRenderParams.TransparencyMethod == Graphic3d_RTM_DEPTH_PEELING_OIT) { - if (myDepthPeelingFbos->BlendBackFboOit()->GetSizeX() != aRendSizeX - || myDepthPeelingFbos->BlendBackFboOit()->GetSizeY() != aRendSizeY) + if (myDepthPeelingFbos->BlendBackFboOit()->GetSize() != aRendSize) { - if (myDepthPeelingFbos->BlendBackFboOit()->Init (aCtx, aRendSizeX, aRendSizeY, GL_RGBA16F, 0)) + if (myDepthPeelingFbos->BlendBackFboOit()->Init (aCtx, aRendSize, GL_RGBA16F, 0)) { for (int aPairIter = 0; aPairIter < 2; ++aPairIter) { @@ -1284,7 +1316,7 @@ bool OpenGl_View::prepareFrameBuffers (Graphic3d_Camera::Projection& theProj) aColorFormats.Append (GL_RG32F); aColorFormats.Append (GL_RGBA16F); aColorFormats.Append (GL_RGBA16F); - myDepthPeelingFbos->DepthPeelFbosOit()[aPairIter]->Init (aCtx, aRendSizeX, aRendSizeY, aColorFormats, 0); + myDepthPeelingFbos->DepthPeelFbosOit()[aPairIter]->Init (aCtx, aRendSize, aColorFormats, 0); NCollection_Sequence anAttachments; anAttachments.Append (myDepthPeelingFbos->DepthPeelFbosOit()[aPairIter]->ColorTexture (1)); @@ -1317,9 +1349,8 @@ bool OpenGl_View::prepareFrameBuffers (Graphic3d_Camera::Projection& theProj) Handle(OpenGl_FrameBuffer)& anImmediateSceneFbo = myImmediateSceneFbos [anFboIt]; Handle(OpenGl_FrameBuffer)& anImmediateSceneFboOit = myImmediateSceneFbosOit[anFboIt]; if (aMainSceneFbo->IsValid() - && (aMainSceneFboOit->GetVPSizeX() != aRendSizeX - || aMainSceneFboOit->GetVPSizeY() != aRendSizeY - || aMainSceneFboOit->NbSamples() != aNbSamples)) + && (aMainSceneFboOit->GetVPSize() != aRendSize + || aMainSceneFboOit->NbSamples() != aNbSamples)) { Standard_Integer aColorConfig = 0; for (;;) // seemly responding to driver limitation (GL_FRAMEBUFFER_UNSUPPORTED) @@ -1331,7 +1362,7 @@ bool OpenGl_View::prepareFrameBuffers (Graphic3d_Camera::Projection& theProj) break; } } - if (aMainSceneFboOit->Init (aCtx, aRendSizeX, aRendSizeY, myFboOitColorConfig, aMainSceneFbo->DepthStencilTexture(), aNbSamples)) + if (aMainSceneFboOit->Init (aCtx, aRendSize, myFboOitColorConfig, aMainSceneFbo->DepthStencilTexture(), aNbSamples)) { break; } @@ -1349,11 +1380,10 @@ bool OpenGl_View::prepareFrameBuffers (Graphic3d_Camera::Projection& theProj) } if (anImmediateSceneFbo->IsValid() - && (anImmediateSceneFboOit->GetVPSizeX() != aRendSizeX - || anImmediateSceneFboOit->GetVPSizeY() != aRendSizeY - || anImmediateSceneFboOit->NbSamples() != aNbSamples)) + && (anImmediateSceneFboOit->GetVPSize() != aRendSize + || anImmediateSceneFboOit->NbSamples() != aNbSamples)) { - if (!anImmediateSceneFboOit->Init (aCtx, aRendSizeX, aRendSizeY, myFboOitColorConfig, + if (!anImmediateSceneFboOit->Init (aCtx, aRendSize, myFboOitColorConfig, anImmediateSceneFbo->DepthStencilTexture(), aNbSamples)) { break; @@ -1430,7 +1460,7 @@ bool OpenGl_View::prepareFrameBuffers (Graphic3d_Camera::Projection& theProj) && toUseShadowMap) { OpenGl_ColorFormats aDummy; - if (!aShadowFbo->Init (aCtx, myRenderParams.ShadowMapResolution, myRenderParams.ShadowMapResolution, aDummy, myFboDepthFormat, 0)) + if (!aShadowFbo->Init (aCtx, Graphic3d_Vec2i (myRenderParams.ShadowMapResolution), aDummy, myFboDepthFormat, 0)) { toUseShadowMap = false; } @@ -2306,20 +2336,22 @@ void OpenGl_View::renderStructs (Graphic3d_Camera::Projection theProjection, if (!toRenderGL) { - const Standard_Integer aSizeX = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeX() : myWindow->Width(); - const Standard_Integer aSizeY = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeY() : myWindow->Height(); + const Graphic3d_Vec2i aSizeXY = theReadDrawFbo != NULL + ? theReadDrawFbo->GetVPSize() + : Graphic3d_Vec2i (myWindow->Width(), myWindow->Height()); - toRenderGL = !initRaytraceResources (aSizeX, aSizeY, aCtx) + toRenderGL = !initRaytraceResources (aSizeXY.x(), aSizeXY.y(), aCtx) || !updateRaytraceGeometry (OpenGl_GUM_CHECK, myId, aCtx); toRenderGL |= !myIsRaytraceDataValid; // if no ray-trace data use OpenGL if (!toRenderGL) { - myOpenGlFBO ->InitLazy (aCtx, aSizeX, aSizeY, myFboColorFormat, myFboDepthFormat, 0); - + myOpenGlFBO ->InitLazy (aCtx, aSizeXY, myFboColorFormat, myFboDepthFormat, 0); if (theReadDrawFbo != NULL) + { theReadDrawFbo->UnbindBuffer (aCtx); + } // Prepare preliminary OpenGL output if (aCtx->arbFBOBlit != NULL) @@ -2361,7 +2393,7 @@ void OpenGl_View::renderStructs (Graphic3d_Camera::Projection theProjection, myWorkspace->ResetAppliedAspect(); // Ray-tracing polygonal primitive arrays - raytrace (aSizeX, aSizeY, theProjection, theReadDrawFbo, aCtx); + raytrace (aSizeXY.x(), aSizeXY.y(), theProjection, theReadDrawFbo, aCtx); // Render upper (top and topmost) OpenGL layers myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Upper, theReadDrawFbo, theOitAccumFbo); @@ -2731,8 +2763,8 @@ void OpenGl_View::drawStereoPair (OpenGl_FrameBuffer* theDrawFbo) if (aPair[0]->NbSamples() != 0) { // resolve MSAA buffers before drawing - if (!myOpenGlFBO ->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, myFboDepthFormat, 0) - || !myOpenGlFBO2->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, 0, 0)) + if (!myOpenGlFBO ->InitLazy (aCtx, aPair[0]->GetVPSize(), myFboColorFormat, myFboDepthFormat, 0) + || !myOpenGlFBO2->InitLazy (aCtx, aPair[0]->GetVPSize(), myFboColorFormat, 0, 0)) { aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, @@ -3001,33 +3033,6 @@ Standard_Boolean OpenGl_View::checkOitCompatibility (const Handle(OpenGl_Context return Standard_False; } -// ======================================================================= -// function : chooseOitColorConfiguration -// purpose : -// ======================================================================= -bool OpenGl_View::chooseOitColorConfiguration (const Handle(OpenGl_Context)& theGlContext, - const Standard_Integer theConfigIndex, - OpenGl_ColorFormats& theFormats) -{ - theFormats.Clear(); - switch (theConfigIndex) - { - case 0: // choose best applicable color format combination - { - theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F); - theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_R16F : GL_R32F); - return true; - } - case 1: // choose non-optimal applicable color format combination - { - theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F); - theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F); - return true; - } - } - return false; // color combination does not exist -} - // ======================================================================= // function : checkPBRAvailability // purpose : diff --git a/src/OpenGl/OpenGl_View.hxx b/src/OpenGl/OpenGl_View.hxx index 8fe6df45cf..fde391e89c 100644 --- a/src/OpenGl/OpenGl_View.hxx +++ b/src/OpenGl/OpenGl_View.hxx @@ -16,53 +16,44 @@ #ifndef _OpenGl_View_Header #define _OpenGl_View_Header -#include -#include - -#include -#include -#include - -#include #include #include - #include #include #include #include #include +#include #include #include #include +#include #include -#include -#include #include #include #include #include -#include #include #include -#include -#include #include #include #include class Graphic3d_StructureManager; +class OpenGl_BackgroundArray; class OpenGl_DepthPeeling; class OpenGl_GraphicDriver; class OpenGl_PBREnvironment; +struct OpenGl_RaytraceMaterial; class OpenGl_StateCounter; class OpenGl_ShadowMap; class OpenGl_ShadowMapArray; +class OpenGl_ShaderObject; +class OpenGl_TextureBuffer; class OpenGl_TriangleSet; class OpenGl_Workspace; -class OpenGl_View; DEFINE_STANDARD_HANDLE(OpenGl_View,Graphic3d_CView) @@ -108,8 +99,7 @@ public: const Aspect_RenderingContext theContext) Standard_OVERRIDE; //! Returns window associated with the view. - virtual Handle(Aspect_Window) Window() const Standard_OVERRIDE - { return myWindow->PlatformWindow(); } + Standard_EXPORT virtual Handle(Aspect_Window) Window() const Standard_OVERRIDE; //! Returns True if the window associated to the view is defined. virtual Standard_Boolean IsDefined() const Standard_OVERRIDE @@ -468,11 +458,6 @@ private: bool checkOitCompatibility (const Handle(OpenGl_Context)& theGlContext, const Standard_Boolean theMSAA); - //! Chooses compatible internal color format for OIT frame buffer. - bool chooseOitColorConfiguration (const Handle(OpenGl_Context)& theGlContext, - const Standard_Integer theConfigIndex, - OpenGl_ColorFormats& theFormats); - protected: OpenGl_GraphicDriver* myDriver; @@ -1053,27 +1038,27 @@ protected: //! @name fields related to ray-tracing Handle(OpenGl_ShaderProgram) myOutImageProgram; //! Texture buffer of data records of bottom-level BVH nodes. - Handle(OpenGl_TextureBufferArb) mySceneNodeInfoTexture; + Handle(OpenGl_TextureBuffer) mySceneNodeInfoTexture; //! Texture buffer of minimum points of bottom-level BVH nodes. - Handle(OpenGl_TextureBufferArb) mySceneMinPointTexture; + Handle(OpenGl_TextureBuffer) mySceneMinPointTexture; //! Texture buffer of maximum points of bottom-level BVH nodes. - Handle(OpenGl_TextureBufferArb) mySceneMaxPointTexture; + Handle(OpenGl_TextureBuffer) mySceneMaxPointTexture; //! Texture buffer of transformations of high-level BVH nodes. - Handle(OpenGl_TextureBufferArb) mySceneTransformTexture; + Handle(OpenGl_TextureBuffer) mySceneTransformTexture; //! Texture buffer of vertex coords. - Handle(OpenGl_TextureBufferArb) myGeometryVertexTexture; + Handle(OpenGl_TextureBuffer) myGeometryVertexTexture; //! Texture buffer of vertex normals. - Handle(OpenGl_TextureBufferArb) myGeometryNormalTexture; + Handle(OpenGl_TextureBuffer) myGeometryNormalTexture; //! Texture buffer of vertex UV coords. - Handle(OpenGl_TextureBufferArb) myGeometryTexCrdTexture; + Handle(OpenGl_TextureBuffer) myGeometryTexCrdTexture; //! Texture buffer of triangle indices. - Handle(OpenGl_TextureBufferArb) myGeometryTriangTexture; + Handle(OpenGl_TextureBuffer) myGeometryTriangTexture; //! Texture buffer of material properties. - Handle(OpenGl_TextureBufferArb) myRaytraceMaterialTexture; + Handle(OpenGl_TextureBuffer) myRaytraceMaterialTexture; //! Texture buffer of light source properties. - Handle(OpenGl_TextureBufferArb) myRaytraceLightSrcTexture; + Handle(OpenGl_TextureBuffer) myRaytraceLightSrcTexture; //! 1st framebuffer (FBO) to perform adaptive FSAA. //! Used in compatibility mode (no adaptive sampling). diff --git a/src/OpenGl/OpenGl_View_Raytrace.cxx b/src/OpenGl/OpenGl_View_Raytrace.cxx index 399fb56375..4dcb082063 100644 --- a/src/OpenGl/OpenGl_View_Raytrace.cxx +++ b/src/OpenGl/OpenGl_View_Raytrace.cxx @@ -16,8 +16,13 @@ #include #include +#include +#include #include #include +#include +#include +#include #include #include #include @@ -1885,8 +1890,8 @@ Standard_Boolean OpenGl_View::updateRaytraceBuffers (const Standard_Integer if (myRaytraceParameters.AdaptiveScreenSampling) { Graphic3d_Vec2i aMaxViewport = myTileSampler.OffsetTilesViewportMax().cwiseMax (Graphic3d_Vec2i (theSizeX, theSizeY)); - myRaytraceFBO1[0]->InitLazy (theGlContext, aMaxViewport.x(), aMaxViewport.y(), GL_RGBA32F, myFboDepthFormat); - myRaytraceFBO2[0]->InitLazy (theGlContext, aMaxViewport.x(), aMaxViewport.y(), GL_RGBA32F, myFboDepthFormat); + myRaytraceFBO1[0]->InitLazy (theGlContext, aMaxViewport, GL_RGBA32F, myFboDepthFormat); + myRaytraceFBO2[0]->InitLazy (theGlContext, aMaxViewport, GL_RGBA32F, myFboDepthFormat); if (myRaytraceFBO1[1]->IsValid()) // second FBO not needed { myRaytraceFBO1[1]->Release (theGlContext.operator->()); @@ -1969,8 +1974,8 @@ Standard_Boolean OpenGl_View::updateRaytraceBuffers (const Standard_Integer myAccumFrames = 0; // accumulation should be restarted } - myRaytraceFBO1[aViewIter]->InitLazy (theGlContext, theSizeX, theSizeY, GL_RGBA32F, myFboDepthFormat); - myRaytraceFBO2[aViewIter]->InitLazy (theGlContext, theSizeX, theSizeY, GL_RGBA32F, myFboDepthFormat); + myRaytraceFBO1[aViewIter]->InitLazy (theGlContext, Graphic3d_Vec2i (theSizeX, theSizeY), GL_RGBA32F, myFboDepthFormat); + myRaytraceFBO2[aViewIter]->InitLazy (theGlContext, Graphic3d_Vec2i (theSizeX, theSizeY), GL_RGBA32F, myFboDepthFormat); } } return Standard_True; @@ -2149,10 +2154,10 @@ Standard_Boolean OpenGl_View::uploadRaytraceData (const Handle(OpenGl_Context)& if (mySceneNodeInfoTexture.IsNull()) // create scene BVH buffers { - mySceneNodeInfoTexture = new OpenGl_TextureBufferArb; - mySceneMinPointTexture = new OpenGl_TextureBufferArb; - mySceneMaxPointTexture = new OpenGl_TextureBufferArb; - mySceneTransformTexture = new OpenGl_TextureBufferArb; + mySceneNodeInfoTexture = new OpenGl_TextureBuffer(); + mySceneMinPointTexture = new OpenGl_TextureBuffer(); + mySceneMaxPointTexture = new OpenGl_TextureBuffer(); + mySceneTransformTexture = new OpenGl_TextureBuffer(); if (!mySceneNodeInfoTexture->Create (theGlContext) || !mySceneMinPointTexture->Create (theGlContext) @@ -2166,10 +2171,10 @@ Standard_Boolean OpenGl_View::uploadRaytraceData (const Handle(OpenGl_Context)& if (myGeometryVertexTexture.IsNull()) // create geometry buffers { - myGeometryVertexTexture = new OpenGl_TextureBufferArb; - myGeometryNormalTexture = new OpenGl_TextureBufferArb; - myGeometryTexCrdTexture = new OpenGl_TextureBufferArb; - myGeometryTriangTexture = new OpenGl_TextureBufferArb; + myGeometryVertexTexture = new OpenGl_TextureBuffer(); + myGeometryNormalTexture = new OpenGl_TextureBuffer(); + myGeometryTexCrdTexture = new OpenGl_TextureBuffer(); + myGeometryTriangTexture = new OpenGl_TextureBuffer(); if (!myGeometryVertexTexture->Create (theGlContext) || !myGeometryNormalTexture->Create (theGlContext) @@ -2183,7 +2188,7 @@ Standard_Boolean OpenGl_View::uploadRaytraceData (const Handle(OpenGl_Context)& if (myRaytraceMaterialTexture.IsNull()) // create material buffer { - myRaytraceMaterialTexture = new OpenGl_TextureBufferArb(); + myRaytraceMaterialTexture = new OpenGl_TextureBuffer(); if (!myRaytraceMaterialTexture->Create (theGlContext)) { Message::SendTrace() << "Error: Failed to create buffers for material data"; @@ -2516,7 +2521,7 @@ Standard_Boolean OpenGl_View::updateRaytraceLightSources (const OpenGl_Mat4& the if (myRaytraceLightSrcTexture.IsNull()) // create light source buffer { - myRaytraceLightSrcTexture = new OpenGl_TextureBufferArb; + myRaytraceLightSrcTexture = new OpenGl_TextureBuffer(); } if (myRaytraceGeometry.Sources.size() != 0 && wasUpdated) diff --git a/src/OpenGl/OpenGl_Window.cxx b/src/OpenGl/OpenGl_Window.cxx index ce4b4ce802..f722dfdc53 100644 --- a/src/OpenGl/OpenGl_Window.cxx +++ b/src/OpenGl/OpenGl_Window.cxx @@ -785,7 +785,7 @@ void OpenGl_Window::Init() aDefFbo = new OpenGl_FrameBuffer(); } - if (!aDefFbo->InitWithRB (myGlContext, myWidth, myHeight, GL_RGBA8, GL_DEPTH24_STENCIL8)) + if (!aDefFbo->InitWithRB (myGlContext, Graphic3d_Vec2i (myWidth, myHeight), GL_RGBA8, GL_DEPTH24_STENCIL8)) { TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: default FBO creation failed"); throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString()); diff --git a/src/OpenGl/OpenGl_Window_1.mm b/src/OpenGl/OpenGl_Window_1.mm index 13c4f1f27c..93e0470ac6 100644 --- a/src/OpenGl/OpenGl_Window_1.mm +++ b/src/OpenGl/OpenGl_Window_1.mm @@ -329,7 +329,7 @@ void OpenGl_Window::Init() ::glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &myHeight); ::glBindRenderbuffer (GL_RENDERBUFFER, 0); - if (!aDefFbo->InitWithRB (myGlContext, myWidth, myHeight, GL_RGBA8, GL_DEPTH24_STENCIL8, aWinRBColor)) + if (!aDefFbo->InitWithRB (myGlContext, Graphic3d_Vec2i (myWidth, myHeight), GL_RGBA8, GL_DEPTH24_STENCIL8, aWinRBColor)) { TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: default FBO creation failed"); throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString()); diff --git a/src/OpenGl/OpenGl_Workspace.cxx b/src/OpenGl/OpenGl_Workspace.cxx index f4f877418d..ed5b8a3f52 100644 --- a/src/OpenGl/OpenGl_Workspace.cxx +++ b/src/OpenGl/OpenGl_Workspace.cxx @@ -15,7 +15,6 @@ #include -#include #include #include #include @@ -386,7 +385,7 @@ Handle(OpenGl_FrameBuffer) OpenGl_Workspace::FBOCreate (const Standard_Integer t const Handle(OpenGl_Context)& aCtx = GetGlContext(); aCtx->BindTextures (Handle(OpenGl_TextureSet)(), Handle(OpenGl_ShaderProgram)()); Handle(OpenGl_FrameBuffer) aFrameBuffer = new OpenGl_FrameBuffer(); - if (!aFrameBuffer->Init (aCtx, theWidth, theHeight, GL_SRGB8_ALPHA8, GL_DEPTH24_STENCIL8, 0)) + if (!aFrameBuffer->Init (aCtx, Graphic3d_Vec2i (theWidth, theHeight), GL_SRGB8_ALPHA8, GL_DEPTH24_STENCIL8, 0)) { aFrameBuffer->Release (aCtx.operator->()); return Handle(OpenGl_FrameBuffer)(); diff --git a/src/OpenGl/OpenGl_Workspace.hxx b/src/OpenGl/OpenGl_Workspace.hxx index af0b920d16..8151e46165 100644 --- a/src/OpenGl/OpenGl_Workspace.hxx +++ b/src/OpenGl/OpenGl_Workspace.hxx @@ -17,19 +17,15 @@ #define _OpenGl_Workspace_Header #include +#include #include -#include -#include -#include -#include -#include -#include -#include #include -#include +class OpenGl_FrameBuffer; +class OpenGl_Group; class OpenGl_View; +class OpenGl_Window; class Image_PixMap; class OpenGl_Workspace;