-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShaderLibrary.cpp
More file actions
81 lines (64 loc) · 2.97 KB
/
ShaderLibrary.cpp
File metadata and controls
81 lines (64 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "ShaderLibrary.h"
#include "GraphicsEngine.h"
#include "VertexShader.h"
#include "PixelShader.h"
ShaderLibrary* ShaderLibrary::sharedInstance = NULL;
ShaderLibrary* ShaderLibrary::getInstance()
{
return sharedInstance;
}
void ShaderLibrary::initialize()
{
sharedInstance = new ShaderLibrary();
}
void ShaderLibrary::destroy()
{
delete sharedInstance;
}
void ShaderLibrary::requestVertexShaderData(String vertexShaderName, void** shaderByteCode, size_t* sizeShader)
{
GraphicsEngine* graphEngine = GraphicsEngine::get();
graphEngine->compileVertexShader(vertexShaderName.c_str(), "main", shaderByteCode, sizeShader);
}
VertexShader* ShaderLibrary::getVertexShader(String vertexShaderName)
{
if (this->activeVertexShaders[vertexShaderName] == NULL) {
std::cout << "Vertex shader " << vertexShaderName.c_str() << " not found. Have you initialized it? \n";
}
return this->activeVertexShaders[vertexShaderName];
}
PixelShader* ShaderLibrary::getPixelShader(String pixelShaderName)
{
if (this->activePixelShaders[pixelShaderName] == NULL) {
std::cout << "Pixel shader " << pixelShaderName.c_str() << " not found. Have you initialized it? \n";
}
return this->activePixelShaders[pixelShaderName];
}
ShaderLibrary::ShaderLibrary()
{
//initialize and load all shaders for use
GraphicsEngine* graphEngine = GraphicsEngine::get();
ShaderNames shaderNames;
ShaderData shaderData;
graphEngine->compileVertexShader(shaderNames.BASE_VERTEX_SHADER_NAME.c_str(), "main", &shaderData.shaderByteCode, &shaderData.sizeShader);
this->activeVertexShaders[shaderNames.BASE_VERTEX_SHADER_NAME] = graphEngine->createVertexShader(shaderData.shaderByteCode, shaderData.sizeShader);
graphEngine->compilePixelShader(shaderNames.BASE_PIXEL_SHADER_NAME.c_str(), "main", &shaderData.shaderByteCode, &shaderData.sizeShader);
this->activePixelShaders[shaderNames.BASE_PIXEL_SHADER_NAME] = graphEngine->createPixelShader(shaderData.shaderByteCode, shaderData.sizeShader);
graphEngine->compileVertexShader(shaderNames.TEXTURED_VERTEX_SHADER_NAME.c_str(), "main", &shaderData.shaderByteCode, &shaderData.sizeShader);
this->activeVertexShaders[shaderNames.TEXTURED_VERTEX_SHADER_NAME] = graphEngine->createVertexShader(shaderData.shaderByteCode, shaderData.sizeShader);
graphEngine->compilePixelShader(shaderNames.TEXTURED_PIXEL_SHADER_NAME.c_str(), "main", &shaderData.shaderByteCode, &shaderData.sizeShader);
this->activePixelShaders[shaderNames.TEXTURED_PIXEL_SHADER_NAME] = graphEngine->createPixelShader(shaderData.shaderByteCode, shaderData.sizeShader);
std::cout << "Shader library initialized. \n";
}
ShaderLibrary::~ShaderLibrary()
{
ShaderNames shaderNames;
this->activeVertexShaders[shaderNames.BASE_VERTEX_SHADER_NAME]->release();
this->activePixelShaders[shaderNames.BASE_PIXEL_SHADER_NAME]->release();
for(auto& vs : this->activeVertexShaders)
vs.second->release();
for (auto& ps : this->activePixelShaders)
ps.second->release();
this->activeVertexShaders.clear();
this->activePixelShaders.clear();
}