From fb51881fab11a89f527c808d47e8912411f54188 Mon Sep 17 00:00:00 2001 From: lanariel Date: Thu, 5 Dec 2013 14:56:34 +0100 Subject: [PATCH] Fixed Mem leaks on gpu and start on deffered pipeline --- Code/Misc/Resource/OResourceHandler.cpp | 9 ++-- Code/OysterGraphics/Core/ShaderManager.cpp | 2 +- Code/OysterGraphics/DllInterfaces/GFXAPI.cpp | 45 ++++++++++++++----- Code/OysterGraphics/DllInterfaces/GFXAPI.h | 11 +++-- .../OysterGraphics/FileLoader/GeneralLoader.h | 3 +- .../{TextureLoader.cpp => ModelLoader.cpp} | 25 +++++++++++ .../FileLoader/ShaderLoader.cpp | 2 + Code/OysterGraphics/OysterGraphics.vcxproj | 4 +- .../Render/Rendering/BasicRender.cpp | 2 + .../Render/Resources/Resources.cpp | 3 +- .../Deffered Shaders/Render/Defines.hlsli | 32 +++++++++++-- .../Deffered Shaders/Render/LightCalc.hlsli | 23 ++++++++++ .../Deffered Shaders/Render/LightPass.hlsl | 7 ++- .../Render/PosManipulation.hlsli | 21 +++++++++ Code/Tester/MainTest.cpp | 10 +++-- Code/Tester/Tester.vcxproj | 6 +-- 16 files changed, 171 insertions(+), 34 deletions(-) rename Code/OysterGraphics/FileLoader/{TextureLoader.cpp => ModelLoader.cpp} (97%) create mode 100644 Code/OysterGraphics/Shader/HLSL/Deffered Shaders/Render/LightCalc.hlsli create mode 100644 Code/OysterGraphics/Shader/HLSL/Deffered Shaders/Render/PosManipulation.hlsli diff --git a/Code/Misc/Resource/OResourceHandler.cpp b/Code/Misc/Resource/OResourceHandler.cpp index f86c434c..918ecd1d 100644 --- a/Code/Misc/Resource/OResourceHandler.cpp +++ b/Code/Misc/Resource/OResourceHandler.cpp @@ -120,11 +120,12 @@ void OysterResource::Clean() //Remove all the references while (!OResource::Release(i->second)); - const wchar_t* temp = i->second->GetResourceFilename(); + std::wstring temp = i->second->GetResourceFilename(); delete resourcePrivate.resources[temp]; - resourcePrivate.resources.erase(temp); + } + resourcePrivate.resources.clear(); } void OysterResource::ReleaseResource(const OHRESOURCE& resourceData) { @@ -133,7 +134,7 @@ void OysterResource::ReleaseResource(const OHRESOURCE& resourceData) { if(OResource::Release(t)) { - const wchar_t* temp = t->GetResourceFilename(); + std::wstring temp = t->GetResourceFilename(); delete resourcePrivate.resources[temp]; resourcePrivate.resources.erase(temp); } @@ -146,7 +147,7 @@ void OysterResource::ReleaseResource(const wchar_t filename[]) { if(OResource::Release(t)) { - const wchar_t* temp = t->GetResourceFilename(); + std::wstring temp = t->GetResourceFilename(); delete resourcePrivate.resources[temp]; resourcePrivate.resources.erase(temp); } diff --git a/Code/OysterGraphics/Core/ShaderManager.cpp b/Code/OysterGraphics/Core/ShaderManager.cpp index 94f7fd57..c4f1b38e 100644 --- a/Code/OysterGraphics/Core/ShaderManager.cpp +++ b/Code/OysterGraphics/Core/ShaderManager.cpp @@ -279,7 +279,7 @@ namespace Oyster void Core::ShaderManager::Clean() { - for(int i = 0; i < VData.size(); ++i) + for(int i = 0; i < (int)VData.size(); ++i) { delete[] VData[i].data; } diff --git a/Code/OysterGraphics/DllInterfaces/GFXAPI.cpp b/Code/OysterGraphics/DllInterfaces/GFXAPI.cpp index 86df58a2..fcb3481c 100644 --- a/Code/OysterGraphics/DllInterfaces/GFXAPI.cpp +++ b/Code/OysterGraphics/DllInterfaces/GFXAPI.cpp @@ -4,11 +4,18 @@ #include "../Render/Rendering/Render.h" #include "../FileLoader/ObjReader.h" #include "../../Misc/Resource/OysterResource.h" +#include "../FileLoader/GeneralLoader.h" namespace Oyster { namespace Graphics { + namespace + { + Math::Float4x4 View; + Math::Float4x4 Projection; + } + API::State API::Init(HWND Window, bool MSAA_Quality, bool Fullscreen, Math::Float2 resulotion) { Core::resolution = resulotion; @@ -26,16 +33,31 @@ namespace Oyster return API::Sucsess; } - void API::NewFrame(Oyster::Math::Float4x4 View, Oyster::Math::Float4x4 Projection) + void API::SetProjection(Math::Float4x4& projection) + { + Projection = projection; + } + + void API::SetView(Math::Float4x4& view) + { + View = view; + } + + void API::NewFrame() { Render::Rendering::Basic::NewFrame(View, Projection); } - void API::RenderScene(Model::Model* models, int count) + void API::RenderScene(Model::Model models[], int count) { Render::Rendering::Basic::RenderScene(models,count); } + void API::RenderModel(Model::Model& m) + { + Render::Rendering::Basic::RenderScene(&m,1); + } + void API::EndFrame() { Render::Rendering::Basic::EndFrame(); @@ -52,9 +74,7 @@ namespace Oyster m->WorldMatrix = Oyster::Math::Float4x4::identity; m->Visible = true; - OBJReader or; - or.readOBJFile(filename); - m->info = or.toModel(); + m->info = Oyster::Resource::OysterResource::LoadResource(filename.c_str(),Oyster::Graphics::Loading::LoadOBJ); return m; } @@ -63,12 +83,7 @@ namespace Oyster { Model::ModelInfo* info = (Model::ModelInfo*)model->info; delete model; - SAFE_DELETE(info->Vertices); - if(info->Indexed) - { - SAFE_DELETE(info->Indecies); - } - delete info; + Oyster::Resource::OysterResource::ReleaseResource((Oyster::Resource::OHRESOURCE)info); } void API::Clean() @@ -77,6 +92,14 @@ namespace Oyster Oyster::Resource::OysterResource::Clean(); Oyster::Graphics::Core::ShaderManager::Clean(); Oyster::Graphics::Render::Resources::Clean(); + + SAFE_RELEASE(Core::depthStencil); + SAFE_RELEASE(Core::backBufferRTV); + SAFE_RELEASE(Core::backBufferUAV); + + SAFE_RELEASE(Core::swapChain); + SAFE_RELEASE(Core::deviceContext); + SAFE_RELEASE(Core::device); } } } \ No newline at end of file diff --git a/Code/OysterGraphics/DllInterfaces/GFXAPI.h b/Code/OysterGraphics/DllInterfaces/GFXAPI.h index f7665ba6..f4c38076 100644 --- a/Code/OysterGraphics/DllInterfaces/GFXAPI.h +++ b/Code/OysterGraphics/DllInterfaces/GFXAPI.h @@ -28,9 +28,14 @@ namespace Oyster static State Init(HWND Window, bool MSAA_Quality, bool Fullscreen, Oyster::Math::Float2 StartResulotion); static void Clean(); - //! @brief from Oyster::Math Float4x4, expects corect methods - static void NewFrame(Oyster::Math::Float4x4 View, Oyster::Math::Float4x4 Projection); - static void RenderScene(Oyster::Graphics::Model::Model* models, int count); + + static void SetView(Oyster::Math::Float4x4& View); + static void SetProjection(Oyster::Math::Float4x4& Projection); + + //! @brief will internally use last values from SetView and SetProjection + static void NewFrame(); + static void RenderScene(Oyster::Graphics::Model::Model models[], int count); + static void RenderModel(Oyster::Graphics::Model::Model& model); static void EndFrame(); static Oyster::Graphics::Model::Model* CreateModel(std::wstring filename); diff --git a/Code/OysterGraphics/FileLoader/GeneralLoader.h b/Code/OysterGraphics/FileLoader/GeneralLoader.h index fcce1e02..4eab570b 100644 --- a/Code/OysterGraphics/FileLoader/GeneralLoader.h +++ b/Code/OysterGraphics/FileLoader/GeneralLoader.h @@ -27,7 +27,8 @@ namespace Oyster void UnloadShaderD(void* loadedData); void LoadShaderD(const wchar_t filename[], Oyster::Resource::CustomData& out); - void LoadShader(const wchar_t filename[], Oyster::Resource::CustomData& out, int type); + void UnloadOBJ(void* loadedData); + void LoadOBJ(const wchar_t filename[], Oyster::Resource::CustomData& out); } } } \ No newline at end of file diff --git a/Code/OysterGraphics/FileLoader/TextureLoader.cpp b/Code/OysterGraphics/FileLoader/ModelLoader.cpp similarity index 97% rename from Code/OysterGraphics/FileLoader/TextureLoader.cpp rename to Code/OysterGraphics/FileLoader/ModelLoader.cpp index 1c6ba263..0fe623a9 100644 --- a/Code/OysterGraphics/FileLoader/TextureLoader.cpp +++ b/Code/OysterGraphics/FileLoader/ModelLoader.cpp @@ -1,6 +1,7 @@ #include "GeneralLoader.h" #include "..\Core\Dx11Includes.h" #include "..\Core\Core.h" +#include "ObjReader.h" HRESULT CreateWICTextureFromFileEx( ID3D11Device* d3dDevice, ID3D11DeviceContext* d3dContext, @@ -35,6 +36,30 @@ void Oyster::Graphics::Loading::UnloadTexture(void* data) SAFE_RELEASE(srv); } +void Oyster::Graphics::Loading::LoadOBJ(const wchar_t filename[], Oyster::Resource::CustomData& out) +{ + OBJReader obj; + obj.readOBJFile(filename); + Model::ModelInfo* info; + info = obj.toModel(); + out.loadedData = info; + out.resourceUnloadFnc = Oyster::Graphics::Loading::UnloadOBJ; +} + +void Oyster::Graphics::Loading::UnloadOBJ(void* data) +{ + Model::ModelInfo* info = (Model::ModelInfo*) data; + SAFE_DELETE(info->Vertices); + if(info->Indexed) + { + SAFE_DELETE(info->Indecies); + } + for(int i =0;iMaterial.size();++i) + { + Oyster::Resource::OysterResource::ReleaseResource(info->Material[i]); + } + delete info; +} #include #include diff --git a/Code/OysterGraphics/FileLoader/ShaderLoader.cpp b/Code/OysterGraphics/FileLoader/ShaderLoader.cpp index 05420abf..b3c8a0b7 100644 --- a/Code/OysterGraphics/FileLoader/ShaderLoader.cpp +++ b/Code/OysterGraphics/FileLoader/ShaderLoader.cpp @@ -10,6 +10,8 @@ namespace Oyster { namespace Loading { + void LoadShader(const wchar_t filename[], Oyster::Resource::CustomData& out, int type); + void UnloadShaderP(void* loadedData) { ID3D11PixelShader* ps = ((ID3D11PixelShader*)loadedData); diff --git a/Code/OysterGraphics/OysterGraphics.vcxproj b/Code/OysterGraphics/OysterGraphics.vcxproj index ed998710..cd1a5670 100644 --- a/Code/OysterGraphics/OysterGraphics.vcxproj +++ b/Code/OysterGraphics/OysterGraphics.vcxproj @@ -178,7 +178,7 @@ - + @@ -270,6 +270,8 @@ + + diff --git a/Code/OysterGraphics/Render/Rendering/BasicRender.cpp b/Code/OysterGraphics/Render/Rendering/BasicRender.cpp index 9626c954..2edb318b 100644 --- a/Code/OysterGraphics/Render/Rendering/BasicRender.cpp +++ b/Code/OysterGraphics/Render/Rendering/BasicRender.cpp @@ -2,6 +2,8 @@ #include "../Resources/Resources.h" #include "../../Definitions/GraphicalDefinition.h" #include "../../Model/ModelInfo.h" +#include +#include namespace Oyster { diff --git a/Code/OysterGraphics/Render/Resources/Resources.cpp b/Code/OysterGraphics/Render/Resources/Resources.cpp index 66fa4cc9..ceef5160 100644 --- a/Code/OysterGraphics/Render/Resources/Resources.cpp +++ b/Code/OysterGraphics/Render/Resources/Resources.cpp @@ -159,10 +159,9 @@ namespace Oyster void Resources::Clean() { Resources::ModelData.~Buffer(); - Resources::VPData.~Buffer(); for(int i = 0; i < obj.CBuffers.Vertex.size(); ++i) { - //SAFE_RELEASE(obj.CBuffers.Vertex[i]); + obj.CBuffers.Vertex[i]->~Buffer(); } for(int i = 0; i < obj.CBuffers.Pixel.size(); ++i) { diff --git a/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/Render/Defines.hlsli b/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/Render/Defines.hlsli index e5a2333f..3f9fd0bf 100644 --- a/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/Render/Defines.hlsli +++ b/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/Render/Defines.hlsli @@ -1,7 +1,31 @@ +#ifndef DEFINES +#define DEFINES + struct PointLight { - float3 Pos; - float Radius; + float4 PosRadius; + float4 ColorBright; +}; - float3 Color; -} \ No newline at end of file +struct DiffSpec +{ + float3 Diffuse; + float3 Specular; +}; + +cbuffer PointLights : register(b0) +{ + PointLight pl; +} + +cbuffer LightConstants : register(b1) +{ + float4x4 InvProj; + int2 Pixels; +} + +Texture2D DiffuseGlow : register(t0); +Texture2D NormalSpec : register(t1); +Texture2D DepthTexture : register(t2); + +#endif \ No newline at end of file diff --git a/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/Render/LightCalc.hlsli b/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/Render/LightCalc.hlsli new file mode 100644 index 00000000..88234de0 --- /dev/null +++ b/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/Render/LightCalc.hlsli @@ -0,0 +1,23 @@ +#include "Defines.hlsli" + +DiffSpec LightCalc(PointLight pl, float3 pos, int2 texCoord) +{ + DiffSpec output; + float4 normalSpec = NormalSpec[texCoord]; + float3 lightVec = pl.PosRadius.xyz - pos.xyz; + float d = length(lightVec); + lightVec = lightVec/d; + + float diffFactor = max(dot(lightVec, normalSpec.xyz), 0.0f); + float3 v = reflect(-lightVec, normalSpec.xyz); + float specFactor = pow(max(dot(v,normalize(-pos)), 0.0f),normalSpec.w); + //Check att later + float att = (max(d-pl.PosRadius.w,0)/pow(pl.PosRadius.w,2)); + + //fix Ilum calcs instead of PhongBlinn + output.Diffuse = pl.ColorBright.w * att * diffFactor * pl.ColorBright.xyz; + output.Specular = pl.ColorBright.w * att * specFactor * pl.ColorBright.xyz; + if(diffFactor == 0) + output.Specular * 0; + return output; +} \ No newline at end of file diff --git a/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/Render/LightPass.hlsl b/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/Render/LightPass.hlsl index 8fdac3c8..3e544f2f 100644 --- a/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/Render/LightPass.hlsl +++ b/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/Render/LightPass.hlsl @@ -1,11 +1,16 @@ - +#include "Defines.hlsli" +#include "LightCalc.hlsli" +#include "PosManipulation.hlsli" //todo //LightCulling //Calc Diff + Spec //Calc Ambience //Write Glow + [numthreads(1, 1, 1)] void main( uint3 DTid : SV_DispatchThreadID ) { + float3 ViewPos = ToVpos(DTid.xy); + //DiffSpec LightCalc(pl, float3 pos) } \ No newline at end of file diff --git a/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/Render/PosManipulation.hlsli b/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/Render/PosManipulation.hlsli new file mode 100644 index 00000000..ab44bd38 --- /dev/null +++ b/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/Render/PosManipulation.hlsli @@ -0,0 +1,21 @@ +#include "Defines.hlsli" + +//assumes ProperfloatTexCoords +float3 ToVpos(float2 texCoord) +{ + //Get proper UV + float2 UV = float2(texCoord) / float2(Pixels); + + float4 ViewPos; + // Get the depth value for this pixel + ViewPos.z= DepthTexture[texCoord].x; + //Get X/w + ViewPos.x = UV.x * 2 - 1; + //Get Y/w + ViewPos.y = 1 - 2 * UV.y; + ViewPos.w = 1; + + //Un project + ViewPos = mul(ViewPos, InvProj); + return ViewPos.xyz / ViewPos.w; +} \ No newline at end of file diff --git a/Code/Tester/MainTest.cpp b/Code/Tester/MainTest.cpp index c8772c89..3d55a0a7 100644 --- a/Code/Tester/MainTest.cpp +++ b/Code/Tester/MainTest.cpp @@ -93,6 +93,7 @@ int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdL } Oyster::Graphics::API::DeleteModel(m); + Oyster::Graphics::API::DeleteModel(m2); Oyster::Graphics::API::Clean(); return (int) msg.wParam; } @@ -194,6 +195,8 @@ HRESULT InitDirect3D() P = Oyster::Math3D::ProjectionMatrix_Perspective(Oyster::Math::pi/2,1024.0f/768.0f,.1f,1000); + Oyster::Graphics::API::SetProjection(P); + P.Invert(); V = Oyster::Math3D::OrientationMatrix_LookAtDirection(Oyster::Math::Float3(0,0,-1),Oyster::Math::Float3(0,1,0),Oyster::Math::Float3(0,0,5.4f)); V = Oyster::Math3D::InverseOrientationMatrix(V); @@ -212,10 +215,11 @@ HRESULT Update(float deltaTime) HRESULT Render(float deltaTime) { - Oyster::Graphics::API::NewFrame(V,P); + Oyster::Graphics::API::SetView(V); + Oyster::Graphics::API::NewFrame(); - Oyster::Graphics::API::RenderScene(m,1); - Oyster::Graphics::API::RenderScene(m2,1); + Oyster::Graphics::API::RenderModel(*m); + Oyster::Graphics::API::RenderModel(*m2); Oyster::Graphics::API::EndFrame(); diff --git a/Code/Tester/Tester.vcxproj b/Code/Tester/Tester.vcxproj index 36184b93..80853c72 100644 --- a/Code/Tester/Tester.vcxproj +++ b/Code/Tester/Tester.vcxproj @@ -69,7 +69,7 @@ true $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ - $(SolutionDir)..\Bin\Executable\$(ProjectName)\ + $(SolutionDir)..\Bin\Executable\ $(ProjectName)_$(PlatformShortName)D C:\Program Files (x86)\Visual Leak Detector\include;$(IncludePath) C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath) @@ -77,7 +77,7 @@ true $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ - $(SolutionDir)..\Bin\Executable\$(ProjectName)\ + $(SolutionDir)..\Bin\Executable\ $(ProjectName)_$(PlatformShortName)D C:\Program Files (x86)\Visual Leak Detector\include;$(IncludePath) C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath) @@ -93,7 +93,7 @@ false $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ - $(SolutionDir)..\Bin\Executable\$(ProjectName)\ + $(SolutionDir)..\Bin\Executable\ $(ProjectName)_$(PlatformShortName) C:\Program Files (x86)\Visual Leak Detector\include;$(IncludePath) C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath)