From c9b8ff54a1b86de6c5d2bd156cc859f96cfe9ca7 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 17 May 2014 12:10:01 +0000 Subject: [PATCH] misc: some refactoring in texture handling. --- demos/tutorial/04_texture.cpp | 2 +- demos/tutorial/08_fbo.cpp | 4 +-- demos/tutorial/12_voronoi.cpp | 10 +++---- src/gpu/framebuffer.cpp | 4 +-- src/gpu/shader.cpp | 2 +- src/gpu/texture.cpp | 4 +-- src/lol/gpu/framebuffer.h | 2 +- src/lol/gpu/shader.h | 6 ++--- src/lol/gpu/texture.h | 2 +- src/mesh/mesh.cpp | 2 +- src/sys/file.cpp | 31 ++++++++++++---------- src/tileset.cpp | 4 +-- src/tileset.h | 4 +-- test/btphystest.cpp | 4 +-- test/btphystest.h | 2 +- test/physicobject.h | 50 +++++++++++++++++------------------ 16 files changed, 68 insertions(+), 65 deletions(-) diff --git a/demos/tutorial/04_texture.cpp b/demos/tutorial/04_texture.cpp index 8a6e614c..6b1fe863 100644 --- a/demos/tutorial/04_texture.cpp +++ b/demos/tutorial/04_texture.cpp @@ -94,7 +94,7 @@ public: m_texture->SetData(m_heightmap); m_shader->Bind(); - m_shader->SetUniform(m_texture_uni, m_texture->GetTexture(), 0); + m_shader->SetUniform(m_texture_uni, m_texture->GetTextureUniform(), 0); m_vdecl->SetStream(m_vbo, m_coord); m_vdecl->Bind(); m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6); diff --git a/demos/tutorial/08_fbo.cpp b/demos/tutorial/08_fbo.cpp index 764fd807..e1c55087 100644 --- a/demos/tutorial/08_fbo.cpp +++ b/demos/tutorial/08_fbo.cpp @@ -100,7 +100,7 @@ public: * we cannot expect the render target contents to be preserved. * This code snippet should be moved inside the Framebuffer class. */ m_shader->SetUniform(m_uni_flag, 1.f); - m_shader->SetUniform(m_uni_texture, m_fbo->GetTexture(), 0); + m_shader->SetUniform(m_uni_texture, m_fbo->GetTextureUniform(), 0); m_vdecl->SetStream(m_vbo, m_coord); m_vdecl->Bind(); m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6); @@ -119,7 +119,7 @@ public: m_shader->Bind(); m_shader->SetUniform(m_uni_flag, 1.f); - m_shader->SetUniform(m_uni_texture, m_fbo->GetTexture(), 0); + m_shader->SetUniform(m_uni_texture, m_fbo->GetTextureUniform(), 0); m_vdecl->SetStream(m_vbo, m_coord); m_vdecl->Bind(); m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6); diff --git a/demos/tutorial/12_voronoi.cpp b/demos/tutorial/12_voronoi.cpp index 768e28ae..32c9e68b 100644 --- a/demos/tutorial/12_voronoi.cpp +++ b/demos/tutorial/12_voronoi.cpp @@ -277,7 +277,7 @@ public: m_fbos[f].m2->Bind(); int i = 0; - m_fbos[f].m2->SetUniform(m_fbos[f].m3[i++], src_buf->GetTexture(), 0); //"in_texture" + m_fbos[f].m2->SetUniform(m_fbos[f].m3[i++], src_buf->GetTextureUniform(), 0); //"in_texture" m_fbos[f].m2->SetUniform(m_fbos[f].m3[i++], voronoi_points[j].m1); //"in_source_point" m_fbos[f].m2->SetUniform(m_fbos[f].m3[i++], vec2(512.f, 512.f)); //"in_screen_res" @@ -344,7 +344,7 @@ public: * we cannot expect the render target contents to be preserved. * This code snippet should be moved inside the Framebuffer class. */ //m_fbos[m_cur_fbo].m2->SetUniform(m_uni_flag, 1.f); - //m_fbos[m_cur_fbo].m2->SetUniform(m_uni_texture, m_fbo->GetTexture(), 0); + //m_fbos[m_cur_fbo].m2->SetUniform(m_uni_texture, m_fbo->GetTextureUniform(), 0); //m_vdecl->SetStream(m_vbo, m_fbos[m_cur_fbo].m4.Last()); //m_vdecl->Bind(); //m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6); @@ -353,10 +353,10 @@ public: int i = 0; if (curres == ivec2::zero) - m_screen_shader->SetUniform(m_screen_texture, src_buf->GetTexture(), 0); + m_screen_shader->SetUniform(m_screen_texture, src_buf->GetTextureUniform(), 0); else if (m_cur_fbo == VoronoiFbo) { - shader->SetUniform(m_fbos[m_cur_fbo].m3[i++], src_buf->GetTexture(), 0); //"in_texture" + shader->SetUniform(m_fbos[m_cur_fbo].m3[i++], src_buf->GetTextureUniform(), 0); //"in_texture" shader->SetUniform(m_fbos[m_cur_fbo].m3[i++], ((float)curres.x) / 512.f); //"in_step" shader->SetUniform(m_fbos[m_cur_fbo].m3[i++], vec2(512.f, 512.f)); //"in_screen_res" } @@ -384,7 +384,7 @@ public: //SCREEN DRAW m_screen_shader->Bind(); - m_screen_shader->SetUniform(m_screen_texture, m_fbos[m_cur_fbo].m1->GetTexture(), 0); + m_screen_shader->SetUniform(m_screen_texture, m_fbos[m_cur_fbo].m1->GetTextureUniform(), 0); m_vdecl->SetStream(m_vbo, m_screen_coord); m_vdecl->Bind(); m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6); diff --git a/src/gpu/framebuffer.cpp b/src/gpu/framebuffer.cpp index 8ce9ad71..ad20b93a 100644 --- a/src/gpu/framebuffer.cpp +++ b/src/gpu/framebuffer.cpp @@ -434,9 +434,9 @@ Framebuffer::~Framebuffer() delete m_data; } -ShaderTexture Framebuffer::GetTexture() const +TextureUniform Framebuffer::GetTextureUniform() const { - ShaderTexture ret; + TextureUniform ret; #if defined USE_D3D9 || defined _XBOX ret.m_flags = (uint64_t)(uintptr_t)m_data->m_texture; #else diff --git a/src/gpu/shader.cpp b/src/gpu/shader.cpp index b9614a70..fa1f995e 100644 --- a/src/gpu/shader.cpp +++ b/src/gpu/shader.cpp @@ -664,7 +664,7 @@ void Shader::SetUniform(ShaderUniform const &uni, mat4 const &m) #endif } -void Shader::SetUniform(ShaderUniform const &uni, ShaderTexture tex, int index) +void Shader::SetUniform(ShaderUniform const &uni, TextureUniform tex, int index) { #if defined USE_D3D9 || defined _XBOX data->m_dev->SetTexture(index, (LPDIRECT3DTEXTURE9)tex.m_flags); diff --git a/src/gpu/texture.cpp b/src/gpu/texture.cpp index 5882faf0..0c9628d8 100644 --- a/src/gpu/texture.cpp +++ b/src/gpu/texture.cpp @@ -175,9 +175,9 @@ Texture::Texture(ivec2 size, PixelFormat format) #endif } -ShaderTexture Texture::GetTexture() const +TextureUniform Texture::GetTextureUniform() const { - ShaderTexture ret; + TextureUniform ret; #if defined USE_D3D9 || defined _XBOX ret.m_flags = (uint64_t)(uintptr_t)m_data->m_texture; ret.m_attrib = m_data->m_mag_filter; diff --git a/src/lol/gpu/framebuffer.h b/src/lol/gpu/framebuffer.h index e400cf81..a9935122 100644 --- a/src/lol/gpu/framebuffer.h +++ b/src/lol/gpu/framebuffer.h @@ -101,7 +101,7 @@ public: Framebuffer(ivec2 size, FramebufferFormat fbo_format = FramebufferFormat()); ~Framebuffer(); - ShaderTexture GetTexture() const; + TextureUniform GetTextureUniform() const; ivec2 GetSize() const; void Bind(); diff --git a/src/lol/gpu/shader.h b/src/lol/gpu/shader.h index 51cec1f4..e41e2b54 100644 --- a/src/lol/gpu/shader.h +++ b/src/lol/gpu/shader.h @@ -133,14 +133,14 @@ private: uint64_t m_flags; }; -struct ShaderTexture +struct TextureUniform { friend class Shader; friend class Framebuffer; friend class Texture; public: - inline ShaderTexture() : m_flags(0) {} + inline TextureUniform() : m_flags(0) {} private: uint64_t m_flags; @@ -172,7 +172,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); - void SetUniform(ShaderUniform const &uni, ShaderTexture tex, int index); + void SetUniform(ShaderUniform const &uni, TextureUniform tex, int index); void SetUniform(ShaderUniform const &uni, Array const &v); void SetUniform(ShaderUniform const &uni, Array const &v); diff --git a/src/lol/gpu/texture.h b/src/lol/gpu/texture.h index 82faea5a..906f3a34 100644 --- a/src/lol/gpu/texture.h +++ b/src/lol/gpu/texture.h @@ -102,7 +102,7 @@ public: void SetMinFiltering(TextureMinFilter filter); void GenerateMipmaps(); - ShaderTexture GetTexture() const; + TextureUniform GetTextureUniform() const; private: class TextureData *m_data; diff --git a/src/mesh/mesh.cpp b/src/mesh/mesh.cpp index da8abbbb..601598d6 100644 --- a/src/mesh/mesh.cpp +++ b/src/mesh/mesh.cpp @@ -104,7 +104,7 @@ void SubMesh::Render(Shader* shader) { // TODO: might be good to cache this ShaderUniform u_tex = shader->GetUniformLocation(m_textures[i].m1.C()); - shader->SetUniform(u_tex, m_textures[i].m2->GetTexture(), i); + shader->SetUniform(u_tex, m_textures[i].m2->GetTextureUniform(), i); } m_vdecl->Bind(); diff --git a/src/sys/file.cpp b/src/sys/file.cpp index 99f91fec..ac14e8d4 100644 --- a/src/sys/file.cpp +++ b/src/sys/file.cpp @@ -299,18 +299,20 @@ class DirectoryData DirectoryData() : m_type(StreamType::File) { -#if WIN32 +#if __CELLOS_LV2__ || __ANDROID__ + /* FIXME: not implemented */ +#elif WIN32 m_handle = INVALID_HANDLE_VALUE; #elif HAVE_STDIO_H m_dd = nullptr; -#endif WIN32 +#endif } void Open(String const &directory, FileAccess mode) { m_type = StreamType::File; #if __CELLOS_LV2__ || __ANDROID__ - //NO IMPLEMENTATION + /* FIXME: not implemented */ #elif WIN32 m_directory = directory; String filter = m_directory + String("*"); @@ -330,15 +332,16 @@ class DirectoryData if (IsValid()) { #if __CELLOS_LV2__ || __ANDROID__ - //NO IMPLEMENTATION + /* FIXME: not implemented */ #elif WIN32 FindClose(m_handle); #elif HAVE_STDIO_H closedir(m_dd); #endif } + #if __CELLOS_LV2__ || __ANDROID__ - //NO IMPLEMENTATION + /* FIXME: not implemented */ #elif WIN32 m_handle = INVALID_HANDLE_VALUE; #elif HAVE_STDIO_H @@ -352,7 +355,7 @@ class DirectoryData return false; #if __CELLOS_LV2__ || __ANDROID__ - //NO IMPLEMENTATION + /* FIXME: not implemented */ #elif WIN32 String filter = m_directory + String("*"); filter.Replace('/', '\\', true); @@ -364,7 +367,7 @@ class DirectoryData { if (find_data.cFileName[0] != '.') { - //We have a directory + // We have a directory if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (directories) @@ -380,7 +383,7 @@ class DirectoryData file_valid = FindNextFile(m_handle, &find_data); } #elif HAVE_STDIO_H - //NO IMPLEMENTATION + /* FIXME: not implemented */ #endif return ((files && files->Count()) || (directories && directories->Count())); } @@ -388,7 +391,7 @@ class DirectoryData inline bool IsValid() const { #if __CELLOS_LV2__ || __ANDROID__ - //NO IMPLEMENTATION + /* FIXME: not implemented */ #elif WIN32 return (m_handle != INVALID_HANDLE_VALUE); #elif HAVE_STDIO_H @@ -399,7 +402,7 @@ class DirectoryData } #if __CELLOS_LV2__ || __ANDROID__ - //NO IMPLEMENTATION + /* FIXME: not implemented */ #elif WIN32 HANDLE m_handle; String m_directory; @@ -522,14 +525,14 @@ String Directory::GetCurrent() { String result; #if __CELLOS_LV2__ || __ANDROID__ - //NO IMPLEMENTATION + /* FIXME: not implemented */ #elif WIN32 TCHAR buff[MAX_PATH * 2]; GetCurrentDirectory(MAX_PATH, buff); result = buff; result.Replace('\\', '/', true); #elif HAVE_STDIO_H - //NO IMPLEMENTATION + /* FIXME: not implemented */ #endif return result; } @@ -538,13 +541,13 @@ String Directory::GetCurrent() bool Directory::SetCurrent(String directory) { #if __CELLOS_LV2__ || __ANDROID__ - //NO IMPLEMENTATION + /* FIXME: not implemented */ #elif WIN32 String result = directory; result.Replace('/', '\\', true); return SetCurrentDirectory(result.C()); #elif HAVE_STDIO_H - //NO IMPLEMENTATION + /* FIXME: not implemented */ #endif return false; } diff --git a/src/tileset.cpp b/src/tileset.cpp index 523872d5..5c49f1e5 100644 --- a/src/tileset.cpp +++ b/src/tileset.cpp @@ -180,9 +180,9 @@ ivec2 TileSet::GetTextureSize() const return m_data->m_texture_size; } -ShaderTexture TileSet::GetShaderTexture() const +Texture * TileSet::GetTexture() { - return m_data->m_texture->GetTexture(); + return m_data->m_texture; } Texture const * TileSet::GetTexture() const diff --git a/src/tileset.h b/src/tileset.h index b98558ea..97b59e1b 100644 --- a/src/tileset.h +++ b/src/tileset.h @@ -48,9 +48,9 @@ public: int GetTileCount() const; ivec2 GetTileSize(int tileid) const; - ivec2 GetTextureSize() const; - ShaderTexture GetShaderTexture() const; + Texture * GetTexture(); Texture const * GetTexture() const; + ivec2 GetTextureSize() const; void Bind(); void Unbind(); void BlitTile(uint32_t id, vec3 pos, int o, vec2 scale, float angle, diff --git a/test/btphystest.cpp b/test/btphystest.cpp index 9d973c8d..7fbb1973 100644 --- a/test/btphystest.cpp +++ b/test/btphystest.cpp @@ -585,7 +585,7 @@ void BtPhysTest::TickDraw(float seconds) (1 << VertexUsage::TexCoord) | (1 << VertexUsage::TexCoordExt)), m_cat_shader); - m_cat_sdata->m_shader_texture = m_cat_texture->GetShaderTexture(); + m_cat_sdata->m_tex_uniform = m_cat_texture->GetTexture()->GetTextureUniform(); m_cat_sdata->m_sprite_flip = ((rand(2) == 1)?(1.f):(0.f)) / (float)(NB_SPRITE * PARTICLE_SIZE); PhysObj->SetCustomShaderData(m_cat_sdata); m_cat_sdata = NULL; @@ -708,7 +708,7 @@ void CatShaderData::SetupShaderDatas(mat4 const &model) m_shader->SetUniform(*GetUniform("in_model_view"), modelview); m_shader->SetUniform(*GetUniform("in_normal_mat"), normalmat); m_shader->SetUniform(*GetUniform("in_proj"), proj); - m_shader->SetUniform(*GetUniform("in_texture"), m_shader_texture, 0); + m_shader->SetUniform(*GetUniform("in_texture"), m_tex_uniform, 0); m_shader->SetUniform(*GetUniform("in_sprite_orientation"), m_sprite_orientation); m_shader->SetUniform(*GetUniform("in_sprite_flip"), m_sprite_flip); } diff --git a/test/btphystest.h b/test/btphystest.h index c63cbb53..def68dae 100644 --- a/test/btphystest.h +++ b/test/btphystest.h @@ -24,7 +24,7 @@ public: virtual lol::String GetInColorName() { return lol::String("in_color"); } virtual lol::String GetInTexCoordName() { return lol::String("in_texcoord"); } - ShaderTexture m_shader_texture; + TextureUniform m_tex_uniform; float m_sprite_orientation; float m_sprite_flip; }; diff --git a/test/physicobject.h b/test/physicobject.h index 545d896b..273f86d4 100644 --- a/test/physicobject.h +++ b/test/physicobject.h @@ -34,11 +34,12 @@ class PhysicsObject : public WorldEntity { public: PhysicsObject(Simulation* new_sim, const vec3 &base_location, const quat &base_rotation) - : m_ready(false), m_should_render(true), m_is_character(false), m_custom_shader(0) + : m_custom_shader(0), + m_ready(false), + m_should_render(true), + m_is_character(false), + m_is_phys(false) { -#if CAT_MODE - m_is_phys = false; -#endif //CAT_MODE m_physics = new EasyPhysic(this); m_mesh.Compile("[sc#ddd afcb 60 1 60 -.1]"); @@ -52,11 +53,12 @@ public: } PhysicsObject(Simulation* new_sim, const vec3 &base_location, const quat &base_rotation, int dummy) - : m_ready(false), m_should_render(true), m_is_character(false), m_custom_shader(0) + : m_custom_shader(0), + m_ready(false), + m_should_render(true), + m_is_character(false), + m_is_phys(false) { -#if CAT_MODE - m_is_phys = false; -#endif //CAT_MODE if (dummy == 1) //for platform purpose { m_physics = new EasyPhysic(this); @@ -122,16 +124,16 @@ public: } PhysicsObject(Simulation* new_sim, float base_mass, const vec3 &base_location, int RandValue = -1) - : m_ready(false), m_should_render(true), m_is_character(false), m_custom_shader(0) + : m_custom_shader(0), + m_ready(false), + m_should_render(true), + m_is_character(false), + m_is_phys(false) { Array MeshRand; Array MeshLimit; Array MeshType; -#if CAT_MODE - m_is_phys = true; -#endif //CAT_MODE - MeshLimit << 0; #if USE_BOX @@ -342,18 +344,16 @@ protected: } private: - //Base datas - EasyMesh m_mesh; - EasyPhysic* m_physics; - EasyCharacterController* m_character; - GpuShaderData* m_custom_shader; - - bool m_ready; - bool m_should_render; - bool m_is_character; -#if CAT_MODE - bool m_is_phys; -#endif //CAT_MODE + // Base data + EasyMesh m_mesh; + EasyPhysic* m_physics; + EasyCharacterController* m_character; + GpuShaderData* m_custom_shader; + + bool m_ready; + bool m_should_render; + bool m_is_character; + bool m_is_phys; /* Only used in CAT_MODE */ }; #endif /* __PHYSICOBJECT_H__ */