-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGBufferMaterialGenerator.cpp
More file actions
292 lines (242 loc) · 9.73 KB
/
GBufferMaterialGenerator.cpp
File metadata and controls
292 lines (242 loc) · 9.73 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2009 Torus Knot Software Ltd
Also see acknowledgements in Readme.html
You may use this sample code for anything you like, it is not covered by the
same license as the rest of the engine.
-----------------------------------------------------------------------------
*/
#include "GBufferMaterialGenerator.h"
#include "OgreGpuProgramManager.h"
#include "OgreHighLevelGpuProgramManager.h"
#include "OgreMaterialManager.h"
#include "OgreStringConverter.h"
#include "OgreTechnique.h"
// Use this directive to control whether you are writing projective (regular) or
// linear depth.
#define WRITE_LINEAR_DEPTH
// This is the concrete implementation of the material generator.
class GBufferMaterialGeneratorImpl : public MaterialGenerator::Impl {
public:
GBufferMaterialGeneratorImpl(const Ogre::String &baseName)
: mBaseName(baseName) {}
protected:
Ogre::String mBaseName;
virtual Ogre::GpuProgramPtr
generateVertexShader(MaterialGenerator::Perm permutation);
virtual Ogre::GpuProgramPtr
generateFragmentShader(MaterialGenerator::Perm permutation);
virtual Ogre::MaterialPtr
generateTemplateMaterial(MaterialGenerator::Perm permutation);
};
GBufferMaterialGenerator::GBufferMaterialGenerator() {
vsMask = VS_MASK;
fsMask = FS_MASK;
matMask = MAT_MASK;
materialBaseName = "DeferredShading/GBuffer/";
mImpl = new GBufferMaterialGeneratorImpl(materialBaseName);
}
Ogre::GpuProgramPtr GBufferMaterialGeneratorImpl::generateVertexShader(
MaterialGenerator::Perm permutation) {
Ogre::StringStream ss;
ss << "void ToGBufferVP(" << std::endl;
ss << " float4 iPosition : POSITION," << std::endl;
ss << " float3 iNormal : NORMAL," << std::endl;
Ogre::uint32 numTexCoords =
(permutation & GBufferMaterialGenerator::GBP_TEXCOORD_MASK) >> 8;
for (Ogre::uint32 i = 0; i < numTexCoords; i++) {
ss << " float2 iUV" << i << " : TEXCOORD" << i << ',' << std::endl;
}
if (permutation & GBufferMaterialGenerator::GBP_NORMAL_MAP) {
ss << " float3 iTangent : TANGENT0," << std::endl;
}
// TODO : Skinning inputs
ss << std::endl;
ss << " out float4 oPosition : POSITION," << std::endl;
#ifdef WRITE_LINEAR_DEPTH
ss << " out float3 oViewPos : TEXCOORD0," << std::endl;
#else
ss << " out float oDepth : TEXCOORD0," << std::endl;
#endif
ss << " out float3 oNormal : TEXCOORD1," << std::endl;
int texCoordNum = 2;
if (permutation & GBufferMaterialGenerator::GBP_NORMAL_MAP) {
ss << " out float3 oTangent : TEXCOORD" << texCoordNum++ << ','
<< std::endl;
ss << " out float3 oBiNormal : TEXCOORD" << texCoordNum++ << ','
<< std::endl;
}
for (Ogre::uint32 i = 0; i < numTexCoords; i++) {
ss << " out float2 oUV" << i << " : TEXCOORD" << texCoordNum++ << ','
<< std::endl;
}
ss << std::endl;
ss << " uniform float4x4 cWorldViewProj," << std::endl;
ss << " uniform float4x4 cWorldView" << std::endl;
ss << " )" << std::endl;
ss << "{" << std::endl;
ss << " oPosition = mul(cWorldViewProj, iPosition);" << std::endl;
ss << " oNormal = mul(cWorldView, float4(iNormal,0)).xyz;" << std::endl;
if (permutation & GBufferMaterialGenerator::GBP_NORMAL_MAP) {
ss << " oTangent = mul(cWorldView, float4(iTangent,0)).xyz;" << std::endl;
ss << " oBiNormal = cross(oNormal, oTangent);" << std::endl;
}
#ifdef WRITE_LINEAR_DEPTH
ss << " oViewPos = mul(cWorldView, iPosition).xyz;" << std::endl;
#else
ss << " oDepth = oPosition.w;" << std::endl;
#endif
for (Ogre::uint32 i = 0; i < numTexCoords; i++) {
ss << " oUV" << i << " = iUV" << i << ';' << std::endl;
}
ss << "}" << std::endl;
Ogre::String programSource = ss.str();
Ogre::String programName =
mBaseName + "VP_" + Ogre::StringConverter::toString(permutation);
#if OGRE_DEBUG_MODE
Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(programSource);
#endif
// Create shader object
Ogre::HighLevelGpuProgramPtr ptrProgram =
Ogre::HighLevelGpuProgramManager::getSingleton().createProgram(
programName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
"cg", Ogre::GPT_VERTEX_PROGRAM);
ptrProgram->setSource(programSource);
ptrProgram->setParameter("entry_point", "ToGBufferVP");
ptrProgram->setParameter("profiles", "vs_1_1 arbvp1");
const Ogre::GpuProgramParametersSharedPtr ¶ms =
ptrProgram->getDefaultParameters();
params->setNamedAutoConstant(
"cWorldViewProj", Ogre::GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
params->setNamedAutoConstant(
"cWorldView", Ogre::GpuProgramParameters::ACT_WORLDVIEW_MATRIX);
ptrProgram->load();
return Ogre::GpuProgramPtr(ptrProgram);
}
Ogre::GpuProgramPtr GBufferMaterialGeneratorImpl::generateFragmentShader(
MaterialGenerator::Perm permutation) {
Ogre::StringStream ss;
ss << "void ToGBufferFP(" << std::endl;
#ifdef WRITE_LINEAR_DEPTH
ss << " float3 iViewPos : TEXCOORD0," << std::endl;
#else
ss << " float1 iDepth : TEXCOORD0," << std::endl;
#endif
ss << " float3 iNormal : TEXCOORD1," << std::endl;
int texCoordNum = 2;
if (permutation & GBufferMaterialGenerator::GBP_NORMAL_MAP) {
ss << " float3 iTangent : TEXCOORD" << texCoordNum++ << ',' << std::endl;
ss << " float3 iBiNormal : TEXCOORD" << texCoordNum++ << ',' << std::endl;
}
Ogre::uint32 numTexCoords =
(permutation & GBufferMaterialGenerator::GBP_TEXCOORD_MASK) >> 8;
for (Ogre::uint32 i = 0; i < numTexCoords; i++) {
ss << " float2 iUV" << i << " : TEXCOORD" << texCoordNum++ << ','
<< std::endl;
}
ss << std::endl;
ss << " out float4 oColor0 : COLOR0," << std::endl;
ss << " out float4 oColor1 : COLOR1," << std::endl;
ss << std::endl;
int samplerNum = 0;
if (permutation & GBufferMaterialGenerator::GBP_NORMAL_MAP) {
ss << " uniform sampler sNormalMap : register(s" << samplerNum++ << "),"
<< std::endl;
}
Ogre::uint32 numTextures =
permutation & GBufferMaterialGenerator::GBP_TEXTURE_MASK;
for (Ogre::uint32 i = 0; i < numTextures; i++) {
ss << " uniform sampler sTex" << i << " : register(s" << samplerNum++
<< ")," << std::endl;
}
if (numTextures == 0 ||
permutation & GBufferMaterialGenerator::GBP_HAS_DIFFUSE_COLOUR) {
ss << " uniform float4 cDiffuseColour," << std::endl;
}
#ifdef WRITE_LINEAR_DEPTH
ss << " uniform float cFarDistance," << std::endl;
#endif
ss << " uniform float cSpecularity" << std::endl;
ss << " )" << std::endl;
ss << "{" << std::endl;
if (numTexCoords > 0 && numTextures > 0) {
ss << " oColor0.rgb = tex2D(sTex0, iUV0);" << std::endl;
if (permutation & GBufferMaterialGenerator::GBP_HAS_DIFFUSE_COLOUR) {
ss << " oColor0.rgb *= cDiffuseColour.rgb;" << std::endl;
}
} else {
ss << " oColor0.rgb = cDiffuseColour.rgb;" << std::endl;
}
ss << " oColor0.a = cSpecularity;" << std::endl;
if (permutation & GBufferMaterialGenerator::GBP_NORMAL_MAP) {
ss << " float3 texNormal = (tex2D(sNormalMap, iUV0)-0.5)*2;" << std::endl;
ss << " float3x3 normalRotation = float3x3(iTangent, iBiNormal, "
"iNormal);"
<< std::endl;
ss << " oColor1.rgb = normalize(mul(texNormal, normalRotation));"
<< std::endl;
} else {
ss << " oColor1.rgb = normalize(iNormal);" << std::endl;
}
#ifdef WRITE_LINEAR_DEPTH
ss << " oColor1.a = length(iViewPos) / cFarDistance;" << std::endl;
#else
ss << " oColor1.a = iDepth;" << std::endl;
#endif
ss << "}" << std::endl;
Ogre::String programSource = ss.str();
Ogre::String programName =
mBaseName + "FP_" + Ogre::StringConverter::toString(permutation);
#if OGRE_DEBUG_MODE
Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(programSource);
#endif
// Create shader object
Ogre::HighLevelGpuProgramPtr ptrProgram =
Ogre::HighLevelGpuProgramManager::getSingleton().createProgram(
programName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
"cg", Ogre::GPT_FRAGMENT_PROGRAM);
ptrProgram->setSource(programSource);
ptrProgram->setParameter("entry_point", "ToGBufferFP");
ptrProgram->setParameter("profiles", "ps_2_0 arbfp1");
const Ogre::GpuProgramParametersSharedPtr ¶ms =
ptrProgram->getDefaultParameters();
params->setNamedAutoConstant(
"cSpecularity", Ogre::GpuProgramParameters::ACT_SURFACE_SHININESS);
if (numTextures == 0 ||
permutation & GBufferMaterialGenerator::GBP_HAS_DIFFUSE_COLOUR) {
params->setNamedAutoConstant(
"cDiffuseColour",
Ogre::GpuProgramParameters::ACT_SURFACE_DIFFUSE_COLOUR);
}
#ifdef WRITE_LINEAR_DEPTH
// TODO : Should this be the distance to the far corner, not the far clip
// distance?
params->setNamedAutoConstant(
"cFarDistance", Ogre::GpuProgramParameters::ACT_FAR_CLIP_DISTANCE);
#endif
ptrProgram->load();
return Ogre::GpuProgramPtr(ptrProgram);
}
Ogre::MaterialPtr GBufferMaterialGeneratorImpl::generateTemplateMaterial(
MaterialGenerator::Perm permutation) {
Ogre::String matName =
mBaseName + "Mat_" + Ogre::StringConverter::toString(permutation);
Ogre::MaterialPtr matPtr = Ogre::MaterialManager::getSingleton().create(
matName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
Ogre::Pass *pass = matPtr->getTechnique(0)->getPass(0);
pass->setName(mBaseName + "Pass_" +
Ogre::StringConverter::toString(permutation));
pass->setLightingEnabled(false);
if (permutation & GBufferMaterialGenerator::GBP_NORMAL_MAP) {
pass->createTextureUnitState();
}
Ogre::uint32 numTextures =
permutation & GBufferMaterialGenerator::GBP_TEXTURE_MASK;
for (Ogre::uint32 i = 0; i < numTextures; i++) {
pass->createTextureUnitState();
}
return matPtr;
}