diff --git a/Code/OysterGraphics/Core/Buffer.cpp b/Code/OysterGraphics/Core/Buffer.cpp index d071ff10..f530239c 100644 --- a/Code/OysterGraphics/Core/Buffer.cpp +++ b/Code/OysterGraphics/Core/Buffer.cpp @@ -1,9 +1,9 @@ #include "Buffer.h" #include "Core.h" -using namespace Oyster; +using namespace Oyster::Graphics; -Oyster::Buffer::Buffer() +Buffer::Buffer() { mBuffer = NULL; } @@ -23,32 +23,32 @@ HRESULT Buffer::Apply(UINT32 misc) const { UINT32 vertexSize = mElementSize; UINT32 offset = 0; - Oyster::Core::DeviceContext->IASetVertexBuffers(misc, 1, &mBuffer, &vertexSize, &offset ); + Core::deviceContext->IASetVertexBuffers(misc, 1, &mBuffer, &vertexSize, &offset ); } break; case INDEX_BUFFER: { - Oyster::Core::DeviceContext->IASetIndexBuffer(mBuffer, DXGI_FORMAT_R32_UINT, 0); + Core::deviceContext->IASetIndexBuffer(mBuffer, DXGI_FORMAT_R32_UINT, 0); } break; case CONSTANT_BUFFER_VS: { - Oyster::Core::DeviceContext->VSSetConstantBuffers(misc, 1, &mBuffer); + Core::deviceContext->VSSetConstantBuffers(misc, 1, &mBuffer); } break; case CONSTANT_BUFFER_GS: { - Oyster::Core::DeviceContext->GSSetConstantBuffers(misc, 1, &mBuffer); + Core::deviceContext->GSSetConstantBuffers(misc, 1, &mBuffer); } break; case CONSTANT_BUFFER_PS: { - Oyster::Core::DeviceContext->PSSetConstantBuffers(misc, 1, &mBuffer); + Core::deviceContext->PSSetConstantBuffers(misc, 1, &mBuffer); } break; case CONSTANT_BUFFER_CS: { - Oyster::Core::DeviceContext->CSSetConstantBuffers(misc,1,&mBuffer); + Core::deviceContext->CSSetConstantBuffers(misc,1,&mBuffer); } break; default: @@ -145,11 +145,11 @@ HRESULT Buffer::Init(const BUFFER_INIT_DESC& initDesc) data.pSysMem = initDesc.InitData; data.SysMemPitch=0; data.SysMemSlicePitch = 0; - hr = Oyster::Core::Device->CreateBuffer(&bufferDesc, &data, &mBuffer); + hr = Core::device->CreateBuffer(&bufferDesc, &data, &mBuffer); } else { - hr = Oyster::Core::Device->CreateBuffer(&bufferDesc, NULL, &mBuffer); + hr = Core::device->CreateBuffer(&bufferDesc, NULL, &mBuffer); } if(FAILED(hr)) @@ -173,7 +173,7 @@ void* Buffer::Map() else if(mUsage == BUFFER_CPU_WRITE_DISCARD) mapType = D3D11_MAP_WRITE_DISCARD; HRESULT hr = S_OK; - if(FAILED(hr = Oyster::Core::DeviceContext->Map( + if(FAILED(hr = Core::deviceContext->Map( mBuffer, 0, (D3D11_MAP)mapType, @@ -194,10 +194,10 @@ void* Buffer::Map() void Buffer::Unmap() { - Oyster::Core::DeviceContext->Unmap( mBuffer, 0 ); + Core::deviceContext->Unmap( mBuffer, 0 ); } -Oyster::Buffer::operator ID3D11Buffer *() +Buffer::operator ID3D11Buffer *() { return this->mBuffer; } diff --git a/Code/OysterGraphics/Core/Buffer.h b/Code/OysterGraphics/Core/Buffer.h index 3a887a19..1a52d3f8 100644 --- a/Code/OysterGraphics/Core/Buffer.h +++ b/Code/OysterGraphics/Core/Buffer.h @@ -6,72 +6,74 @@ namespace Oyster { - class Buffer + namespace Graphics { - public: - enum BUFFER_TYPE + class Buffer { - VERTEX_BUFFER, - INDEX_BUFFER, - CONSTANT_BUFFER_VS, - CONSTANT_BUFFER_GS, - CONSTANT_BUFFER_PS, - CONSTANT_BUFFER_CS, - STRUCTURED_BUFFER, - BUFFER_TYPE_COUNT - }; - - enum BUFFER_USAGE - { - BUFFER_DEFAULT, - BUFFER_STREAM_OUT_TARGET, - BUFFER_CPU_WRITE, - BUFFER_CPU_WRITE_DISCARD, - BUFFER_CPU_READ, - BUFFER_USAGE_COUNT, - BUFFER_USAGE_IMMUTABLE - }; - - struct BUFFER_INIT_DESC - { - BUFFER_TYPE Type; - UINT32 NumElements; - UINT32 ElementSize; - BUFFER_USAGE Usage; - void* InitData; - - BUFFER_INIT_DESC() + public: + enum BUFFER_TYPE { - InitData = NULL; - Usage = BUFFER_DEFAULT; - } + VERTEX_BUFFER, + INDEX_BUFFER, + CONSTANT_BUFFER_VS, + CONSTANT_BUFFER_GS, + CONSTANT_BUFFER_PS, + CONSTANT_BUFFER_CS, + STRUCTURED_BUFFER, + BUFFER_TYPE_COUNT + }; + + enum BUFFER_USAGE + { + BUFFER_DEFAULT, + BUFFER_STREAM_OUT_TARGET, + BUFFER_CPU_WRITE, + BUFFER_CPU_WRITE_DISCARD, + BUFFER_CPU_READ, + BUFFER_USAGE_COUNT, + BUFFER_USAGE_IMMUTABLE + }; + + struct BUFFER_INIT_DESC + { + BUFFER_TYPE Type; + UINT32 NumElements; + UINT32 ElementSize; + BUFFER_USAGE Usage; + void* InitData; + + BUFFER_INIT_DESC() + { + InitData = NULL; + Usage = BUFFER_DEFAULT; + } + }; + protected: + ID3D11Buffer* mBuffer; + BUFFER_TYPE mType; + BUFFER_USAGE mUsage; + + UINT32 mElementSize; + UINT32 mElementCount; + public: + Buffer(); + virtual ~Buffer(); + + HRESULT Init(const BUFFER_INIT_DESC& initDesc); + + void* Map(); + void Unmap(); + + operator ID3D11Buffer*(); + operator const ID3D11Buffer*() const; + + HRESULT Apply(UINT32 misc = 0) const; + + ID3D11Buffer* GetBufferPointer(); + UINT32 GetVertexSize(); + UINT32 GetElementCount(); }; - protected: - ID3D11Buffer* mBuffer; - BUFFER_TYPE mType; - BUFFER_USAGE mUsage; - - UINT32 mElementSize; - UINT32 mElementCount; - public: - Buffer(); - virtual ~Buffer(); - - HRESULT Init(const BUFFER_INIT_DESC& initDesc); - - void* Map(); - void Unmap(); - - operator ID3D11Buffer*(); - operator const ID3D11Buffer*() const; - - HRESULT Apply(UINT32 misc = 0) const; - - ID3D11Buffer* GetBufferPointer(); - UINT32 GetVertexSize(); - UINT32 GetElementCount(); - }; - + } } #endif \ No newline at end of file diff --git a/Code/OysterGraphics/Core/Core.cpp b/Code/OysterGraphics/Core/Core.cpp index 189b2573..a425ae57 100644 --- a/Code/OysterGraphics/Core/Core.cpp +++ b/Code/OysterGraphics/Core/Core.cpp @@ -1,169 +1,25 @@ #include "Core.h" -using namespace Oyster; +using namespace Oyster::Graphics; using std::string; //GPU -ID3D11Device *Core::Device = NULL; +ID3D11Device *Core::device = NULL; //API -ID3D11DeviceContext *Core::DeviceContext = NULL; +ID3D11DeviceContext *Core::deviceContext = NULL; //SwapChain -IDXGISwapChain* Core::SwapChain = NULL; +IDXGISwapChain* Core::swapChain = NULL; -std::stringstream Log; +std::stringstream Core::log; -inline std::stringstream* AccesLog(){return &Log;} +ID3D11RenderTargetView* Core::backBufferRTV = NULL; -bool Core::Init(bool SingleThreaded, bool Reference,bool ForceDX11) -{ - UINT createDeviceFlags = 0; +ID3D11UnorderedAccessView* Core::backBufferUAV = NULL; - if( SingleThreaded ) - createDeviceFlags = ::D3D11_CREATE_DEVICE_SINGLETHREADED; +ID3D11DepthStencilView* Core::depthStencil = NULL; - ::D3D_DRIVER_TYPE driverType = ::D3D_DRIVER_TYPE_HARDWARE; - - if(Reference) - driverType = D3D_DRIVER_TYPE_REFERENCE; - - #if defined(DEBUG) || defined(_DEBUG) - Log << "DirectX running in debug mode.\n"; - createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG; - #endif +D3D11_VIEWPORT* Core::viewPort = NULL; - - D3D_FEATURE_LEVEL featureLevelsToTry[] = - { - D3D_FEATURE_LEVEL_11_0, - D3D_FEATURE_LEVEL_10_1, - D3D_FEATURE_LEVEL_10_0 - }; - D3D_FEATURE_LEVEL initiatedFeatureLevel; - - if( FAILED( ::D3D11CreateDevice( NULL, // default adapter - driverType, - NULL, // no software device - createDeviceFlags, - featureLevelsToTry, 3, // default feature level array. DX11 support assumed - D3D11_SDK_VERSION, - &Device, // device - &initiatedFeatureLevel, - &DeviceContext ) ) ) // context - { // if failed - if( DeviceContext ) { DeviceContext->Release(); DeviceContext = NULL; } // safe cleanup - if( Device ) { Device->Release(); Device = NULL; } // safe cleanup - } - - if( driverType == ::D3D_DRIVER_TYPE_HARDWARE ) - Log << "D3D_DRIVER_TYPE_HARDWARE support discovered.\n"; - else - Log << "D3D_DRIVER_TYPE_REFERENCE support discovered.\n"; - - if( initiatedFeatureLevel == ::D3D_FEATURE_LEVEL_11_0 ) - { - Log << "DirectX Featurelevel 11.0 supported.\n"; - } - else - { - if(ForceDX11) - return false; - if( initiatedFeatureLevel == ::D3D_FEATURE_LEVEL_10_1 ) - { - Log << "DirectX Featurelevel 10.1 supported.\n"; - } - else - { - if( initiatedFeatureLevel == ::D3D_FEATURE_LEVEL_10_0 ) - { - Log << "DirectX Featurelevel 10.0 supported.\n"; - } - } - } - if(Device) - return true; - - return false; -} - -bool Core::CreateSwapChain(HWND Window, int NrofBuffers,bool MSAA_Quality,bool Fullscreen) -{ - //generate static Swapchain Desc - DXGI_SWAP_CHAIN_DESC desc; - desc.OutputWindow=Window; - desc.BufferCount=NrofBuffers; - desc.Windowed=!Fullscreen; - desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT | DXGI_USAGE_UNORDERED_ACCESS; - desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; - desc.Flags=0; - desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; - desc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; - desc.BufferDesc.Scaling = DXGI_MODE_SCALING_CENTERED; - desc.BufferDesc.RefreshRate.Denominator=1; - desc.BufferDesc.RefreshRate.Numerator=60; - - RECT rc; - GetClientRect( Window, &rc ); - int screenWidth = rc.right - rc.left; - int screenHeight = rc.bottom - rc.top; - - desc.BufferDesc.Height = screenHeight; - desc.BufferDesc.Width = screenWidth; - - //Check and Set multiSampling - if(MSAA_Quality) - { - if(FAILED(Device->CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM,4,&desc.SampleDesc.Quality))) - { - Log<< "Failed to check multisample quality levels (MSAAQuality).\n"; - return false; - } - desc.SampleDesc.Count=4; - --desc.SampleDesc.Quality; - Log << "Supported multisample quality levels (MSAAQuality): " << desc.SampleDesc.Quality+1 << "x\n"; - } - else - { - desc.SampleDesc.Count=1; - desc.SampleDesc.Quality=0; - } - - //Get Device Factory - ::IDXGIDevice *dxgiDevice = NULL; - if( FAILED( Device->QueryInterface( __uuidof( IDXGIDevice ), (void**)&dxgiDevice ) ) ) - { - Log << "Failed to Query for the GPU's dxgiDevice.\nFailed to create swapChain for the GPU.\n"; - return false; - } - - ::IDXGIAdapter *dxgiAdapter = NULL; - if( FAILED( dxgiDevice->GetParent( __uuidof( IDXGIAdapter ), (void**)&dxgiAdapter ) ) ) - { - dxgiDevice->Release(); - Log << "Failed to get GPU's parent dxgiAdapter.\nFailed to create swapChain for the GPU.\n"; - return false; - } - dxgiDevice->Release(); - - ::IDXGIFactory *dxgiFactory = NULL; - if( FAILED( dxgiAdapter->GetParent( __uuidof( IDXGIFactory ), (void**)&dxgiFactory ) ) ) - { - dxgiAdapter->Release(); - Log << "Failed to get GPU's parent dxgiFactory.\nFailed to create swapChain for the GPU.\n"; - return false; - } - dxgiAdapter->Release(); - - //Create SwapChain - if( FAILED( dxgiFactory->CreateSwapChain( Device, &desc, &SwapChain ) ) ) - { - dxgiFactory->Release(); - Log << "Failed to create swapChain for the GPU.\n"; - return false; - } - - dxgiFactory->Release(); - - return true; -} \ No newline at end of file +Oyster::Math::Float2 Core::resolution = Oyster::Math::Float2::null; \ No newline at end of file diff --git a/Code/OysterGraphics/Core/Core.h b/Code/OysterGraphics/Core/Core.h index 3926a943..28d47e31 100644 --- a/Code/OysterGraphics/Core/Core.h +++ b/Code/OysterGraphics/Core/Core.h @@ -7,108 +7,146 @@ #include "CoreIncludes.h" #include #include "Buffer.h" +#include "OysterMath.h"; + namespace Oyster { - class Core + namespace Graphics { - public: - - static ID3D11Device* Device; - - static ID3D11DeviceContext* DeviceContext; - - static IDXGISwapChain* SwapChain; - - static std::stringstream* AccesLog(); - - static bool Init(bool SingleThreaded,bool Reference,bool ForceDX11); - - static bool CreateSwapChain(HWND Window, int NrofBuffers,bool MSAA_Quality,bool Fullscreen); - - class ShaderManager + class Core { public: - struct ShaderEffect + + static ID3D11Device* device; + + static ID3D11DeviceContext* deviceContext; + + static IDXGISwapChain* swapChain; + + static std::stringstream log; + + //BackBufferRTV + static ID3D11RenderTargetView* backBufferRTV; + //BackBufferUAV + static ID3D11UnorderedAccessView* backBufferUAV; + //DepthStencil + static ID3D11DepthStencilView* depthStencil; + //ViewPort + static D3D11_VIEWPORT* viewPort; + + static Oyster::Math::Float2 resolution; + + + class ShaderManager { - struct + public: + struct ShaderEffect { - int Pixel,Vertex,Geometry,Compute,Hull,Domain; - }Shaders; + struct + { + int Pixel,Vertex,Geometry,Compute,Hull,Domain; + }Shaders; - struct IAStage_ - { - ID3D11InputLayout* Layout; - D3D11_PRIMITIVE_TOPOLOGY Topology; - }IAStage; + struct IAStage_ + { + ID3D11InputLayout* Layout; + D3D11_PRIMITIVE_TOPOLOGY Topology; + }IAStage; - struct RenderStates_ - { - ID3D11DepthStencilState *DepthStencil; - ID3D11RasterizerState *Rasterizer; - ID3D11SamplerState **SampleState; - int SampleCount; - ID3D11BlendState *BlendState; - }RenderStates; + struct RenderStates_ + { + ID3D11DepthStencilState *DepthStencil; + ID3D11RasterizerState *Rasterizer; + ID3D11SamplerState **SampleState; + int SampleCount; + ID3D11BlendState *BlendState; + }RenderStates; - struct - { - std::vector Vertex; - std::vector Geometry; - std::vector Pixel; - }CBuffers; + struct + { + std::vector Vertex; + std::vector Geometry; + std::vector Pixel; + }CBuffers; - ShaderEffect() + ShaderEffect() + { + RenderStates.BlendState=NULL; + RenderStates.DepthStencil=NULL; + RenderStates.Rasterizer=NULL; + RenderStates.SampleState=NULL; + RenderStates.SampleCount=0; + Shaders.Compute=-1; + Shaders.Domain=-1; + Shaders.Geometry=-1; + Shaders.Hull=-1; + Shaders.Pixel=-1; + Shaders.Vertex=-1; + } + }; + enum ShaderType { - RenderStates.BlendState=NULL; - RenderStates.DepthStencil=NULL; - RenderStates.Rasterizer=NULL; - RenderStates.SampleState=NULL; - RenderStates.SampleCount=0; - Shaders.Compute=-1; - Shaders.Domain=-1; - Shaders.Geometry=-1; - Shaders.Hull=-1; - Shaders.Pixel=-1; - Shaders.Vertex=-1; - } - }; - enum ShaderType - { - Vertex, - Hull, - Domain, - Geometry, - Pixel, - Compute + Vertex, + Hull, + Domain, + Geometry, + Pixel, + Compute + }; + + static void SetShaderEffect(ShaderEffect); + + static void CreateInputLayout(const D3D11_INPUT_ELEMENT_DESC *desc, int ElementCount,int VertexIndex,ID3D11InputLayout *&Layout); + + static bool Init(std::wstring filename, ShaderType type, std::wstring name, bool Precompiled = true); + + struct Get + { + static int Pixel(std::wstring Name); + static int Vertex(std::wstring Name); + static int Geometry(std::wstring Name); + static int Compute(std::wstring Name); + static int Hull(std::wstring Name); + static int Domain(std::wstring Name); + }; + + struct Set + { + static void Pixel(int Index); + static void Vertex(int Index); + static void Geometry(int Index); + static void Compute(int Index); + static void Hull(int Index); + static void Domain(int Index); + }; }; - static void SetShaderEffect(ShaderEffect); - - static void CreateInputLayout(const D3D11_INPUT_ELEMENT_DESC *desc, int ElementCount,int VertexIndex,ID3D11InputLayout *&Layout); - - static bool Init(std::wstring filename, ShaderType type, std::wstring name, bool Precompiled = true); - - struct Get + //Set resulotion Before Calling Full Init + class Init { - static int Pixel(std::wstring Name); - static int Vertex(std::wstring Name); - static int Geometry(std::wstring Name); - static int Compute(std::wstring Name); - static int Hull(std::wstring Name); - static int Domain(std::wstring Name); - }; + public: + enum State + { + Sucsess, + Fail + }; - struct Set - { - static void Pixel(int Index); - static void Vertex(int Index); - static void Geometry(int Index); - static void Compute(int Index); - static void Hull(int Index); - static void Domain(int Index); + static State CreateDeviceAndDeviceContext(bool SingleThreaded = true, bool Reference = false, bool ForceDX11 = true); + + static State CreateSwapChain(HWND Window, int NrofBuffers,bool MSAA_Quality,bool Fullscreen, Oyster::Math::Float2 Size); + + static State CreateDepthStencil(bool MSAA_Quality, Oyster::Math::Float2 Size); + + static State CreateBackBufferViews(); + + static State CreateViewPort(Oyster::Math::Float2 Origin, Oyster::Math::Float2 Size); + + static State FullInit(HWND Window, bool MSAA_Quality, bool Fullscreen); + + static State ReInitialize(HWND Window, bool MSAA_Quality, bool Fullscreen); }; }; - }; + } } diff --git a/Code/OysterGraphics/Core/CoreIncludes.h b/Code/OysterGraphics/Core/CoreIncludes.h index 3e1d9306..26b1ce0c 100644 --- a/Code/OysterGraphics/Core/CoreIncludes.h +++ b/Code/OysterGraphics/Core/CoreIncludes.h @@ -6,7 +6,7 @@ // http://lolengine.net/blog/2011/3/4/fuck-you-microsoft-near-far-macros #include #include -#include +#include #pragma comment(lib, "d3d11.lib") #pragma comment(lib, "d3dcompiler.lib") diff --git a/Code/OysterGraphics/Core/Init.cpp b/Code/OysterGraphics/Core/Init.cpp new file mode 100644 index 00000000..7802654c --- /dev/null +++ b/Code/OysterGraphics/Core/Init.cpp @@ -0,0 +1,337 @@ +#include "Core.h" + +namespace Oyster +{ + namespace Graphics + { + Core::Init::State Core::Init::CreateDeviceAndDeviceContext(bool SingleThreaded,bool Reference,bool ForceDX11) + { + UINT createDeviceFlags = 0; + + if(Core::deviceContext) + { + Core::deviceContext->Release(); + delete Core::deviceContext; + } + if(Core::device) + { + Core::device->Release(); + delete Core::device; + } + + + if( SingleThreaded ) + createDeviceFlags = ::D3D11_CREATE_DEVICE_SINGLETHREADED; + + ::D3D_DRIVER_TYPE driverType = ::D3D_DRIVER_TYPE_HARDWARE; + + if(Reference) + driverType = D3D_DRIVER_TYPE_REFERENCE; + + #if defined(DEBUG) || defined(_DEBUG) + log << "DirectX running in debug mode.\n"; + createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG; + #endif + + + D3D_FEATURE_LEVEL featureLevelsToTry[] = + { + D3D_FEATURE_LEVEL_11_0, + D3D_FEATURE_LEVEL_10_1, + D3D_FEATURE_LEVEL_10_0 + }; + D3D_FEATURE_LEVEL initiatedFeatureLevel; + + if( FAILED( ::D3D11CreateDevice( NULL, // default adapter + driverType, + NULL, // no software device + createDeviceFlags, + featureLevelsToTry, 3, // default feature level array. DX11 support assumed + D3D11_SDK_VERSION, + &Core::device, // device + &initiatedFeatureLevel, + &Core::deviceContext ) ) ) // context + { // if failed + if( Core::deviceContext ) { Core::deviceContext->Release(); Core::deviceContext = NULL; } // safe cleanup + if( Core::device ) { Core::device->Release(); Core::device = NULL; } // safe cleanup + } + + if( driverType == ::D3D_DRIVER_TYPE_HARDWARE ) + log << "D3D_DRIVER_TYPE_HARDWARE support discovered.\n"; + else + log << "D3D_DRIVER_TYPE_REFERENCE support discovered.\n"; + + if( initiatedFeatureLevel == ::D3D_FEATURE_LEVEL_11_0 ) + { + log << "DirectX Featurelevel 11.0 supported.\n"; + } + else + { + if(ForceDX11) + return Init::Fail; + if( initiatedFeatureLevel == ::D3D_FEATURE_LEVEL_10_1 ) + { + log << "DirectX Featurelevel 10.1 supported.\n"; + } + else + { + if( initiatedFeatureLevel == ::D3D_FEATURE_LEVEL_10_0 ) + { + log << "DirectX Featurelevel 10.0 supported.\n"; + } + } + } + if(Core::device) + return Init::Sucsess; + + return Init::Fail; + } + + Core::Init::State Core::Init::CreateSwapChain(HWND Window, int NrofBuffers,bool MSAA_Quality,bool Fullscreen, Oyster::Math::Float2 Size) + { + //generate static Swapchain Desc + DXGI_SWAP_CHAIN_DESC desc; + desc.OutputWindow=Window; + desc.BufferCount=NrofBuffers; + desc.Windowed=!Fullscreen; + desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT | DXGI_USAGE_UNORDERED_ACCESS; + desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; + desc.Flags=0; + desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + desc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; + desc.BufferDesc.Scaling = DXGI_MODE_SCALING_CENTERED; + desc.BufferDesc.RefreshRate.Denominator=1; + desc.BufferDesc.RefreshRate.Numerator=60; + + desc.BufferDesc.Height = Size.y; + desc.BufferDesc.Width = Size.x; + + if(Core::swapChain) + { + Core::swapChain->Release(); + delete Core::swapChain; + } + + + //Check and Set multiSampling + if(MSAA_Quality) + { + if(FAILED(Core::device->CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM,4,&desc.SampleDesc.Quality))) + { + log<< "Failed to check multisample quality levels (MSAAQuality).\n"; + return Init::Fail; + } + desc.SampleDesc.Count=4; + --desc.SampleDesc.Quality; + log << "Supported multisample quality levels (MSAAQuality): " << desc.SampleDesc.Quality+1 << "x\n"; + } + else + { + desc.SampleDesc.Count=1; + desc.SampleDesc.Quality=0; + } + + //Get Device Factory + ::IDXGIDevice *dxgiDevice = NULL; + if( FAILED( Core::device->QueryInterface( __uuidof( IDXGIDevice ), (void**)&dxgiDevice ) ) ) + { + log << "Failed to Query for the GPU's dxgiDevice.\nFailed to create swapChain for the GPU.\n"; + return Init::Fail; + } + + ::IDXGIAdapter *dxgiAdapter = NULL; + if( FAILED( dxgiDevice->GetParent( __uuidof( IDXGIAdapter ), (void**)&dxgiAdapter ) ) ) + { + dxgiDevice->Release(); + log << "Failed to get GPU's parent dxgiAdapter.\nFailed to create swapChain for the GPU.\n"; + return Init::Fail; + } + dxgiDevice->Release(); + + ::IDXGIFactory *dxgiFactory = NULL; + if( FAILED( dxgiAdapter->GetParent( __uuidof( IDXGIFactory ), (void**)&dxgiFactory ) ) ) + { + dxgiAdapter->Release(); + log << "Failed to get GPU's parent dxgiFactory.\nFailed to create swapChain for the GPU.\n"; + return Init::Fail; + } + dxgiAdapter->Release(); + + //Create SwapChain + if( FAILED( dxgiFactory->CreateSwapChain( Core::device, &desc, &Core::swapChain ) ) ) + { + dxgiFactory->Release(); + log << "Failed to create swapChain for the GPU.\n"; + return Init::Fail; + } + + dxgiFactory->Release(); + + return Init::Sucsess; + } + + Core::Init::State Core::Init::CreateDepthStencil(bool MSAA_Quality, Oyster::Math::Float2 Size) + { + D3D11_TEXTURE2D_DESC desc; + desc.MipLevels=1; + desc.ArraySize=1; + desc.Format = DXGI_FORMAT_D32_FLOAT; + desc.Usage = D3D11_USAGE_DEFAULT; + desc.BindFlags = D3D11_BIND_DEPTH_STENCIL; + desc.CPUAccessFlags=0; + desc.MiscFlags=0; + desc.Height = Size.y; + desc.Width = Size.x; + + if(Core::depthStencil) + { + Core::depthStencil->Release(); + delete Core::depthStencil; + } + + //Check and Set multiSampling + if(MSAA_Quality) + { + if(FAILED(Core::device->CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM,4,&desc.SampleDesc.Quality))) + { + log<< "Failed to check multisample quality levels (MSAAQuality).\n"; + return Init::Fail; + } + desc.SampleDesc.Count=4; + --desc.SampleDesc.Quality; + log << "Supported multisample quality levels (MSAAQuality): " << desc.SampleDesc.Quality+1 << "x\n"; + } + else + { + desc.SampleDesc.Count=1; + desc.SampleDesc.Quality=0; + } + + ID3D11Texture2D* depthstencil; + + if(FAILED(Core::device->CreateTexture2D(&desc,0,&depthstencil))) + return Init::Fail; + if(FAILED(Core::device->CreateDepthStencilView(depthstencil,0,&Core::depthStencil))) + { + depthstencil->Release(); + return Init::Fail; + } + depthstencil->Release(); + + return Init::Sucsess; + } + + Core::Init::State Core::Init::CreateBackBufferViews() + { + D3D11_UNORDERED_ACCESS_VIEW_DESC descView; + ZeroMemory( &descView, sizeof(descView) ); + descView.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2D; + descView.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + descView.Texture2D.MipSlice=0; + + ID3D11Texture2D* backBuffer; + if(FAILED(Core::swapChain->GetBuffer(0,__uuidof(ID3D11Texture2D),reinterpret_cast(&backBuffer)))) + { + log << "Failed to get BackBuffer from Swapchain"; + return Init::Fail; + } + if(Core::backBufferRTV) + { + Core::backBufferRTV->Release(); + delete Core::backBufferRTV; + } + if(FAILED(Core::device->CreateRenderTargetView(backBuffer,0,&Core::backBufferRTV))) + { + log << "Failed to create RTV for BackBuffer"; + backBuffer->Release(); + return Init::Fail; + } + if(Core::backBufferUAV) + { + Core::backBufferUAV->Release(); + delete Core::backBufferUAV; + } + if(FAILED(Core::device->CreateUnorderedAccessView(backBuffer,0,&Core::backBufferUAV))) + { + log << "Failed to create UAV for BackBuffer"; + backBuffer->Release(); + return Init::Fail; + } + + backBuffer->Release(); + + return Init::Sucsess; + } + + Core::Init::State Core::Init::CreateViewPort(Oyster::Math::Float2 Origin, Oyster::Math::Float2 Size) + { + if(Core::viewPort) + delete Core::viewPort; + Core::viewPort = new D3D11_VIEWPORT; + + Core::viewPort->TopLeftX = Origin.x; + Core::viewPort->TopLeftY = Origin.y; + Core::viewPort->Width = Size.x; + Core::viewPort->Height = Size.y; + Core::viewPort->MinDepth = 0.0f; + Core::viewPort->MaxDepth = 1.0f; + + return Init::Sucsess; + } + + Core::Init::State Core::Init::FullInit(HWND Window, bool MSAA_Quality, bool Fullscreen) + { + if(Init::CreateDeviceAndDeviceContext() == Init::Fail) + { + return Init::Fail; + } + + if(Init::CreateSwapChain(Window, 1, MSAA_Quality, Fullscreen, Core::resolution) == Init::Fail) + { + return Init::Fail; + } + + if(Init::CreateDepthStencil(MSAA_Quality, Core::resolution) == Init::Fail) + { + return Init::Fail; + } + + if(Init::CreateBackBufferViews() == Init::Fail) + { + return Init::Fail; + } + + if(Init::CreateViewPort(Oyster::Math::Float2::null, Core::resolution) == Init::Fail) + { + return Init::Fail; + } + + return Init::Sucsess; + } + + Core::Init::State Core::Init::ReInitialize(HWND Window, bool MSAA_Quality, bool Fullscreen) + { + if(Init::CreateSwapChain(Window, 1, MSAA_Quality, Fullscreen, Core::resolution) == Init::Fail) + { + return Init::Fail; + } + + if(Init::CreateDepthStencil(MSAA_Quality, Core::resolution) == Init::Fail) + { + return Init::Fail; + } + + if(Init::CreateBackBufferViews() == Init::Fail) + { + return Init::Fail; + } + + if(Init::CreateViewPort(Oyster::Math::Float2::null, Core::resolution) == Init::Fail) + { + return Init::Fail; + } + + return Init::Sucsess; + } + } +} \ No newline at end of file diff --git a/Code/OysterGraphics/Core/ShaderManager.cpp b/Code/OysterGraphics/Core/ShaderManager.cpp index 58242107..fc5be1ed 100644 --- a/Code/OysterGraphics/Core/ShaderManager.cpp +++ b/Code/OysterGraphics/Core/ShaderManager.cpp @@ -5,345 +5,346 @@ const char* ShaderFunction = "main"; namespace Oyster { - bool LoadPrecompiled(std::wstring filename, Core::ShaderManager::ShaderType type, std::wstring name); - bool LoadCompile(std::wstring filename, Core::ShaderManager::ShaderType type, std::wstring name); - namespace + namespace Graphics { - struct ShaderData + bool LoadPrecompiled(std::wstring filename, Core::ShaderManager::ShaderType type, std::wstring name); + bool LoadCompile(std::wstring filename, Core::ShaderManager::ShaderType type, std::wstring name); + namespace { - size_t size; - char* data; - }; - std::vector PS; - std::map PSMap; + struct ShaderData + { + size_t size; + char* data; + }; + std::vector PS; + std::map PSMap; - std::vector GS; - std::map GSMap; + std::vector GS; + std::map GSMap; - std::vector CS; - std::map CSMap; + std::vector CS; + std::map CSMap; - std::vector VS; - std::vector VData; - std::map VSMap; - } - -#pragma region Init - bool Core::ShaderManager::Init(std::wstring filename, ShaderType type, std::wstring name, bool Precompiled) - { - if(Precompiled) - { - return LoadPrecompiled(filename, type, name); + std::vector VS; + std::vector VData; + std::map VSMap; } - else + + #pragma region Init + bool Core::ShaderManager::Init(std::wstring filename, ShaderType type, std::wstring name, bool Precompiled) { - return LoadCompile(filename, type, name); + if(Precompiled) + { + return LoadPrecompiled(filename, type, name); + } + else + { + return LoadCompile(filename, type, name); + } + return true; } - return true; - } - bool LoadPrecompiled(std::wstring filename, Core::ShaderManager::ShaderType type, std::wstring name) - { + bool LoadPrecompiled(std::wstring filename, Core::ShaderManager::ShaderType type, std::wstring name) + { - std::ifstream stream; - ShaderData sd; + std::ifstream stream; + ShaderData sd; - //Create Vertex shader and Layout - stream.open(filename, std::ifstream::in | std::ifstream::binary); - if(stream.good()) - { - stream.seekg(0, std::ios::end); - sd.size = size_t(stream.tellg()); - sd.data = new char[sd.size]; - stream.seekg(0, std::ios::beg); - stream.read(&sd.data[0], sd.size); - stream.close(); + //Create Vertex shader and Layout + stream.open(filename, std::ifstream::in | std::ifstream::binary); + if(stream.good()) + { + stream.seekg(0, std::ios::end); + sd.size = size_t(stream.tellg()); + sd.data = new char[sd.size]; + stream.seekg(0, std::ios::beg); + stream.read(&sd.data[0], sd.size); + stream.close(); - ID3D11VertexShader* vertex; - ID3D11GeometryShader* geometry; - ID3D11PixelShader* pixel; - ID3D11ComputeShader* compute; + ID3D11VertexShader* vertex; + ID3D11GeometryShader* geometry; + ID3D11PixelShader* pixel; + ID3D11ComputeShader* compute; + switch(type) + { + case Core::ShaderManager::ShaderType::Vertex: + + if(VSMap.count(name)) + return false; + + if(FAILED(Core::device->CreateVertexShader(sd.data, sd.size, 0, &vertex))) + { + return false; + } + VSMap[name] = VS.size(); + VS.push_back(vertex); + VData.push_back(sd); + break; + + case Core::ShaderManager::ShaderType::Hull: + case Core::ShaderManager::ShaderType::Domain: + + return false; + + case Core::ShaderManager::ShaderType::Geometry: + + if(GSMap.count(name)) + return false; + if(FAILED(Core::device->CreateGeometryShader(sd.data, sd.size, 0, &geometry))) + { + return false; + } + GSMap[name] = GS.size(); + GS.push_back(geometry); + break; + + case Core::ShaderManager::ShaderType::Pixel: + + if(PSMap.count(name)) + return false; + if(FAILED(Core::device->CreatePixelShader(sd.data, sd.size, 0, &pixel))) + { + return false; + } + PSMap[name] = PS.size(); + PS.push_back(pixel); + break; + + case Core::ShaderManager::ShaderType::Compute: + + if(CSMap.count(name)) + return false; + if(FAILED(Core::device->CreateComputeShader(sd.data, sd.size, 0, &compute))) + { + return false; + } + CSMap[name] = CS.size(); + CS.push_back(compute); + break; + } + } + else + { + return false; + } + return true; + } + + bool LoadCompile(std::wstring filename, Core::ShaderManager::ShaderType type, std::wstring name) + { + /// \todo error reporting + ID3D10Blob *Shader,*Error; switch(type) { - case Core::ShaderManager::ShaderType::Vertex: + case Core::ShaderManager::ShaderType::Pixel: - if(VSMap.count(name)) - return false; + ID3D11PixelShader* pixel; - if(FAILED(Core::Device->CreateVertexShader(sd.data, sd.size, 0, &vertex))) + if(FAILED(D3DCompileFromFile(filename.c_str(),NULL,NULL,ShaderFunction,"ps_5_0",0,0,&Shader,&Error))) { + std::string fel = (char*)Error->GetBufferPointer(); + Error->Release(); return false; } - VSMap[name] = VS.size(); - VS.push_back(vertex); - VData.push_back(sd); - break; - - case Core::ShaderManager::ShaderType::Hull: - case Core::ShaderManager::ShaderType::Domain: - - return false; - - case Core::ShaderManager::ShaderType::Geometry: - - if(GSMap.count(name)) - return false; - if(FAILED(Core::Device->CreateGeometryShader(sd.data, sd.size, 0, &geometry))) + if(FAILED(Core::device->CreatePixelShader(Shader->GetBufferPointer(),Shader->GetBufferSize(),NULL,&pixel))) { + Shader->Release(); return false; } - GSMap[name] = GS.size(); - GS.push_back(geometry); - break; - - case Core::ShaderManager::ShaderType::Pixel: - - if(PSMap.count(name)) - return false; - if(FAILED(Core::Device->CreatePixelShader(sd.data, sd.size, 0, &pixel))) - { - return false; - } - PSMap[name] = PS.size(); - PS.push_back(pixel); - break; - - case Core::ShaderManager::ShaderType::Compute: - - if(CSMap.count(name)) - return false; - if(FAILED(Core::Device->CreateComputeShader(sd.data, sd.size, 0, &compute))) - { - return false; - } - CSMap[name] = CS.size(); - CS.push_back(compute); - break; - } - } - else - { - return false; - } - return true; - } - - bool LoadCompile(std::wstring filename, Core::ShaderManager::ShaderType type, std::wstring name) - { - /// \todo error reporting - ID3D10Blob *Shader,*Error; - switch(type) - { - case Core::ShaderManager::ShaderType::Pixel: - - ID3D11PixelShader* pixel; - - if(FAILED(D3DCompileFromFile(filename.c_str(),NULL,NULL,ShaderFunction,"ps_5_0",0,0,&Shader,&Error))) - { - std::string fel = (char*)Error->GetBufferPointer(); - Error->Release(); - return false; - } - if(FAILED(Oyster::Core::Device->CreatePixelShader(Shader->GetBufferPointer(),Shader->GetBufferSize(),NULL,&pixel))) - { Shader->Release(); - return false; - } - Shader->Release(); - if(!PSMap.count(name)) - { - PSMap[name] = PS.size(); - PS.push_back(pixel); - } - else - { - PS[PSMap[name]] = pixel; - } - break; + if(!PSMap.count(name)) + { + PSMap[name] = PS.size(); + PS.push_back(pixel); + } + else + { + PS[PSMap[name]] = pixel; + } + break; - case Core::ShaderManager::ShaderType::Geometry: + case Core::ShaderManager::ShaderType::Geometry: - ID3D11GeometryShader* geometry; + ID3D11GeometryShader* geometry; - if(FAILED(D3DCompileFromFile(filename.c_str(),NULL,NULL,ShaderFunction,"gs_5_0",0,0,&Shader,&Error))) - { - std::string fel = (char*)Error->GetBufferPointer(); - Error->Release(); - return false; - } - if(FAILED(Oyster::Core::Device->CreateGeometryShader(Shader->GetBufferPointer(),Shader->GetBufferSize(),NULL,&geometry))) - { - Error->Release(); + if(FAILED(D3DCompileFromFile(filename.c_str(),NULL,NULL,ShaderFunction,"gs_5_0",0,0,&Shader,&Error))) + { + std::string fel = (char*)Error->GetBufferPointer(); + Error->Release(); + return false; + } + if(FAILED(Core::device->CreateGeometryShader(Shader->GetBufferPointer(),Shader->GetBufferSize(),NULL,&geometry))) + { + Shader->Release(); + return false; + } Shader->Release(); - return false; - } - Shader->Release(); - if(!GSMap.count(name)) - { - GSMap[name] = GS.size(); - GS.push_back(geometry); - } - else - { - GS[GSMap[name]] = geometry; - } - break; + if(!GSMap.count(name)) + { + GSMap[name] = GS.size(); + GS.push_back(geometry); + } + else + { + GS[GSMap[name]] = geometry; + } + break; - case Core::ShaderManager::ShaderType::Vertex: + case Core::ShaderManager::ShaderType::Vertex: - ID3D11VertexShader* vertex; + ID3D11VertexShader* vertex; - if(FAILED(D3DCompileFromFile(filename.c_str(),NULL,NULL,ShaderFunction,"vs_5_0",0,0,&Shader,&Error))) - { - std::string fel = (char*)Error->GetBufferPointer(); - Error->Release(); - return false; - } - if(FAILED(Oyster::Core::Device->CreateVertexShader(Shader->GetBufferPointer(),Shader->GetBufferSize(),NULL,&vertex))) - { - Shader->Release(); - return false; - } - if(!VSMap.count(name)) - { - VSMap[name] = VS.size(); - VS.push_back(vertex); - ShaderData sd; - sd.size = Shader->GetBufferSize(); - sd.data = new char[sd.size]; - memcpy(sd.data,Shader->GetBufferPointer(),sd.size); - VData.push_back(sd); - } - else - { - VS[VSMap[name]] = vertex; - delete[] VData[VSMap[name]].data; - VData[VSMap[name]].size = Shader->GetBufferSize(); - VData[VSMap[name]].data = new char[VData[VSMap[name]].size]; - memcpy(VData[VSMap[name]].data,Shader->GetBufferPointer(),VData[VSMap[name]].size); - } + if(FAILED(D3DCompileFromFile(filename.c_str(),NULL,NULL,ShaderFunction,"vs_5_0",0,0,&Shader,&Error))) + { + std::string fel = (char*)Error->GetBufferPointer(); + Error->Release(); + return false; + } + if(FAILED(Core::device->CreateVertexShader(Shader->GetBufferPointer(),Shader->GetBufferSize(),NULL,&vertex))) + { + Shader->Release(); + return false; + } + if(!VSMap.count(name)) + { + VSMap[name] = VS.size(); + VS.push_back(vertex); + ShaderData sd; + sd.size = Shader->GetBufferSize(); + sd.data = new char[sd.size]; + memcpy(sd.data,Shader->GetBufferPointer(),sd.size); + VData.push_back(sd); + } + else + { + VS[VSMap[name]] = vertex; + delete[] VData[VSMap[name]].data; + VData[VSMap[name]].size = Shader->GetBufferSize(); + VData[VSMap[name]].data = new char[VData[VSMap[name]].size]; + memcpy(VData[VSMap[name]].data,Shader->GetBufferPointer(),VData[VSMap[name]].size); + } - Shader->Release(); - break; + Shader->Release(); + break; + } + return true; } - return true; - } -#pragma endregion + #pragma endregion - void Core::ShaderManager::CreateInputLayout(const D3D11_INPUT_ELEMENT_DESC *desc, int ElementCount,int VertexIndex,ID3D11InputLayout *&Layout) - { - if(VertexIndex==-1) + void Core::ShaderManager::CreateInputLayout(const D3D11_INPUT_ELEMENT_DESC *desc, int ElementCount,int VertexIndex,ID3D11InputLayout *&Layout) + { + if(VertexIndex==-1) + { + Layout=0; + return; + } + Core::device->CreateInputLayout(desc,ElementCount,VData[VertexIndex].data,VData[VertexIndex].size,&Layout); + } + + #pragma region Get + int Core::ShaderManager::Get::Pixel(std::wstring Name) + { + if(PSMap.count(Name)) + return PSMap[Name]; + return -1; + } + + int Core::ShaderManager::Get::Vertex(std::wstring Name) + { + if(VSMap.count(Name)) + return VSMap[Name]; + return -1; + } + int Core::ShaderManager::Get::Geometry(std::wstring Name) + { + if(GSMap.count(Name)) + return GSMap[Name]; + return -1; + } + + int Core::ShaderManager::Get::Compute(std::wstring Name) + { + if(CSMap.count(Name)) + return CSMap[Name]; + return -1; + } + int Core::ShaderManager::Get::Hull(std::wstring Name) + { + return -1; + } + int Core::ShaderManager::Get::Domain(std::wstring Name) + { + return -1; + } + #pragma endregion + + #pragma region Set + /// \todo smart set + void Core::ShaderManager::Set::Pixel(int Index) + { + if(Index==-1) + Core::deviceContext->PSSetShader( NULL,NULL,0); + else + Core::deviceContext->PSSetShader( PS[Index],NULL,0); + } + void Core::ShaderManager::Set::Vertex(int Index) + { + if(Index==-1) + Core::deviceContext->VSSetShader( NULL,NULL,0); + else + Core::deviceContext->VSSetShader( VS[Index],NULL,0); + } + void Core::ShaderManager::Set::Geometry(int Index) + { + if(Index==-1) + Core::deviceContext->GSSetShader( NULL,NULL,0); + else + Core::deviceContext->GSSetShader( GS[Index],NULL,0); + } + void Core::ShaderManager::Set::Compute(int Index) + { + if(Index==-1) + Core::deviceContext->CSSetShader( NULL,NULL,0); + else + Core::deviceContext->CSSetShader( CS[Index],NULL,0); + } + /// \todo set Hull + void Core::ShaderManager::Set::Hull(int Index) { - Layout=0; return; } - Device->CreateInputLayout(desc,ElementCount,VData[VertexIndex].data,VData[VertexIndex].size,&Layout); - } + /// \todo set Domain + void Core::ShaderManager::Set::Domain(int Index) + { + return; + } + #pragma endregion -#pragma region Get - int Core::ShaderManager::Get::Pixel(std::wstring Name) - { - if(PSMap.count(Name)) - return PSMap[Name]; - return -1; + /// \todo smart Set ie. not resetting the shader + /// \todo research states + /// \todo smart buffer set + void Core::ShaderManager::SetShaderEffect(ShaderEffect se) + { + Set::Pixel(se.Shaders.Pixel); + Set::Vertex(se.Shaders.Vertex); + Set::Geometry(se.Shaders.Geometry); + Set::Compute(se.Shaders.Compute); + Core::deviceContext->IASetInputLayout(se.IAStage.Layout); + Core::deviceContext->IASetPrimitiveTopology(se.IAStage.Topology); + for(unsigned int i=0;iApply(i); + for(unsigned int i=0;iApply(i); + for(unsigned int i=0;iApply(i); + Core::deviceContext->RSSetState(se.RenderStates.Rasterizer); + Core::deviceContext->PSSetSamplers(0,se.RenderStates.SampleCount,se.RenderStates.SampleState); + float test[4] = {0}; + Core::deviceContext->OMSetBlendState(se.RenderStates.BlendState,test,-1); + } } - - int Core::ShaderManager::Get::Vertex(std::wstring Name) - { - if(VSMap.count(Name)) - return VSMap[Name]; - return -1; - } - int Core::ShaderManager::Get::Geometry(std::wstring Name) - { - if(GSMap.count(Name)) - return GSMap[Name]; - return -1; - } - - int Core::ShaderManager::Get::Compute(std::wstring Name) - { - if(CSMap.count(Name)) - return CSMap[Name]; - return -1; - } - int Core::ShaderManager::Get::Hull(std::wstring Name) - { - return -1; - } - int Core::ShaderManager::Get::Domain(std::wstring Name) - { - return -1; - } -#pragma endregion - -#pragma region Set - /// \todo smart set - void Core::ShaderManager::Set::Pixel(int Index) - { - if(Index==-1) - DeviceContext->PSSetShader( NULL,NULL,0); - else - DeviceContext->PSSetShader( PS[Index],NULL,0); - } - void Core::ShaderManager::Set::Vertex(int Index) - { - if(Index==-1) - DeviceContext->VSSetShader( NULL,NULL,0); - else - DeviceContext->VSSetShader( VS[Index],NULL,0); - } - void Core::ShaderManager::Set::Geometry(int Index) - { - if(Index==-1) - DeviceContext->GSSetShader( NULL,NULL,0); - else - DeviceContext->GSSetShader( GS[Index],NULL,0); - } - void Core::ShaderManager::Set::Compute(int Index) - { - if(Index==-1) - DeviceContext->CSSetShader( NULL,NULL,0); - else - DeviceContext->CSSetShader( CS[Index],NULL,0); - } - /// \todo set Hull - void Core::ShaderManager::Set::Hull(int Index) - { - return; - } - /// \todo set Domain - void Core::ShaderManager::Set::Domain(int Index) - { - return; - } -#pragma endregion - - /// \todo smart Set ie. not resetting the shader - /// \todo research states - /// \todo smart buffer set - void Core::ShaderManager::SetShaderEffect(ShaderEffect se) - { - Set::Pixel(se.Shaders.Pixel); - Set::Vertex(se.Shaders.Vertex); - Set::Geometry(se.Shaders.Geometry); - Set::Compute(se.Shaders.Compute); - Oyster::Core::DeviceContext->IASetInputLayout(se.IAStage.Layout); - Oyster::Core::DeviceContext->IASetPrimitiveTopology(se.IAStage.Topology); - for(unsigned int i=0;iApply(i); - for(unsigned int i=0;iApply(i); - for(unsigned int i=0;iApply(i); - Oyster::Core::DeviceContext->RSSetState(se.RenderStates.Rasterizer); - Oyster::Core::DeviceContext->PSSetSamplers(0,se.RenderStates.SampleCount,se.RenderStates.SampleState); - float test[4] = {0}; - Oyster::Core::DeviceContext->OMSetBlendState(se.RenderStates.BlendState,test,-1); - } - } \ No newline at end of file diff --git a/Code/OysterGraphics/Engine.h b/Code/OysterGraphics/Engine.h index bf335d05..0486bdf2 100644 --- a/Code/OysterGraphics/Engine.h +++ b/Code/OysterGraphics/Engine.h @@ -9,157 +9,160 @@ namespace Oyster { - class Engine + namespace Graphics { - private: - Engine(); - ~Engine(); + class Engine + { + private: + Engine(); + ~Engine(); - public: - class Init - { public: - struct Setup - { - int NrOfBuffers; - bool MSAA_Quality; - bool Fullscreen; - bool SingleThreaded; - bool Reference; - bool ForceDX11; - bool GenerateDepthStencil; - bool BindDefault; - HWND window; - //all but Window params have Default Values - Setup() - { - NrOfBuffers=1; - MSAA_Quality = false; - Fullscreen = true; - SingleThreaded = true; - Reference = false; - ForceDX11 = false; - GenerateDepthStencil = true; - BindDefault = true; - } - - }; - - static bool IsInstanced(); - - //Creates Device and DeviceContext, if not Initialized - static bool Instance(bool SingleThreaded=true,bool Reference=false,bool ForceDX11=false); - static bool HasSwapChain(); - - //Creates Swapchain, if not Aready Created - static bool CreateSwapChain(HWND Window, int NrofBuffers=1,bool MSAA_Quality=false,bool Fullscreen=true); - - //CreateWindow, if Not Already Created - static bool InitializeWindow(const LPCSTR appName, const LPCSTR className,const HINSTANCE &hInstance, const int &nCmdShow, WNDPROC wProc, bool HandleLoop = false ); - - //Performs a full initialization of a rendering pipeline, including a Window - static bool FullInit(const Setup& setup); - struct Buffers - { - static Buffer* CreateBuffer(const Buffer::BUFFER_INIT_DESC BufferDesc); - }; - private: - Init(); - ~Init(); - }; - - class States - { - public: - //SSAO Quality - static void SetNrOfSSAOSamples(int); - static int GetNrOfSSAOSamples(); - - //SSAO Frequency - static void SetSSAOSampleSpread(int); - static int GetSSAOSampleSpread(); - - //PointLights - static void SetMaxPointlights(int); - static int GetMaxPointlights(); - - - private: - States(); - ~States(); - }; - - class Render - { - public: - /// Render a number of models, setting the Per model data to the included cBuffer - /// specify NULL if no such data exists - static void Geometry(const Oyster::Render::Model* models,int count,Oyster::Buffer* cBufferEveryObject, int slot); - static void Text(std::string text, Oyster::Math::Float2 size, Oyster::Math::Float3 Pos); - //static void TextBox(const Oyster::Render:: - - //ensure that a compatible 2D shadereffect is applied - static void ScreenQuad(ID3D11ShaderResourceView* srv, float ZPos=1); - - //ensure that a compatible 2D shadereffect is applied and that pos.z is between 0 and 1 - static void Sprite(ID3D11ShaderResourceView* srv, Oyster::Math::Float2 size, Oyster::Math::Float3 Pos); - - static void PresentScene(); - - private: - Render(); - ~Render(); - }; - - class PrepareForRendering - { - public: - //Binds several rendertargets and a depthstencil - static void BindRenderTargets(ID3D11RenderTargetView** RenderTargets,int NrOfTargets,ID3D11DepthStencilView* depth); - - //Binds several Rendertargest and a default depthstencil - static void BindRenderTargets(ID3D11RenderTargetView** RenderTargets,int NrOfTargets); - - //Binds the backbuffer and a depthstencil - static void BindBackBuffer(ID3D11DepthStencilView* depth); - - //Binds the backbuffer and a default depthstencil - static void BindBackBuffer(); - - //Binds the backbuffer to the compute shader - static void BindBackBufferAsUAV(); - - //binds several UAV to the computeshader - static void BindUAV(ID3D11UnorderedAccessView** uav, int NrOfUavs); - - //Clears the backbuffer and default depthstencil - static void ClearBackBuffer(Math::Float4 color); - - static void Begin2DRender(); - - static void Begin2DTextRender(); - }; - - class Pipeline - { - public: - class Deffered_Lightning + /*class Init { public: - //Basic Setup - static void NewFrame(const Float4& Color, const Matrix& View, const Matrix& Projection); + struct Setup + { + int NrOfBuffers; + bool MSAA_Quality; + bool Fullscreen; + bool SingleThreaded; + bool Reference; + bool ForceDX11; + bool GenerateDepthStencil; + bool BindDefault; + HWND window; + //all but Window params have Default Values + Setup() + { + NrOfBuffers=1; + MSAA_Quality = false; + Fullscreen = true; + SingleThreaded = true; + Reference = false; + ForceDX11 = false; + GenerateDepthStencil = true; + BindDefault = true; + } - //Geometry Pass - static void BeginRenderGeometry(); - static void RenderGeometry(const Oyster::Render::Model* models,int count); - static void EndRenderGeometry(); + }; - //Lightning Pass - static void InputPointLights(Oyster::Resources::BufferDefinitions::PointLightDescription *p, int NrOfPointlights ); - static void RenderLightning(); + static bool IsInstanced(); + + //Creates Device and DeviceContext, if not Initialized + static bool Instance(bool SingleThreaded=true,bool Reference=false,bool ForceDX11=false); + static bool HasSwapChain(); + + //Creates Swapchain, if not Aready Created + static bool CreateSwapChain(HWND Window, int NrofBuffers=1,bool MSAA_Quality=false,bool Fullscreen=true); + + //CreateWindow, if Not Already Created + static bool InitializeWindow(const LPCSTR appName, const LPCSTR className,const HINSTANCE &hInstance, const int &nCmdShow, WNDPROC wProc, bool HandleLoop = false ); + + //Performs a full initialization of a rendering pipeline, including a Window + static bool FullInit(const Setup& setup); + struct Buffers + { + static Buffer* CreateBuffer(const Buffer::BUFFER_INIT_DESC BufferDesc); + }; + private: + Init(); + ~Init(); + };*/ + + class States + { + public: + //SSAO Quality + static void SetNrOfSSAOSamples(int); + static int GetNrOfSSAOSamples(); + + //SSAO Frequency + static void SetSSAOSampleSpread(int); + static int GetSSAOSampleSpread(); + + //PointLights + static void SetMaxPointlights(int); + static int GetMaxPointlights(); + + + private: + States(); + ~States(); + }; + + class Render + { + public: + /// Render a number of models, setting the Per model data to the included cBuffer + /// specify NULL if no such data exists + //static void Geometry(const Oyster::Graphics::Render::Model* models,int count,Buffer* cBufferEveryObject, int slot); + static void Text(std::string text, Oyster::Math::Float2 size, Oyster::Math::Float3 Pos); + //static void TextBox(const Oyster::Render:: + + //ensure that a compatible 2D shadereffect is applied + static void ScreenQuad(ID3D11ShaderResourceView* srv, float ZPos=1); + + //ensure that a compatible 2D shadereffect is applied and that pos.z is between 0 and 1 + static void Sprite(ID3D11ShaderResourceView* srv, Oyster::Math::Float2 size, Oyster::Math::Float3 Pos); + + static void PresentScene(); + + private: + Render(); + ~Render(); + }; + + class PrepareForRendering + { + public: + //Binds several rendertargets and a depthstencil + static void BindRenderTargets(ID3D11RenderTargetView** RenderTargets,int NrOfTargets,ID3D11DepthStencilView* depth); + + //Binds several Rendertargest and a default depthstencil + static void BindRenderTargets(ID3D11RenderTargetView** RenderTargets,int NrOfTargets); + + //Binds the backbuffer and a depthstencil + static void BindBackBuffer(ID3D11DepthStencilView* depth); + + //Binds the backbuffer and a default depthstencil + static void BindBackBuffer(); + + //Binds the backbuffer to the compute shader + static void BindBackBufferAsUAV(); + + //binds several UAV to the computeshader + static void BindUAV(ID3D11UnorderedAccessView** uav, int NrOfUavs); + + //Clears the backbuffer and default depthstencil + static void ClearBackBuffer(Math::Float4 color); + + static void Begin2DRender(); + + static void Begin2DTextRender(); + }; + + class Pipeline + { + public: + class Deffered_Lightning + { + public: + //Basic Setup + //static void NewFrame(const Float4& Color, const Matrix& View, const Matrix& Projection); + + //Geometry Pass + static void BeginRenderGeometry(); + //static void RenderGeometry(const Oyster::Graphics::Render::Model* models,int count); + static void EndRenderGeometry(); + + //Lightning Pass + //static void InputPointLights(Oyster::Resources::BufferDefinitions::PointLightDescription *p, int NrOfPointlights ); + static void RenderLightning(); + }; }; }; - }; + } }; #endif \ No newline at end of file diff --git a/Code/OysterGraphics/EngineIncludes.h b/Code/OysterGraphics/EngineIncludes.h index b4eed794..a2a1e966 100644 --- a/Code/OysterGraphics/EngineIncludes.h +++ b/Code/OysterGraphics/EngineIncludes.h @@ -1,9 +1,9 @@ //Oyster // Render -#include "Render\Model.h" -#include "Render\Camera.h" -#include "Render\TextBox.h" +//#include "Render\Model.h" +//#include "Render\Camera.h" +//#include "Render\TextBox.h" // Core #include "Core\Core.h" @@ -16,8 +16,8 @@ #include "OysterMath.h" // Resources -#include "Resourses\ShaderEffects.h" -#include "Resourses\Buffers.h" -#include "Resourses\PipelineResources.h" -#include "Resourses\GraphicsDefinitions.h" -#include "Resourses\Manager.h" \ No newline at end of file +//#include "Resourses\ShaderEffects.h" +//#include "Resourses\Buffers.h" +//#include "Resourses\PipelineResources.h" +//#include "Resourses\GraphicsDefinitions.h" +//#include "Resourses\Manager.h" \ No newline at end of file diff --git a/Code/OysterGraphics/FileLoader/ObjReader.cpp b/Code/OysterGraphics/FileLoader/ObjReader.cpp index e4d12b91..b5ad5d4e 100644 --- a/Code/OysterGraphics/FileLoader/ObjReader.cpp +++ b/Code/OysterGraphics/FileLoader/ObjReader.cpp @@ -1,268 +1,122 @@ -#include "ObjReader.h" -#include "Utilities.h" -#include "..\Core\Core.h" +#include "OBJReader.h" +#include #include -#include using namespace std; -using namespace Oyster::FileLoaders; -using namespace Oyster; -using namespace Oyster::Math; - -ObjReader *ObjReader::LoadFile(std::string fileName, Oyster::Math::Float4x4 transform) +OBJReader::OBJReader() { - static std::map cache; - - ObjReader *reader = NULL; - - if (cache.count(fileName)) - { - reader = cache[fileName]; - } - else - { - reader = new ObjReader(); - reader->ParseFile(fileName, transform); - - cache[fileName] = reader; - } - - return reader; + _mPos = 0; + _mNormal = 0; + _mTexel = 0; } -ObjReader::ObjReader(void) +OBJReader::~OBJReader() { + } - -ObjReader::~ObjReader(void) +void OBJReader::readOBJFile( wstring fileName ) { -} + fstream inStream; + string typeOfData = " "; + float vertexData; + string face1, face2, face3; -void ObjReader::ParseFile(std::string fileName, Float4x4 transform) -{ - ifstream input; - input.open(fileName.c_str()); - - if(!input.is_open()) + inStream.open( fileName, fstream::in ); + + if( inStream.is_open() ) { - return; - } - - string path; - Utility::String::extractDirPath(path,fileName,'\\'); - - std::vector VertexList; - std::vector vList; - std::vector nList; - std::vector uvList; - Vertex vertex1, vertex2, vertex3; - Float3 face[3]; - Float3 position, normal; - Float2 uv; - string s; - - while(!input.eof()) - { - getline(input,s); - int offset = (int)s.find(' '); - - if(offset!=-1) + while( !inStream.eof() ) { - string c = s.substr(0,offset); + inStream >> typeOfData; - if(c=="v") + if( typeOfData == "v" ) { - position = readVertex(offset,s); - vList.push_back(position); + Oyster::Math::Float3 position; + + inStream >> vertexData; + position.x = vertexData; + + inStream >> vertexData; + position.y = vertexData; + + inStream >> vertexData; + position.z = vertexData; + + _mVertexCoord.push_back( position ); + } - else if(c=="vt") + else if( typeOfData == "vt" ) { - uv = readUV(offset,s); - uvList.push_back(uv); + Oyster::Math::Float2 texel; + inStream >> vertexData; + texel.x = vertexData; + + inStream >> vertexData; + texel.y = 1 - vertexData; + + _mVertexTexture.push_back( texel ); } - else if(c=="vn") + else if( typeOfData == "vn" ) { - normal = readNormal(offset,s); - nList.push_back(normal); + Oyster::Math::Float3 normal; + inStream >> vertexData; + normal.x = vertexData; + + inStream >> vertexData; + normal.y = vertexData; + + inStream >> vertexData; + normal.z = vertexData; + + _mVertexNormal.push_back( normal ); } - else if(c=="f") + else if( typeOfData == "f" ) { - readFace(offset, s, face); + inStream >> face1; + stringSplit( face1 ); + + addToOBJarray(); - vertex1.Position = vList[(int)face[0].x]; - vertex1.UV = uvList[(int)face[0].y]; - vertex1.Normal = nList[(int)face[0].z]; + inStream >> face2; + stringSplit( face2 ); - vertex2.Position = vList[(int)face[1].x]; - vertex2.UV = uvList[(int)face[1].y]; - vertex2.Normal = nList[(int)face[1].z]; + addToOBJarray(); - vertex3.Position = vList[(int)face[2].x]; - vertex3.UV = uvList[(int)face[2].y]; - vertex3.Normal = nList[(int)face[2].z]; + inStream >> face3; + stringSplit( face3 ); - VertexList.push_back(vertex1); - VertexList.push_back(vertex3); - VertexList.push_back(vertex2); - } - else if(c=="mtllib") - { - this->materials = GetMaterials(path+s.substr(offset+1)); + addToOBJarray(); } } } - input.close(); - - this->numVertices = VertexList.size(); - this->vertices = new Vertex[this->numVertices]; - - for(size_t i=0;inumVertices;++i) - { - vertices[i].Position=Math::transformVector(Math::Float4(VertexList[i].Position,1),transform); - vertices[i].Normal=Math::transformVector(Math::Float4(VertexList[i].Normal,0),transform); - vertices[i].UV = VertexList[i].UV; - } + inStream.close(); } -void ObjReader::GetVertexData(Vertex **vertex,int &numVertex, std::map &Textures) +//Private functions +void OBJReader::stringSplit( string strToSplit ) { - numVertex=(int)this->numVertices; - (*vertex)=this->vertices; - Textures = this->materials; + char delim = '/'; + string vPos, vNormal, vTexel; + stringstream aStream(strToSplit); + getline( aStream, vPos, delim ); + getline( aStream, vTexel, delim ); + getline( aStream, vNormal ); + + _mPos = atoi( vPos.c_str() ); + _mNormal = atoi( vNormal.c_str() ); + _mTexel = atoi( vTexel.c_str() ); + } -Float3 ObjReader::extract(std::string d) +void OBJReader::addToOBJarray() { - Float3 data; - int offset=(int)d.find('/'); - data.x=(float)atoi(d.substr(1,offset).c_str())-1; + OBJFormat temp; - int newOffset = (int)d.find('/',offset+1); - string d2=d.substr(offset+1,newOffset-offset-1); - data.y=(float)atoi(d2.c_str())-1; - offset=newOffset; + temp._d3VertexCoord = _mVertexCoord.at( _mPos - 1 ); + temp._d3VertexNormal = _mVertexNormal.at( _mNormal - 1 ); + temp._d3VertexTexture = _mVertexTexture.at( _mTexel - 1 ); - newOffset = (int)d.find('/',offset+1); - string d3=d.substr(offset+1,newOffset-offset-1); - data.z=(float)atoi(d3.c_str())-1; - - return data; -} - -Float3 ObjReader::readVertex(int offset,string s) -{ - int newOffset = (int)s.find(' ',offset+1); - Float3 vertex; - string d = s.substr(offset,newOffset-offset); - vertex.x = (float)atof(d.c_str()); - offset=newOffset; - - newOffset = (int)s.find(' ',offset+1); - vertex.y = (float)atof(s.substr(offset,newOffset-offset).c_str()); - offset=newOffset; - - newOffset = (int)s.find(' ',offset+1); - vertex.z = (float)-atof(s.substr(offset,newOffset-offset).c_str()); - - return vertex; -} - -Float2 ObjReader::readUV(int offset,string s) -{ - int newOffset = (int)s.find(' ',offset+1); - Float2 uv; - string d = s.substr(offset,newOffset-offset); - uv.x =(float)atof(d.c_str()); - offset=newOffset; - - newOffset = (int)s.find(' ',offset+1); - d = s.substr(offset,newOffset-offset); - uv.y =1- (float)atof(d.c_str()); - offset=newOffset; - - return uv; -} - -Float3 ObjReader::readNormal(int offset,string s) -{ - int newOffset = (int)s.find(' ',offset+1); - Float3 vertex; - string d = s.substr(offset,newOffset-offset); - vertex.x = (float)atof(d.c_str()); - offset=newOffset; - - newOffset = (int)s.find(' ',offset+1); - vertex.y = (float)atof(s.substr(offset,newOffset-offset).c_str()); - offset=newOffset; - - newOffset = (int)s.find(' ',offset+1); - vertex.z = (float)-atof(s.substr(offset,newOffset-offset).c_str()); - - return vertex; -} - -void ObjReader::readFace(int offset,string s, Oyster::Math::Float3 face[3]) -{ - int newOffset = (int)s.find(' ',offset+1); - string point1 = s.substr(offset,newOffset-offset); - - offset = newOffset; - newOffset = (int)s.find(' ',offset+1); - string point2 = s.substr(offset,newOffset-offset); - - offset = newOffset; - newOffset = (int)s.find(' ',offset+1); - string point3 = s.substr(offset,newOffset-offset); - - face[0] = extract(point1); - face[1] = extract(point2); - face[2] = extract(point3); -} - -std::map ObjReader::GetMaterials(std::string fileName) -{ - ifstream input; - input.open(fileName.c_str()); - - std::map materials; - ID3D11ShaderResourceView *srv; - string texture; - string s; - string path; - Utility::String::extractDirPath(path,fileName,'\\'); - if(!input.is_open()) - return materials; - - while(!input.eof()) - { - getline(input,s); - int offset = (int)s.find(' '); - if(offset!=-1) - { - string c = s.substr(0,offset); - if(c=="map_Kd") - { - texture = path+s.substr(offset+1); - D3DX11CreateShaderResourceViewFromFile(Oyster::Core::Device,texture.c_str(), NULL, NULL, &srv, NULL); - materials["Diffuse"] = srv; - } - if(c=="map_G") - { - texture = path+s.substr(offset+1); - D3DX11CreateShaderResourceViewFromFile(Oyster::Core::Device,texture.c_str(), NULL, NULL, &srv, NULL); - materials["Glow"] = srv; - } - if(c=="map_Ks") - { - texture = path+s.substr(offset+1); - D3DX11CreateShaderResourceViewFromFile(Oyster::Core::Device,texture.c_str(), NULL, NULL, &srv, NULL); - materials["Specular"] = srv; - } - } - } - input.close(); - - return materials; -} + _myOBJ.push_back( temp ); +} \ No newline at end of file diff --git a/Code/OysterGraphics/FileLoader/ObjReader.h b/Code/OysterGraphics/FileLoader/ObjReader.h index a846181e..562d81fc 100644 --- a/Code/OysterGraphics/FileLoader/ObjReader.h +++ b/Code/OysterGraphics/FileLoader/ObjReader.h @@ -1,42 +1,53 @@ -#pragma once -#include "..\Core\CoreIncludes.h" -#include "..\Math\OysterMath.h" +#ifndef OBJREADER_H +#define OBJREADER_H +#include "..\..\Misc\Utilities.h" +#include "..\..\OysterMath\OysterMath.h" -namespace Oyster +//#include + + + +class OBJReader { - namespace FileLoaders - { - class ObjReader + public: + struct OBJFormat { - public: - struct Vertex - { - Oyster::Math::Float3 Position; - Oyster::Math::Float3 Normal; - Oyster::Math::Float2 UV; - }; - - static ObjReader *LoadFile(std::string fileName, Oyster::Math::Float4x4 transform = Oyster::Math::Float4x4::identity); - - ObjReader(void); - ~ObjReader(void); - - void GetVertexData(Vertex **vertex,int &numVertex, std::map &textures); - - private: - Vertex *vertices; - size_t numVertices; - std::map materials; - - void ParseFile(std::string fileName, Oyster::Math::Float4x4 transform = Oyster::Math::Float4x4::identity); - - Oyster::Math::Float3 extract(std::string d); - Oyster::Math::Float3 readVertex(int offset,std::string s); - Oyster::Math::Float2 readUV(int offset,std::string s); - Oyster::Math::Float3 readNormal(int offset,std::string s); - void readFace(int offset,std::string s, Oyster::Math::Float3 face[3]); - - std::map GetMaterials(std::string fileName); + Oyster::Math::Float3 _d3VertexCoord; + Oyster::Math::Float2 _d3VertexTexture; + Oyster::Math::Float3 _d3VertexNormal; }; - } -} \ No newline at end of file + + struct OBJMaterialData + { + string _name; + string _mapKd; + float _kd[3]; + float _ka[3]; + float _tf[3]; + float _ni; + + OBJMaterialData() + { + _name = " "; + _mapKd = " "; + } + }; + std::vector _myOBJ; + private: + + vector _mVertexCoord, _mVertexNormal; + vector _mVertexTexture; + + int _mNrOfCoords, _mNrOfNormals, _mNrOfTexels, _mNrOfFaces; + int _mPos, _mNormal, _mTexel; + void stringSplit( string strToSplit ); + void addToOBJarray(); + + public: + OBJReader(); + ~OBJReader(); + + void readOBJFile( wstring fileName); + +}; +#endif \ No newline at end of file diff --git a/Code/OysterGraphics/Render/Model.h b/Code/OysterGraphics/Model/Model.h similarity index 67% rename from Code/OysterGraphics/Render/Model.h rename to Code/OysterGraphics/Model/Model.h index c6f59050..05a9a5c3 100644 --- a/Code/OysterGraphics/Render/Model.h +++ b/Code/OysterGraphics/Model/Model.h @@ -15,16 +15,17 @@ using namespace Oyster::Math; namespace Oyster { - namespace Render + namespace Graphics { - - struct Model + namespace Render { - ModelInfo* info; - Float4x4 *World; - bool Visible; - }; - + struct Model + { + ModelInfo* info; + Float4x4 *World; + bool Visible; + }; + } }; }; diff --git a/Code/OysterGraphics/Render/ModelInfo.h b/Code/OysterGraphics/Model/ModelInfo.h similarity index 55% rename from Code/OysterGraphics/Render/ModelInfo.h rename to Code/OysterGraphics/Model/ModelInfo.h index a4702ca1..2c968b00 100644 --- a/Code/OysterGraphics/Render/ModelInfo.h +++ b/Code/OysterGraphics/Model/ModelInfo.h @@ -14,16 +14,19 @@ using namespace Oyster::Math; namespace Oyster { - namespace Render + namespace Graphics { - struct ModelInfo + namespace Render { - std::vector Material; - Oyster::Buffer Vertices,Indecies; - bool Indexed; - int VertexCount; - }; - }; + struct ModelInfo + { + std::vector Material; + Buffer Vertices,Indecies; + bool Indexed; + int VertexCount; + }; + } + } }; #endif \ No newline at end of file diff --git a/Code/OysterGraphics/Render/Camera.cpp b/Code/OysterGraphics/OldRender/Camera.cpp similarity index 100% rename from Code/OysterGraphics/Render/Camera.cpp rename to Code/OysterGraphics/OldRender/Camera.cpp diff --git a/Code/OysterGraphics/Render/Camera.h b/Code/OysterGraphics/OldRender/Camera.h similarity index 100% rename from Code/OysterGraphics/Render/Camera.h rename to Code/OysterGraphics/OldRender/Camera.h diff --git a/Code/OysterGraphics/Render/Lights.h b/Code/OysterGraphics/OldRender/Lights.h similarity index 100% rename from Code/OysterGraphics/Render/Lights.h rename to Code/OysterGraphics/OldRender/Lights.h diff --git a/Code/OysterGraphics/Render/Model.cpp b/Code/OysterGraphics/OldRender/Model.cpp similarity index 100% rename from Code/OysterGraphics/Render/Model.cpp rename to Code/OysterGraphics/OldRender/Model.cpp diff --git a/Code/OysterGraphics/Render/TextBox.cpp b/Code/OysterGraphics/OldRender/TextBox.cpp similarity index 100% rename from Code/OysterGraphics/Render/TextBox.cpp rename to Code/OysterGraphics/OldRender/TextBox.cpp diff --git a/Code/OysterGraphics/OldRender/TextBox.h b/Code/OysterGraphics/OldRender/TextBox.h new file mode 100644 index 00000000..6b95dce7 --- /dev/null +++ b/Code/OysterGraphics/OldRender/TextBox.h @@ -0,0 +1,70 @@ +#pragma once + +#include "..\Engine.h" +const int MAX_LETTER_COUNT=60; +const int TEXT_NR_LETTERS=95; +const float TEXT_SIZE=2.5; +struct Text2D +{ + Oyster::Math::Float Pos; + int offset; + float coff; +}; +/*struct TextInstanceData +{ + Oyster::Buffer InstanceBuffer; + bool Visible; + int NumLetters; + Oyster::Math::Float4x4 World; +};*/ +/*struct TextData +{ + Oyster::Math::Float3 pos; + Oyster::Math::Float2 uv; +}; + +struct PerCharData +{ + float data; + Oyster::Math::Float3 charOffset; +}; +struct TextInstanceData +{ + Oyster::Buffer InstanceBuffer; + bool Visible; + int NumLetters; + Oyster::Math::Float4x4 World; +};*/ + +namespace Oyster +{ + namespace Graphics + { + namespace Render + { + class Textbox + { + private: + static float getCharID(char _in); + static HRESULT CreateVertexBuffer(); + static HRESULT CreateTextfield(int _id); + public: + //static Oyster::Buffer TextBuffer; + //static int NumVertices; + //static std::vector TextInstances; + static Buffer TextBuffer; + static int NumLetters; + static ID3D11ShaderResourceView* Texture; + + static bool Init(); + static bool UpdateTextField(std::string _str); + static bool SetTexture(const char* _file); + //Updates a textbox with the certain id + static void Update(std::string _str, float _scale); + //Removes all old instances and recreates it with the input data + static HRESULT Reset(int _count, std::string* _str, Float3* _pos); + static void Apply(int _id); + }; + } + } +} diff --git a/Code/OysterGraphics/Resourses/Buffers.cpp b/Code/OysterGraphics/OldResourses/Buffers.cpp similarity index 100% rename from Code/OysterGraphics/Resourses/Buffers.cpp rename to Code/OysterGraphics/OldResourses/Buffers.cpp diff --git a/Code/OysterGraphics/Resourses/Buffers.h b/Code/OysterGraphics/OldResourses/Buffers.h similarity index 100% rename from Code/OysterGraphics/Resourses/Buffers.h rename to Code/OysterGraphics/OldResourses/Buffers.h diff --git a/Code/OysterGraphics/Resourses/GraphicsDefinitions.h b/Code/OysterGraphics/OldResourses/GraphicsDefinitions.h similarity index 100% rename from Code/OysterGraphics/Resourses/GraphicsDefinitions.h rename to Code/OysterGraphics/OldResourses/GraphicsDefinitions.h diff --git a/Code/OysterGraphics/Resourses/Manager.cpp b/Code/OysterGraphics/OldResourses/Manager.cpp similarity index 100% rename from Code/OysterGraphics/Resourses/Manager.cpp rename to Code/OysterGraphics/OldResourses/Manager.cpp diff --git a/Code/OysterGraphics/Resourses/Manager.h b/Code/OysterGraphics/OldResourses/Manager.h similarity index 100% rename from Code/OysterGraphics/Resourses/Manager.h rename to Code/OysterGraphics/OldResourses/Manager.h diff --git a/Code/OysterGraphics/Resourses/PipelineResources.cpp b/Code/OysterGraphics/OldResourses/PipelineResources.cpp similarity index 100% rename from Code/OysterGraphics/Resourses/PipelineResources.cpp rename to Code/OysterGraphics/OldResourses/PipelineResources.cpp diff --git a/Code/OysterGraphics/Resourses/PipelineResources.h b/Code/OysterGraphics/OldResourses/PipelineResources.h similarity index 100% rename from Code/OysterGraphics/Resourses/PipelineResources.h rename to Code/OysterGraphics/OldResourses/PipelineResources.h diff --git a/Code/OysterGraphics/Resourses/ShaderEffects.cpp b/Code/OysterGraphics/OldResourses/ShaderEffects.cpp similarity index 100% rename from Code/OysterGraphics/Resourses/ShaderEffects.cpp rename to Code/OysterGraphics/OldResourses/ShaderEffects.cpp diff --git a/Code/OysterGraphics/Resourses/ShaderEffects.h b/Code/OysterGraphics/OldResourses/ShaderEffects.h similarity index 100% rename from Code/OysterGraphics/Resourses/ShaderEffects.h rename to Code/OysterGraphics/OldResourses/ShaderEffects.h diff --git a/Code/OysterGraphics/OysterGraphics.vcxproj b/Code/OysterGraphics/OysterGraphics.vcxproj index 45df7d9d..7dfb0e7a 100644 --- a/Code/OysterGraphics/OysterGraphics.vcxproj +++ b/Code/OysterGraphics/OysterGraphics.vcxproj @@ -90,11 +90,14 @@ Level3 Disabled true - ..\OysterMath;..\Misc;%(AdditionalIncludeDirectories) + $(SolutionDir)OysterMath;$(SolutionDir)Misc;%(AdditionalIncludeDirectories) true + + true + @@ -140,31 +143,22 @@ + - - - - - - - + + + - - - - - - - - - - - + + + + + @@ -175,6 +169,12 @@ + + Vertex + Vertex + Vertex + Vertex + Pixel Pixel @@ -192,6 +192,8 @@ true 5.0 main + + diff --git a/Code/OysterGraphics/OysterGraphics.vcxproj.filters b/Code/OysterGraphics/OysterGraphics.vcxproj.filters index 412e083a..734eba8b 100644 --- a/Code/OysterGraphics/OysterGraphics.vcxproj.filters +++ b/Code/OysterGraphics/OysterGraphics.vcxproj.filters @@ -21,30 +21,21 @@ Source Files - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - Source Files + + Source Files + + + Source Files + + + Source Files + + + Source Files + @@ -56,45 +47,28 @@ Header Files - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - Header Files - + + Header Files + + + Header Files + + + Header Files + + + Header Files + + Header Files + \ No newline at end of file diff --git a/Code/OysterGraphics/Render/Preparations/Basic.cpp b/Code/OysterGraphics/Render/Preparations/Basic.cpp new file mode 100644 index 00000000..e6857664 --- /dev/null +++ b/Code/OysterGraphics/Render/Preparations/Basic.cpp @@ -0,0 +1,84 @@ +#include "Preparations.h" + +namespace Oyster +{ + namespace Graphics + { + namespace Render + { + namespace Preparations + { + void Basic::BindBackBufferRTV(bool DepthStencil) + { + if(DepthStencil) + { + Core::deviceContext->OMSetRenderTargets(1,&Core::backBufferRTV,Core::depthStencil); + } + else + { + Core::deviceContext->OMSetRenderTargets(1,&Core::backBufferRTV,NULL); + } + } + + void Basic::BindBackBufferRTV(ID3D11DepthStencilView& depthStencil) + { + Core::deviceContext->OMSetRenderTargets(1,&Core::backBufferRTV,&depthStencil); + } + + void Basic::BindBackBufferUAV() + { + Core::deviceContext->CSSetUnorderedAccessViews(0,1,&Core::backBufferUAV,0); + } + + void Basic::BindRTV(ID3D11RenderTargetView* RTVs[], int size, bool UseDepthStencil) + { + if(UseDepthStencil) + { + BindRTV(RTVs, size, Core::depthStencil); + } + else + { + Core::deviceContext->OMSetRenderTargets(size,RTVs,NULL); + } + } + + void Basic::BindRTV(ID3D11RenderTargetView* RTVs[], int size,ID3D11DepthStencilView& depthStencil) + { + Core::deviceContext->OMSetRenderTargets(size,RTVs,&depthStencil); + } + + void Basic::BindUAV(ID3D11UnorderedAccessView* UAVs[], int size) + { + Core::deviceContext->CSSetUnorderedAccessViews(0,size,UAVs,0); + } + + void Basic::ClearBackBuffer(Oyster::Math::Float4 Color, bool ClearDefaultDepthStencil) + { + Core::deviceContext->ClearRenderTargetView(Core::backBufferRTV,Color); + if(ClearDefaultDepthStencil) + { + Core::deviceContext->ClearDepthStencilView(Core::depthStencil,1,1,0); + } + } + + void Basic::ClearRTV(ID3D11RenderTargetView* RTVs[], int size,Oyster::Math::Float4 Color) + { + for(int i = 0; i < size; ++i) + { + Core::deviceContext->ClearRenderTargetView(RTVs[i],Color); + } + } + + void Basic::ClearDepthStencil(ID3D11DepthStencilView &depthStencil) + { + Core::deviceContext->ClearDepthStencilView(&depthStencil,1,1,0); + } + + void Basic::SetViewPort() + { + Core::deviceContext->RSSetViewports(1,Core::viewPort); + } + } + } + } +} \ No newline at end of file diff --git a/Code/OysterGraphics/Render/Preparations/Preparations.h b/Code/OysterGraphics/Render/Preparations/Preparations.h new file mode 100644 index 00000000..cea28590 --- /dev/null +++ b/Code/OysterGraphics/Render/Preparations/Preparations.h @@ -0,0 +1,47 @@ +#pragma once + +#include "..\..\Core\Core.h" + +namespace Oyster +{ + namespace Graphics + { + namespace Render + { + namespace Preparations + { + static class Basic + { + public: + /** @brief Binds the backbuffer as a RenderTargetView with the specified DepthStencil*/ + static void BindBackBufferRTV(ID3D11DepthStencilView& depthStencil); + /** @brief Binds the backbuffer as a RenderTargetView with or without the default DepthStencil*/ + static void BindBackBufferRTV(bool UseDefaultDepthStencil = true); + + + /** @brief Binds the backbuffer as a UnorderedAccessView*/ + static void BindBackBufferUAV(); + + /** @brief Binds the specified RenderTargetViews with or without the default DepthStencil*/ + static void BindRTV(ID3D11RenderTargetView* RTVs[], int size, bool UseDepthStencil = true); + /** @brief Binds the specified RenderTargetViews with the specified DepthStencil*/ + static void BindRTV(ID3D11RenderTargetView* RTVs[], int size,ID3D11DepthStencilView& depthStencil); + + /** @brief Binds the specified UnorderedAccessViews*/ + static void BindUAV(ID3D11UnorderedAccessView* UAVs[], int size); + + /** @brief Clear the BackBuffer and if true the default DepthStencil*/ + static void ClearBackBuffer(Oyster::Math::Float4 Color, bool ClearDefaultDepthStencil = true); + + /** @brief Clear the specified RenderTargetViews*/ + static void ClearRTV(ID3D11RenderTargetView* RTVs[], int size,Oyster::Math::Float4 Color); + /** @brief Clear the specified DepthStencil*/ + static void ClearDepthStencil(ID3D11DepthStencilView &depthStencil); + + /** @brief Binds the default ViewPort*/ + static void SetViewPort(); + }; + } + } + } +} \ No newline at end of file diff --git a/Code/OysterGraphics/Render/Rendering/Basic.cpp b/Code/OysterGraphics/Render/Rendering/Basic.cpp new file mode 100644 index 00000000..272a2a7c --- /dev/null +++ b/Code/OysterGraphics/Render/Rendering/Basic.cpp @@ -0,0 +1,33 @@ +#include "Render.h" + +namespace Oyster +{ + namespace Graphics + { + namespace Render + { + namespace Rendering + { + Core::ShaderManager::ShaderEffect Basic::Resources::se = Core::ShaderManager::ShaderEffect(); + + void Basic::Resources::Init() + { + se.Shaders.Vertex = Core::ShaderManager::Get::Vertex(L"DebugCamera"); + se.Shaders.Pixel = Core::ShaderManager::Get::Pixel(L"Debug"); + + } + + void Basic::NewFrame(Oyster::Math::Float4x4 View, Oyster::Math::Float4 Projection) + { + Preparations::Basic::ClearBackBuffer(Oyster::Math::Float4(0,0,0,1)); + } + void Basic::RenderScene(Model* models, int count) + { + } + void Basic::EndFrame() + { + } + } + } + } +} \ No newline at end of file diff --git a/Code/OysterGraphics/Render/Rendering/Render.h b/Code/OysterGraphics/Render/Rendering/Render.h new file mode 100644 index 00000000..22229078 --- /dev/null +++ b/Code/OysterGraphics/Render/Rendering/Render.h @@ -0,0 +1,33 @@ +#pragma once + +#include "..\..\Core\Core.h" +#include "..\Preparations\Preparations.h" +#include "..\..\Model\Model.h" + +namespace Oyster +{ + namespace Graphics + { + namespace Render + { + namespace Rendering + { + static class Basic + { + public: + class Resources + { + static Core::ShaderManager::ShaderEffect se; + + static void Init(); + }; + + + static void NewFrame(Oyster::Math::Float4x4 View, Oyster::Math::Float4 Projection); + static void RenderScene(Model* models, int count); + static void EndFrame(); + }; + } + } + } +} \ No newline at end of file diff --git a/Code/OysterGraphics/Render/TextBox.h b/Code/OysterGraphics/Render/TextBox.h deleted file mode 100644 index 633d0366..00000000 --- a/Code/OysterGraphics/Render/TextBox.h +++ /dev/null @@ -1,67 +0,0 @@ -#pragma once - -#include "..\Engine.h" -const int MAX_LETTER_COUNT=60; -const int TEXT_NR_LETTERS=95; -const float TEXT_SIZE=2.5; -struct Text2D -{ - Oyster::Math::Float Pos; - int offset; - float coff; -}; -/*struct TextInstanceData -{ - Oyster::Buffer InstanceBuffer; - bool Visible; - int NumLetters; - Oyster::Math::Float4x4 World; -};*/ -/*struct TextData -{ - Oyster::Math::Float3 pos; - Oyster::Math::Float2 uv; -}; - -struct PerCharData -{ - float data; - Oyster::Math::Float3 charOffset; -}; -struct TextInstanceData -{ - Oyster::Buffer InstanceBuffer; - bool Visible; - int NumLetters; - Oyster::Math::Float4x4 World; -};*/ - -namespace Oyster -{ - namespace Render - { - class Textbox - { - private: - static float getCharID(char _in); - static HRESULT CreateVertexBuffer(); - static HRESULT CreateTextfield(int _id); - public: - //static Oyster::Buffer TextBuffer; - //static int NumVertices; - //static std::vector TextInstances; - static Oyster::Buffer TextBuffer; - static int NumLetters; - static ID3D11ShaderResourceView* Texture; - - static bool Init(); - static bool UpdateTextField(std::string _str); - static bool SetTexture(const char* _file); - //Updates a textbox with the certain id - static void Update(std::string _str, float _scale); - //Removes all old instances and recreates it with the input data - static HRESULT Reset(int _count, std::string* _str, Float3* _pos); - static void Apply(int _id); - }; - } -} diff --git a/Code/OysterGraphics/Resources/Resources.cpp b/Code/OysterGraphics/Resources/Resources.cpp new file mode 100644 index 00000000..154c5cc7 --- /dev/null +++ b/Code/OysterGraphics/Resources/Resources.cpp @@ -0,0 +1,52 @@ +#include "Resources.h" + +const std::wstring PathFromExeToHlsl = L""; +const std::wstring VertexTransformDebug = L"TransformDebugVertex"; +const std::wstring VertexDebug = L"DebugVertex"; +const std::wstring PixelRed = L"DebugPixel"; + +typedef Oyster::Graphics::Core::ShaderManager::ShaderType Shader; + +namespace Oyster +{ + namespace Graphics + { + namespace Render + { + Core::Init::State Resources::Init() + { + +#pragma region LoadShaders + +#ifdef _DEBUG + + /** Load Vertex Shader for d3dcompile*/ + Core::ShaderManager::Init(PathFromExeToHlsl + L"SimpleDebug\\" +L"DebugCameraVertex",Shader::Vertex, VertexTransformDebug, false); + Core::ShaderManager::Init(PathFromExeToHlsl + L"SimpleDebug\\" +L"DebugVertex",Shader::Vertex, VertexDebug, false); + + /** Load Pixel Shader for d3dcompile */ + Core::ShaderManager::Init(PathFromExeToHlsl + L"SimpleDebug\\" + L"DebugPixel", Shader::Pixel, PixelRed, false); + +#else + /** Load Vertex Shader with Precompiled */ +#endif + +#pragma endregion + +#pragma region CreateBuffers + /** @todo Create Buffers */ +#pragma endregion + +#pragma region Setup Render States + /** @todo Create DX States */ +#pragma endregion + + +#pragma region Create Shader Effects + /** @todo Create ShaderEffects */ +#pragma endregion + return Core::Init::Sucsess; + } + } + } +} \ No newline at end of file diff --git a/Code/OysterGraphics/Resources/Resources.h b/Code/OysterGraphics/Resources/Resources.h new file mode 100644 index 00000000..5dac3c5e --- /dev/null +++ b/Code/OysterGraphics/Resources/Resources.h @@ -0,0 +1,21 @@ +#pragma once + +#include +#include "..\Core\Core.h" + +namespace Oyster +{ + namespace Graphics + { + namespace Render + { + static class Resources + { + const Core::ShaderManager::ShaderEffect basic; + const Buffer ModelData; + + Core::Init::State Init(); + }; + } + } +} \ No newline at end of file diff --git a/Code/OysterGraphics/Shader/HLSL/SimpleDebug/DebugCameraVertex.hlsl b/Code/OysterGraphics/Shader/HLSL/SimpleDebug/DebugCameraVertex.hlsl new file mode 100644 index 00000000..e2123062 --- /dev/null +++ b/Code/OysterGraphics/Shader/HLSL/SimpleDebug/DebugCameraVertex.hlsl @@ -0,0 +1,17 @@ +cbuffer PerFrame : register(b0) +{ + matrix View; + float4x4 Projection; +} + +cbuffer PerModel : register(b1) +{ + matrix World; +} + +float4 main( float4 pos : POSITION ) : SV_POSITION +{ + matrix VP = mul(View, Projection); + matrix WVP = mul(World, VP); + return mul(WVP, pos); +} \ No newline at end of file diff --git a/Code/OysterGraphics/Shader/Shader.h b/Code/OysterGraphics/Shader/Shader.h index 957d4314..f44b967c 100644 --- a/Code/OysterGraphics/Shader/Shader.h +++ b/Code/OysterGraphics/Shader/Shader.h @@ -4,75 +4,78 @@ namespace Oyster { - class Shader + namespace Graphics { - public: - struct ShaderEffect + class Shader { - struct + public: + struct ShaderEffect { - int Pixel,Vertex,Geometry,Compute,Hull,Domain; - }Shaders; - struct IAStage_ - { - ID3D11InputLayout* Layout; - D3D11_PRIMITIVE_TOPOLOGY Topology; - }IAStage; - struct RenderStates_ - { - ID3D11DepthStencilState *DepthStencil; - ID3D11RasterizerState *Rasterizer; - ID3D11SamplerState **SampleState; - int SampleCount; - ID3D11BlendState *BlendState; - }RenderStates; - struct - { - std::vector Vertex; - std::vector Geometry; - std::vector Pixel; - }CBuffers; - ShaderEffect() - { - RenderStates.BlendState=NULL; - RenderStates.DepthStencil=NULL; - RenderStates.Rasterizer=NULL; - RenderStates.SampleState=NULL; - RenderStates.SampleCount=0; - Shaders.Compute=-1; - Shaders.Domain=-1; - Shaders.Geometry=-1; - Shaders.Hull=-1; - Shaders.Pixel=-1; - Shaders.Vertex=-1; - } - }; - static bool InitShaders(const std::string &name = "..\\Shaders\\ShaderConfig.txt"); + struct + { + int Pixel,Vertex,Geometry,Compute,Hull,Domain; + }Shaders; + struct IAStage_ + { + ID3D11InputLayout* Layout; + D3D11_PRIMITIVE_TOPOLOGY Topology; + }IAStage; + struct RenderStates_ + { + ID3D11DepthStencilState *DepthStencil; + ID3D11RasterizerState *Rasterizer; + ID3D11SamplerState **SampleState; + int SampleCount; + ID3D11BlendState *BlendState; + }RenderStates; + struct + { + std::vector Vertex; + std::vector Geometry; + std::vector Pixel; + }CBuffers; + ShaderEffect() + { + RenderStates.BlendState=NULL; + RenderStates.DepthStencil=NULL; + RenderStates.Rasterizer=NULL; + RenderStates.SampleState=NULL; + RenderStates.SampleCount=0; + Shaders.Compute=-1; + Shaders.Domain=-1; + Shaders.Geometry=-1; + Shaders.Hull=-1; + Shaders.Pixel=-1; + Shaders.Vertex=-1; + } + }; + static bool InitShaders(const std::string &name = "..\\Shaders\\ShaderConfig.txt"); - static void SetShaderEffect(ShaderEffect); + static void SetShaderEffect(ShaderEffect); - static void CreateInputLayout(const D3D11_INPUT_ELEMENT_DESC *desc, int ElementCount,int VertexIndex,ID3D11InputLayout *&Layout); + static void CreateInputLayout(const D3D11_INPUT_ELEMENT_DESC *desc, int ElementCount,int VertexIndex,ID3D11InputLayout *&Layout); - struct Set - { - static void SetPixel(int Index); - static void SetVertex(int Index); - static void SetGeometry(int Index); - static void SetCompute(int Index); - static void SetHull(int Index); - static void SetDomain(int Index); + struct Set + { + static void SetPixel(int Index); + static void SetVertex(int Index); + static void SetGeometry(int Index); + static void SetCompute(int Index); + static void SetHull(int Index); + static void SetDomain(int Index); + }; + + struct Get + { + static int GetPixel(std::string Name); + static int GetVertex(std::string Name); + static int GetGeometry(std::string Name); + static int GetCompute(std::string Name); + static int GetHull(std::string Name); + static int GetDomain(std::string Name); + }; + + static std::stringstream* AccesLog(); }; - - struct Get - { - static int GetPixel(std::string Name); - static int GetVertex(std::string Name); - static int GetGeometry(std::string Name); - static int GetCompute(std::string Name); - static int GetHull(std::string Name); - static int GetDomain(std::string Name); - }; - - static std::stringstream* AccesLog(); - }; + } } \ No newline at end of file diff --git a/Code/Tester/MainTest.cpp b/Code/Tester/MainTest.cpp index be8b433f..53b19782 100644 --- a/Code/Tester/MainTest.cpp +++ b/Code/Tester/MainTest.cpp @@ -7,7 +7,8 @@ //-------------------------------------------------------------------------------------- #define NOMINMAX #include -#include "Engine.h" +#include "Core/Core.h" +#include "Render\Preparations\Preparations.h" //-------------------------------------------------------------------------------------- // Global Variables @@ -131,22 +132,21 @@ HRESULT InitDirect3D() { HRESULT hr = S_OK;; - Oyster::Engine::Init::Setup setup; - setup.Fullscreen = false; - setup.ForceDX11 = true; - setup.SingleThreaded = true; - setup.window = g_hWnd; + Oyster::Graphics::Core::resolution = Oyster::Math::Float2( 1024, 768 ); - Oyster::Engine::Init::FullInit( setup ); + if(Oyster::Graphics::Core::Init::FullInit(g_hWnd,false,false)==Oyster::Graphics::Core::Init::Fail) + return E_FAIL; + + std::wstring ShaderPath = L"..\\OysterGraphics\\Shader\\HLSL\\"; std::wstring EffectPath = L"SimpleDebug\\"; - Oyster::Core::ShaderManager::Init(ShaderPath + EffectPath + L"DebugPixel.hlsl",Oyster::Core::ShaderManager::ShaderType::Pixel,L"Debug",false); - Oyster::Core::ShaderManager::Init(ShaderPath + EffectPath + L"DebugVertex.hlsl",Oyster::Core::ShaderManager::ShaderType::Vertex,L"Debug",false); + Oyster::Graphics::Core::ShaderManager::Init(ShaderPath + EffectPath + L"DebugPixel.hlsl",Oyster::Graphics::Core::ShaderManager::ShaderType::Pixel,L"Debug",false); + Oyster::Graphics::Core::ShaderManager::Init(ShaderPath + EffectPath + L"DebugVertex.hlsl",Oyster::Graphics::Core::ShaderManager::ShaderType::Vertex,L"PassThroughFloat4",false); - Oyster::Core::ShaderManager::Set::Vertex(Oyster::Core::ShaderManager::Get::Vertex(L"Debug")); - Oyster::Core::ShaderManager::Set::Pixel(Oyster::Core::ShaderManager::Get::Pixel(L"Debug")); + Oyster::Graphics::Core::ShaderManager::Set::Vertex(Oyster::Graphics::Core::ShaderManager::Get::Vertex(L"PassThroughFloat4")); + Oyster::Graphics::Core::ShaderManager::Set::Pixel(Oyster::Graphics::Core::ShaderManager::Get::Pixel(L"Debug")); D3D11_INPUT_ELEMENT_DESC inputDesc[] = { @@ -155,12 +155,14 @@ HRESULT InitDirect3D() ID3D11InputLayout* layout; - Oyster::Core::ShaderManager::CreateInputLayout( inputDesc, 1, Oyster::Core::ShaderManager::Get::Vertex(L"Debug"), layout); + Oyster::Graphics::Core::ShaderManager::CreateInputLayout( inputDesc, 1, Oyster::Graphics::Core::ShaderManager::Get::Vertex(L"PassThroughFloat4"), layout); - Oyster::Core::DeviceContext->IASetInputLayout(layout); - Oyster::Core::DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + Oyster::Graphics::Core::deviceContext->IASetInputLayout(layout); + Oyster::Graphics::Core::deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); - Oyster::Engine::PrepareForRendering::BindBackBuffer(); + Oyster::Graphics::Render::Preparations::Basic::BindBackBufferRTV(); + + Oyster::Graphics::Render::Preparations::Basic::SetViewPort(); struct float4 { @@ -174,14 +176,14 @@ HRESULT InitDirect3D() {1.0f,-1.0f,0.0f,1.0f}, }; - Oyster::Buffer::BUFFER_INIT_DESC desc; + Oyster::Graphics::Buffer::BUFFER_INIT_DESC desc; desc.ElementSize= sizeof(float4); desc.NumElements = 3; desc.InitData=mesh; - desc.Type = Oyster::Buffer::BUFFER_TYPE::VERTEX_BUFFER; - desc.Usage = Oyster::Buffer::BUFFER_USAGE::BUFFER_USAGE_IMMUTABLE; + desc.Type = Oyster::Graphics::Buffer::BUFFER_TYPE::VERTEX_BUFFER; + desc.Usage = Oyster::Graphics::Buffer::BUFFER_USAGE::BUFFER_USAGE_IMMUTABLE; - Oyster::Buffer b; + Oyster::Graphics::Buffer b; b.Init(desc); b.Apply(0); @@ -195,11 +197,11 @@ HRESULT Update(float deltaTime) HRESULT Render(float deltaTime) { - Oyster::Engine::PrepareForRendering::ClearBackBuffer(Oyster::Math::Float4(0,0,1,1)); + Oyster::Graphics::Render::Preparations::Basic::ClearBackBuffer(Oyster::Math::Float4(0,0,1,1)); - Oyster::Core::DeviceContext->Draw(3,0); + Oyster::Graphics::Core::deviceContext->Draw(3,0); - Oyster::Core::SwapChain->Present(0,0); + Oyster::Graphics::Core::swapChain->Present(0,0); return S_OK; } diff --git a/OysterGraphics/Core/ShaderManager.cpp b/OysterGraphics/Core/ShaderManager.cpp new file mode 100644 index 00000000..ca607249 --- /dev/null +++ b/OysterGraphics/Core/ShaderManager.cpp @@ -0,0 +1,351 @@ +#include "Core.h" +#include + +const char* ShaderFunction = "main"; + +namespace Oyster +{ + bool LoadPrecompiled(std::wstring filename, Core::ShaderManager::ShaderType type, std::wstring name); + bool LoadCompile(std::wstring filename, Core::ShaderManager::ShaderType type, std::wstring name); + namespace + { + struct ShaderData + { + size_t size; + char* data; + }; + std::vector PS; + std::map PSMap; + + std::vector GS; + std::map GSMap; + + std::vector CS; + std::map CSMap; + + std::vector VS; + std::vector VData; + std::map VSMap; + } + +#pragma region Init + bool Core::ShaderManager::Init(std::wstring filename, ShaderType type, std::wstring name, bool Precompiled) + { + if(Precompiled) + { + return LoadPrecompiled(filename, type, name); + } + else + { + return LoadCompile(filename, type, name); + } + return true; + } + + bool LoadPrecompiled(std::wstring filename, Core::ShaderManager::ShaderType type, std::wstring name) + { + + std::ifstream stream; + ShaderData sd; + + + //Create Vertex shader and Layout + stream.open(filename, std::ifstream::in | std::ifstream::binary); + if(stream.good()) + { + stream.seekg(0, std::ios::end); + sd.size = size_t(stream.tellg()); + sd.data = new char[sd.size]; + stream.seekg(0, std::ios::beg); + stream.read(&sd.data[0], sd.size); + stream.close(); + + + ID3D11VertexShader* vertex; + ID3D11GeometryShader* geometry; + ID3D11PixelShader* pixel; + ID3D11ComputeShader* compute; + + switch(type) + { + case Core::ShaderManager::ShaderType::Vertex: + + if(VSMap.count(name)) + return false; + + if(FAILED(Core::Device->CreateVertexShader(sd.data, sd.size, 0, &vertex))) + { + return false; + } + VSMap[name] = VS.size(); + VS.push_back(vertex); + VData.push_back(sd); + break; + + case Core::ShaderManager::ShaderType::Hull: + case Core::ShaderManager::ShaderType::Domain: + + return false; + + case Core::ShaderManager::ShaderType::Geometry: + + if(GSMap.count(name)) + return false; + if(FAILED(Core::Device->CreateGeometryShader(sd.data, sd.size, 0, &geometry))) + { + return false; + } + GSMap[name] = GS.size(); + GS.push_back(geometry); + break; + + case Core::ShaderManager::ShaderType::Pixel: + + if(PSMap.count(name)) + return false; + if(FAILED(Core::Device->CreatePixelShader(sd.data, sd.size, 0, &pixel))) + { + return false; + } + PSMap[name] = PS.size(); + PS.push_back(pixel); + break; + + case Core::ShaderManager::ShaderType::Compute: + + if(CSMap.count(name)) + return false; + if(FAILED(Core::Device->CreateComputeShader(sd.data, sd.size, 0, &compute))) + { + return false; + } + CSMap[name] = CS.size(); + CS.push_back(compute); + break; + } + } + else + { + return false; + } + return true; + } + + bool LoadCompile(std::wstring filename, Core::ShaderManager::ShaderType type, std::wstring name) + { + /// \todo error reporting + ID3D10Blob *Shader,*Error; + switch(type) + { + case Core::ShaderManager::ShaderType::Pixel: + + ID3D11PixelShader* pixel; + + if(FAILED(D3DCompileFromFile(filename.c_str(),NULL,NULL,ShaderFunction,"ps_5_0",0,0,&Shader,&Error))) + { + std::string fel = (char*)Error->GetBufferPointer(); + Error->Release(); + return false; + } + if(FAILED(Oyster::Core::Device->CreatePixelShader(Shader->GetBufferPointer(),Shader->GetBufferSize(),NULL,&pixel))) + { + Error->Release(); + Shader->Release(); + return false; + } + Shader->Release(); + if(!PSMap.count(name)) + { + PSMap[name] = PS.size(); + PS.push_back(pixel); + } + else + { + PS[PSMap[name]] = pixel; + } + break; + + case Core::ShaderManager::ShaderType::Geometry: + + ID3D11GeometryShader* geometry; + + if(FAILED(D3DCompileFromFile(filename.c_str(),NULL,NULL,ShaderFunction,"gs_5_0",0,0,&Shader,&Error))) + { + std::string fel = (char*)Error->GetBufferPointer(); + Error->Release(); + return false; + } + if(FAILED(Oyster::Core::Device->CreateGeometryShader(Shader->GetBufferPointer(),Shader->GetBufferSize(),NULL,&geometry))) + { + Error->Release(); + Shader->Release(); + return false; + } + Shader->Release(); + if(!GSMap.count(name)) + { + GSMap[name] = GS.size(); + GS.push_back(geometry); + } + else + { + GS[GSMap[name]] = geometry; + } + break; + + case Core::ShaderManager::ShaderType::Vertex: + + ID3D11VertexShader* vertex; + + if(FAILED(D3DCompileFromFile(filename.c_str(),NULL,NULL,ShaderFunction,"vs_5_0",0,0,&Shader,&Error))) + { + std::string fel = (char*)Error->GetBufferPointer(); + Error->Release(); + return false; + } + if(FAILED(Oyster::Core::Device->CreateVertexShader(Shader->GetBufferPointer(),Shader->GetBufferSize(),NULL,&vertex))) + { + Error->Release(); + Shader->Release(); + return false; + } + if(!VSMap.count(name)) + { + VSMap[name] = VS.size(); + VS.push_back(vertex); + ShaderData sd; + sd.size = Shader->GetBufferSize(); + sd.data = new char[sd.size]; + memcpy(sd.data,Shader->GetBufferPointer(),sd.size); + VData.push_back(sd); + } + else + { + VS[VSMap[name]] = vertex; + delete[] VData[VSMap[name]].data; + VData[VSMap[name]].size = Shader->GetBufferSize(); + VData[VSMap[name]].data = new char[VData[VSMap[name]].size]; + memcpy(VData[VSMap[name]].data,Shader->GetBufferPointer(),VData[VSMap[name]].size); + } + + Shader->Release(); + break; + + } + return true; + } +#pragma endregion + + void Core::ShaderManager::CreateInputLayout(const D3D11_INPUT_ELEMENT_DESC *desc, int ElementCount,int VertexIndex,ID3D11InputLayout *&Layout) + { + if(VertexIndex==-1) + { + Layout=0; + return; + } + Device->CreateInputLayout(desc,ElementCount,VData[VertexIndex].data,VData[VertexIndex].size,&Layout); + } + +#pragma region Get + int Core::ShaderManager::Get::Pixel(std::wstring Name) + { + if(PSMap.count(Name)) + return PSMap[Name]; + return -1; + } + + int Core::ShaderManager::Get::Vertex(std::wstring Name) + { + if(VSMap.count(Name)) + return VSMap[Name]; + return -1; + } + int Core::ShaderManager::Get::Geometry(std::wstring Name) + { + if(GSMap.count(Name)) + return GSMap[Name]; + return -1; + } + + int Core::ShaderManager::Get::Compute(std::wstring Name) + { + if(CSMap.count(Name)) + return CSMap[Name]; + return -1; + } + int Core::ShaderManager::Get::Hull(std::wstring Name) + { + return -1; + } + int Core::ShaderManager::Get::Domain(std::wstring Name) + { + return -1; + } +#pragma endregion + +#pragma region Set + /// \todo smart set + void Core::ShaderManager::Set::Pixel(int Index) + { + if(Index==-1) + DeviceContext->PSSetShader( NULL,NULL,0); + else + DeviceContext->PSSetShader( PS[Index],NULL,0); + } + void Core::ShaderManager::Set::Vertex(int Index) + { + if(Index==-1) + DeviceContext->VSSetShader( NULL,NULL,0); + else + DeviceContext->VSSetShader( VS[Index],NULL,0); + } + void Core::ShaderManager::Set::Geometry(int Index) + { + if(Index==-1) + DeviceContext->GSSetShader( NULL,NULL,0); + else + DeviceContext->GSSetShader( GS[Index],NULL,0); + } + void Core::ShaderManager::Set::Compute(int Index) + { + if(Index==-1) + DeviceContext->CSSetShader( NULL,NULL,0); + else + DeviceContext->CSSetShader( CS[Index],NULL,0); + } + /// \todo set Hull + void Core::ShaderManager::Set::Hull(int Index) + { + return; + } + /// \todo set Domain + void Core::ShaderManager::Set::Domain(int Index) + { + return; + } +#pragma endregion + + /// \todo smart Set ie. not resetting the shader + /// \todo research states + /// \todo smart buffer set + void Core::ShaderManager::SetShaderEffect(ShaderEffect se) + { + Set::Pixel(se.Shaders.Pixel); + Set::Vertex(se.Shaders.Vertex); + Set::Geometry(se.Shaders.Geometry); + Set::Compute(se.Shaders.Compute); + Oyster::Core::DeviceContext->IASetInputLayout(se.IAStage.Layout); + Oyster::Core::DeviceContext->IASetPrimitiveTopology(se.IAStage.Topology); + for(unsigned int i=0;iApply(i); + for(unsigned int i=0;iApply(i); + for(unsigned int i=0;iApply(i); + Oyster::Core::DeviceContext->RSSetState(se.RenderStates.Rasterizer); + Oyster::Core::DeviceContext->PSSetSamplers(0,se.RenderStates.SampleCount,se.RenderStates.SampleState); + float test[4] = {0}; + Oyster::Core::DeviceContext->OMSetBlendState(se.RenderStates.BlendState,test,-1); + } + +} \ No newline at end of file diff --git a/OysterGraphics/OysterGraphics.vcxproj.filters b/OysterGraphics/OysterGraphics.vcxproj.filters new file mode 100644 index 00000000..412e083a --- /dev/null +++ b/OysterGraphics/OysterGraphics.vcxproj.filters @@ -0,0 +1,100 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + + + + + \ No newline at end of file diff --git a/Tester/Tester.vcxproj b/Tester/Tester.vcxproj new file mode 100644 index 00000000..ccfb910f --- /dev/null +++ b/Tester/Tester.vcxproj @@ -0,0 +1,98 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {1B3BEA4C-CF75-438A-9693-60FB8444BBF3} + Win32Proj + Tester + + + + Application + true + v110 + Unicode + + + Application + false + v110 + true + Unicode + + + + + + + + + + + + + true + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + true + ..\OysterGraphics;..\OysterMath;..\Misc;%(AdditionalIncludeDirectories) + + + Windows + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + true + + + Windows + true + true + true + + + + + + + + {2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee} + + + {0ec83e64-230e-48ef-b08c-6ac9651b4f82} + + + {f10cbc03-9809-4cba-95d8-327c287b18ee} + + + + + + \ No newline at end of file