Danbias/Code/OysterGraphics/Core/ShaderManager.cpp

350 lines
8.5 KiB
C++
Raw Normal View History

#include "Core.h"
#include <fstream>
2013-11-15 11:05:19 +01:00
const char* ShaderFunction = "main";
namespace Oyster
{
2013-11-20 10:22:01 +01:00
namespace Graphics
{
2013-11-20 10:22:01 +01:00
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
{
2013-11-20 10:22:01 +01:00
struct ShaderData
{
size_t size;
char* data;
};
std::vector<ID3D11PixelShader*> PS;
std::map<std::wstring,int> PSMap;
2013-11-20 10:22:01 +01:00
std::vector<ID3D11GeometryShader*> GS;
std::map<std::wstring,int> GSMap;
2013-11-20 10:22:01 +01:00
std::vector<ID3D11ComputeShader*> CS;
std::map<std::wstring,int> CSMap;
2013-11-20 10:22:01 +01:00
std::vector<ID3D11VertexShader*> VS;
std::vector<ShaderData> VData;
std::map<std::wstring,int> VSMap;
}
2013-11-20 10:22:01 +01:00
#pragma region Init
bool Core::ShaderManager::Init(std::wstring filename, ShaderType type, std::wstring name, bool Precompiled)
{
2013-11-20 10:22:01 +01:00
if(Precompiled)
{
return LoadPrecompiled(filename, type, name);
}
else
{
return LoadCompile(filename, type, name);
}
return true;
}
2013-11-20 10:22:01 +01:00
bool LoadPrecompiled(std::wstring filename, Core::ShaderManager::ShaderType type, std::wstring name)
{
2013-11-20 10:22:01 +01:00
std::ifstream stream;
ShaderData sd;
2013-11-20 10:22:01 +01:00
//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();
2013-11-20 10:22:01 +01:00
ID3D11VertexShader* vertex;
ID3D11GeometryShader* geometry;
ID3D11PixelShader* pixel;
ID3D11ComputeShader* compute;
2013-11-20 10:22:01 +01:00
switch(type)
{
case Core::ShaderManager::ShaderType::Vertex:
2013-11-20 10:22:01 +01:00
if(VSMap.count(name))
return false;
2013-11-20 10:22:01 +01:00
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;
2013-11-20 10:22:01 +01:00
case Core::ShaderManager::ShaderType::Hull:
case Core::ShaderManager::ShaderType::Domain:
2013-11-20 10:22:01 +01:00
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;
2013-11-20 10:22:01 +01:00
}
return true;
}
2013-11-20 10:22:01 +01:00
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:
2013-11-20 10:22:01 +01:00
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;
2013-11-20 10:22:01 +01:00
}
if(FAILED(Core::device->CreatePixelShader(Shader->GetBufferPointer(),Shader->GetBufferSize(),NULL,&pixel)))
{
2013-11-20 10:22:01 +01:00
Shader->Release();
return false;
}
2013-11-20 10:22:01 +01:00
Shader->Release();
if(!PSMap.count(name))
{
PSMap[name] = PS.size();
PS.push_back(pixel);
}
else
{
PS[PSMap[name]] = pixel;
}
break;
2013-11-20 10:22:01 +01:00
case Core::ShaderManager::ShaderType::Geometry:
ID3D11GeometryShader* geometry;
2013-11-20 10:22:01 +01:00
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;
2013-11-20 10:22:01 +01:00
}
if(FAILED(Core::device->CreateGeometryShader(Shader->GetBufferPointer(),Shader->GetBufferSize(),NULL,&geometry)))
{
2013-11-20 10:22:01 +01:00
Shader->Release();
return false;
}
2013-11-20 10:22:01 +01:00
Shader->Release();
if(!GSMap.count(name))
{
GSMap[name] = GS.size();
GS.push_back(geometry);
}
else
{
GS[GSMap[name]] = geometry;
}
break;
2013-11-20 10:22:01 +01:00
case Core::ShaderManager::ShaderType::Vertex:
2013-11-20 10:22:01 +01:00
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;
2013-11-20 10:22:01 +01:00
}
if(FAILED(Core::device->CreateVertexShader(Shader->GetBufferPointer(),Shader->GetBufferSize(),NULL,&vertex)))
{
2013-11-20 10:22:01 +01:00
Shader->Release();
return false;
}
2013-11-20 10:22:01 +01:00
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;
2013-11-20 10:22:01 +01:00
}
2013-11-20 10:22:01 +01:00
return true;
}
2013-11-20 10:22:01 +01:00
#pragma endregion
2013-11-20 10:22:01 +01:00
void Core::ShaderManager::CreateInputLayout(const D3D11_INPUT_ELEMENT_DESC *desc, int ElementCount,int VertexIndex,ID3D11InputLayout *&Layout)
{
2013-11-20 10:22:01 +01:00
if(VertexIndex==-1)
{
2013-11-20 10:22:01 +01:00
Layout=0;
return;
}
2013-11-20 10:22:01 +01:00
Core::device->CreateInputLayout(desc,ElementCount,VData[VertexIndex].data,VData[VertexIndex].size,&Layout);
}
2013-11-20 10:22:01 +01:00
#pragma region Get
int Core::ShaderManager::Get::Pixel(std::wstring Name)
{
if(PSMap.count(Name))
return PSMap[Name];
return -1;
}
2013-11-20 10:22:01 +01:00
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;
}
2013-11-20 10:22:01 +01:00
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
2013-11-20 10:22:01 +01:00
#pragma region Set
/// \todo smart set
void Core::ShaderManager::Set::Pixel(int Index)
{
if(Index==-1)
Core::deviceContext->PSSetShader( NULL,NULL,0);
else
2013-11-20 10:22:01 +01:00
Core::deviceContext->PSSetShader( PS[Index],NULL,0);
}
2013-11-20 10:22:01 +01:00
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)
{
return;
}
2013-11-20 10:22:01 +01:00
/// \todo set Domain
void Core::ShaderManager::Set::Domain(int Index)
{
return;
}
#pragma endregion
2013-11-20 10:22:01 +01:00
/// \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;i<se.CBuffers.Vertex.size();++i)
se.CBuffers.Vertex[i]->Apply(i);
for(unsigned int i=0;i<se.CBuffers.Geometry.size();++i)
se.CBuffers.Geometry[i]->Apply(i);
for(unsigned int i=0;i<se.CBuffers.Pixel.size();++i)
se.CBuffers.Pixel[i]->Apply(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);
}
}
}