-
Notifications
You must be signed in to change notification settings - Fork 1
/
Shader.cpp
213 lines (191 loc) · 7.27 KB
/
Shader.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
203
204
205
206
207
208
209
210
211
212
213
//This file should only contain DX9/GL independent shader code. For specific functions see ShaderGL.cpp and ShaderDX9.cpp
#include "stdafx.h"
#include "Shader.h"
#include "typeDefs3D.h"
#include "RenderDevice.h"
RenderDevice *Shader::m_renderDevice = NULL;
Shader* Shader::getCurrentShader() {
return m_currentShader;
}
Shader* Shader::m_currentShader = NULL;
int Shader::shaderCount = 0;
Shader::Shader(RenderDevice *renderDevice)
{
shaderCount++;
m_renderDevice = renderDevice;
#ifndef ENABLE_SDL
m_shader = 0;
#endif
for (unsigned int i = 0; i < TEXTURESET_STATE_CACHE_SIZE; ++i)
currentTexture[i] = 0;
currentAlphaTestValue = -FLT_MAX;
currentDisableLighting = vec4(-FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX);
currentFlasherData = vec4(-FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX);
currentFlasherColor = vec4(-FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX);
currentLightColor = vec4(-FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX);
currentLightColor2 = vec4(-FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX);
currentLightData = vec4(-FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX);
currentLightImageMode = ~0u;
currentLightBackglassMode = ~0u;
currentTechnique[0] = 0;
memset(¤tMaterial, 0xCC, sizeof(Material));
}
void Shader::SetMaterial(const Material * const mat)
{
COLORREF cBase, cGlossy, cClearcoat;
float fWrapLighting, fRoughness, fGlossyImageLerp, fThickness, fEdge, fEdgeAlpha, fOpacity;
bool bIsMetal, bOpacityActive;
if (mat)
{
fWrapLighting = mat->m_fWrapLighting;
fRoughness = exp2f(10.0f * mat->m_fRoughness + 1.0f); // map from 0..1 to 2..2048
fGlossyImageLerp = mat->m_fGlossyImageLerp;
fThickness = mat->m_fThickness;
fEdge = mat->m_fEdge;
fEdgeAlpha = mat->m_fEdgeAlpha;
fOpacity = mat->m_fOpacity;
cBase = mat->m_cBase;
cGlossy = mat->m_cGlossy;
cClearcoat = mat->m_cClearcoat;
bIsMetal = mat->m_bIsMetal;
bOpacityActive = mat->m_bOpacityActive;
}
else
{
fWrapLighting = 0.0f;
fRoughness = exp2f(10.0f * 0.0f + 1.0f); // map from 0..1 to 2..2048
fGlossyImageLerp = 1.0f;
fThickness = 0.05f;
fEdge = 1.0f;
fEdgeAlpha = 1.0f;
fOpacity = 1.0f;
cBase = g_pvp->dummyMaterial.m_cBase;
cGlossy = 0;
cClearcoat = 0;
bIsMetal = false;
bOpacityActive = false;
}
// bIsMetal is nowadays handled via a separate technique! (so not in here)
if (fRoughness != currentMaterial.m_fRoughness ||
fEdge != currentMaterial.m_fEdge ||
fWrapLighting != currentMaterial.m_fWrapLighting ||
fThickness != currentMaterial.m_fThickness)
{
const vec4 rwem(fRoughness, fWrapLighting, fEdge, fThickness);
SetVector("Roughness_WrapL_Edge_Thickness", &rwem);
currentMaterial.m_fRoughness = fRoughness;
currentMaterial.m_fWrapLighting = fWrapLighting;
currentMaterial.m_fEdge = fEdge;
currentMaterial.m_fThickness = fThickness;
}
const float alpha = bOpacityActive ? fOpacity : 1.0f;
if (cBase != currentMaterial.m_cBase || alpha != currentMaterial.m_fOpacity)
{
const vec4 cBaseF = convertColor(cBase, alpha);
SetVector("cBase_Alpha", &cBaseF);
currentMaterial.m_cBase = cBase;
currentMaterial.m_fOpacity = alpha;
}
if (!bIsMetal) // Metal has no glossy
if (cGlossy != currentMaterial.m_cGlossy ||
fGlossyImageLerp != currentMaterial.m_fGlossyImageLerp)
{
const vec4 cGlossyF = convertColor(cGlossy, fGlossyImageLerp);
SetVector("cGlossy_ImageLerp", &cGlossyF);
currentMaterial.m_cGlossy = cGlossy;
currentMaterial.m_fGlossyImageLerp = fGlossyImageLerp;
}
if (cClearcoat != currentMaterial.m_cClearcoat ||
(bOpacityActive && fEdgeAlpha != currentMaterial.m_fEdgeAlpha))
{
const vec4 cClearcoatF = convertColor(cClearcoat, fEdgeAlpha);
SetVector("cClearcoat_EdgeAlpha", &cClearcoatF);
currentMaterial.m_cClearcoat = cClearcoat;
currentMaterial.m_fEdgeAlpha = fEdgeAlpha;
}
if (bOpacityActive /*&& (alpha < 1.0f)*/)
g_pplayer->m_pin3d.EnableAlphaBlend(false);
else
g_pplayer->m_pin3d.m_pd3dPrimaryDevice->SetRenderState(RenderDevice::ALPHABLENDENABLE, RenderDevice::RS_FALSE);
}
void Shader::SetDisableLighting(const float value) // only set top
{
if (currentDisableLighting.x != value || currentDisableLighting.y != 0.f)
{
currentDisableLighting.x = value;
currentDisableLighting.y = 0.f;
currentDisableLighting.z = 0.f;
currentDisableLighting.w = 0.f;
SetVector("fDisableLighting_top_below", ¤tDisableLighting);
}
}
void Shader::SetDisableLighting(const vec4& value) // set top and below
{
if (currentDisableLighting.x != value.x || currentDisableLighting.y != value.y)
{
currentDisableLighting = value;
SetVector("fDisableLighting_top_below", &value);
}
}
void Shader::SetAlphaTestValue(const float value)
{
if (currentAlphaTestValue != value)
{
currentAlphaTestValue = value;
SetFloat("fAlphaTestValue", value);
}
}
void Shader::SetFlasherColorAlpha(const vec4& color)
{
if (currentFlasherColor.x != color.x || currentFlasherColor.y != color.y || currentFlasherColor.z != color.z || currentFlasherColor.w != color.w)
{
currentFlasherColor = color;
SetVector("staticColor_Alpha", &color);
}
}
void Shader::SetFlasherData(const vec4& color, const float mode)
{
if (currentFlasherData.x != color.x || currentFlasherData.y != color.y || currentFlasherData.z != color.z || currentFlasherData.w != color.w)
{
currentFlasherData = color;
SetVector("alphaTestValueAB_filterMode_addBlend", &color);
}
if (currentFlasherMode != mode)
{
currentFlasherMode = mode;
SetFloat("flasherMode", mode);
}
}
void Shader::SetLightColorIntensity(const vec4& color)
{
if (currentLightColor.x != color.x || currentLightColor.y != color.y || currentLightColor.z != color.z || currentLightColor.w != color.w)
{
currentLightColor = color;
SetVector("lightColor_intensity", &color);
}
}
void Shader::SetLightColor2FalloffPower(const vec4& color)
{
if (currentLightColor2.x != color.x || currentLightColor2.y != color.y || currentLightColor2.z != color.z || currentLightColor2.w != color.w)
{
currentLightColor2 = color;
SetVector("lightColor2_falloff_power", &color);
}
}
void Shader::SetLightData(const vec4& color)
{
if (currentLightData.x != color.x || currentLightData.y != color.y || currentLightData.z != color.z || currentLightData.w != color.w)
{
currentLightData = color;
SetVector("lightCenter_maxRange", &color);
}
}
void Shader::SetLightImageBackglassMode(const bool imageMode, const bool backglassMode)
{
if (currentLightImageMode != (unsigned int)imageMode || currentLightBackglassMode != (unsigned int)backglassMode)
{
currentLightImageMode = (unsigned int)imageMode;
currentLightBackglassMode = (unsigned int)backglassMode;
SetBool("lightingOff", imageMode || backglassMode); // at the moment can be combined into a single bool due to what the shader actually does in the end
}
}