renderer_opengl: Add OGLRenderbuffer to resource/state management.

This commit is contained in:
bunnei 2020-02-13 22:49:15 -05:00
parent 0c82b00dfd
commit add2c38b73
4 changed files with 62 additions and 0 deletions

View file

@ -15,6 +15,24 @@ MICROPROFILE_DEFINE(OpenGL_ResourceDeletion, "OpenGL", "Resource Deletion", MP_R
namespace OpenGL { namespace OpenGL {
void OGLRenderbuffer::Create() {
if (handle != 0)
return;
MICROPROFILE_SCOPE(OpenGL_ResourceCreation);
glGenRenderbuffers(1, &handle);
}
void OGLRenderbuffer::Release() {
if (handle == 0)
return;
MICROPROFILE_SCOPE(OpenGL_ResourceDeletion);
glDeleteRenderbuffers(1, &handle);
OpenGLState::GetCurState().ResetRenderbuffer(handle).Apply();
handle = 0;
}
void OGLTexture::Create(GLenum target) { void OGLTexture::Create(GLenum target) {
if (handle != 0) if (handle != 0)
return; return;

View file

@ -11,6 +11,31 @@
namespace OpenGL { namespace OpenGL {
class OGLRenderbuffer : private NonCopyable {
public:
OGLRenderbuffer() = default;
OGLRenderbuffer(OGLRenderbuffer&& o) noexcept : handle(std::exchange(o.handle, 0)) {}
~OGLRenderbuffer() {
Release();
}
OGLRenderbuffer& operator=(OGLRenderbuffer&& o) noexcept {
Release();
handle = std::exchange(o.handle, 0);
return *this;
}
/// Creates a new internal OpenGL resource and stores the handle
void Create();
/// Deletes the internal OpenGL resource
void Release();
GLuint handle = 0;
};
class OGLTexture : private NonCopyable { class OGLTexture : private NonCopyable {
public: public:
OGLTexture() = default; OGLTexture() = default;

View file

@ -423,6 +423,13 @@ void OpenGLState::ApplyClipControl() {
} }
} }
void OpenGLState::ApplyRenderBuffer() {
if (cur_state.renderbuffer != renderbuffer) {
cur_state.renderbuffer = renderbuffer;
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
}
}
void OpenGLState::ApplyTextures() { void OpenGLState::ApplyTextures() {
const std::size_t size = std::size(textures); const std::size_t size = std::size(textures);
for (std::size_t i = 0; i < size; ++i) { for (std::size_t i = 0; i < size; ++i) {
@ -478,6 +485,7 @@ void OpenGLState::Apply() {
ApplyPolygonOffset(); ApplyPolygonOffset();
ApplyAlphaTest(); ApplyAlphaTest();
ApplyClipControl(); ApplyClipControl();
ApplyRenderBuffer();
} }
void OpenGLState::EmulateViewportWithScissor() { void OpenGLState::EmulateViewportWithScissor() {
@ -551,4 +559,11 @@ OpenGLState& OpenGLState::ResetFramebuffer(GLuint handle) {
return *this; return *this;
} }
OpenGLState& OpenGLState::ResetRenderbuffer(GLuint handle) {
if (renderbuffer == handle) {
renderbuffer = 0;
}
return *this;
}
} // namespace OpenGL } // namespace OpenGL

View file

@ -158,6 +158,8 @@ public:
GLenum depth_mode = GL_NEGATIVE_ONE_TO_ONE; GLenum depth_mode = GL_NEGATIVE_ONE_TO_ONE;
} clip_control; } clip_control;
GLuint renderbuffer{}; // GL_RENDERBUFFER_BINDING
OpenGLState(); OpenGLState();
/// Get the currently active OpenGL state /// Get the currently active OpenGL state
@ -196,6 +198,7 @@ public:
void ApplyPolygonOffset(); void ApplyPolygonOffset();
void ApplyAlphaTest(); void ApplyAlphaTest();
void ApplyClipControl(); void ApplyClipControl();
void ApplyRenderBuffer();
/// Resets any references to the given resource /// Resets any references to the given resource
OpenGLState& UnbindTexture(GLuint handle); OpenGLState& UnbindTexture(GLuint handle);
@ -204,6 +207,7 @@ public:
OpenGLState& ResetPipeline(GLuint handle); OpenGLState& ResetPipeline(GLuint handle);
OpenGLState& ResetVertexArray(GLuint handle); OpenGLState& ResetVertexArray(GLuint handle);
OpenGLState& ResetFramebuffer(GLuint handle); OpenGLState& ResetFramebuffer(GLuint handle);
OpenGLState& ResetRenderbuffer(GLuint handle);
/// Viewport does not affects glClearBuffer so emulate viewport using scissor test /// Viewport does not affects glClearBuffer so emulate viewport using scissor test
void EmulateViewportWithScissor(); void EmulateViewportWithScissor();