Skip to content

Commit

Permalink
gsdx-ogl: use DSA for texture management
Browse files Browse the repository at this point in the history
Yeah code is much nicer :)
  • Loading branch information
gregory38 committed Apr 24, 2015
1 parent 6d31d1e commit 672e3f9
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 81 deletions.
2 changes: 0 additions & 2 deletions plugins/GSdx/GLState.cpp
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
1 change: 0 additions & 1 deletion plugins/GSdx/GLState.h
Expand Up @@ -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;
Expand Down
49 changes: 11 additions & 38 deletions plugins/GSdx/GSDeviceOGL.cpp
Expand Up @@ -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
// ****************************************************************
Expand Down Expand Up @@ -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<GSTextureOGL*>(m_date.t)->GetID());
// Get back to the expected active texture unit
gl_ActiveTexture(GL_TEXTURE0 + 3);
gl_BindTextureUnit(5, static_cast<GSTextureOGL*>(m_date.t)->GetID());
#endif

BindDateTexture();
Expand Down Expand Up @@ -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<GSTextureOGL*>(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);
}
Expand Down Expand Up @@ -841,7 +830,7 @@ void GSDeviceOGL::StretchRect(GSTexture* st, const GSVector4& sr, GSTexture* dt,
GLuint64 handle[2] = {static_cast<GSTextureOGL*>(st)->GetHandle(linear ? m_convert.ln : m_convert.pt) , 0};
m_shader->PS_ressources(handle);
} else {
PSSetShaderResource(static_cast<GSTextureOGL*>(st)->GetID());
PSSetShaderResource(0, st);
PSSetSamplerState(linear ? m_convert.ln : m_convert.pt);
}

Expand Down Expand Up @@ -1008,7 +997,7 @@ void GSDeviceOGL::SetupDATE(GSTexture* rt, GSTexture* ds, const GSVertexPT1* ver
GLuint64 handle[2] = {static_cast<GSTextureOGL*>(rt)->GetHandle(m_convert.pt) , 0};
m_shader->PS_ressources(handle);
} else {
PSSetShaderResource(static_cast<GSTextureOGL*>(rt)->GetID());
PSSetShaderResource(0, rt);
PSSetSamplerState(m_convert.pt);
}

Expand Down Expand Up @@ -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<GSTextureOGL*>(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)
Expand Down
4 changes: 2 additions & 2 deletions plugins/GSdx/GSDeviceOGL.h
Expand Up @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions plugins/GSdx/GSRendererOGL.cpp
Expand Up @@ -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<GSTextureOGL*>(tex->m_texture)->GetID(), static_cast<GSTextureOGL*>(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<GSTextureOGL*>(tex->m_texture)->GetID());
dev->PSSetShaderResource(0, tex->m_texture);
}
}
}
Expand Down
39 changes: 7 additions & 32 deletions plugins/GSdx/GSTextureOGL.cpp
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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])
Expand All @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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);
Expand All @@ -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;
}

2 changes: 0 additions & 2 deletions plugins/GSdx/GSTextureOGL.h
Expand Up @@ -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); }

Expand Down

0 comments on commit 672e3f9

Please sign in to comment.