From 672e3f953390f4ab6d569286e415cd1d2c40d5ff Mon Sep 17 00:00:00 2001 From: Gregory Hainaut Date: Fri, 24 Apr 2015 19:34:17 +0200 Subject: [PATCH] gsdx-ogl: use DSA for texture management Yeah code is much nicer :) --- plugins/GSdx/GLState.cpp | 2 -- plugins/GSdx/GLState.h | 1 - plugins/GSdx/GSDeviceOGL.cpp | 49 ++++++++-------------------------- plugins/GSdx/GSDeviceOGL.h | 4 +-- plugins/GSdx/GSRendererOGL.cpp | 6 ++--- plugins/GSdx/GSTextureOGL.cpp | 39 +++++---------------------- plugins/GSdx/GSTextureOGL.h | 2 -- 7 files changed, 22 insertions(+), 81 deletions(-) diff --git a/plugins/GSdx/GLState.cpp b/plugins/GSdx/GLState.cpp index f3c66592d9..3f2f5e19e4 100644 --- a/plugins/GSdx/GLState.cpp +++ b/plugins/GSdx/GLState.cpp @@ -55,7 +55,6 @@ namespace GLState { GLuint rt = 0; GLuint ds = 0; GLuint tex_unit[2] = {0, 0}; - GLuint tex = 0; GLuint64 tex_handle[2] = { 0, 0}; bool dirty_ressources = false; @@ -109,7 +108,6 @@ namespace GLState { ds = 0; tex_unit[0] = 0; tex_unit[1] = 0; - tex = 0; tex_handle[0] = 0; tex_handle[1] = 0; diff --git a/plugins/GSdx/GLState.h b/plugins/GSdx/GLState.h index 5358480ab7..ed4d775848 100644 --- a/plugins/GSdx/GLState.h +++ b/plugins/GSdx/GLState.h @@ -57,7 +57,6 @@ namespace GLState { extern GLuint rt; // render target extern GLuint ds; // Depth-Stencil extern GLuint tex_unit[2]; // shader input texture - extern GLuint tex; // Generic texture (for tex operation) extern GLuint64 tex_handle[2]; // shader input texture extern GLuint ps; diff --git a/plugins/GSdx/GSDeviceOGL.cpp b/plugins/GSdx/GSDeviceOGL.cpp index ed5189d749..a43345875f 100644 --- a/plugins/GSdx/GSDeviceOGL.cpp +++ b/plugins/GSdx/GSDeviceOGL.cpp @@ -212,13 +212,6 @@ bool GSDeviceOGL::Create(GSWnd* wnd) }; m_va = new GSVertexBufferStateOGL(sizeof(GSVertexPT1), il_convert, countof(il_convert)); - // **************************************************************** - // Texture unit state - // **************************************************************** - // By default use unit 3 for texture modification - // unit 0-2 will allocated to shader input - gl_ActiveTexture(GL_TEXTURE0 + 3); - // **************************************************************** // Pre Generate the different sampler object // **************************************************************** @@ -578,10 +571,7 @@ void GSDeviceOGL::InitPrimDateTexture(int w, int h) ClearRenderTarget_ui(m_date.t, 0x0FFFFFFF); #ifdef ENABLE_OGL_STENCIL_DEBUG - gl_ActiveTexture(GL_TEXTURE0 + 5); - glBindTexture(GL_TEXTURE_2D, static_cast(m_date.t)->GetID()); - // Get back to the expected active texture unit - gl_ActiveTexture(GL_TEXTURE0 + 3); + gl_BindTextureUnit(5, static_cast(m_date.t)->GetID()); #endif BindDateTexture(); @@ -743,8 +733,7 @@ void GSDeviceOGL::CopyRect(GSTexture* st, GSTexture* dt, const GSVector4i& r) gl_FramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, static_cast(st_ogl)->GetID(), 0); glReadBuffer(GL_COLOR_ATTACHMENT0); - dt_ogl->EnableUnit(); - glCopyTexSubImage2D(GL_TEXTURE_2D, 0, r.x, r.y, r.x, r.y, r.width(), r.height()); + gl_CopyTextureSubImage2D(dt_ogl->GetID(), GL_TEX_LEVEL_0, r.x, r.y, r.x, r.y, r.width(), r.height()); gl_BindFramebuffer(GL_READ_FRAMEBUFFER, 0); } @@ -841,7 +830,7 @@ void GSDeviceOGL::StretchRect(GSTexture* st, const GSVector4& sr, GSTexture* dt, GLuint64 handle[2] = {static_cast(st)->GetHandle(linear ? m_convert.ln : m_convert.pt) , 0}; m_shader->PS_ressources(handle); } else { - PSSetShaderResource(static_cast(st)->GetID()); + PSSetShaderResource(0, st); PSSetSamplerState(linear ? m_convert.ln : m_convert.pt); } @@ -1008,7 +997,7 @@ void GSDeviceOGL::SetupDATE(GSTexture* rt, GSTexture* ds, const GSVertexPT1* ver GLuint64 handle[2] = {static_cast(rt)->GetHandle(m_convert.pt) , 0}; m_shader->PS_ressources(handle); } else { - PSSetShaderResource(static_cast(rt)->GetID()); + PSSetShaderResource(0, rt); PSSetSamplerState(m_convert.pt); } @@ -1051,34 +1040,18 @@ void GSDeviceOGL::IASetPrimitiveTopology(GLenum topology) m_va->SetTopology(topology); } -void GSDeviceOGL::PSSetShaderResource(GLuint sr) +void GSDeviceOGL::PSSetShaderResource(int i, GSTexture* sr) { - if (GLState::tex_unit[0] != sr) { - GLState::tex_unit[0] = sr; - - gl_ActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, sr); - - // Get back to the expected active texture unit - gl_ActiveTexture(GL_TEXTURE0 + 3); + GLuint id = static_cast(sr)->GetID(); + if (GLState::tex_unit[i] != id) { + gl_BindTextureUnit(i, id); } } -void GSDeviceOGL::PSSetShaderResources(GLuint tex[2]) +void GSDeviceOGL::PSSetShaderResources(GSTexture* sr0, GSTexture* sr1) { - if (GLState::tex_unit[0] != tex[0] || GLState::tex_unit[1] != tex[1]) { - GLState::tex_unit[0] = tex[0]; - GLState::tex_unit[1] = tex[1]; - - gl_ActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, tex[0]); - - gl_ActiveTexture(GL_TEXTURE0 + 1); - glBindTexture(GL_TEXTURE_2D, tex[1]); - - // Get back to the expected active texture unit - gl_ActiveTexture(GL_TEXTURE0 + 3); - } + PSSetShaderResource(0, sr0); + PSSetShaderResource(1, sr1); } void GSDeviceOGL::PSSetSamplerState(GLuint ss) diff --git a/plugins/GSdx/GSDeviceOGL.h b/plugins/GSdx/GSDeviceOGL.h index 9b113335da..267f48ecbb 100644 --- a/plugins/GSdx/GSDeviceOGL.h +++ b/plugins/GSdx/GSDeviceOGL.h @@ -629,8 +629,8 @@ class GSDeviceOGL : public GSDevice void IASetVertexBuffer(const void* vertices, size_t count); void IASetIndexBuffer(const void* index, size_t count); - void PSSetShaderResource(GLuint sr); - void PSSetShaderResources(GLuint tex[2]); + void PSSetShaderResource(int i, GSTexture* sr); + void PSSetShaderResources(GSTexture* sr0, GSTexture* sr1); void PSSetSamplerState(GLuint ss); void OMSetDepthStencilState(GSDepthStencilOGL* dss, uint8 sref); diff --git a/plugins/GSdx/GSRendererOGL.cpp b/plugins/GSdx/GSRendererOGL.cpp index b9f97888ea..b4040fb41c 100644 --- a/plugins/GSdx/GSRendererOGL.cpp +++ b/plugins/GSdx/GSRendererOGL.cpp @@ -474,11 +474,9 @@ void GSRendererOGL::DrawPrims(GSTexture* rt, GSTexture* ds, GSTextureCache::Sour dev->SetupSampler(ps_ssel); if (tex->m_palette) { - GLuint textures[2] = {static_cast(tex->m_texture)->GetID(), static_cast(tex->m_palette)->GetID()}; - dev->PSSetShaderResources(textures); + dev->PSSetShaderResources(tex->m_texture, tex->m_palette); } else if (tex->m_texture) { - // Only main texture - dev->PSSetShaderResource(static_cast(tex->m_texture)->GetID()); + dev->PSSetShaderResource(0, tex->m_texture); } } } diff --git a/plugins/GSdx/GSTextureOGL.cpp b/plugins/GSdx/GSTextureOGL.cpp index e3f38785cc..0cdf99b4d9 100644 --- a/plugins/GSdx/GSTextureOGL.cpp +++ b/plugins/GSdx/GSTextureOGL.cpp @@ -225,7 +225,7 @@ GSTextureOGL::GSTextureOGL(int type, int w, int h, int format, GLuint fbo_read) case GSTexture::Texture: case GSTexture::RenderTarget: case GSTexture::DepthStencil: - glGenTextures(1, &m_texture_id); + gl_CreateTextures(GL_TEXTURE_2D, 1, &m_texture_id); break; case GSTexture::Backbuffer: break; @@ -249,8 +249,7 @@ GSTextureOGL::GSTextureOGL(int type, int w, int h, int format, GLuint fbo_read) case GSTexture::DepthStencil: case GSTexture::RenderTarget: case GSTexture::Texture: - EnableUnit(); - gl_TexStorage2D(GL_TEXTURE_2D, 1, m_format, m_size.x, m_size.y); + gl_TextureStorage2D(m_texture_id, 1+GL_TEX_LEVEL_0, m_format, m_size.x, m_size.y); break; default: break; } @@ -264,8 +263,6 @@ GSTextureOGL::~GSTextureOGL() GLState::rt = 0; if (m_texture_id == GLState::ds) GLState::ds = 0; - if (m_texture_id == GLState::tex) - GLState::tex = 0; if (m_texture_id == GLState::tex_unit[0]) GLState::tex_unit[0] = 0; if (m_texture_id == GLState::tex_unit[1]) @@ -279,8 +276,6 @@ bool GSTextureOGL::Update(const GSVector4i& r, const void* data, int pitch) { ASSERT(m_type != GSTexture::DepthStencil && m_type != GSTexture::Offscreen); - EnableUnit(); - // Note: reduce noise for gl retracers // It might introduce bug after an emulator pause so always set it in standard mode if (GLLoader::in_replayer) { @@ -317,7 +312,7 @@ bool GSTextureOGL::Update(const GSVector4i& r, const void* data, int pitch) } else { glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch >> m_int_shift); } - glTexSubImage2D(GL_TEXTURE_2D, 0, r.x, r.y, r.width(), r.height(), m_int_format, m_int_type, (const void*)PboPool::Offset()); + gl_TextureSubImage2D(m_texture_id, GL_TEX_LEVEL_0, r.x, r.y, r.width(), r.height(), m_int_format, m_int_type, (const void*)PboPool::Offset()); // Normally only affect TexSubImage call. (i.e. only the previous line) //glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); @@ -356,19 +351,9 @@ GLuint64 GSTextureOGL::GetHandle(GLuint sampler_id) return m_handles[sampler_id]; } -void GSTextureOGL::EnableUnit() -{ - /* Not a real texture */ - ASSERT(!IsBackbuffer()); - - if (GLState::tex != m_texture_id) { - GLState::tex = m_texture_id; - glBindTexture(GL_TEXTURE_2D, m_texture_id); - } -} - bool GSTextureOGL::Map(GSMap& m, const GSVector4i* r) { + // LOTS OF CRAP CODE!!!! PLEASE FIX ME !!! if (m_type != GSTexture::Offscreen) return false; // The function allow to modify the texture from the CPU @@ -380,7 +365,6 @@ bool GSTextureOGL::Map(GSMap& m, const GSVector4i* r) // Can be used on GL_PIXEL_UNPACK_BUFFER or GL_TEXTURE_BUFFER // Bind the texture to the read framebuffer to avoid any disturbance - EnableUnit(); gl_BindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo_read); gl_FramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_texture_id, 0); glReadBuffer(GL_COLOR_ATTACHMENT0); @@ -565,7 +549,8 @@ bool GSTextureOGL::Save(const string& fn, bool dds) { // Collect the texture data uint32 pitch = 4 * m_size.x; - char* image = (char*)malloc(pitch * m_size.y); + uint32 buf_size = pitch * m_size.y * 2;// Note *2 for security (depth/stencil) + char* image = (char*)malloc(buf_size); bool status = true; // FIXME instead of swapping manually B and R maybe you can request the driver to do it @@ -582,11 +567,8 @@ bool GSTextureOGL::Save(const string& fn, bool dds) gl_BindFramebuffer(GL_READ_FRAMEBUFFER, 0); } else if(m_format == GL_R32I) { - gl_ActiveTexture(GL_TEXTURE0 + 6); - glBindTexture(GL_TEXTURE_2D, m_texture_id); - #ifndef ENABLE_GLES - glGetTexImage(GL_TEXTURE_2D, 0, GL_RED_INTEGER, GL_INT, image); + gl_GetTextureImage(m_texture_id, 0, GL_RED_INTEGER, GL_INT, buf_size, image); SaveRaw(fn, image, pitch); #endif @@ -596,9 +578,6 @@ bool GSTextureOGL::Save(const string& fn, bool dds) } else { gl_BindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo_read); - gl_ActiveTexture(GL_TEXTURE0 + 6); - glBindTexture(GL_TEXTURE_2D, m_texture_id); - gl_FramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_texture_id, 0); glReadBuffer(GL_COLOR_ATTACHMENT0); @@ -623,10 +602,6 @@ bool GSTextureOGL::Save(const string& fn, bool dds) if (status) Save(fn, image, pitch); free(image); - // Restore state - gl_ActiveTexture(GL_TEXTURE0 + 3); - glBindTexture(GL_TEXTURE_2D, GLState::tex); - return status; } diff --git a/plugins/GSdx/GSTextureOGL.h b/plugins/GSdx/GSTextureOGL.h index 864c067c0b..1fe69b49e1 100644 --- a/plugins/GSdx/GSTextureOGL.h +++ b/plugins/GSdx/GSTextureOGL.h @@ -69,8 +69,6 @@ class GSTextureOGL : public GSTexture void Save(const string& fn, const void* image, uint32 pitch); void SaveRaw(const string& fn, const void* image, uint32 pitch); - void EnableUnit(); - bool IsBackbuffer() { return (m_type == GSTexture::Backbuffer); } bool IsDss() { return (m_type == GSTexture::DepthStencil); }