-
Notifications
You must be signed in to change notification settings - Fork 1
/
ShaderDX9.cpp
202 lines (171 loc) · 6.38 KB
/
ShaderDX9.cpp
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "stdafx.h"
#include "Shader.h"
#include "typeDefs3D.h"
#include "RenderDevice.h"
// loads an HLSL effect file
// if fromFile is true the shaderName should point to the full filename (with path) to the .fx file
// if fromFile is false the shaderName should be the resource name not the IDC_XX_YY value. Search vpinball_eng.rc for ".fx" to see an example
bool Shader::Load(const BYTE* shaderCodeName, UINT codeSize)
{
LPD3DXBUFFER pBufferErrors;
DWORD dwShaderFlags = 0; //D3DXSHADER_SKIPVALIDATION // these do not have a measurable effect so far (also if used in the offline fxc step): D3DXSHADER_PARTIALPRECISION, D3DXSHADER_PREFER_FLOW_CONTROL/D3DXSHADER_AVOID_FLOW_CONTROL
HRESULT hr;
/*
if (fromFile)
{
dwShaderFlags = D3DXSHADER_DEBUG|D3DXSHADER_SKIPOPTIMIZATION;
hr = D3DXCreateEffectFromFile( m_renderDevice->GetCoreDevice(), // pDevice
shaderName, // pSrcFile
NULL, // pDefines
NULL, // pInclude
dwShaderFlags, // Flags
NULL, // pPool
&m_shader, // ppEffect
&pBufferErrors); // ppCompilationErrors
}
else
{
hr = D3DXCreateEffectFromResource( m_renderDevice->GetCoreDevice(), // pDevice
NULL,
shaderName, // resource name
NULL, // pDefines
NULL, // pInclude
dwShaderFlags, // Flags
NULL, // pPool
&m_shader, // ppEffect
&pBufferErrors); // ppCompilationErrors
}
*/
hr = D3DXCreateEffect(m_renderDevice->GetCoreDevice(), shaderCodeName, codeSize, NULL, NULL, dwShaderFlags, NULL, &m_shader, &pBufferErrors);
if (FAILED(hr))
{
if (pBufferErrors)
{
LPVOID pCompileErrors = pBufferErrors->GetBufferPointer();
MessageBox(NULL, (const char*)pCompileErrors, "Compile Error", MB_OK | MB_ICONEXCLAMATION);
}
else
MessageBox(NULL, "Unknown Error", "Compile Error", MB_OK | MB_ICONEXCLAMATION);
return false;
}
return true;
}
void Shader::Unload()
{
if (m_shader)
{
SAFE_RELEASE(m_shader);
}
}
Shader::~Shader()
{
shaderCount--;
this->Unload();
}
void Shader::Begin(const unsigned int pass)
{
m_currentShader = this;
unsigned int cPasses;
CHECKD3D(m_shader->Begin(&cPasses, 0));
CHECKD3D(m_shader->BeginPass(pass));
}
void Shader::End()
{
CHECKD3D(m_shader->EndPass());
CHECKD3D(m_shader->End());
}
void Shader::SetTextureDepth(const D3DXHANDLE texelName, D3DTexture *texel) {
SetTexture(texelName, texel, false);
}
void Shader::SetTexture(const D3DXHANDLE texelName, Texture *texel, const bool linearRGB)
{
const unsigned int idx = texelName[strlen(texelName) - 1] - '0'; // current convention: SetTexture gets "TextureX", where X 0..4
bool cache = idx < TEXTURESET_STATE_CACHE_SIZE;
if (!texel || !texel->m_pdsBuffer) {
if (cache) {
currentTexture[idx] = NULL; // invalidate cache
}
CHECKD3D(m_shader->SetTexture(texelName, NULL));
m_renderDevice->m_curTextureChanges++;
return;
}
if (!cache || (texel->m_pdsBuffer != currentTexture[idx]))
{
if (cache) {
currentTexture[idx] = texel->m_pdsBuffer;
}
CHECKD3D(m_shader->SetTexture(texelName, m_renderDevice->m_texMan.LoadTexture(texel->m_pdsBuffer, linearRGB)));
m_renderDevice->m_curTextureChanges++;
}
}
void Shader::SetTexture(const D3DXHANDLE texelName, D3DTexture *texel, const bool linearRGB)
{
const unsigned int idx = texelName[strlen(texelName) - 1] - '0'; // current convention: SetTexture gets "TextureX", where X 0..4
if (idx < TEXTURESET_STATE_CACHE_SIZE) {
currentTexture[idx] = NULL; // direct set of device tex invalidates the cache
}
CHECKD3D(m_shader->SetTexture(texelName, texel));
m_renderDevice->m_curTextureChanges++;
}
void Shader::SetTextureNull(const D3DXHANDLE texelName)
{
const unsigned int idx = texelName[strlen(texelName) - 1] - '0'; // current convention: SetTexture gets "TextureX", where X 0..4
bool cache = idx < TEXTURESET_STATE_CACHE_SIZE;
if (cache)
currentTexture[idx] = NULL; // direct set of device tex invalidates the cache
CHECKD3D(m_shader->SetTexture(texelName, NULL));
m_renderDevice->m_curTextureChanges++;
}
void Shader::SetTechnique(const D3DXHANDLE technique)
{
if (strcmp(currentTechnique, technique) /*|| (m_renderDevice->m_curShader != this)*/)
{
strcpy_s(currentTechnique, technique);
//m_renderDevice->m_curShader = this;
CHECKD3D(m_shader->SetTechnique(technique));
m_renderDevice->m_curTechniqueChanges++;
}
}
void Shader::SetMatrix(const D3DXHANDLE hParameter, const Matrix3D* pMatrix)
{
/*CHECKD3D(*/m_shader->SetMatrix(hParameter, (const D3DXMATRIX*)pMatrix)/*)*/; // leads to invalid calls when setting some of the matrices (as hlsl compiler optimizes some down to less than 4x4)
m_renderDevice->m_curParameterChanges++;
}
void Shader::SetVector(const D3DXHANDLE hParameter, const vec4* pVector)
{
CHECKD3D(m_shader->SetVector(hParameter, pVector));
m_renderDevice->m_curParameterChanges++;
}
void Shader::SetVector(const D3DXHANDLE hParameter, const float x, const float y, const float z, const float w)
{
vec4 pVector = vec4(x, y, z, w);
CHECKD3D(m_shader->SetVector(hParameter, &pVector));
}
void Shader::SetFloat(const D3DXHANDLE hParameter, const float f)
{
CHECKD3D(m_shader->SetFloat(hParameter, f));
m_renderDevice->m_curParameterChanges++;
}
void Shader::SetInt(const D3DXHANDLE hParameter, const int i)
{
CHECKD3D(m_shader->SetInt(hParameter, i));
m_renderDevice->m_curParameterChanges++;
}
void Shader::SetBool(const D3DXHANDLE hParameter, const bool b)
{
CHECKD3D(m_shader->SetBool(hParameter, b));
m_renderDevice->m_curParameterChanges++;
}
void Shader::SetFloatArray(const D3DXHANDLE hParameter, const float* pData, const unsigned int count)
{
CHECKD3D(m_shader->SetValue(hParameter, pData, count*sizeof(float)));
m_renderDevice->m_curParameterChanges++;
}
void Shader::GetTransform(const TransformStateType p1, Matrix3D* p2, const int count)
{
CHECKD3D(m_renderDevice->GetCoreDevice()->GetTransform((D3DTRANSFORMSTATETYPE)p1, p2));
}
void Shader::SetTransform(const TransformStateType p1, const Matrix3D * p2, const int count)
{
CHECKD3D(m_renderDevice->GetCoreDevice()->SetTransform((D3DTRANSFORMSTATETYPE)p1, p2));
}