From 7d5fe8a7c63985a9f8e886c3a2ef798dd70e88c2 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 18 Aug 2012 19:26:03 +0000 Subject: [PATCH] gpu: create the ShaderTexture class, replace Shader::SetTexture with a Shader::SetUniform override, and implement most of the FBO code for the Direct3D backend. --- src/gpu/framebuffer.cpp | 26 ++++++++++++++++++++++---- src/gpu/framebuffer.h | 4 +--- src/gpu/shader.cpp | 6 +++--- src/gpu/shader.h | 17 +++++++++++++---- tutorial/08_fbo.cpp | 2 +- 5 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/gpu/framebuffer.cpp b/src/gpu/framebuffer.cpp index 7673adb0..816d710a 100644 --- a/src/gpu/framebuffer.cpp +++ b/src/gpu/framebuffer.cpp @@ -44,6 +44,8 @@ class FrameBufferData ivec2 m_size; #if defined USE_D3D9 + LPDIRECT3DTEXTURE9 m_texture; + LPDIRECT3DSURFACE9 m_surface, m_back_surface; #elif defined _XBOX #else GLuint m_fbo, m_texture, m_depth; @@ -60,7 +62,13 @@ FrameBuffer::FrameBuffer(ivec2 size) { m_data->m_size = size; #if defined USE_D3D9 || defined _XBOX - /* FIXME: not implemented on Direct3D */ + if (FAILED(g_d3ddevice->CreateTexture(size.x, size.y, 1, + D3DUSAGE_RENDERTARGET, + D3DFMT_R8G8B8, D3DPOOL_DEFAULT, + &m_data->m_texture, NULL))) + Abort(); + if (FAILED(m_data->m_texture->GetSurfaceLevel(0, &m_data->m_surface))) + Abort(); #else # if GL_VERSION_1_1 GLenum internal_format = GL_RGBA8; @@ -124,6 +132,8 @@ FrameBuffer::FrameBuffer(ivec2 size) FrameBuffer::~FrameBuffer() { #if defined USE_D3D9 || defined _XBOX + m_data->m_surface->Release(); + m_data->m_texture->Release(); #else # if GL_VERSION_1_1 || GL_ES_VERSION_2_0 glDeleteFramebuffers(1, &m_data->m_fbo); @@ -139,18 +149,24 @@ FrameBuffer::~FrameBuffer() delete m_data; } -int FrameBuffer::GetTexture() const +ShaderTexture FrameBuffer::GetTexture() const { + ShaderTexture ret; #if defined USE_D3D9 || defined _XBOX - return 0; + ret.m_flags = (uint64_t)(uintptr_t)m_data->m_texture; #else - return m_data->m_texture; + ret.m_flags = m_data->m_texture; #endif + return ret; } void FrameBuffer::Bind() { #if defined USE_D3D9 || defined _XBOX + if (FAILED(g_d3ddevice->GetRenderTarget(0, &m_data->m_back_surface))) + Abort(); + if (FAILED(g_d3ddevice->SetRenderTarget(0, m_data->m_surface))) + Abort(); #else # if GL_VERSION_1_1 || GL_ES_VERSION_2_0 glBindFramebuffer(GL_FRAMEBUFFER, m_data->m_fbo); @@ -163,6 +179,8 @@ void FrameBuffer::Bind() void FrameBuffer::Unbind() { #if defined USE_D3D9 || defined _XBOX + if (FAILED(g_d3ddevice->SetRenderTarget(0, m_data->m_back_surface))) + Abort(); #else # if GL_VERSION_1_1 || GL_ES_VERSION_2_0 glBindFramebuffer(GL_FRAMEBUFFER, NULL); diff --git a/src/gpu/framebuffer.h b/src/gpu/framebuffer.h index 0c39fcbb..bab1b95b 100644 --- a/src/gpu/framebuffer.h +++ b/src/gpu/framebuffer.h @@ -27,9 +27,7 @@ public: FrameBuffer(ivec2 size); ~FrameBuffer(); - int GetTexture() const; - void Clear(vec4 color); - void Clear(vec4 color, float depth); + ShaderTexture GetTexture() const; void Bind(); void Unbind(); diff --git a/src/gpu/shader.cpp b/src/gpu/shader.cpp index df6121e7..1bd36de8 100644 --- a/src/gpu/shader.cpp +++ b/src/gpu/shader.cpp @@ -536,14 +536,14 @@ void Shader::SetUniform(ShaderUniform const &uni, mat4 const &m) #endif } -void Shader::SetTexture(ShaderUniform const &uni, int id, int index) +void Shader::SetUniform(ShaderUniform const &uni, ShaderTexture tex, int index) { #if defined USE_D3D9 || defined _XBOX - /* FIXME: unimplemented */ + g_d3ddevice->SetTexture(index, (LPDIRECT3DTEXTURE9)tex.m_flags); #elif !defined __CELLOS_LV2__ glActiveTexture(GL_TEXTURE0 + index); //glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, id); + glBindTexture(GL_TEXTURE_2D, (int)tex.m_flags); SetUniform(uni, index); #else /* FIXME: unimplemented */ diff --git a/src/gpu/shader.h b/src/gpu/shader.h index 03ef8127..46457363 100644 --- a/src/gpu/shader.h +++ b/src/gpu/shader.h @@ -46,6 +46,18 @@ private: uint64_t m_flags; }; +struct ShaderTexture +{ + friend class Shader; + friend class FrameBuffer; + +public: + inline ShaderTexture() : m_flags(0) {} + +private: + uint64_t m_flags; +}; + class ShaderData; class Shader @@ -70,10 +82,7 @@ public: void SetUniform(ShaderUniform const &uni, mat2 const &m); void SetUniform(ShaderUniform const &uni, mat3 const &m); void SetUniform(ShaderUniform const &uni, mat4 const &m); - - /* FIXME: this should be called SetUniform, too, but we need a new - * type to represent textures. */ - void SetTexture(ShaderUniform const &uni, int id, int index); + void SetUniform(ShaderUniform const &uni, ShaderTexture tex, int index); void Bind() const; void Unbind() const; diff --git a/tutorial/08_fbo.cpp b/tutorial/08_fbo.cpp index 755776d7..e1afe7c9 100644 --- a/tutorial/08_fbo.cpp +++ b/tutorial/08_fbo.cpp @@ -102,7 +102,7 @@ public: m_shader->Bind(); m_shader->SetUniform(m_uni_flag, 1.f); - m_shader->SetTexture(m_uni_texture, m_fbo->GetTexture(), 0); + m_shader->SetUniform(m_uni_texture, m_fbo->GetTexture(), 0); m_vdecl->SetStream(m_vbo, m_coord); m_vdecl->Bind(); m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 2);