2 * shaders implementation
4 * Copyright 2002-2003 Jason Edmeades
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wined3d_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
30 /* Shader debugging - Change the following line to enable debugging of software
32 #if 0 /* Musxt not be 1 in cvs version */
33 # define VSTRACE(A) TRACE A
34 # define TRACE_VSVECTOR(name) TRACE( #name "=(%f, %f, %f, %f)\n", name.x, name.y, name.z, name.w)
37 # define TRACE_VSVECTOR(name)
40 #if 0 /* FIXME : Needs sorting when vshader code moved in properly */
43 * DirectX9 SDK download
44 * http://msdn.microsoft.com/library/default.asp?url=/downloads/list/directx.asp
47 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndrive/html/directx07162002.asp
49 * Using Vertex Shaders
50 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndrive/html/directx02192001.asp
53 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/whatsnew.asp
56 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader2_0/VertexShader2_0.asp
57 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader2_0/Instructions/Instructions.asp
58 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/GettingStarted/VertexDeclaration/VertexDeclaration.asp
59 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader3_0/VertexShader3_0.asp
62 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/advancedtopics/VertexPipe/matrixstack/matrixstack.asp
65 * http://msdn.microsoft.com/library/en-us/directx9_c/directx/graphics/programmingguide/GettingStarted/VertexFormats/vformats.asp
67 * NVIDIA: DX8 Vertex Shader to NV Vertex Program
68 * http://developer.nvidia.com/view.asp?IO=vstovp
70 * NVIDIA: Memory Management with VAR
71 * http://developer.nvidia.com/view.asp?IO=var_memory_management
74 typedef void (*shader_fct_t)();
76 typedef struct SHADER_OPCODE {
79 CONST UINT num_params;
80 shader_fct_t soft_fct;
85 /*******************************
86 * vshader functions software VM
89 void vshader_add(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
94 VSTRACE(("executing add: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
95 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
98 void vshader_dp3(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
99 d->x = d->y = d->z = d->w = s0->x * s1->x + s0->y * s1->y + s0->z * s1->z;
100 VSTRACE(("executing dp3: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
101 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
104 void vshader_dp4(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
105 d->x = d->y = d->z = d->w = s0->x * s1->x + s0->y * s1->y + s0->z * s1->z + s0->w * s1->w;
106 VSTRACE(("executing dp4: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
107 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
110 void vshader_dst(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
112 d->y = s0->y * s1->y;
115 VSTRACE(("executing dst: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
116 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
119 void vshader_expp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
125 tmp.f = floorf(s0->w);
126 d->x = powf(2.0f, tmp.f);
127 d->y = s0->w - tmp.f;
128 tmp.f = powf(2.0f, s0->w);
129 tmp.d &= 0xFFFFFF00U;
132 VSTRACE(("executing exp: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
133 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
136 void vshader_lit(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
138 d->y = (0.0f < s0->x) ? s0->x : 0.0f;
139 d->z = (0.0f < s0->x && 0.0f < s0->y) ? powf(s0->y, s0->w) : 0.0f;
141 VSTRACE(("executing lit: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
142 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
145 void vshader_logp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
146 float tmp_f = fabsf(s0->w);
147 d->x = d->y = d->z = d->w = (0.0f != tmp_f) ? logf(tmp_f) / logf(2.0f) : -HUGE_VAL;
148 VSTRACE(("executing logp: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
149 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
152 void vshader_mad(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1, D3DSHADERVECTOR* s2) {
153 d->x = s0->x * s1->x + s2->x;
154 d->y = s0->y * s1->y + s2->y;
155 d->z = s0->z * s1->z + s2->z;
156 d->w = s0->w * s1->w + s2->w;
157 VSTRACE(("executing mad: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) s2=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
158 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, s2->x, s2->y, s2->z, s2->w, d->x, d->y, d->z, d->w));
161 void vshader_max(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
162 d->x = (s0->x >= s1->x) ? s0->x : s1->x;
163 d->y = (s0->y >= s1->y) ? s0->y : s1->y;
164 d->z = (s0->z >= s1->z) ? s0->z : s1->z;
165 d->w = (s0->w >= s1->w) ? s0->w : s1->w;
166 VSTRACE(("executing max: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
167 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
170 void vshader_min(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
171 d->x = (s0->x < s1->x) ? s0->x : s1->x;
172 d->y = (s0->y < s1->y) ? s0->y : s1->y;
173 d->z = (s0->z < s1->z) ? s0->z : s1->z;
174 d->w = (s0->w < s1->w) ? s0->w : s1->w;
175 VSTRACE(("executing min: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
176 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
179 void vshader_mov(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
184 VSTRACE(("executing mov: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
185 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
188 void vshader_mul(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
189 d->x = s0->x * s1->x;
190 d->y = s0->y * s1->y;
191 d->z = s0->z * s1->z;
192 d->w = s0->w * s1->w;
193 VSTRACE(("executing mul: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
194 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
197 void vshader_nop(void) {
198 /* NOPPPP ahhh too easy ;) */
201 void vshader_rcp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
202 d->x = d->y = d->z = d->w = (0.0f == s0->w) ? HUGE_VAL : 1.0f / s0->w;
203 VSTRACE(("executing rcp: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
204 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
207 void vshader_rsq(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
208 float tmp_f = fabsf(s0->w);
209 d->x = d->y = d->z = d->w = (0.0f == tmp_f) ? HUGE_VAL : ((1.0f != tmp_f) ? 1.0f / sqrtf(tmp_f) : 1.0f);
210 VSTRACE(("executing rsq: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
211 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
214 void vshader_sge(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
215 d->x = (s0->x >= s1->x) ? 1.0f : 0.0f;
216 d->y = (s0->y >= s1->y) ? 1.0f : 0.0f;
217 d->z = (s0->z >= s1->z) ? 1.0f : 0.0f;
218 d->w = (s0->w >= s1->w) ? 1.0f : 0.0f;
219 VSTRACE(("executing sge: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
220 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
223 void vshader_slt(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
224 d->x = (s0->x < s1->x) ? 1.0f : 0.0f;
225 d->y = (s0->y < s1->y) ? 1.0f : 0.0f;
226 d->z = (s0->z < s1->z) ? 1.0f : 0.0f;
227 d->w = (s0->w < s1->w) ? 1.0f : 0.0f;
228 VSTRACE(("executing slt: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
229 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
232 void vshader_sub(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
233 d->x = s0->x - s1->x;
234 d->y = s0->y - s1->y;
235 d->z = s0->z - s1->z;
236 d->w = s0->w - s1->w;
237 VSTRACE(("executing sub: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
238 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
242 * Version 1.1 specific
245 void vshader_exp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
246 d->x = d->y = d->z = d->w = powf(2.0f, s0->w);
247 VSTRACE(("executing exp: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
248 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
251 void vshader_log(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
252 float tmp_f = fabsf(s0->w);
253 d->x = d->y = d->z = d->w = (0.0f != tmp_f) ? logf(tmp_f) / logf(2.0f) : -HUGE_VAL;
254 VSTRACE(("executing log: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
255 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
258 void vshader_frc(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
259 d->x = s0->x - floorf(s0->x);
260 d->y = s0->y - floorf(s0->y);
263 VSTRACE(("executing frc: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
264 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
267 typedef FLOAT D3DMATRIX44[4][4];
268 typedef FLOAT D3DMATRIX43[4][3];
269 typedef FLOAT D3DMATRIX34[4][4];
270 typedef FLOAT D3DMATRIX33[4][3];
271 typedef FLOAT D3DMATRIX32[4][2];
273 void vshader_m4x4(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, /*D3DSHADERVECTOR* mat1*/ D3DMATRIX44 mat) {
275 * BuGGY CODE: here only if cast not work for copy/paste
276 D3DSHADERVECTOR* mat2 = mat1 + 1;
277 D3DSHADERVECTOR* mat3 = mat1 + 2;
278 D3DSHADERVECTOR* mat4 = mat1 + 3;
279 d->x = mat1->x * s0->x + mat2->x * s0->y + mat3->x * s0->z + mat4->x * s0->w;
280 d->y = mat1->y * s0->x + mat2->y * s0->y + mat3->y * s0->z + mat4->y * s0->w;
281 d->z = mat1->z * s0->x + mat2->z * s0->y + mat3->z * s0->z + mat4->z * s0->w;
282 d->w = mat1->w * s0->x + mat2->w * s0->y + mat3->w * s0->z + mat4->w * s0->w;
284 d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z + mat[0][3] * s0->w;
285 d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z + mat[1][3] * s0->w;
286 d->z = mat[2][0] * s0->x + mat[2][1] * s0->y + mat[2][2] * s0->z + mat[2][3] * s0->w;
287 d->w = mat[3][0] * s0->x + mat[3][1] * s0->y + mat[3][2] * s0->z + mat[3][3] * s0->w;
288 VSTRACE(("executing m4x4(1): mat=(%f, %f, %f, %f) s0=(%f) d=(%f) \n", mat[0][0], mat[0][1], mat[0][2], mat[0][3], s0->x, d->x));
289 VSTRACE(("executing m4x4(2): mat=(%f, %f, %f, %f) (%f) (%f) \n", mat[1][0], mat[1][1], mat[1][2], mat[1][3], s0->y, d->y));
290 VSTRACE(("executing m4x4(3): mat=(%f, %f, %f, %f) X (%f) = (%f) \n", mat[2][0], mat[2][1], mat[2][2], mat[2][3], s0->z, d->z));
291 VSTRACE(("executing m4x4(4): mat=(%f, %f, %f, %f) (%f) (%f) \n", mat[3][0], mat[3][1], mat[3][2], mat[3][3], s0->w, d->w));
294 void vshader_m4x3(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DMATRIX43 mat) {
295 d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z + mat[0][3] * s0->w;
296 d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z + mat[1][3] * s0->w;
297 d->z = mat[2][0] * s0->x + mat[2][1] * s0->y + mat[2][2] * s0->z + mat[2][3] * s0->w;
299 VSTRACE(("executing m4x3(1): mat=(%f, %f, %f, %f) s0=(%f) d=(%f) \n", mat[0][0], mat[0][1], mat[0][2], mat[0][3], s0->x, d->x));
300 VSTRACE(("executing m4x3(2): mat=(%f, %f, %f, %f) (%f) (%f) \n", mat[1][0], mat[1][1], mat[1][2], mat[1][3], s0->y, d->y));
301 VSTRACE(("executing m4x3(3): mat=(%f, %f, %f, %f) X (%f) = (%f) \n", mat[2][0], mat[2][1], mat[2][2], mat[2][3], s0->z, d->z));
302 VSTRACE(("executing m4x3(4): (%f) (%f) \n", s0->w, d->w));
305 void vshader_m3x4(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DMATRIX34 mat) {
306 d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z;
307 d->y = mat[2][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z;
308 d->z = mat[2][0] * s0->x + mat[2][1] * s0->y + mat[2][2] * s0->z;
309 d->w = mat[3][0] * s0->x + mat[3][1] * s0->y + mat[3][2] * s0->z;
310 VSTRACE(("executing m3x4(1): mat=(%f, %f, %f) s0=(%f) d=(%f) \n", mat[0][0], mat[0][1], mat[0][2], s0->x, d->x));
311 VSTRACE(("executing m3x4(2): mat=(%f, %f, %f) (%f) (%f) \n", mat[1][0], mat[1][1], mat[1][2], s0->y, d->y));
312 VSTRACE(("executing m3x4(3): mat=(%f, %f, %f) X (%f) = (%f) \n", mat[2][0], mat[2][1], mat[2][2], s0->z, d->z));
313 VSTRACE(("executing m3x4(4): mat=(%f, %f, %f) (%f) (%f) \n", mat[3][0], mat[3][1], mat[3][2], s0->w, d->w));
316 void vshader_m3x3(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DMATRIX33 mat) {
317 d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[2][2] * s0->z;
318 d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[2][2] * s0->z;
319 d->z = mat[2][0] * s0->x + mat[2][1] * s0->y + mat[2][2] * s0->z;
321 VSTRACE(("executing m3x3(1): mat=(%f, %f, %f) s0=(%f) d=(%f) \n", mat[0][0], mat[0][1], mat[0][2], s0->x, d->x));
322 VSTRACE(("executing m3x3(2): mat=(%f, %f, %f) (%f) (%f) \n", mat[1][0], mat[1][1], mat[1][2], s0->y, d->y));
323 VSTRACE(("executing m3x3(3): mat=(%f, %f, %f) X (%f) = (%f) \n", mat[2][0], mat[2][1], mat[2][2], s0->z, d->z));
324 VSTRACE(("executing m3x3(4): (%f) \n", d->w));
327 void vshader_m3x2(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DMATRIX32 mat) {
329 d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z;
330 d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z;
336 * Version 2.0 specific
338 void vshader_lrp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1, D3DSHADERVECTOR* s2, D3DSHADERVECTOR* s3) {
339 d->x = s0->x * (s1->x - s2->x) + s2->x;
340 d->y = s0->y * (s1->y - s2->y) + s2->y;
341 d->z = s0->z * (s1->z - s2->z) + s2->z;
342 d->w = s0->w * (s1->w - s2->w) + s2->x;
346 * log, exp, frc, m*x* seems to be macros ins ... to see
348 static CONST SHADER_OPCODE vshader_ins [] = {
349 {D3DSIO_NOP, "nop", 0, vshader_nop, 0, 0},
350 {D3DSIO_MOV, "mov", 2, vshader_mov, 0, 0},
351 {D3DSIO_ADD, "add", 3, vshader_add, 0, 0},
352 {D3DSIO_SUB, "sub", 3, vshader_sub, 0, 0},
353 {D3DSIO_MAD, "mad", 4, vshader_mad, 0, 0},
354 {D3DSIO_MUL, "mul", 3, vshader_mul, 0, 0},
355 {D3DSIO_RCP, "rcp", 2, vshader_rcp, 0, 0},
356 {D3DSIO_RSQ, "rsq", 2, vshader_rsq, 0, 0},
357 {D3DSIO_DP3, "dp3", 3, vshader_dp3, 0, 0},
358 {D3DSIO_DP4, "dp4", 3, vshader_dp4, 0, 0},
359 {D3DSIO_MIN, "min", 3, vshader_min, 0, 0},
360 {D3DSIO_MAX, "max", 3, vshader_max, 0, 0},
361 {D3DSIO_SLT, "slt", 3, vshader_slt, 0, 0},
362 {D3DSIO_SGE, "sge", 3, vshader_sge, 0, 0},
363 {D3DSIO_EXP, "exp", 2, vshader_exp, 0, 0},
364 {D3DSIO_LOG, "log", 2, vshader_log, 0, 0},
365 {D3DSIO_LIT, "lit", 2, vshader_lit, 0, 0},
366 {D3DSIO_DST, "dst", 3, vshader_dst, 0, 0},
367 {D3DSIO_LRP, "lrp", 5, vshader_lrp, 0, 0},
368 {D3DSIO_FRC, "frc", 2, vshader_frc, 0, 0},
369 {D3DSIO_M4x4, "m4x4", 3, vshader_m4x4, 0, 0},
370 {D3DSIO_M4x3, "m4x3", 3, vshader_m4x3, 0, 0},
371 {D3DSIO_M3x4, "m3x4", 3, vshader_m3x4, 0, 0},
372 {D3DSIO_M3x3, "m3x3", 3, vshader_m3x3, 0, 0},
373 {D3DSIO_M3x2, "m3x2", 3, vshader_m3x2, 0, 0},
374 /** FIXME: use direct access so add the others opcodes as stubs */
375 {D3DSIO_EXPP, "expp", 2, vshader_expp, 0, 0},
376 {D3DSIO_LOGP, "logp", 2, vshader_logp, 0, 0},
378 {0, NULL, 0, NULL, 0, 0}
382 inline static const SHADER_OPCODE* vshader_program_get_opcode(const DWORD code) {
384 /** TODO: use dichotomic search */
385 while (NULL != vshader_ins[i].name) {
386 if ((code & D3DSI_OPCODE_MASK) == vshader_ins[i].opcode) {
387 return &vshader_ins[i];
394 inline static void vshader_program_dump_param(const DWORD param, int input) {
395 static const char* rastout_reg_names[] = { "oPos", "oFog", "oPts" };
396 static const char swizzle_reg_chars[] = "xyzw";
398 DWORD reg = param & 0x00001FFF;
399 DWORD regtype = ((param & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT);
401 if ((param & D3DSP_SRCMOD_MASK) == D3DSPSM_NEG) TRACE("-");
403 switch (regtype << D3DSP_REGTYPE_SHIFT) {
405 TRACE("R[%lu]", reg);
408 TRACE("V[%lu]", reg);
411 TRACE("C[%s%lu]", (param & D3DVS_ADDRMODE_RELATIVE) ? "a0.x + " : "", reg);
413 case D3DSPR_ADDR: /*case D3DSPR_TEXTURE:*/
414 TRACE("a[%lu]", reg);
417 TRACE("%s", rastout_reg_names[reg]);
420 TRACE("oD[%lu]", reg);
422 case D3DSPR_TEXCRDOUT:
423 TRACE("oT[%lu]", reg);
430 /** operand output */
431 if ((param & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
432 if (param & D3DSP_WRITEMASK_0) TRACE(".x");
433 if (param & D3DSP_WRITEMASK_1) TRACE(".y");
434 if (param & D3DSP_WRITEMASK_2) TRACE(".z");
435 if (param & D3DSP_WRITEMASK_3) TRACE(".w");
439 DWORD swizzle = (param & D3DVS_SWIZZLE_MASK) >> D3DVS_SWIZZLE_SHIFT;
440 DWORD swizzle_x = swizzle & 0x03;
441 DWORD swizzle_y = (swizzle >> 2) & 0x03;
442 DWORD swizzle_z = (swizzle >> 4) & 0x03;
443 DWORD swizzle_w = (swizzle >> 6) & 0x03;
445 * swizzle bits fields:
448 if ((D3DVS_NOSWIZZLE >> D3DVS_SWIZZLE_SHIFT) != swizzle) { /* ! D3DVS_NOSWIZZLE == 0xE4 << D3DVS_SWIZZLE_SHIFT */
449 if (swizzle_x == swizzle_y &&
450 swizzle_x == swizzle_z &&
451 swizzle_x == swizzle_w) {
452 TRACE(".%c", swizzle_reg_chars[swizzle_x]);
455 swizzle_reg_chars[swizzle_x],
456 swizzle_reg_chars[swizzle_y],
457 swizzle_reg_chars[swizzle_z],
458 swizzle_reg_chars[swizzle_w]);
464 inline static BOOL vshader_is_version_token(DWORD token) {
465 return 0xFFFE0000 == (token & 0xFFFE0000);
468 inline static BOOL vshader_is_comment_token(DWORD token) {
469 return D3DSIO_COMMENT == (token & D3DSI_OPCODE_MASK);
473 * Function parser ...
475 HRESULT WINAPI IDirect3DVertexShaderImpl_ParseProgram(IDirect3DVertexShaderImpl* vshader, CONST DWORD* pFunction) {
476 const DWORD* pToken = pFunction;
477 const SHADER_OPCODE* curOpcode = NULL;
481 if (NULL != pToken) {
482 while (D3DVS_END() != *pToken) {
483 if (vshader_is_version_token(*pToken)) { /** version */
484 TRACE("vs.%lu.%lu\n", (*pToken >> 8) & 0x0F, (*pToken & 0x0F));
489 if (vshader_is_comment_token(*pToken)) { /** comment */
490 DWORD comment_len = (*pToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
492 /*TRACE("comment[%ld] ;%s\n", comment_len, (char*)pToken);*/
493 pToken += comment_len;
494 len += comment_len + 1;
497 curOpcode = vshader_program_get_opcode(*pToken);
500 if (NULL == curOpcode) {
501 /* unkown current opcode ... */
502 while (*pToken & 0x80000000) {
503 TRACE("unrecognized opcode: %08lx\n", *pToken);
508 TRACE("%s ", curOpcode->name);
509 if (curOpcode->num_params > 0) {
510 vshader_program_dump_param(*pToken, 0);
513 for (i = 1; i < curOpcode->num_params; ++i) {
515 vshader_program_dump_param(*pToken, 1);
523 vshader->functionLength = (len + 1) * sizeof(DWORD);
525 vshader->functionLength = 1; /* no Function defined use fixed function vertex processing */
527 /* copy the function ... because it will certainly be released by application */
529 if (NULL != pFunction) {
530 vshader->function = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, vshader->functionLength);
531 memcpy(vshader->function, pFunction, vshader->functionLength);
533 vshader->function = NULL;
538 BOOL IDirect3DVertexShaderImpl_ExecuteHAL(IDirect3DVertexShaderImpl* vshader, VSHADERINPUTDATA* input, VSHADEROUTPUTDATA* output) {
540 * TODO: use the NV_vertex_program (or 1_1) extension
541 * and specifics vendors (ARB_vertex_program??) variants for it
546 HRESULT WINAPI IDirect3DVertexShaderImpl_ExecuteSW(IDirect3DVertexShaderImpl* vshader, VSHADERINPUTDATA* input, VSHADEROUTPUTDATA* output) {
547 /** Vertex Shader Temporary Registers */
548 D3DSHADERVECTOR R[12];
549 /*D3DSHADERSCALAR A0;*/
550 D3DSHADERVECTOR A[1];
551 /** temporary Vector for modifier management */
553 D3DSHADERVECTOR s[3];
555 const DWORD* pToken = vshader->function;
556 const SHADER_OPCODE* curOpcode = NULL;
557 /** functions parameters */
558 D3DSHADERVECTOR* p[4];
559 D3DSHADERVECTOR* p_send[4];
562 /** init temporary register */
563 memset(R, 0, 12 * sizeof(D3DSHADERVECTOR));
565 /* vshader_program_parse(vshader); */
566 #if 0 /* Must not be 1 in cvs */
568 TRACE_VSVECTOR(vshader->data->C[0]);
569 TRACE_VSVECTOR(vshader->data->C[1]);
570 TRACE_VSVECTOR(vshader->data->C[2]);
571 TRACE_VSVECTOR(vshader->data->C[3]);
572 TRACE_VSVECTOR(vshader->data->C[4]);
573 TRACE_VSVECTOR(vshader->data->C[5]);
574 TRACE_VSVECTOR(vshader->data->C[6]);
575 TRACE_VSVECTOR(vshader->data->C[7]);
576 TRACE_VSVECTOR(vshader->data->C[8]);
577 TRACE_VSVECTOR(vshader->data->C[64]);
578 TRACE_VSVECTOR(input->V[D3DVSDE_POSITION]);
579 TRACE_VSVECTOR(input->V[D3DVSDE_BLENDWEIGHT]);
580 TRACE_VSVECTOR(input->V[D3DVSDE_BLENDINDICES]);
581 TRACE_VSVECTOR(input->V[D3DVSDE_NORMAL]);
582 TRACE_VSVECTOR(input->V[D3DVSDE_PSIZE]);
583 TRACE_VSVECTOR(input->V[D3DVSDE_DIFFUSE]);
584 TRACE_VSVECTOR(input->V[D3DVSDE_SPECULAR]);
585 TRACE_VSVECTOR(input->V[D3DVSDE_TEXCOORD0]);
586 TRACE_VSVECTOR(input->V[D3DVSDE_TEXCOORD1]);
589 TRACE_VSVECTOR(vshader->data->C[64]);
591 /* the first dword is the version tag */
594 if (vshader_is_version_token(*pToken)) { /** version */
597 while (D3DVS_END() != *pToken) {
598 if (vshader_is_comment_token(*pToken)) { /** comment */
599 DWORD comment_len = (*pToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
601 pToken += comment_len;
604 curOpcode = vshader_program_get_opcode(*pToken);
606 if (NULL == curOpcode) {
608 /* unkown current opcode ... */
609 while (*pToken & 0x80000000) {
611 TRACE("unrecognized opcode: pos=%d token=%08lX\n", (pToken - 1) - vshader->function, *(pToken - 1));
613 TRACE("unrecognized opcode param: pos=%d token=%08lX what=", pToken - vshader->function, *pToken);
614 vshader_program_dump_param(*pToken, i);
621 if (curOpcode->num_params > 0) {
622 /*TRACE(">> execting opcode: pos=%d opcode_name=%s token=%08lX\n", pToken - vshader->function, curOpcode->name, *pToken);*/
623 for (i = 0; i < curOpcode->num_params; ++i) {
624 DWORD reg = pToken[i] & 0x00001FFF;
625 DWORD regtype = ((pToken[i] & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT);
627 switch (regtype << D3DSP_REGTYPE_SHIFT) {
629 /*TRACE("p[%d]=R[%d]\n", i, reg);*/
633 /*TRACE("p[%d]=V[%s]\n", i, VertexShaderDeclRegister[reg]);*/
634 p[i] = &input->V[reg];
637 if (pToken[i] & D3DVS_ADDRMODE_RELATIVE) {
638 p[i] = &vshader->data->C[(DWORD) A[0].x + reg];
640 p[i] = &vshader->data->C[reg];
643 case D3DSPR_ADDR: /*case D3DSPR_TEXTURE:*/
645 ERR("cannot handle address registers != a0, forcing use of a0\n");
648 /*TRACE("p[%d]=A[%d]\n", i, reg);*/
653 case D3DSRO_POSITION:
654 p[i] = &output->oPos;
657 p[i] = &output->oFog;
659 case D3DSRO_POINT_SIZE:
660 p[i] = &output->oPts;
665 /*TRACE("p[%d]=oD[%d]\n", i, reg);*/
666 p[i] = &output->oD[reg];
668 case D3DSPR_TEXCRDOUT:
669 /*TRACE("p[%d]=oT[%d]\n", i, reg);*/
670 p[i] = &output->oT[reg];
676 if (i > 0) { /* input reg */
677 DWORD swizzle = (pToken[i] & D3DVS_SWIZZLE_MASK) >> D3DVS_SWIZZLE_SHIFT;
678 UINT isNegative = ((pToken[i] & D3DSP_SRCMOD_MASK) == D3DSPSM_NEG);
680 if (!isNegative && (D3DVS_NOSWIZZLE >> D3DVS_SWIZZLE_SHIFT) == swizzle) {
681 /*TRACE("p[%d] not swizzled\n", i);*/
684 DWORD swizzle_x = swizzle & 0x03;
685 DWORD swizzle_y = (swizzle >> 2) & 0x03;
686 DWORD swizzle_z = (swizzle >> 4) & 0x03;
687 DWORD swizzle_w = (swizzle >> 6) & 0x03;
688 /*TRACE("p[%d] swizzled\n", i);*/
689 float* tt = (float*) p[i];
690 s[i].x = (isNegative) ? -tt[swizzle_x] : tt[swizzle_x];
691 s[i].y = (isNegative) ? -tt[swizzle_y] : tt[swizzle_y];
692 s[i].z = (isNegative) ? -tt[swizzle_z] : tt[swizzle_z];
693 s[i].w = (isNegative) ? -tt[swizzle_w] : tt[swizzle_w];
696 } else { /* output reg */
697 if ((pToken[i] & D3DSP_WRITEMASK_ALL) == D3DSP_WRITEMASK_ALL) {
700 p_send[i] = &d; /* to be post-processed for modifiers management */
706 switch (curOpcode->num_params) {
708 curOpcode->soft_fct();
711 curOpcode->soft_fct(p_send[0]);
714 curOpcode->soft_fct(p_send[0], p_send[1]);
717 curOpcode->soft_fct(p_send[0], p_send[1], p_send[2]);
720 curOpcode->soft_fct(p_send[0], p_send[1], p_send[2], p_send[3]);
723 curOpcode->soft_fct(p_send[0], p_send[1], p_send[2], p_send[3], p_send[4]);
726 ERR("%s too many params: %u\n", curOpcode->name, curOpcode->num_params);
729 /* check if output reg modifier post-process */
730 if (curOpcode->num_params > 0 && (pToken[0] & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
731 if (pToken[0] & D3DSP_WRITEMASK_0) p[0]->x = d.x;
732 if (pToken[0] & D3DSP_WRITEMASK_1) p[0]->y = d.y;
733 if (pToken[0] & D3DSP_WRITEMASK_2) p[0]->z = d.z;
734 if (pToken[0] & D3DSP_WRITEMASK_3) p[0]->w = d.w;
738 TRACE_VSVECTOR(output->oPos);
739 TRACE_VSVECTOR(output->oD[0]);
740 TRACE_VSVECTOR(output->oD[1]);
741 TRACE_VSVECTOR(output->oT[0]);
742 TRACE_VSVECTOR(output->oT[1]);
743 TRACE_VSVECTOR(R[0]);
744 TRACE_VSVECTOR(R[1]);
745 TRACE_VSVECTOR(R[2]);
746 TRACE_VSVECTOR(R[3]);
747 TRACE_VSVECTOR(R[4]);
748 TRACE_VSVECTOR(R[5]);
751 /* to next opcode token */
752 pToken += curOpcode->num_params;
755 TRACE("End of current instruction:\n");
756 TRACE_VSVECTOR(output->oPos);
757 TRACE_VSVECTOR(output->oD[0]);
758 TRACE_VSVECTOR(output->oD[1]);
759 TRACE_VSVECTOR(output->oT[0]);
760 TRACE_VSVECTOR(output->oT[1]);
761 TRACE_VSVECTOR(R[0]);
762 TRACE_VSVECTOR(R[1]);
763 TRACE_VSVECTOR(R[2]);
764 TRACE_VSVECTOR(R[3]);
765 TRACE_VSVECTOR(R[4]);
766 TRACE_VSVECTOR(R[5]);
769 #if 0 /* Must not be 1 in cvs */
771 TRACE_VSVECTOR(output->oPos);
772 TRACE_VSVECTOR(output->oD[0]);
773 TRACE_VSVECTOR(output->oD[1]);
774 TRACE_VSVECTOR(output->oT[0]);
775 TRACE_VSVECTOR(output->oT[1]);
780 HRESULT WINAPI IDirect3DVertexShaderImpl_GetFunction(IDirect3DVertexShaderImpl* This, VOID* pData, UINT* pSizeOfData) {
782 *pSizeOfData = This->functionLength;
785 if (*pSizeOfData < This->functionLength) {
786 *pSizeOfData = This->functionLength;
787 return D3DERR_MOREDATA;
789 if (NULL == This->function) { /* no function defined */
790 TRACE("(%p) : GetFunction no User Function defined using NULL to %p\n", This, pData);
791 (*(DWORD **) pData) = NULL;
793 TRACE("(%p) : GetFunction copying to %p\n", This, pData);
794 memcpy(pData, This->function, This->functionLength);
799 HRESULT WINAPI IDirect3DVertexShaderImpl_SetConstantF(IDirect3DVertexShaderImpl* This, UINT StartRegister, CONST FLOAT* pConstantData, UINT Vector4fCount) {
800 FIXME("(%p) : stub\n", This);
804 HRESULT WINAPI IDirect3DVertexShaderImpl_GetConstantF(IDirect3DVertexShaderImpl* This, UINT StartRegister, FLOAT* pConstantData, UINT Vector4fCount) {
805 FIXME("(%p) : stub\n", This);
809 HRESULT WINAPI IDirect3DVertexShaderImpl_SetConstantI(IDirect3DVertexShaderImpl* This, UINT StartRegister, CONST int* pConstantData, UINT Vector4iCount) {
810 if (StartRegister + Vector4iCount > D3D_VSHADER_MAX_CONSTANTS) {
811 ERR("(%p) : SetVertexShaderConstantI C[%u] invalid\n", This, StartRegister);
812 return D3DERR_INVALIDCALL;
814 if (NULL == pConstantData) {
815 return D3DERR_INVALIDCALL;
817 FIXME("(%p) : stub\n", This);
821 HRESULT WINAPI IDirect3DVertexShaderImpl_GetConstantI(IDirect3DVertexShaderImpl* This, UINT StartRegister, int* pConstantData, UINT Vector4iCount) {
822 TRACE("(%p) : C[%u] count=%u\n", This, StartRegister, Vector4iCount);
823 if (StartRegister + Vector4iCount > D3D_VSHADER_MAX_CONSTANTS) {
824 return D3DERR_INVALIDCALL;
826 if (NULL == pConstantData) {
827 return D3DERR_INVALIDCALL;
829 FIXME("(%p) : stub\n", This);
833 HRESULT WINAPI IDirect3DVertexShaderImpl_SetConstantB(IDirect3DVertexShaderImpl* This, UINT StartRegister, CONST BOOL* pConstantData, UINT BoolCount) {
834 if (StartRegister + BoolCount > D3D_VSHADER_MAX_CONSTANTS) {
835 ERR("(%p) : SetVertexShaderConstantB C[%u] invalid\n", This, StartRegister);
836 return D3DERR_INVALIDCALL;
838 if (NULL == pConstantData) {
839 return D3DERR_INVALIDCALL;
841 FIXME("(%p) : stub\n", This);
845 HRESULT WINAPI IDirect3DVertexShaderImpl_GetConstantB(IDirect3DVertexShaderImpl* This, UINT StartRegister, BOOL* pConstantData, UINT BoolCount) {
846 FIXME("(%p) : stub\n", This);
852 /* *******************************************
853 IWineD3DVertexShader IUnknown parts follow
854 ******************************************* */
855 HRESULT WINAPI IWineD3DVertexShaderImpl_QueryInterface(IWineD3DVertexShader *iface, REFIID riid, LPVOID *ppobj)
857 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
858 FIXME("(%p) :stub\n",This);
859 return E_NOINTERFACE;
862 ULONG WINAPI IWineD3DVertexShaderImpl_AddRef(IWineD3DVertexShader *iface) {
863 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
864 TRACE("(%p) : AddRef increasing from %ld\n", This, This->ref);
865 return InterlockedIncrement(&This->ref);
868 ULONG WINAPI IWineD3DVertexShaderImpl_Release(IWineD3DVertexShader *iface) {
869 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
871 TRACE("(%p) : Releasing from %ld\n", This, This->ref);
872 ref = InterlockedDecrement(&This->ref);
874 HeapFree(GetProcessHeap(), 0, This);
879 /* *******************************************
880 IWineD3DVertexShader IWineD3DVertexShader parts follow
881 ******************************************* */
883 HRESULT WINAPI IWineD3DVertexShaderImpl_GetParent(IWineD3DVertexShader *iface, IUnknown** parent){
884 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
886 *parent= (IUnknown*) parent;
887 IUnknown_AddRef(*parent);
888 TRACE("(%p) : returning %p\n", This, *parent);
892 HRESULT WINAPI IWineD3DVertexShaderImpl_GetDevice(IWineD3DVertexShader* iface, IWineD3DDevice **pDevice){
893 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
894 IWineD3DDevice_AddRef((IWineD3DDevice *)This->wineD3DDevice);
895 *pDevice = (IWineD3DDevice *)This->wineD3DDevice;
896 TRACE("(%p) returning %p\n", This, *pDevice);
900 HRESULT WINAPI IWineD3DVertexShaderImpl_GetFunction(IWineD3DVertexShader* impl, VOID* pData, UINT* pSizeOfData) {
901 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)impl;
902 FIXME("(%p) : pData(%p), pSizeOfData(%p)\n", This, pData, pSizeOfData);
905 *pSizeOfData = This->functionLength;
908 if (*pSizeOfData < This->functionLength) {
909 *pSizeOfData = This->functionLength;
910 return D3DERR_MOREDATA;
912 if (NULL == This->function) { /* no function defined */
913 TRACE("(%p) : GetFunction no User Function defined using NULL to %p\n", This, pData);
914 (*(DWORD **) pData) = NULL;
916 if(This->functionLength == 0){
919 TRACE("(%p) : GetFunction copying to %p\n", This, pData);
920 memcpy(pData, This->function, This->functionLength);
927 const IWineD3DVertexShaderVtbl IWineD3DVertexShader_Vtbl =
929 /*** IUnknown methods ***/
930 IWineD3DVertexShaderImpl_QueryInterface,
931 IWineD3DVertexShaderImpl_AddRef,
932 IWineD3DVertexShaderImpl_Release,
933 /*** IWineD3DVertexShader methods ***/
934 IWineD3DVertexShaderImpl_GetParent,
935 IWineD3DVertexShaderImpl_GetDevice,
936 IWineD3DVertexShaderImpl_GetFunction