diff --git a/include/Nazara/Renderer/OpenGL.hpp b/include/Nazara/Renderer/OpenGL.hpp index b2e179117..253fa57f9 100644 --- a/include/Nazara/Renderer/OpenGL.hpp +++ b/include/Nazara/Renderer/OpenGL.hpp @@ -77,6 +77,7 @@ class NAZARA_API NzOpenGL static void BindProgram(GLuint id); static void BindTexture(nzImageType type, GLuint id); static void BindTexture(unsigned int textureUnit, nzImageType type, GLuint id); + static void BindTextureUnit(unsigned int textureUnit); static void DeleteBuffer(nzBufferType type, GLuint id); static void DeleteProgram(GLuint id); @@ -85,6 +86,7 @@ class NAZARA_API NzOpenGL static GLuint GetCurrentBuffer(nzBufferType type); static GLuint GetCurrentProgram(); static GLuint GetCurrentTexture(); + static GLuint GetCurrentTexture(unsigned int textureUnit); static NzOpenGLFunc GetEntry(const NzString& entryPoint); static unsigned int GetGLSLVersion(); static NzString GetRendererName(); @@ -98,6 +100,10 @@ class NAZARA_API NzOpenGL static bool IsSupported(nzOpenGLExtension extension); static bool IsSupported(const NzString& string); + static void SetBuffer(nzBufferType type, GLuint id); + static void SetProgram(GLuint id); + static void SetTexture(GLuint id); + static void SetTexture(unsigned int textureUnit, GLuint id); static void SetTextureUnit(unsigned int textureUnit); static bool TranslateFormat(nzPixelFormat pixelFormat, Format* format, FormatType target); diff --git a/src/Nazara/Renderer/HardwareBuffer.cpp b/src/Nazara/Renderer/HardwareBuffer.cpp index a930fcb6d..aedbe55cb 100644 --- a/src/Nazara/Renderer/HardwareBuffer.cpp +++ b/src/Nazara/Renderer/HardwareBuffer.cpp @@ -66,19 +66,6 @@ m_parent(parent) NzHardwareBuffer::~NzHardwareBuffer() = default; -void NzHardwareBuffer::Bind() -{ - #ifdef NAZARA_DEBUG - if (NzContext::GetCurrent() == nullptr) - { - NazaraError("No active context"); - return; - } - #endif - - glBindBuffer(NzOpenGL::BufferTarget[m_type], m_buffer); -} - bool NzHardwareBuffer::Create(unsigned int size, nzBufferUsage usage) { NzContext::EnsureContext(); @@ -178,3 +165,13 @@ bool NzHardwareBuffer::Unmap() return true; } + +void NzHardwareBuffer::Bind() const +{ + NzOpenGL::BindBuffer(m_type, m_buffer); +} + +unsigned int NzHardwareBuffer::GetOpenGLID() const +{ + return m_buffer; +} diff --git a/src/Nazara/Renderer/HardwareBuffer.hpp b/src/Nazara/Renderer/HardwareBuffer.hpp index e7db8ffc2..76cfa9f9d 100644 --- a/src/Nazara/Renderer/HardwareBuffer.hpp +++ b/src/Nazara/Renderer/HardwareBuffer.hpp @@ -17,8 +17,6 @@ class NzHardwareBuffer : public NzAbstractBuffer NzHardwareBuffer(NzBuffer* parent, nzBufferType type); ~NzHardwareBuffer(); - void Bind(); - bool Create(unsigned int size, nzBufferUsage usage = nzBufferUsage_Static); void Destroy(); @@ -29,6 +27,10 @@ class NzHardwareBuffer : public NzAbstractBuffer void* Map(nzBufferAccess access, unsigned int offset = 0, unsigned int size = 0); bool Unmap(); + // Fonctions OpenGL + void Bind() const; + unsigned int GetOpenGLID() const; + private: GLuint m_buffer; nzBufferType m_type; diff --git a/src/Nazara/Renderer/OpenGL.cpp b/src/Nazara/Renderer/OpenGL.cpp index a28550ba4..5d6f2078d 100644 --- a/src/Nazara/Renderer/OpenGL.cpp +++ b/src/Nazara/Renderer/OpenGL.cpp @@ -342,13 +342,22 @@ void NzOpenGL::BindTexture(unsigned int textureUnit, nzImageType type, GLuint id if (s_contextStates->texturesBinding[textureUnit] != id) { - SetTextureUnit(textureUnit); + BindTextureUnit(textureUnit); glBindTexture(TextureTarget[type], id); s_contextStates->texturesBinding[textureUnit] = id; } } +void NzOpenGL::BindTextureUnit(unsigned int textureUnit) +{ + if (s_contextStates->textureUnit != textureUnit) + { + glActiveTexture(GL_TEXTURE0 + textureUnit); + s_contextStates->textureUnit = textureUnit; + } +} + void NzOpenGL::DeleteBuffer(nzBufferType type, GLuint id) { #ifdef NAZARA_DEBUG @@ -424,6 +433,32 @@ GLuint NzOpenGL::GetCurrentProgram() return s_contextStates->currentProgram; } +GLuint NzOpenGL::GetCurrentTexture() +{ + #ifdef NAZARA_DEBUG + if (!s_contextStates) + { + NazaraError("No context activated"); + return 0; + } + #endif + + return s_contextStates->texturesBinding[s_contextStates->textureUnit]; +} + +GLuint NzOpenGL::GetCurrentTexture(unsigned int textureUnit) +{ + #ifdef NAZARA_DEBUG + if (!s_contextStates) + { + NazaraError("No context activated"); + return 0; + } + #endif + + return s_contextStates->texturesBinding[textureUnit]; +} + NzOpenGLFunc NzOpenGL::GetEntry(const NzString& entryPoint) { return LoadEntry(entryPoint.GetConstBuffer(), false); @@ -1046,13 +1081,69 @@ bool NzOpenGL::IsSupported(const NzString& string) return s_openGLextensionSet.find(string) != s_openGLextensionSet.end(); } +void NzOpenGL::SetBuffer(nzBufferType type, GLuint id) +{ + #ifdef NAZARA_DEBUG + if (!s_contextStates) + { + NazaraError("No context activated"); + return; + } + #endif + + s_contextStates->buffersBinding[type] = id; +} + +void NzOpenGL::SetProgram(GLuint id) +{ + #ifdef NAZARA_DEBUG + if (!s_contextStates) + { + NazaraError("No context activated"); + return; + } + #endif + + s_contextStates->currentProgram = id; +} + +void NzOpenGL::SetTexture(GLuint id) +{ + #ifdef NAZARA_DEBUG + if (!s_contextStates) + { + NazaraError("No context activated"); + return; + } + #endif + + s_contextStates->texturesBinding[s_contextStates->textureUnit] = id; +} + +void NzOpenGL::SetTexture(unsigned int textureUnit, GLuint id) +{ + #ifdef NAZARA_DEBUG + if (!s_contextStates) + { + NazaraError("No context activated"); + return; + } + #endif + + s_contextStates->texturesBinding[textureUnit] = id; +} + void NzOpenGL::SetTextureUnit(unsigned int textureUnit) { - if (s_contextStates->textureUnit != textureUnit) + #ifdef NAZARA_DEBUG + if (!s_contextStates) { - glActiveTexture(GL_TEXTURE0 + textureUnit); - s_contextStates->textureUnit = textureUnit; + NazaraError("No context activated"); + return; } + #endif + + s_contextStates->textureUnit = textureUnit; } bool NzOpenGL::TranslateFormat(nzPixelFormat pixelFormat, Format* format, FormatType type) diff --git a/src/Nazara/Renderer/Renderer.cpp b/src/Nazara/Renderer/Renderer.cpp index fde6e69d4..2a450c7f5 100644 --- a/src/Nazara/Renderer/Renderer.cpp +++ b/src/Nazara/Renderer/Renderer.cpp @@ -1258,7 +1258,7 @@ bool NzRenderer::EnsureStateUpdate() if (!unit.textureUpdated) { - NzOpenGL::SetTextureUnit(i); + NzOpenGL::BindTextureUnit(i); unit.texture->Bind(); unit.textureUpdated = true; @@ -1277,7 +1277,7 @@ bool NzRenderer::EnsureStateUpdate() { TextureUnit& unit = s_textureUnits[i]; - NzOpenGL::SetTextureUnit(i); + NzOpenGL::BindTextureUnit(i); unit.texture->Bind(); unit.textureUpdated = true; @@ -1361,7 +1361,7 @@ bool NzRenderer::EnsureStateUpdate() unsigned int stride; NzHardwareBuffer* vertexBufferImpl = static_cast(s_vertexBuffer->GetBuffer()->GetImpl()); - vertexBufferImpl->Bind(); + glBindBuffer(NzOpenGL::BufferTarget[nzBufferType_Vertex], vertexBufferImpl->GetOpenGLID()); bufferOffset = s_vertexBuffer->GetStartOffset(); vertexDeclaration = s_vertexBuffer->GetVertexDeclaration(); @@ -1390,7 +1390,7 @@ bool NzRenderer::EnsureStateUpdate() if (s_instancing) { NzHardwareBuffer* instanceBufferImpl = static_cast(s_instanceBuffer.GetBuffer()->GetImpl()); - instanceBufferImpl->Bind(); + glBindBuffer(NzOpenGL::BufferTarget[nzBufferType_Vertex], instanceBufferImpl->GetOpenGLID()); bufferOffset = s_instanceBuffer.GetStartOffset(); vertexDeclaration = s_instanceBuffer.GetVertexDeclaration(); @@ -1427,7 +1427,7 @@ bool NzRenderer::EnsureStateUpdate() if (s_indexBuffer) { NzHardwareBuffer* indexBufferImpl = static_cast(s_indexBuffer->GetBuffer()->GetImpl()); - indexBufferImpl->Bind(); + glBindBuffer(NzOpenGL::BufferTarget[nzBufferType_Index], indexBufferImpl->GetOpenGLID()); } else glBindBuffer(NzOpenGL::BufferTarget[nzBufferType_Index], 0); @@ -1442,6 +1442,10 @@ bool NzRenderer::EnsureStateUpdate() // En cas de non-support des VAOs, les attributs doivent être respécifiés à chaque frame s_updateFlags &= ~Update_VAO; } + + // On invalide les bindings des buffers (pour éviter des bugs) + NzOpenGL::SetBuffer(nzBufferType_Index, 0); + NzOpenGL::SetBuffer(nzBufferType_Vertex, 0); } #ifdef NAZARA_DEBUG