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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define WITH_D3D
#include "../rwbase.h"
#include "../rwerror.h"
#include "../rwplg.h"
#include "../rwpipeline.h"
#include "../rwobjects.h"
#include "../rwanim.h"
#include "../rwengine.h"
#include "../rwrender.h"
#include "../rwplugins.h"
#include "rwd3d.h"
#include "rwd3d9.h"
namespace rw {
namespace d3d9 {
using namespace d3d;
#ifndef RW_D3D9
void matfxRenderCB_Shader(Atomic *atomic, InstanceDataHeader *header) {}
#else
static void *matfx_env_amb_VS;
static void *matfx_env_amb_dir_VS;
static void *matfx_env_all_VS;
static void *matfx_env_PS;
static void *matfx_env_tex_PS;
enum
{
VSLOC_texMat = VSLOC_afterLights,
VSLOC_colorClamp = VSLOC_texMat + 4,
VSLOC_envColor,
PSLOC_shininess = 1,
};
void
matfxRender_Default(InstanceDataHeader *header, InstanceData *inst, int32 lightBits)
{
Material *m = inst->material;
// Pick a shader
if((lightBits & VSLIGHT_MASK) == 0)
setVertexShader(default_amb_VS);
else if((lightBits & VSLIGHT_MASK) == VSLIGHT_DIRECT)
setVertexShader(default_amb_dir_VS);
else
setVertexShader(default_all_VS);
SetRenderState(VERTEXALPHA, inst->vertexAlpha || m->color.alpha != 255);
if(inst->material->texture){
d3d::setTexture(0, m->texture);
setPixelShader(default_tex_PS);
}else
setPixelShader(default_PS);
drawInst(header, inst);
}
static Frame *lastEnvFrame;
static RawMatrix normal2texcoord = {
{ 0.5f, 0.0f, 0.0f }, 0.0f,
{ 0.0f, -0.5f, 0.0f }, 0.0f,
{ 0.0f, 0.0f, 1.0f }, 0.0f,
{ 0.5f, 0.5f, 0.0f }, 1.0f
};
void
uploadEnvMatrix(Frame *frame)
{
Matrix invMat;
if(frame == nil)
frame = engine->currentCamera->getFrame();
// cache the matrix across multiple meshes
// can't do it, frame matrix may change
// if(frame == lastEnvFrame)
// return;
// lastEnvFrame = frame;
RawMatrix envMtx, invMtx;
Matrix::invert(&invMat, frame->getLTM());
convMatrix(&invMtx, &invMat);
invMtx.pos.set(0.0f, 0.0f, 0.0f);
float uscale = fabs(normal2texcoord.right.x);
normal2texcoord.right.x = MatFX::envMapFlipU ? -uscale : uscale;
RawMatrix::mult(&envMtx, &invMtx, &normal2texcoord);
d3ddevice->SetVertexShaderConstantF(VSLOC_texMat, (float*)&envMtx, 4);
}
void
matfxRender_EnvMap(InstanceDataHeader *header, InstanceData *inst, int32 lightBits, MatFX::Env *env)
{
Material *m = inst->material;
if(env->tex == nil || env->coefficient == 0.0f){
matfxRender_Default(header, inst, lightBits);
return;
}
d3d::setTexture(1, env->tex);
uploadEnvMatrix(env->frame);
SetRenderState(SRCBLEND, BLENDONE);
static float zero[4];
static float one[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
struct {
float shininess;
float disableFBA;
float unused[2];
} fxparams;
fxparams.shininess = env->coefficient;
fxparams.disableFBA = env->fbAlpha ? 0.0f : 1.0f;
d3ddevice->SetPixelShaderConstantF(PSLOC_shininess, (float*)&fxparams, 1);
// This clamps the vertex color below. With it we can achieve both PC and PS2 style matfx
if(MatFX::envMapApplyLight)
d3ddevice->SetVertexShaderConstantF(VSLOC_colorClamp, zero, 1);
else
d3ddevice->SetVertexShaderConstantF(VSLOC_colorClamp, one, 1);
RGBAf envcol[4];
if(MatFX::envMapUseMatColor)
convColor(envcol, &m->color);
else
convColor(envcol, &MatFX::envMapColor);
d3ddevice->SetVertexShaderConstantF(VSLOC_envColor, (float*)&envcol, 1);
// Pick a shader
if((lightBits & VSLIGHT_MASK) == 0)
setVertexShader(matfx_env_amb_VS);
else if((lightBits & VSLIGHT_MASK) == VSLIGHT_DIRECT)
setVertexShader(matfx_env_amb_dir_VS);
else
setVertexShader(matfx_env_all_VS);
bool32 texAlpha = GETD3DRASTEREXT(env->tex->raster)->hasAlpha;
if(inst->material->texture){
d3d::setTexture(0, m->texture);
setPixelShader(matfx_env_tex_PS);
}else
setPixelShader(matfx_env_PS);
SetRenderState(VERTEXALPHA, texAlpha || inst->vertexAlpha || m->color.alpha != 255);
drawInst(header, inst);
SetRenderState(SRCBLEND, BLENDSRCALPHA);
}
void
matfxRenderCB_Shader(Atomic *atomic, InstanceDataHeader *header)
{
int vsBits;
uint32 flags = atomic->geometry->flags;
setStreamSource(0, (IDirect3DVertexBuffer9*)header->vertexStream[0].vertexBuffer,
0, header->vertexStream[0].stride);
setIndices((IDirect3DIndexBuffer9*)header->indexBuffer);
setVertexDeclaration((IDirect3DVertexDeclaration9*)header->vertexDeclaration);
lastEnvFrame = nil;
vsBits = lightingCB_Shader(atomic);
uploadMatrices(atomic->getFrame()->getLTM());
bool normals = !!(atomic->geometry->flags & Geometry::NORMALS);
InstanceData *inst = header->inst;
for(uint32 i = 0; i < header->numMeshes; i++){
Material *m = inst->material;
setMaterial(flags, m->color, m->surfaceProps);
MatFX *matfx = MatFX::get(m);
if(matfx == nil)
matfxRender_Default(header, inst, vsBits);
else switch(matfx->type){
case MatFX::ENVMAP:
if(normals)
matfxRender_EnvMap(header, inst, vsBits, &matfx->fx[0].env);
else
matfxRender_Default(header, inst, vsBits);
break;
case MatFX::NOTHING:
case MatFX::BUMPMAP:
case MatFX::BUMPENVMAP:
case MatFX::DUAL:
case MatFX::UVTRANSFORM:
case MatFX::DUALUVTRANSFORM:
// not supported yet
matfxRender_Default(header, inst, vsBits);
break;
}
inst++;
}
d3d::setTexture(1, nil);
}
#define VS_NAME g_vs20_main
#define PS_NAME g_ps20_main
void
createMatFXShaders(void)
{
{
static
#include "shaders/matfx_env_amb_VS.h"
matfx_env_amb_VS = createVertexShader((void*)VS_NAME);
assert(matfx_env_amb_VS);
}
{
static
#include "shaders/matfx_env_amb_dir_VS.h"
matfx_env_amb_dir_VS = createVertexShader((void*)VS_NAME);
assert(matfx_env_amb_dir_VS);
}
{
static
#include "shaders/matfx_env_all_VS.h"
matfx_env_all_VS = createVertexShader((void*)VS_NAME);
assert(matfx_env_all_VS);
}
{
static
#include "shaders/matfx_env_PS.h"
matfx_env_PS = createPixelShader((void*)PS_NAME);
assert(matfx_env_PS);
}
{
static
#include "shaders/matfx_env_tex_PS.h"
matfx_env_tex_PS = createPixelShader((void*)PS_NAME);
assert(matfx_env_tex_PS);
}
}
void
destroyMatFXShaders(void)
{
destroyVertexShader(matfx_env_amb_VS);
matfx_env_amb_VS = nil;
destroyVertexShader(matfx_env_amb_dir_VS);
matfx_env_amb_dir_VS = nil;
destroyVertexShader(matfx_env_all_VS);
matfx_env_all_VS = nil;
destroyPixelShader(matfx_env_PS);
matfx_env_PS = nil;
destroyPixelShader(matfx_env_tex_PS);
matfx_env_tex_PS = nil;
}
#endif
static void*
matfxOpen(void *o, int32, int32)
{
#ifdef RW_D3D9
createMatFXShaders();
#endif
matFXGlobals.pipelines[PLATFORM_D3D9] = makeMatFXPipeline();
return o;
}
static void*
matfxClose(void *o, int32, int32)
{
#ifdef RW_D3D9
destroyMatFXShaders();
#endif
((ObjPipeline*)matFXGlobals.pipelines[PLATFORM_D3D9])->destroy();
matFXGlobals.pipelines[PLATFORM_D3D9] = nil;
return o;
}
void
initMatFX(void)
{
Driver::registerPlugin(PLATFORM_D3D9, 0, ID_MATFX,
matfxOpen, matfxClose);
}
ObjPipeline*
makeMatFXPipeline(void)
{
ObjPipeline *pipe = ObjPipeline::create();
pipe->instanceCB = defaultInstanceCB;
pipe->uninstanceCB = defaultUninstanceCB;
pipe->renderCB = matfxRenderCB_Shader;
pipe->pluginID = ID_MATFX;
pipe->pluginData = 0;
return pipe;
}
}
}
|