Extract file directly to their target location, bypassing the need to
[wine/multimedia.git] / dlls / d3d8 / shader.c
blobeee4983c9e8e77fc7b51822a661186806f7bf75b
1 /*
2 * shaders implementation
4 * Copyright 2002-2004 Raphael Junqueira
5 * Copyright 2004 Jason Edmeades
6 * Copyright 2004 Christian Costa
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
25 #include <math.h>
26 #include <stdarg.h>
27 #include <stdio.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "wingdi.h"
33 #include "wine/debug.h"
35 #include "d3d8_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d_hw_shader);
40 /* Shader debugging - Change the following line to enable debugging of software
41 vertex shaders */
42 #if 0 /* Must not be 1 in cvs version */
43 # define VSTRACE(A) TRACE A
44 # define TRACE_VSVECTOR(name) TRACE( #name "=(%f, %f, %f, %f)\n", name.x, name.y, name.z, name.w)
45 #else
46 # define VSTRACE(A)
47 # define TRACE_VSVECTOR(name)
48 #endif
50 /**
51 * DirectX9 SDK download
52 * http://msdn.microsoft.com/library/default.asp?url=/downloads/list/directx.asp
54 * Exploring D3DX
55 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndrive/html/directx07162002.asp
57 * Using Vertex Shaders
58 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndrive/html/directx02192001.asp
60 * Dx9 New
61 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/whatsnew.asp
63 * Dx9 Shaders
64 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader2_0/VertexShader2_0.asp
65 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader2_0/Instructions/Instructions.asp
66 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/GettingStarted/VertexDeclaration/VertexDeclaration.asp
67 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader3_0/VertexShader3_0.asp
69 * Dx9 D3DX
70 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/advancedtopics/VertexPipe/matrixstack/matrixstack.asp
72 * FVF
73 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/GettingStarted/VertexFormats/vformats.asp
75 * NVIDIA: DX8 Vertex Shader to NV Vertex Program
76 * http://developer.nvidia.com/view.asp?IO=vstovp
78 * NVIDIA: Memory Management with VAR
79 * http://developer.nvidia.com/view.asp?IO=var_memory_management
82 typedef void (*shader_fct_t)();
84 typedef struct SHADER_OPCODE {
85 CONST WORD opcode;
86 const char* name;
87 CONST UINT num_params;
88 shader_fct_t soft_fct;
89 DWORD min_version;
90 DWORD max_version;
91 } SHADER_OPCODE;
93 /*******************************
94 * vshader functions software VM
97 void vshader_add(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
98 d->x = s0->x + s1->x;
99 d->y = s0->y + s1->y;
100 d->z = s0->z + s1->z;
101 d->w = s0->w + s1->w;
102 VSTRACE(("executing add: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
103 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
106 void vshader_dp3(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
107 d->x = d->y = d->z = d->w = s0->x * s1->x + s0->y * s1->y + s0->z * s1->z;
108 VSTRACE(("executing dp3: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
109 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
112 void vshader_dp4(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
113 d->x = d->y = d->z = d->w = s0->x * s1->x + s0->y * s1->y + s0->z * s1->z + s0->w * s1->w;
114 VSTRACE(("executing dp4: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
115 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
118 void vshader_dst(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
119 d->x = 1.0f;
120 d->y = s0->y * s1->y;
121 d->z = s0->z;
122 d->w = s1->w;
123 VSTRACE(("executing dst: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
124 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
127 void vshader_expp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
128 union {
129 float f;
130 DWORD d;
131 } tmp;
133 tmp.f = floorf(s0->w);
134 d->x = powf(2.0f, tmp.f);
135 d->y = s0->w - tmp.f;
137 tmp.f = powf(2.0f, s0->w);
138 tmp.d &= 0xFFFFFF00U;
139 d->z = tmp.f;
140 d->w = 1.0f;
141 VSTRACE(("executing exp: 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_lit(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
146 d->x = 1.0f;
147 d->y = (0.0f < s0->x) ? s0->x : 0.0f;
148 d->z = (0.0f < s0->x && 0.0f < s0->y) ? powf(s0->y, s0->w) : 0.0f;
149 d->w = 1.0f;
150 VSTRACE(("executing lit: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
151 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
154 void vshader_logp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
155 float tmp_f = fabsf(s0->w);
156 d->x = d->y = d->z = d->w = (0.0f != tmp_f) ? logf(tmp_f) / logf(2.0f) : -HUGE_VAL;
157 VSTRACE(("executing logp: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
158 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
161 void vshader_mad(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1, D3DSHADERVECTOR* s2) {
162 d->x = s0->x * s1->x + s2->x;
163 d->y = s0->y * s1->y + s2->y;
164 d->z = s0->z * s1->z + s2->z;
165 d->w = s0->w * s1->w + s2->w;
166 VSTRACE(("executing mad: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) s2=(%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, s2->x, s2->y, s2->z, s2->w, d->x, d->y, d->z, d->w));
170 void vshader_max(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 max: 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_min(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
180 d->x = (s0->x < s1->x) ? s0->x : s1->x;
181 d->y = (s0->y < s1->y) ? s0->y : s1->y;
182 d->z = (s0->z < s1->z) ? s0->z : s1->z;
183 d->w = (s0->w < s1->w) ? s0->w : s1->w;
184 VSTRACE(("executing min: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
185 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
188 void vshader_mov(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
189 d->x = s0->x;
190 d->y = s0->y;
191 d->z = s0->z;
192 d->w = s0->w;
193 VSTRACE(("executing mov: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
194 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
197 void vshader_mul(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
198 d->x = s0->x * s1->x;
199 d->y = s0->y * s1->y;
200 d->z = s0->z * s1->z;
201 d->w = s0->w * s1->w;
202 VSTRACE(("executing mul: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
203 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
206 void vshader_nop(void) {
207 /* NOPPPP ahhh too easy ;) */
210 void vshader_rcp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
211 d->x = d->y = d->z = d->w = (0.0f == s0->w) ? HUGE_VAL : 1.0f / s0->w;
212 VSTRACE(("executing rcp: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
213 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
216 void vshader_rsq(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
217 float tmp_f = fabsf(s0->w);
218 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);
219 VSTRACE(("executing rsq: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
220 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
223 void vshader_sge(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 sge: 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_slt(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
233 d->x = (s0->x < s1->x) ? 1.0f : 0.0f;
234 d->y = (s0->y < s1->y) ? 1.0f : 0.0f;
235 d->z = (s0->z < s1->z) ? 1.0f : 0.0f;
236 d->w = (s0->w < s1->w) ? 1.0f : 0.0f;
237 VSTRACE(("executing slt: 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));
241 void vshader_sub(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
242 d->x = s0->x - s1->x;
243 d->y = s0->y - s1->y;
244 d->z = s0->z - s1->z;
245 d->w = s0->w - s1->w;
246 VSTRACE(("executing sub: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
247 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
251 * Version 1.1 specific
254 void vshader_exp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
255 d->x = d->y = d->z = d->w = powf(2.0f, s0->w);
256 VSTRACE(("executing exp: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
257 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
260 void vshader_log(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
261 float tmp_f = fabsf(s0->w);
262 d->x = d->y = d->z = d->w = (0.0f != tmp_f) ? logf(tmp_f) / logf(2.0f) : -HUGE_VAL;
263 VSTRACE(("executing log: 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 void vshader_frc(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
268 d->x = s0->x - floorf(s0->x);
269 d->y = s0->y - floorf(s0->y);
270 d->z = 0.0f;
271 d->w = 1.0f;
272 VSTRACE(("executing frc: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
273 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
276 typedef FLOAT D3DMATRIX44[4][4];
277 typedef FLOAT D3DMATRIX43[3][4];
278 typedef FLOAT D3DMATRIX34[4][3];
279 typedef FLOAT D3DMATRIX33[3][3];
280 typedef FLOAT D3DMATRIX32[2][3];
282 void vshader_m4x4(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, /*D3DSHADERVECTOR* mat1*/ D3DMATRIX44 mat) {
284 * BuGGY CODE: here only if cast not work for copy/paste
285 D3DSHADERVECTOR* mat2 = mat1 + 1;
286 D3DSHADERVECTOR* mat3 = mat1 + 2;
287 D3DSHADERVECTOR* mat4 = mat1 + 3;
288 d->x = mat1->x * s0->x + mat2->x * s0->y + mat3->x * s0->z + mat4->x * s0->w;
289 d->y = mat1->y * s0->x + mat2->y * s0->y + mat3->y * s0->z + mat4->y * s0->w;
290 d->z = mat1->z * s0->x + mat2->z * s0->y + mat3->z * s0->z + mat4->z * s0->w;
291 d->w = mat1->w * s0->x + mat2->w * s0->y + mat3->w * s0->z + mat4->w * s0->w;
293 d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z + mat[0][3] * s0->w;
294 d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z + mat[1][3] * s0->w;
295 d->z = mat[2][0] * s0->x + mat[2][1] * s0->y + mat[2][2] * s0->z + mat[2][3] * s0->w;
296 d->w = mat[3][0] * s0->x + mat[3][1] * s0->y + mat[3][2] * s0->z + mat[3][3] * s0->w;
297 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));
298 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));
299 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));
300 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));
303 void vshader_m4x3(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DMATRIX43 mat) {
304 d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z + mat[0][3] * s0->w;
305 d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z + mat[1][3] * s0->w;
306 d->z = mat[2][0] * s0->x + mat[2][1] * s0->y + mat[2][2] * s0->z + mat[2][3] * s0->w;
307 d->w = 1.0f;
308 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));
309 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));
310 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));
311 VSTRACE(("executing m4x3(4): (%f) (%f) \n", s0->w, d->w));
314 void vshader_m3x4(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DMATRIX34 mat) {
315 d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z;
316 d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z;
317 d->z = mat[2][0] * s0->x + mat[2][1] * s0->y + mat[2][2] * s0->z;
318 d->w = mat[3][0] * s0->x + mat[3][1] * s0->y + mat[3][2] * s0->z;
319 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));
320 VSTRACE(("executing m3x4(2): mat=(%f, %f, %f) (%f) (%f) \n", mat[1][0], mat[1][1], mat[1][2], s0->y, d->y));
321 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));
322 VSTRACE(("executing m3x4(4): mat=(%f, %f, %f) (%f) (%f) \n", mat[3][0], mat[3][1], mat[3][2], s0->w, d->w));
325 void vshader_m3x3(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DMATRIX33 mat) {
326 d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z;
327 d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z;
328 d->z = mat[2][0] * s0->x + mat[2][1] * s0->y + mat[2][2] * s0->z;
329 d->w = 1.0f;
330 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));
331 VSTRACE(("executing m3x3(2): mat=(%f, %f, %f) (%f) (%f) \n", mat[1][0], mat[1][1], mat[1][2], s0->y, d->y));
332 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));
333 VSTRACE(("executing m3x3(4): (%f) \n", d->w));
336 void vshader_m3x2(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DMATRIX32 mat) {
337 FIXME("check\n");
338 d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z;
339 d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z;
340 d->z = 0.0f;
341 d->w = 1.0f;
345 * Version 2.0 specific
347 void vshader_lrp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1, D3DSHADERVECTOR* s2, D3DSHADERVECTOR* s3) {
348 d->x = s0->x * (s1->x - s2->x) + s2->x;
349 d->y = s0->y * (s1->y - s2->y) + s2->y;
350 d->z = s0->z * (s1->z - s2->z) + s2->z;
351 d->w = s0->w * (s1->w - s2->w) + s2->x;
355 * log, exp, frc, m*x* seems to be macros ins ... to see
356 * Note opcode must be in uppercase if direct mapping to GL hw shaders
358 static CONST SHADER_OPCODE vshader_ins [] = {
359 {D3DSIO_NOP, "NOP", 0, vshader_nop, 0, 0},
360 {D3DSIO_MOV, "MOV", 2, vshader_mov, 0, 0},
361 {D3DSIO_ADD, "ADD", 3, vshader_add, 0, 0},
362 {D3DSIO_SUB, "SUB", 3, vshader_sub, 0, 0},
363 {D3DSIO_MAD, "MAD", 4, vshader_mad, 0, 0},
364 {D3DSIO_MUL, "MUL", 3, vshader_mul, 0, 0},
365 {D3DSIO_RCP, "RCP", 2, vshader_rcp, 0, 0},
366 {D3DSIO_RSQ, "RSQ", 2, vshader_rsq, 0, 0},
367 {D3DSIO_DP3, "DP3", 3, vshader_dp3, 0, 0},
368 {D3DSIO_DP4, "DP4", 3, vshader_dp4, 0, 0},
369 {D3DSIO_MIN, "MIN", 3, vshader_min, 0, 0},
370 {D3DSIO_MAX, "MAX", 3, vshader_max, 0, 0},
371 {D3DSIO_SLT, "SLT", 3, vshader_slt, 0, 0},
372 {D3DSIO_SGE, "SGE", 3, vshader_sge, 0, 0},
373 {D3DSIO_EXP, "EXP", 2, vshader_exp, 0, 0},
374 {D3DSIO_LOG, "LOG", 2, vshader_log, 0, 0},
375 {D3DSIO_LIT, "LIT", 2, vshader_lit, 0, 0},
376 {D3DSIO_DST, "DST", 3, vshader_dst, 0, 0},
377 {D3DSIO_LRP, "LRP", 5, vshader_lrp, 0, 0},
378 {D3DSIO_FRC, "FRC", 2, vshader_frc, 0, 0},
379 {D3DSIO_M4x4, "M4X4", 3, vshader_m4x4, 0, 0},
380 {D3DSIO_M4x3, "M4X3", 3, vshader_m4x3, 0, 0},
381 {D3DSIO_M3x4, "M3X4", 3, vshader_m3x4, 0, 0},
382 {D3DSIO_M3x3, "M3X3", 3, vshader_m3x3, 0, 0},
383 {D3DSIO_M3x2, "M3X2", 3, vshader_m3x2, 0, 0},
384 /** FIXME: use direct access so add the others opcodes as stubs */
385 {D3DSIO_EXPP, "EXPP", 2, vshader_expp, 0, 0},
386 {D3DSIO_LOGP, "LOGP", 2, vshader_logp, 0, 0},
388 {0, NULL, 0, NULL, 0, 0}
392 inline static const SHADER_OPCODE* vshader_program_get_opcode(const DWORD code) {
393 DWORD i = 0;
394 /** TODO: use dichotomic search */
395 while (NULL != vshader_ins[i].name) {
396 if ((code & D3DSI_OPCODE_MASK) == vshader_ins[i].opcode) {
397 return &vshader_ins[i];
399 ++i;
401 return NULL;
404 inline static BOOL vshader_is_version_token(DWORD token) {
405 return 0xFFFE0000 == (token & 0xFFFE0000);
408 inline static BOOL vshader_is_comment_token(DWORD token) {
409 return D3DSIO_COMMENT == (token & D3DSI_OPCODE_MASK);
412 inline static void vshader_program_dump_param(const DWORD param, int input) {
413 static const char* rastout_reg_names[] = { "oPos", "oFog", "oPts" };
414 static const char swizzle_reg_chars[] = "xyzw";
416 DWORD reg = param & 0x00001FFF;
417 DWORD regtype = ((param & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT);
419 if ((param & D3DSP_SRCMOD_MASK) == D3DSPSM_NEG) TRACE("-");
421 switch (regtype << D3DSP_REGTYPE_SHIFT) {
422 case D3DSPR_TEMP:
423 TRACE("R[%lu]", reg);
424 break;
425 case D3DSPR_INPUT:
426 TRACE("V[%lu]", reg);
427 break;
428 case D3DSPR_CONST:
429 TRACE("C[%s%lu]", (param & D3DVS_ADDRMODE_RELATIVE) ? "a0.x + " : "", reg);
430 break;
431 case D3DSPR_ADDR: /*case D3DSPR_TEXTURE:*/
432 TRACE("a[%lu]", reg);
433 break;
434 case D3DSPR_RASTOUT:
435 TRACE("%s", rastout_reg_names[reg]);
436 break;
437 case D3DSPR_ATTROUT:
438 TRACE("oD[%lu]", reg);
439 break;
440 case D3DSPR_TEXCRDOUT:
441 TRACE("oT[%lu]", reg);
442 break;
443 default:
444 break;
447 if (!input) {
448 /** operand output */
449 if ((param & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
450 if (param & D3DSP_WRITEMASK_0) TRACE(".x");
451 if (param & D3DSP_WRITEMASK_1) TRACE(".y");
452 if (param & D3DSP_WRITEMASK_2) TRACE(".z");
453 if (param & D3DSP_WRITEMASK_3) TRACE(".w");
455 } else {
456 /** operand input */
457 DWORD swizzle = (param & D3DVS_SWIZZLE_MASK) >> D3DVS_SWIZZLE_SHIFT;
458 DWORD swizzle_x = swizzle & 0x03;
459 DWORD swizzle_y = (swizzle >> 2) & 0x03;
460 DWORD swizzle_z = (swizzle >> 4) & 0x03;
461 DWORD swizzle_w = (swizzle >> 6) & 0x03;
463 * swizzle bits fields:
464 * WWZZYYXX
466 if ((D3DVS_NOSWIZZLE >> D3DVS_SWIZZLE_SHIFT) != swizzle) { /* ! D3DVS_NOSWIZZLE == 0xE4 << D3DVS_SWIZZLE_SHIFT */
467 if (swizzle_x == swizzle_y &&
468 swizzle_x == swizzle_z &&
469 swizzle_x == swizzle_w) {
470 TRACE(".%c", swizzle_reg_chars[swizzle_x]);
471 } else {
472 TRACE(".%c%c%c%c",
473 swizzle_reg_chars[swizzle_x],
474 swizzle_reg_chars[swizzle_y],
475 swizzle_reg_chars[swizzle_z],
476 swizzle_reg_chars[swizzle_w]);
482 inline static void vshader_program_add_param(const DWORD param, int input, char *hwLine) {
483 /*static const char* rastout_reg_names[] = { "oPos", "oFog", "oPts" }; */
484 static const char* hwrastout_reg_names[] = { "result.position", "result.fogcoord", "result.pointsize" };
485 static const char swizzle_reg_chars[] = "xyzw";
487 DWORD reg = param & 0x00001FFF;
488 DWORD regtype = ((param & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT);
489 char tmpReg[255];
491 if ((param & D3DSP_SRCMOD_MASK) == D3DSPSM_NEG) {
492 strcat(hwLine, " -");
493 } else {
494 strcat(hwLine, " ");
497 switch (regtype << D3DSP_REGTYPE_SHIFT) {
498 case D3DSPR_TEMP:
499 sprintf(tmpReg, "T%lu", reg);
500 strcat(hwLine, tmpReg);
501 break;
502 case D3DSPR_INPUT:
503 sprintf(tmpReg, "vertex.attrib[%lu]", reg);
504 strcat(hwLine, tmpReg);
505 break;
506 case D3DSPR_CONST:
507 sprintf(tmpReg, "C[%s%lu]", (param & D3DVS_ADDRMODE_RELATIVE) ? "A0.x + " : "", reg);
508 strcat(hwLine, tmpReg);
509 break;
510 case D3DSPR_ADDR: /*case D3DSPR_TEXTURE:*/
511 sprintf(tmpReg, "A%lu", reg);
512 strcat(hwLine, tmpReg);
513 break;
514 case D3DSPR_RASTOUT:
515 sprintf(tmpReg, "%s", hwrastout_reg_names[reg]);
516 strcat(hwLine, tmpReg);
517 break;
518 case D3DSPR_ATTROUT:
519 if (reg==0) {
520 strcat(hwLine, "result.color.primary");
521 } else {
522 strcat(hwLine, "result.color.secondary");
524 break;
525 case D3DSPR_TEXCRDOUT:
526 sprintf(tmpReg, "result.texcoord[%lu]", reg);
527 strcat(hwLine, tmpReg);
528 break;
529 default:
530 break;
533 if (!input) {
534 /** operand output */
535 if ((param & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
536 strcat(hwLine, ".");
537 if (param & D3DSP_WRITEMASK_0) {
538 strcat(hwLine, "x");
540 if (param & D3DSP_WRITEMASK_1) {
541 strcat(hwLine, "y");
543 if (param & D3DSP_WRITEMASK_2) {
544 strcat(hwLine, "z");
546 if (param & D3DSP_WRITEMASK_3) {
547 strcat(hwLine, "w");
550 } else {
551 /** operand input */
552 DWORD swizzle = (param & D3DVS_SWIZZLE_MASK) >> D3DVS_SWIZZLE_SHIFT;
553 DWORD swizzle_x = swizzle & 0x03;
554 DWORD swizzle_y = (swizzle >> 2) & 0x03;
555 DWORD swizzle_z = (swizzle >> 4) & 0x03;
556 DWORD swizzle_w = (swizzle >> 6) & 0x03;
558 * swizzle bits fields:
559 * WWZZYYXX
561 if ((D3DVS_NOSWIZZLE >> D3DVS_SWIZZLE_SHIFT) != swizzle) { /* ! D3DVS_NOSWIZZLE == 0xE4 << D3DVS_SWIZZLE_SHIFT */
562 if (swizzle_x == swizzle_y &&
563 swizzle_x == swizzle_z &&
564 swizzle_x == swizzle_w) {
565 sprintf(tmpReg, ".%c", swizzle_reg_chars[swizzle_x]);
566 strcat(hwLine, tmpReg);
567 } else {
568 sprintf(tmpReg, ".%c%c%c%c",
569 swizzle_reg_chars[swizzle_x],
570 swizzle_reg_chars[swizzle_y],
571 swizzle_reg_chars[swizzle_z],
572 swizzle_reg_chars[swizzle_w]);
573 strcat(hwLine, tmpReg);
579 DWORD MacroExpansion[4*4];
581 int ExpandMxMacro(DWORD macro_opcode, const DWORD* args) {
582 int i;
583 int nComponents = 0;
584 DWORD opcode =0;
585 switch(macro_opcode) {
586 case D3DSIO_M4x4:
587 nComponents = 4;
588 opcode = D3DSIO_DP4;
589 break;
590 case D3DSIO_M4x3:
591 nComponents = 3;
592 opcode = D3DSIO_DP4;
593 break;
594 case D3DSIO_M3x4:
595 nComponents = 4;
596 opcode = D3DSIO_DP3;
597 break;
598 case D3DSIO_M3x3:
599 nComponents = 3;
600 opcode = D3DSIO_DP3;
601 break;
602 case D3DSIO_M3x2:
603 nComponents = 2;
604 opcode = D3DSIO_DP3;
605 break;
606 default:
607 break;
609 for (i = 0; i < nComponents; i++) {
610 MacroExpansion[i*4+0] = opcode;
611 MacroExpansion[i*4+1] = ((*args) & ~D3DSP_WRITEMASK_ALL)|(D3DSP_WRITEMASK_0<<i);
612 MacroExpansion[i*4+2] = *(args+1);
613 MacroExpansion[i*4+3] = (*(args+2))+i;
615 return nComponents;
619 * Function parser ...
621 inline static VOID IDirect3DVertexShaderImpl_GenerateProgramArbHW(IDirect3DVertexShaderImpl* vshader, CONST DWORD* pFunction) {
622 const DWORD* pToken = pFunction;
623 const DWORD* pSavedToken = NULL;
624 const SHADER_OPCODE* curOpcode = NULL;
625 int nRemInstr = -1;
626 DWORD i;
627 unsigned lineNum = 0;
628 char *pgmStr = NULL;
629 char tmpLine[255];
630 DWORD nUseAddressRegister = 0;
631 DWORD nUseTempRegister = 0;
632 DWORD regtype;
633 DWORD reg;
634 IDirect3DDevice8Impl* This = vshader->device;
636 pgmStr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 65535); /* 64kb should be enough */
639 * First pass to determine what we need to declare:
640 * - Temporary variables
641 * - Address variables
643 if (NULL != pToken) {
644 while (D3DVS_END() != *pToken) {
645 if (vshader_is_version_token(*pToken)) {
646 /** skip version */
647 ++pToken;
648 continue;
650 if (vshader_is_comment_token(*pToken)) { /** comment */
651 DWORD comment_len = (*pToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
652 ++pToken;
653 pToken += comment_len;
654 continue;
656 curOpcode = vshader_program_get_opcode(*pToken);
657 ++pToken;
658 if (NULL == curOpcode) {
659 while (*pToken & 0x80000000) {
660 /* skip unrecognized opcode */
661 ++pToken;
663 } else {
664 if (curOpcode->num_params > 0) {
665 regtype = ((((*pToken) & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT) << D3DSP_REGTYPE_SHIFT);
666 reg = ((*pToken) & 0x00001FFF);
667 /** we should validate GL_MAX_PROGRAM_ADDRESS_REGISTERS_AR limits here */
668 if (D3DSPR_ADDR == regtype && nUseAddressRegister <= reg) nUseAddressRegister = reg + 1;
669 /** we should validate GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB limits here */
670 if (D3DSPR_TEMP == regtype && nUseTempRegister <= reg) nUseTempRegister = reg + 1;
671 ++pToken;
672 for (i = 1; i < curOpcode->num_params; ++i) {
673 regtype = ((((*pToken) & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT) << D3DSP_REGTYPE_SHIFT);
674 reg = ((*pToken) & 0x00001FFF);
675 /** we should validate GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB limits here */
676 if (D3DSPR_TEMP == regtype && nUseTempRegister <= reg) nUseTempRegister = reg + 1;
677 ++pToken;
684 /** second pass, now generate */
685 pToken = pFunction;
687 if (NULL != pToken) {
688 while (1) {
689 tmpLine[0] = 0;
691 if ((nRemInstr >= 0) && (--nRemInstr == -1))
692 /* Macro is finished, continue normal path */
693 pToken = pSavedToken;
695 if (D3DVS_END() == *pToken)
696 break;
698 if (vshader_is_version_token(*pToken)) { /** version */
700 /* Extract version *10 into integer value (ie. 1.0 == 10, 1.1==11 etc */
701 int version = (((*pToken >> 8) & 0x0F) * 10) + (*pToken & 0x0F);
702 int numTemps;
703 int numConstants;
705 TRACE_(d3d_hw_shader)("vs.%lu.%lu;\n", (*pToken >> 8) & 0x0F, (*pToken & 0x0F));
707 /* Each release of vertex shaders has had different numbers of temp registers */
708 switch (version) {
709 case 10:
710 case 11: numTemps=12;
711 numConstants=96;
712 strcpy(tmpLine, "!!ARBvp1.0\n");
713 TRACE_(d3d_hw_shader)("GL HW (%u) : %s", strlen(pgmStr), tmpLine); /* Don't add \n to this line as already in tmpLine */
714 break;
715 case 20: numTemps=12;
716 numConstants=256;
717 strcpy(tmpLine, "!!ARBvp2.0\n");
718 FIXME_(d3d_hw_shader)("No work done yet to support vs2.0 in hw\n");
719 TRACE_(d3d_hw_shader)("GL HW (%u) : %s", strlen(pgmStr), tmpLine); /* Don't add \n to this line as already in tmpLine */
720 break;
721 case 30: numTemps=32;
722 numConstants=256;
723 strcpy(tmpLine, "!!ARBvp3.0\n");
724 FIXME_(d3d_hw_shader)("No work done yet to support vs3.0 in hw\n");
725 TRACE_(d3d_hw_shader)("GL HW (%u) : %s", strlen(pgmStr), tmpLine); /* Don't add \n to this line as already in tmpLine */
726 break;
727 default:
728 numTemps=12;
729 numConstants=96;
730 strcpy(tmpLine, "!!ARBvp1.0\n");
731 FIXME_(d3d_hw_shader)("Unrecognized vertex shader version!\n");
733 strcat(pgmStr,tmpLine);
734 ++lineNum;
736 for (i = 0; i < nUseTempRegister/*we should check numTemps here*/; i++) {
737 sprintf(tmpLine, "TEMP T%ld;\n", i);
738 ++lineNum;
739 TRACE_(d3d_hw_shader)("GL HW (%u, %u) : %s", lineNum, strlen(pgmStr), tmpLine); /* Don't add \n to this line as already in tmpLine */
740 strcat(pgmStr,tmpLine);
742 for (i = 0; i < nUseAddressRegister; i++) {
743 sprintf(tmpLine, "ADDRESS A%ld;\n", i);
744 ++lineNum;
745 TRACE_(d3d_hw_shader)("GL HW (%u, %u) : %s", lineNum, strlen(pgmStr), tmpLine); /* Don't add \n to this line as already in tmpLine */
746 strcat(pgmStr,tmpLine);
748 /* Due to the dynamic constants binding mechanism, we need to declare
749 * all the constants for relative addressing. */
750 /* Mesa supports nly 95 constants for VS1.X although we should have at least 96. */
751 if (This->direct3d8->gl_info.gl_vendor == VENDOR_MESA ||
752 This->direct3d8->gl_info.gl_vendor == VENDOR_WINE) {
753 numConstants = 95;
755 sprintf(tmpLine, "PARAM C[%d] = { program.env[0..%d] };\n", numConstants, numConstants-1);
756 TRACE_(d3d_hw_shader)("GL HW (%u,%u) : %s", lineNum, strlen(pgmStr), tmpLine); /* Don't add \n to this line as already in tmpLine */
757 strcat(pgmStr, tmpLine);
758 ++lineNum;
760 ++pToken;
761 continue;
763 if (vshader_is_comment_token(*pToken)) { /** comment */
764 DWORD comment_len = (*pToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
765 ++pToken;
766 /*TRACE("comment[%ld] ;%s\n", comment_len, (char*)pToken);*/
767 pToken += comment_len;
768 continue;
770 curOpcode = vshader_program_get_opcode(*pToken);
771 ++pToken;
772 if (NULL == curOpcode) {
773 /* unkown current opcode ... */
774 while (*pToken & 0x80000000) {
775 TRACE_(d3d_hw_shader)("unrecognized opcode: %08lx\n", *pToken);
776 ++pToken;
778 } else {
779 /* Build opcode for GL vertex_program */
780 switch (curOpcode->opcode) {
781 case D3DSIO_NOP:
782 continue;
783 case D3DSIO_MOV:
784 /* Address registers must be loaded with the ARL instruction */
785 if (((*pToken) & D3DSP_REGTYPE_MASK) == D3DSPR_ADDR) {
786 if (0 < nUseAddressRegister) {
787 strcpy(tmpLine, "ARL");
788 break;
789 } else
790 FIXME_(d3d_hw_shader)("Try to load an undeclared address register!\n");
792 /* fall through */
793 case D3DSIO_ADD:
794 case D3DSIO_SUB:
795 case D3DSIO_MAD:
796 case D3DSIO_MUL:
797 case D3DSIO_RCP:
798 case D3DSIO_RSQ:
799 case D3DSIO_DP3:
800 case D3DSIO_DP4:
801 case D3DSIO_MIN:
802 case D3DSIO_MAX:
803 case D3DSIO_SLT:
804 case D3DSIO_SGE:
805 case D3DSIO_LIT:
806 case D3DSIO_DST:
807 case D3DSIO_FRC:
808 strcpy(tmpLine, curOpcode->name);
809 break;
811 case D3DSIO_EXPP:
812 strcpy(tmpLine, "EXP");
813 break;
814 case D3DSIO_LOGP:
815 strcpy(tmpLine, "LOG");
816 break;
817 case D3DSIO_EXP:
818 strcpy(tmpLine, "EX2");
819 break;
820 case D3DSIO_LOG:
821 strcpy(tmpLine, "LG2");
822 break;
824 case D3DSIO_M4x4:
825 case D3DSIO_M4x3:
826 case D3DSIO_M3x4:
827 case D3DSIO_M3x3:
828 case D3DSIO_M3x2:
829 /* Expand the macro and get number of generated instruction */
830 nRemInstr = ExpandMxMacro(curOpcode->opcode, pToken);
831 /* Save point to next instruction */
832 pSavedToken = pToken + 3;
833 /* Execute expanded macro */
834 pToken = MacroExpansion;
835 continue;
837 default:
838 FIXME_(d3d_hw_shader)("Can't handle opcode %s in hwShader\n", curOpcode->name);
841 if (curOpcode->num_params > 0) {
842 vshader_program_add_param(*pToken, 0, tmpLine);
844 ++pToken;
845 for (i = 1; i < curOpcode->num_params; ++i) {
846 strcat(tmpLine, ",");
847 vshader_program_add_param(*pToken, 1, tmpLine);
848 ++pToken;
851 strcat(tmpLine,";\n");
852 ++lineNum;
853 TRACE_(d3d_hw_shader)("GL HW (%u, %u) : %s", lineNum, strlen(pgmStr), tmpLine); /* Don't add \n to this line as already in tmpLine */
854 strcat(pgmStr, tmpLine);
857 strcpy(tmpLine, "END\n");
858 ++lineNum;
859 TRACE_(d3d_hw_shader)("GL HW (%u, %u) : %s", lineNum, strlen(pgmStr), tmpLine); /* Don't add \n to this line as already in tmpLine */
860 strcat(pgmStr, tmpLine);
863 /* Create the hw shader */
864 GL_EXTCALL(glGenProgramsARB(1, &vshader->prgId));
865 TRACE_(d3d_hw_shader)("Creating a hw vertex shader, prg=%d\n", vshader->prgId);
867 GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vshader->prgId));
869 /* Create the program and check for errors */
870 GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(pgmStr), pgmStr));
871 if (glGetError() == GL_INVALID_OPERATION) {
872 GLint errPos;
873 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
874 FIXME_(d3d_hw_shader)("HW VertexShader Error at position: %d\n%s\n", errPos, glGetString(GL_PROGRAM_ERROR_STRING_ARB));
875 vshader->prgId = -1;
878 HeapFree(GetProcessHeap(), 0, pgmStr);
881 inline static VOID IDirect3DVertexShaderImpl_ParseProgram(IDirect3DVertexShaderImpl* vshader, CONST DWORD* pFunction, int useHW) {
882 const DWORD* pToken = pFunction;
883 const SHADER_OPCODE* curOpcode = NULL;
884 DWORD len = 0;
885 DWORD i;
887 if (NULL != pToken) {
888 while (D3DVS_END() != *pToken) {
889 if (vshader_is_version_token(*pToken)) { /** version */
890 TRACE("vs.%lu.%lu\n", (*pToken >> 8) & 0x0F, (*pToken & 0x0F));
891 ++pToken;
892 ++len;
893 continue;
895 if (vshader_is_comment_token(*pToken)) { /** comment */
896 DWORD comment_len = (*pToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
897 ++pToken;
898 /*TRACE("comment[%ld] ;%s\n", comment_len, (char*)pToken);*/
899 pToken += comment_len;
900 len += comment_len + 1;
901 continue;
903 curOpcode = vshader_program_get_opcode(*pToken);
904 ++pToken;
905 ++len;
906 if (NULL == curOpcode) {
907 /* unkown current opcode ... */
908 while (*pToken & 0x80000000) {
909 TRACE("unrecognized opcode: %08lx\n", *pToken);
910 ++pToken;
911 ++len;
913 } else {
914 TRACE("%s ", curOpcode->name);
915 if (curOpcode->num_params > 0) {
916 vshader_program_dump_param(*pToken, 0);
917 ++pToken;
918 ++len;
919 for (i = 1; i < curOpcode->num_params; ++i) {
920 TRACE(", ");
921 vshader_program_dump_param(*pToken, 1);
922 ++pToken;
923 ++len;
926 TRACE("\n");
929 vshader->functionLength = (len + 1) * sizeof(DWORD);
930 } else {
931 vshader->functionLength = 1; /* no Function defined use fixed function vertex processing */
934 /* Generate HW shader in needed */
935 if (useHW && NULL != pFunction) {
936 IDirect3DVertexShaderImpl_GenerateProgramArbHW(vshader, pFunction);
939 /* copy the function ... because it will certainly be released by application */
940 if (NULL != pFunction) {
941 vshader->function = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, vshader->functionLength);
942 memcpy(vshader->function, pFunction, vshader->functionLength);
943 } else {
944 vshader->function = NULL;
948 HRESULT WINAPI IDirect3DDeviceImpl_CreateVertexShader(IDirect3DDevice8Impl* This, CONST DWORD* pFunction, DWORD Usage, IDirect3DVertexShaderImpl** ppVertexShader) {
949 IDirect3DVertexShaderImpl* object;
950 int useHW;
952 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DVertexShaderImpl));
953 if (NULL == object) {
954 *ppVertexShader = NULL;
955 return D3DERR_OUTOFVIDEOMEMORY;
957 /*object->lpVtbl = &Direct3DVextexShader9_Vtbl;*/
958 object->device = This; /* FIXME: AddRef(This) */
959 object->ref = 1;
961 object->usage = Usage;
962 object->data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(VSHADERDATA8));
964 useHW = (((vs_mode == VS_HW) && GL_SUPPORT(ARB_VERTEX_PROGRAM)) &&
965 This->devType != D3DDEVTYPE_REF &&
966 object->usage != D3DUSAGE_SOFTWAREPROCESSING);
968 IDirect3DVertexShaderImpl_ParseProgram(object, pFunction, useHW);
970 *ppVertexShader = object;
971 return D3D_OK;
974 BOOL IDirect3DVertexShaderImpl_ExecuteHAL(IDirect3DVertexShaderImpl* vshader, VSHADERINPUTDATA8* input, VSHADEROUTPUTDATA8* output) {
975 /**
976 * TODO: use the NV_vertex_program (or 1_1) extension
977 * and specifics vendors (ARB_vertex_program??) variants for it
979 return TRUE;
982 HRESULT WINAPI IDirect3DVertexShaderImpl_ExecuteSW(IDirect3DVertexShaderImpl* vshader, VSHADERINPUTDATA8* input, VSHADEROUTPUTDATA8* output) {
983 /** Vertex Shader Temporary Registers */
984 D3DSHADERVECTOR R[12];
985 /*D3DSHADERSCALAR A0;*/
986 D3DSHADERVECTOR A[1];
987 /** temporary Vector for modifier management */
988 D3DSHADERVECTOR d;
989 D3DSHADERVECTOR s[3];
990 /** parser datas */
991 const DWORD* pToken = vshader->function;
992 const SHADER_OPCODE* curOpcode = NULL;
993 /** functions parameters */
994 D3DSHADERVECTOR* p[5];
995 D3DSHADERVECTOR* p_send[5];
996 DWORD i;
998 /** init temporary register */
999 memset(R, 0, 12 * sizeof(D3DSHADERVECTOR));
1001 /* vshader_program_parse(vshader); */
1002 #if 0 /* Must not be 1 in cvs */
1003 TRACE("Input:\n");
1004 TRACE_VSVECTOR(vshader->data->C[0]);
1005 TRACE_VSVECTOR(vshader->data->C[1]);
1006 TRACE_VSVECTOR(vshader->data->C[2]);
1007 TRACE_VSVECTOR(vshader->data->C[3]);
1008 TRACE_VSVECTOR(vshader->data->C[4]);
1009 TRACE_VSVECTOR(vshader->data->C[5]);
1010 TRACE_VSVECTOR(vshader->data->C[6]);
1011 TRACE_VSVECTOR(vshader->data->C[7]);
1012 TRACE_VSVECTOR(vshader->data->C[8]);
1013 TRACE_VSVECTOR(vshader->data->C[64]);
1014 TRACE_VSVECTOR(input->V[D3DVSDE_POSITION]);
1015 TRACE_VSVECTOR(input->V[D3DVSDE_BLENDWEIGHT]);
1016 TRACE_VSVECTOR(input->V[D3DVSDE_BLENDINDICES]);
1017 TRACE_VSVECTOR(input->V[D3DVSDE_NORMAL]);
1018 TRACE_VSVECTOR(input->V[D3DVSDE_PSIZE]);
1019 TRACE_VSVECTOR(input->V[D3DVSDE_DIFFUSE]);
1020 TRACE_VSVECTOR(input->V[D3DVSDE_SPECULAR]);
1021 TRACE_VSVECTOR(input->V[D3DVSDE_TEXCOORD0]);
1022 TRACE_VSVECTOR(input->V[D3DVSDE_TEXCOORD1]);
1023 #endif
1025 TRACE_VSVECTOR(vshader->data->C[64]);
1027 /* the first dword is the version tag */
1028 /* TODO: parse it */
1030 if (vshader_is_version_token(*pToken)) { /** version */
1031 ++pToken;
1033 while (D3DVS_END() != *pToken) {
1034 if (vshader_is_comment_token(*pToken)) { /** comment */
1035 DWORD comment_len = (*pToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
1036 ++pToken;
1037 pToken += comment_len;
1038 continue ;
1040 curOpcode = vshader_program_get_opcode(*pToken);
1041 ++pToken;
1042 if (NULL == curOpcode) {
1043 i = 0;
1044 /* unkown current opcode ... */
1045 while (*pToken & 0x80000000) {
1046 if (i == 0) {
1047 TRACE("unrecognized opcode: pos=%d token=%08lX\n", (pToken - 1) - vshader->function, *(pToken - 1));
1049 TRACE("unrecognized opcode param: pos=%d token=%08lX what=", pToken - vshader->function, *pToken);
1050 vshader_program_add_param(*pToken, i, NULL); /* Add function just used for trace error scenario */
1051 TRACE("\n");
1052 ++i;
1053 ++pToken;
1055 /*return FALSE;*/
1056 } else {
1057 if (curOpcode->num_params > 0) {
1058 /*TRACE(">> execting opcode: pos=%d opcode_name=%s token=%08lX\n", pToken - vshader->function, curOpcode->name, *pToken);*/
1059 for (i = 0; i < curOpcode->num_params; ++i) {
1060 DWORD reg = pToken[i] & 0x00001FFF;
1061 DWORD regtype = ((pToken[i] & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT);
1063 switch (regtype << D3DSP_REGTYPE_SHIFT) {
1064 case D3DSPR_TEMP:
1065 /*TRACE("p[%d]=R[%d]\n", i, reg);*/
1066 p[i] = &R[reg];
1067 break;
1068 case D3DSPR_INPUT:
1069 /*TRACE("p[%d]=V[%s]\n", i, VertexShaderDeclRegister[reg]);*/
1070 p[i] = &input->V[reg];
1071 break;
1072 case D3DSPR_CONST:
1073 if (pToken[i] & D3DVS_ADDRMODE_RELATIVE) {
1074 p[i] = &vshader->data->C[(DWORD) A[0].x + reg];
1075 } else {
1076 p[i] = &vshader->data->C[reg];
1078 break;
1079 case D3DSPR_ADDR: /*case D3DSPR_TEXTURE:*/
1080 if (0 != reg) {
1081 ERR("cannot handle address registers != a0, forcing use of a0\n");
1082 reg = 0;
1084 /*TRACE("p[%d]=A[%d]\n", i, reg);*/
1085 p[i] = &A[reg];
1086 break;
1087 case D3DSPR_RASTOUT:
1088 switch (reg) {
1089 case D3DSRO_POSITION:
1090 p[i] = &output->oPos;
1091 break;
1092 case D3DSRO_FOG:
1093 p[i] = &output->oFog;
1094 break;
1095 case D3DSRO_POINT_SIZE:
1096 p[i] = &output->oPts;
1097 break;
1099 break;
1100 case D3DSPR_ATTROUT:
1101 /*TRACE("p[%d]=oD[%d]\n", i, reg);*/
1102 p[i] = &output->oD[reg];
1103 break;
1104 case D3DSPR_TEXCRDOUT:
1105 /*TRACE("p[%d]=oT[%d]\n", i, reg);*/
1106 p[i] = &output->oT[reg];
1107 break;
1108 default:
1109 break;
1112 if (i > 0) { /* input reg */
1113 DWORD swizzle = (pToken[i] & D3DVS_SWIZZLE_MASK) >> D3DVS_SWIZZLE_SHIFT;
1114 UINT isNegative = ((pToken[i] & D3DSP_SRCMOD_MASK) == D3DSPSM_NEG);
1116 if (!isNegative && (D3DVS_NOSWIZZLE >> D3DVS_SWIZZLE_SHIFT) == swizzle) {
1117 /*TRACE("p[%d] not swizzled\n", i);*/
1118 p_send[i] = p[i];
1119 } else {
1120 DWORD swizzle_x = swizzle & 0x03;
1121 DWORD swizzle_y = (swizzle >> 2) & 0x03;
1122 DWORD swizzle_z = (swizzle >> 4) & 0x03;
1123 DWORD swizzle_w = (swizzle >> 6) & 0x03;
1124 /*TRACE("p[%d] swizzled\n", i);*/
1125 float* tt = (float*) p[i];
1126 s[i].x = (isNegative) ? -tt[swizzle_x] : tt[swizzle_x];
1127 s[i].y = (isNegative) ? -tt[swizzle_y] : tt[swizzle_y];
1128 s[i].z = (isNegative) ? -tt[swizzle_z] : tt[swizzle_z];
1129 s[i].w = (isNegative) ? -tt[swizzle_w] : tt[swizzle_w];
1130 p_send[i] = &s[i];
1132 } else { /* output reg */
1133 if ((pToken[i] & D3DSP_WRITEMASK_ALL) == D3DSP_WRITEMASK_ALL) {
1134 p_send[i] = p[i];
1135 } else {
1136 p_send[i] = &d; /* to be post-processed for modifiers management */
1142 switch (curOpcode->num_params) {
1143 case 0:
1144 curOpcode->soft_fct();
1145 break;
1146 case 1:
1147 curOpcode->soft_fct(p_send[0]);
1148 break;
1149 case 2:
1150 curOpcode->soft_fct(p_send[0], p_send[1]);
1151 break;
1152 case 3:
1153 curOpcode->soft_fct(p_send[0], p_send[1], p_send[2]);
1154 break;
1155 case 4:
1156 curOpcode->soft_fct(p_send[0], p_send[1], p_send[2], p_send[3]);
1157 break;
1158 case 5:
1159 curOpcode->soft_fct(p_send[0], p_send[1], p_send[2], p_send[3], p_send[4]);
1160 break;
1161 default:
1162 ERR("%s too many params: %u\n", curOpcode->name, curOpcode->num_params);
1165 /* check if output reg modifier post-process */
1166 if (curOpcode->num_params > 0 && (pToken[0] & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
1167 if (pToken[0] & D3DSP_WRITEMASK_0) p[0]->x = d.x;
1168 if (pToken[0] & D3DSP_WRITEMASK_1) p[0]->y = d.y;
1169 if (pToken[0] & D3DSP_WRITEMASK_2) p[0]->z = d.z;
1170 if (pToken[0] & D3DSP_WRITEMASK_3) p[0]->w = d.w;
1173 #if 0
1174 TRACE_VSVECTOR(output->oPos);
1175 TRACE_VSVECTOR(output->oD[0]);
1176 TRACE_VSVECTOR(output->oD[1]);
1177 TRACE_VSVECTOR(output->oT[0]);
1178 TRACE_VSVECTOR(output->oT[1]);
1179 TRACE_VSVECTOR(R[0]);
1180 TRACE_VSVECTOR(R[1]);
1181 TRACE_VSVECTOR(R[2]);
1182 TRACE_VSVECTOR(R[3]);
1183 TRACE_VSVECTOR(R[4]);
1184 TRACE_VSVECTOR(R[5]);
1185 #endif
1187 /* to next opcode token */
1188 pToken += curOpcode->num_params;
1190 #if 0
1191 TRACE("End of current instruction:\n");
1192 TRACE_VSVECTOR(output->oPos);
1193 TRACE_VSVECTOR(output->oD[0]);
1194 TRACE_VSVECTOR(output->oD[1]);
1195 TRACE_VSVECTOR(output->oT[0]);
1196 TRACE_VSVECTOR(output->oT[1]);
1197 TRACE_VSVECTOR(R[0]);
1198 TRACE_VSVECTOR(R[1]);
1199 TRACE_VSVECTOR(R[2]);
1200 TRACE_VSVECTOR(R[3]);
1201 TRACE_VSVECTOR(R[4]);
1202 TRACE_VSVECTOR(R[5]);
1203 #endif
1205 #if 0 /* Must not be 1 in cvs */
1206 TRACE("Output:\n");
1207 TRACE_VSVECTOR(output->oPos);
1208 TRACE_VSVECTOR(output->oD[0]);
1209 TRACE_VSVECTOR(output->oD[1]);
1210 TRACE_VSVECTOR(output->oT[0]);
1211 TRACE_VSVECTOR(output->oT[1]);
1212 #endif
1213 return D3D_OK;
1216 HRESULT WINAPI IDirect3DVertexShaderImpl_GetFunction(IDirect3DVertexShaderImpl* This, VOID* pData, UINT* pSizeOfData) {
1217 if (NULL == pData) {
1218 *pSizeOfData = This->functionLength;
1219 return D3D_OK;
1221 if (*pSizeOfData < This->functionLength) {
1222 *pSizeOfData = This->functionLength;
1223 return D3DERR_MOREDATA;
1225 if (NULL == This->function) { /* no function defined */
1226 TRACE("(%p) : GetFunction no User Function defined using NULL to %p\n", This, pData);
1227 (*(DWORD **) pData) = NULL;
1228 } else {
1229 TRACE("(%p) : GetFunction copying to %p\n", This, pData);
1230 memcpy(pData, This->function, This->functionLength);
1232 return D3D_OK;
1235 HRESULT WINAPI IDirect3DVertexShaderImpl_SetConstantF(IDirect3DVertexShaderImpl* This, UINT StartRegister, CONST FLOAT* pConstantData, UINT Vector4fCount) {
1236 if (StartRegister + Vector4fCount > D3D8_VSHADER_MAX_CONSTANTS) {
1237 return D3DERR_INVALIDCALL;
1239 if (NULL == This->data) { /* temporary while datas not supported */
1240 FIXME("(%p) : VertexShader_SetConstant not fully supported yet\n", This);
1241 return D3DERR_INVALIDCALL;
1243 memcpy(&This->data->C[StartRegister], pConstantData, Vector4fCount * 4 * sizeof(FLOAT));
1244 return D3D_OK;
1247 HRESULT WINAPI IDirect3DVertexShaderImpl_GetConstantF(IDirect3DVertexShaderImpl* This, UINT StartRegister, FLOAT* pConstantData, UINT Vector4fCount) {
1248 if (StartRegister + Vector4fCount > D3D8_VSHADER_MAX_CONSTANTS) {
1249 return D3DERR_INVALIDCALL;
1251 if (NULL == This->data) { /* temporary while datas not supported */
1252 return D3DERR_INVALIDCALL;
1254 memcpy(pConstantData, &This->data->C[StartRegister], Vector4fCount * 4 * sizeof(FLOAT));
1255 return D3D_OK;
1259 /**********************************************************************************************************************************************
1260 **********************************************************************************************************************************************
1261 **********************************************************************************************************************************************
1262 **********************************************************************************************************************************************
1263 **********************************************************************************************************************************************/
1265 void pshader_texcoord(D3DSHADERVECTOR* d) {
1268 void pshader_texkill(D3DSHADERVECTOR* d) {
1271 void pshader_tex(D3DSHADERVECTOR* d) {
1274 void pshader_texbem(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
1277 void pshader_texbeml(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
1280 void pshader_texreg2ar(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
1283 void pshader_texreg2gb(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
1286 void pshader_texm3x2pad(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
1289 void pshader_texm3x2tex(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
1292 void pshader_texm3x3pad(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
1295 void pshader_texm3x3tex(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
1298 void pshader_texm3x3diff(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
1301 void pshader_texm3x3spec(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
1304 void pshader_texm3x3vspec(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
1307 void pshader_cnd(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1, D3DSHADERVECTOR* s2) {
1310 void pshader_def(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1, D3DSHADERVECTOR* s2, D3DSHADERVECTOR* s3) {
1313 void pshader_texreg2rgb(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
1316 void pshader_texdp3tex(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
1319 void pshader_texm3x2depth(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
1322 void pshader_texdp3(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
1325 void pshader_texm3x3(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
1328 void pshader_texdepth(D3DSHADERVECTOR* d) {
1331 void pshader_cmp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1, D3DSHADERVECTOR* s2) {
1334 void pshader_bem(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
1337 static CONST SHADER_OPCODE pshader_ins [] = {
1338 {D3DSIO_NOP, "nop", 0, vshader_nop, 0, 0},
1339 {D3DSIO_MOV, "mov", 2, vshader_mov, 0, 0},
1340 {D3DSIO_ADD, "add", 3, vshader_add, 0, 0},
1341 {D3DSIO_SUB, "sub", 3, vshader_sub, 0, 0},
1342 {D3DSIO_MAD, "mad", 4, vshader_mad, 0, 0},
1343 {D3DSIO_MUL, "mul", 3, vshader_mul, 0, 0},
1344 {D3DSIO_RCP, "rcp", 2, vshader_rcp, 0, 0},
1345 {D3DSIO_RSQ, "rsq", 2, vshader_rsq, 0, 0},
1346 {D3DSIO_DP3, "dp3", 3, vshader_dp3, 0, 0},
1347 {D3DSIO_DP4, "dp4", 3, vshader_dp4, 0, 0},
1348 {D3DSIO_MIN, "min", 3, vshader_min, 0, 0},
1349 {D3DSIO_MAX, "max", 3, vshader_max, 0, 0},
1350 {D3DSIO_SLT, "slt", 3, vshader_slt, 0, 0},
1351 {D3DSIO_SGE, "sge", 3, vshader_sge, 0, 0},
1352 {D3DSIO_EXP, "exp", 2, vshader_exp, 0, 0},
1353 {D3DSIO_LOG, "log", 2, vshader_log, 0, 0},
1354 {D3DSIO_LIT, "lit", 2, vshader_lit, 0, 0},
1355 {D3DSIO_DST, "dst", 3, vshader_dst, 0, 0},
1356 {D3DSIO_LRP, "lrp", 4, vshader_lrp, 0, 0},
1357 {D3DSIO_FRC, "frc", 2, vshader_frc, 0, 0},
1358 {D3DSIO_M4x4, "m4x4", 3, vshader_m4x4, 0, 0},
1359 {D3DSIO_M4x3, "m4x3", 3, vshader_m4x3, 0, 0},
1360 {D3DSIO_M3x4, "m3x4", 3, vshader_m3x4, 0, 0},
1361 {D3DSIO_M3x3, "m3x3", 3, vshader_m3x3, 0, 0},
1362 {D3DSIO_M3x2, "m3x2", 3, vshader_m3x2, 0, 0},
1364 {D3DSIO_TEXCOORD, "texcoord", 1, pshader_texcoord, D3DPS_VERSION(1,0), D3DPS_VERSION(1,3)},
1365 {D3DSIO_TEXCOORD, "texcrd", 2, pshader_texcoord, D3DPS_VERSION(1,4), D3DPS_VERSION(1,4)},
1366 {D3DSIO_TEXKILL, "texkill", 1, pshader_texkill, D3DPS_VERSION(1,0), D3DPS_VERSION(1,4)},
1367 {D3DSIO_TEX, "tex", 1, pshader_tex, D3DPS_VERSION(1,0), D3DPS_VERSION(1,3)},
1368 {D3DSIO_TEX, "texld", 2, pshader_tex, D3DPS_VERSION(1,4), D3DPS_VERSION(1,4)},
1369 {D3DSIO_TEXBEM, "texbem", 2, pshader_texbem, D3DPS_VERSION(1,0), D3DPS_VERSION(1,3)},
1370 {D3DSIO_TEXBEML, "texbeml", 2, pshader_texbeml, D3DPS_VERSION(1,0), D3DPS_VERSION(1,3)},
1371 {D3DSIO_TEXREG2AR, "texreg2ar", 2, pshader_texreg2ar, D3DPS_VERSION(1,1), D3DPS_VERSION(1,3)},
1372 {D3DSIO_TEXREG2GB, "texreg2gb", 2, pshader_texreg2gb, D3DPS_VERSION(1,2), D3DPS_VERSION(1,3)},
1373 {D3DSIO_TEXM3x2PAD, "texm3x2pad", 2, pshader_texm3x2pad, D3DPS_VERSION(1,0), D3DPS_VERSION(1,3)},
1374 {D3DSIO_TEXM3x2TEX, "texm3x2tex", 2, pshader_texm3x2tex, D3DPS_VERSION(1,0), D3DPS_VERSION(1,3)},
1375 {D3DSIO_TEXM3x3PAD, "texm3x3pad", 2, pshader_texm3x3pad, D3DPS_VERSION(1,0), D3DPS_VERSION(1,3)},
1376 {D3DSIO_TEXM3x3TEX, "texm3x3tex", 2, pshader_texm3x3tex, D3DPS_VERSION(1,0), D3DPS_VERSION(1,3)},
1377 {D3DSIO_TEXM3x3DIFF, "texm3x3diff", 2, pshader_texm3x3diff, D3DPS_VERSION(0,0), D3DPS_VERSION(0,0)},
1378 {D3DSIO_TEXM3x3SPEC, "texm3x3spec", 3, pshader_texm3x3spec, D3DPS_VERSION(1,0), D3DPS_VERSION(1,3)},
1379 {D3DSIO_TEXM3x3VSPEC, "texm3x3vspec", 2, pshader_texm3x3vspec, D3DPS_VERSION(1,0), D3DPS_VERSION(1,3)},
1381 {D3DSIO_EXPP, "expp", 2, vshader_expp, 0, 0},
1382 {D3DSIO_LOGP, "logp", 2, vshader_logp, 0, 0},
1384 {D3DSIO_CND, "cnd", 4, pshader_cnd, D3DPS_VERSION(1,1), D3DPS_VERSION(1,4)},
1385 {D3DSIO_DEF, "def", 5, pshader_def, D3DPS_VERSION(1,0), D3DPS_VERSION(3,0)},
1386 {D3DSIO_TEXREG2RGB, "texbreg2rgb", 2, pshader_texreg2rgb, D3DPS_VERSION(1,2), D3DPS_VERSION(1,3)},
1388 {D3DSIO_TEXDP3TEX, "texdp3tex", 2, pshader_texdp3tex, D3DPS_VERSION(1,2), D3DPS_VERSION(1,3)},
1389 {D3DSIO_TEXM3x2DEPTH, "texm3x2depth", 2, pshader_texm3x2depth, D3DPS_VERSION(1,3), D3DPS_VERSION(1,3)},
1390 {D3DSIO_TEXDP3, "texdp3", 2, pshader_texdp3, D3DPS_VERSION(1,2), D3DPS_VERSION(1,3)},
1391 {D3DSIO_TEXM3x3, "texm3x3", 2, pshader_texm3x3, D3DPS_VERSION(1,2), D3DPS_VERSION(1,3)},
1392 {D3DSIO_TEXDEPTH, "texdepth", 1, pshader_texdepth, D3DPS_VERSION(1,4), D3DPS_VERSION(1,4)},
1393 {D3DSIO_CMP, "cmp", 4, pshader_cmp, D3DPS_VERSION(1,1), D3DPS_VERSION(3,0)},
1394 {D3DSIO_BEM, "bem", 3, pshader_bem, D3DPS_VERSION(1,4), D3DPS_VERSION(1,4)},
1396 {D3DSIO_PHASE, "phase", 0, vshader_nop, 0, 0},
1398 {0, NULL, 0, NULL}
1401 inline static const SHADER_OPCODE* pshader_program_get_opcode(const DWORD code, const int version) {
1402 DWORD i = 0;
1403 DWORD hex_version = D3DPS_VERSION(version/10, version%10);
1404 /** TODO: use dichotomic search */
1405 while (NULL != pshader_ins[i].name) {
1406 if ( ( (code & D3DSI_OPCODE_MASK) == pshader_ins[i].opcode) &&
1407 ( ( (hex_version >= pshader_ins[i].min_version) && (hex_version <= pshader_ins[i].max_version)) ||
1408 ( (pshader_ins[i].min_version == 0) && (pshader_ins[i].max_version == 0) ) ) ) {
1409 return &pshader_ins[i];
1411 ++i;
1413 return NULL;
1416 inline static BOOL pshader_is_version_token(DWORD token) {
1417 return 0xFFFF0000 == (token & 0xFFFF0000);
1420 inline static BOOL pshader_is_comment_token(DWORD token) {
1421 return D3DSIO_COMMENT == (token & D3DSI_OPCODE_MASK);
1424 inline static void pshader_program_dump_opcode(const SHADER_OPCODE* curOpcode, const DWORD code, const DWORD output) {
1425 if (0 != (code & ~D3DSI_OPCODE_MASK)) {
1426 DWORD mask = (code & ~D3DSI_OPCODE_MASK);
1427 switch (mask) {
1428 case 0x40000000: TRACE("+"); break;
1429 default:
1430 TRACE(" unhandled modifier(0x%08lx) ", mask);
1433 TRACE("%s", curOpcode->name);
1435 * normally this is a destination reg modifier
1436 * but in pixel shaders asm code its specified as:
1437 * dp3_x4 t1.rgba, r1, c1
1438 * or
1439 * dp3_x2_sat r0, t0_bx2, v0_bx2
1440 * so for better debbuging i use the same norm
1442 if (0 != (output & D3DSP_DSTSHIFT_MASK)) {
1443 DWORD shift = (output & D3DSP_DSTSHIFT_MASK) >> D3DSP_DSTSHIFT_SHIFT;
1444 if (shift < 8) {
1445 TRACE("_x%u", 1 << shift);
1446 } else {
1447 TRACE("_d%u", 1 << (16-shift));
1450 if (0 != (output & D3DSP_DSTMOD_MASK)) {
1451 DWORD mask = output & D3DSP_DSTMOD_MASK;
1452 switch (mask) {
1453 case D3DSPDM_SATURATE: TRACE("_sat"); break;
1454 default:
1455 TRACE("_unhandled_modifier(0x%08lx)", mask);
1458 TRACE(" ");
1461 inline static void pshader_program_dump_param(const DWORD param, int input) {
1462 static const char* rastout_reg_names[] = { "oC0", "oC1", "oC2", "oC3", "oDepth" };
1463 static const char swizzle_reg_chars[] = "rgba";
1465 DWORD reg = param & 0x00001FFF;
1466 DWORD regtype = ((param & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT);
1468 if (input) {
1469 if ( ((param & D3DSP_SRCMOD_MASK) == D3DSPSM_NEG) ||
1470 ((param & D3DSP_SRCMOD_MASK) == D3DSPSM_BIASNEG) ||
1471 ((param & D3DSP_SRCMOD_MASK) == D3DSPSM_SIGNNEG) ||
1472 ((param & D3DSP_SRCMOD_MASK) == D3DSPSM_X2NEG) )
1473 TRACE("-");
1474 else if ((param & D3DSP_SRCMOD_MASK) == D3DSPSM_COMP)
1475 TRACE("1-");
1478 switch (regtype << D3DSP_REGTYPE_SHIFT) {
1479 case D3DSPR_TEMP:
1480 TRACE("R[%lu]", reg);
1481 break;
1482 case D3DSPR_INPUT:
1483 TRACE("V[%lu]", reg);
1484 break;
1485 case D3DSPR_CONST:
1486 TRACE("C[%s%lu]", (param & D3DVS_ADDRMODE_RELATIVE) ? "a0.x + " : "", reg);
1487 break;
1488 case D3DSPR_TEXTURE: /* case D3DSPR_ADDR: */
1489 TRACE("t[%lu]", reg);
1490 break;
1491 case D3DSPR_RASTOUT:
1492 TRACE("%s", rastout_reg_names[reg]);
1493 break;
1494 case D3DSPR_ATTROUT:
1495 TRACE("oD[%lu]", reg);
1496 break;
1497 case D3DSPR_TEXCRDOUT:
1498 TRACE("oT[%lu]", reg);
1499 break;
1500 default:
1501 break;
1504 if (!input) {
1505 /** operand output */
1507 * for better debugging traces it's done into opcode dump code
1508 * @see pshader_program_dump_opcode
1509 if (0 != (param & D3DSP_DSTMOD_MASK)) {
1510 DWORD mask = param & D3DSP_DSTMOD_MASK;
1511 switch (mask) {
1512 case D3DSPDM_SATURATE: TRACE("_sat"); break;
1513 default:
1514 TRACE("_unhandled_modifier(0x%08lx)", mask);
1517 if (0 != (param & D3DSP_DSTSHIFT_MASK)) {
1518 DWORD shift = (param & D3DSP_DSTSHIFT_MASK) >> D3DSP_DSTSHIFT_SHIFT;
1519 if (shift > 0) {
1520 TRACE("_x%u", 1 << shift);
1524 if ((param & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
1525 TRACE(".");
1526 if (param & D3DSP_WRITEMASK_0) TRACE("r");
1527 if (param & D3DSP_WRITEMASK_1) TRACE("g");
1528 if (param & D3DSP_WRITEMASK_2) TRACE("b");
1529 if (param & D3DSP_WRITEMASK_3) TRACE("a");
1531 } else {
1532 /** operand input */
1533 DWORD swizzle = (param & D3DSP_SWIZZLE_MASK) >> D3DSP_SWIZZLE_SHIFT;
1534 DWORD swizzle_x = swizzle & 0x03;
1535 DWORD swizzle_y = (swizzle >> 2) & 0x03;
1536 DWORD swizzle_z = (swizzle >> 4) & 0x03;
1537 DWORD swizzle_w = (swizzle >> 6) & 0x03;
1539 if (0 != (param & D3DSP_SRCMOD_MASK)) {
1540 DWORD mask = param & D3DSP_SRCMOD_MASK;
1541 /*TRACE("_modifier(0x%08lx) ", mask);*/
1542 switch (mask) {
1543 case D3DSPSM_NONE: break;
1544 case D3DSPSM_NEG: break;
1545 case D3DSPSM_BIAS: TRACE("_bias"); break;
1546 case D3DSPSM_BIASNEG: TRACE("_bias"); break;
1547 case D3DSPSM_SIGN: TRACE("_bx2"); break;
1548 case D3DSPSM_SIGNNEG: TRACE("_bx2"); break;
1549 case D3DSPSM_COMP: break;
1550 case D3DSPSM_X2: TRACE("_x2"); break;
1551 case D3DSPSM_X2NEG: TRACE("_x2"); break;
1552 case D3DSPSM_DZ: TRACE("_dz"); break;
1553 case D3DSPSM_DW: TRACE("_dw"); break;
1554 default:
1555 TRACE("_unknown(0x%08lx)", mask);
1560 * swizzle bits fields:
1561 * WWZZYYXX
1563 if ((D3DSP_NOSWIZZLE >> D3DSP_SWIZZLE_SHIFT) != swizzle) { /* ! D3DVS_NOSWIZZLE == 0xE4 << D3DVS_SWIZZLE_SHIFT */
1564 if (swizzle_x == swizzle_y &&
1565 swizzle_x == swizzle_z &&
1566 swizzle_x == swizzle_w) {
1567 TRACE(".%c", swizzle_reg_chars[swizzle_x]);
1568 } else {
1569 TRACE(".%c%c%c%c",
1570 swizzle_reg_chars[swizzle_x],
1571 swizzle_reg_chars[swizzle_y],
1572 swizzle_reg_chars[swizzle_z],
1573 swizzle_reg_chars[swizzle_w]);
1579 static int constants[D3D8_PSHADER_MAX_CONSTANTS];
1581 inline static void get_register_name(const DWORD param, char* regstr)
1583 static const char* rastout_reg_names[] = { "oC0", "oC1", "oC2", "oC3", "oDepth" };
1585 DWORD reg = param & 0x00001FFF;
1586 DWORD regtype = ((param & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT);
1588 switch (regtype << D3DSP_REGTYPE_SHIFT) {
1589 case D3DSPR_TEMP:
1590 sprintf(regstr, "R%lu", reg);
1591 break;
1592 case D3DSPR_INPUT:
1593 if (reg==0) {
1594 strcpy(regstr, "fragment.color.primary");
1595 } else {
1596 strcpy(regstr, "fragment.color.secondary");
1598 break;
1599 case D3DSPR_CONST:
1600 if (constants[reg])
1601 sprintf(regstr, "C%lu", reg);
1602 else
1603 sprintf(regstr, "program.env[%lu]", reg);
1604 break;
1605 case D3DSPR_TEXTURE: /* case D3DSPR_ADDR: */
1606 sprintf(regstr,"T%lu", reg);
1607 break;
1608 case D3DSPR_RASTOUT:
1609 sprintf(regstr, "%s", rastout_reg_names[reg]);
1610 break;
1611 case D3DSPR_ATTROUT:
1612 sprintf(regstr, "oD[%lu]", reg);
1613 break;
1614 case D3DSPR_TEXCRDOUT:
1615 sprintf(regstr, "oT[%lu]", reg);
1616 break;
1617 default:
1618 break;
1622 inline static void addline(unsigned int* lineNum, char* pgm, char* line)
1624 ++(*lineNum);
1625 TRACE_(d3d_hw_shader)("GL HW (%u, %u) : %s\n", *lineNum, strlen(pgm), line);
1626 strcat(pgm, line);
1627 strcat(pgm, "\n");
1630 static const char* shift_tab[] = {
1631 "dummy", /* 0 (none) */
1632 "coefmul.x", /* 1 (x2) */
1633 "coefmul.y", /* 2 (x4) */
1634 "coefmul.z", /* 3 (x8) */
1635 "coefmul.w", /* 4 (x16) */
1636 "dummy", /* 5 (x32) */
1637 "dummy", /* 6 (x64) */
1638 "dummy", /* 7 (x128) */
1639 "dummy", /* 8 (d256) */
1640 "dummy", /* 9 (d128) */
1641 "dummy", /* 10 (d64) */
1642 "dummy", /* 11 (d32) */
1643 "coefdiv.w", /* 12 (d16) */
1644 "coefdiv.z", /* 13 (d8) */
1645 "coefdiv.y", /* 14 (d4) */
1646 "coefdiv.x" /* 15 (d2) */
1649 inline static void get_write_mask(const DWORD output_reg, char* write_mask)
1651 *write_mask = 0;
1652 if ((output_reg & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
1653 strcat(write_mask, ".");
1654 if (output_reg & D3DSP_WRITEMASK_0) strcat(write_mask, "r");
1655 if (output_reg & D3DSP_WRITEMASK_1) strcat(write_mask, "g");
1656 if (output_reg & D3DSP_WRITEMASK_2) strcat(write_mask, "b");
1657 if (output_reg & D3DSP_WRITEMASK_3) strcat(write_mask, "a");
1661 inline static void get_input_register_swizzle(const DWORD instr, char* swzstring)
1663 static const char swizzle_reg_chars[] = "rgba";
1664 DWORD swizzle = (instr & D3DSP_SWIZZLE_MASK) >> D3DSP_SWIZZLE_SHIFT;
1665 DWORD swizzle_x = swizzle & 0x03;
1666 DWORD swizzle_y = (swizzle >> 2) & 0x03;
1667 DWORD swizzle_z = (swizzle >> 4) & 0x03;
1668 DWORD swizzle_w = (swizzle >> 6) & 0x03;
1670 * swizzle bits fields:
1671 * WWZZYYXX
1673 *swzstring = 0;
1674 if ((D3DSP_NOSWIZZLE >> D3DSP_SWIZZLE_SHIFT) != swizzle) { /* ! D3DVS_NOSWIZZLE == 0xE4 << D3DVS_SWIZZLE_SHIFT */
1675 if (swizzle_x == swizzle_y &&
1676 swizzle_x == swizzle_z &&
1677 swizzle_x == swizzle_w) {
1678 sprintf(swzstring, ".%c", swizzle_reg_chars[swizzle_x]);
1679 } else {
1680 sprintf(swzstring, ".%c%c%c%c",
1681 swizzle_reg_chars[swizzle_x],
1682 swizzle_reg_chars[swizzle_y],
1683 swizzle_reg_chars[swizzle_z],
1684 swizzle_reg_chars[swizzle_w]);
1689 inline static void gen_output_modifier_line(int saturate, char* write_mask, int shift, char *regstr, char* line)
1691 /* Generate a line that does the output modifier computation */
1692 sprintf(line, "MUL%s %s%s, %s, %s;", saturate ? "_SAT" : "", regstr, write_mask, regstr, shift_tab[shift]);
1695 inline static int gen_input_modifier_line(const DWORD instr, int tmpreg, char* outregstr, char* line)
1697 /* Generate a line that does the input modifier computation and return the input register to use */
1698 static char regstr[256];
1699 static char tmpline[256];
1700 int insert_line;
1702 /* Assume a new line will be added */
1703 insert_line = 1;
1705 /* Get register name */
1706 get_register_name(instr, regstr);
1708 switch (instr & D3DSP_SRCMOD_MASK) {
1709 case D3DSPSM_NONE:
1710 strcpy(outregstr, regstr);
1711 insert_line = 0;
1712 break;
1713 case D3DSPSM_NEG:
1714 sprintf(outregstr, "-%s", regstr);
1715 insert_line = 0;
1716 break;
1717 case D3DSPSM_BIAS:
1718 sprintf(line, "ADD T%c, %s, -coefdiv.x;", 'A' + tmpreg, regstr);
1719 break;
1720 case D3DSPSM_BIASNEG:
1721 sprintf(line, "ADD T%c, -%s, coefdiv.x;", 'A' + tmpreg, regstr);
1722 break;
1723 case D3DSPSM_SIGN:
1724 sprintf(line, "MAD T%c, %s, coefmul.x, -one.x;", 'A' + tmpreg, regstr);
1725 break;
1726 case D3DSPSM_SIGNNEG:
1727 sprintf(line, "MAD T%c, %s, -coefmul.x, one.x;", 'A' + tmpreg, regstr);
1728 break;
1729 case D3DSPSM_COMP:
1730 sprintf(line, "SUB T%c, one.x, %s;", 'A' + tmpreg, regstr);
1731 break;
1732 case D3DSPSM_X2:
1733 sprintf(line, "ADD T%c, %s, %s;", 'A' + tmpreg, regstr, regstr);
1734 break;
1735 case D3DSPSM_X2NEG:
1736 sprintf(line, "ADD T%c, -%s, -%s;", 'A' + tmpreg, regstr, regstr);
1737 break;
1738 case D3DSPSM_DZ:
1739 sprintf(line, "RCP T%c, %s.z;", 'A' + tmpreg, regstr);
1740 sprintf(tmpline, "MUL T%c, %s, T%c;", 'A' + tmpreg, regstr, 'A' + tmpreg);
1741 strcat(line, "\n"); /* Hack */
1742 strcat(line, tmpline);
1743 break;
1744 case D3DSPSM_DW:
1745 sprintf(line, "RCP T%c, %s;", 'A' + tmpreg, regstr);
1746 sprintf(tmpline, "MUL T%c, %s, T%c;", 'A' + tmpreg, regstr, 'A' + tmpreg);
1747 strcat(line, "\n"); /* Hack */
1748 strcat(line, tmpline);
1749 break;
1750 default:
1751 strcpy(outregstr, regstr);
1752 insert_line = 0;
1755 if (insert_line) {
1756 /* Substitute the register name */
1757 sprintf(outregstr, "T%c", 'A' + tmpreg);
1760 return insert_line;
1764 * Pixel Shaders
1766 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/PixelShader1_X/modifiers/sourceregistermodifiers.asp
1767 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/PixelShader2_0/Registers/Registers.asp
1768 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/d3d/interfaces/IDirect3DPixelShader9/_IDirect3DPixelShader9.asp
1771 inline static VOID IDirect3DPixelShaderImpl_GenerateProgramArbHW(IDirect3DPixelShaderImpl* pshader, CONST DWORD* pFunction) {
1772 const DWORD* pToken = pFunction;
1773 const SHADER_OPCODE* curOpcode = NULL;
1774 const DWORD* pInstr;
1775 DWORD code;
1776 DWORD i;
1777 int autoparam;
1778 unsigned lineNum = 0;
1779 char *pgmStr = NULL;
1780 char tmpLine[255];
1781 BOOL saturate;
1782 int row = 0;
1783 DWORD tcw[2];
1784 IDirect3DDevice8Impl* This = pshader->device;
1785 int version = 0;
1787 for(i = 0; i < D3D8_PSHADER_MAX_CONSTANTS; i++)
1788 constants[i] = 0;
1790 pgmStr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 65535); /* 64kb should be enough */
1792 if (NULL != pToken) {
1793 while (D3DPS_END() != *pToken) {
1794 if (pshader_is_version_token(*pToken)) { /** version */
1795 int numTemps;
1796 int numConstants;
1798 /* Extract version *10 into integer value (ie. 1.0 == 10, 1.1==11 etc */
1799 version = (((*pToken >> 8) & 0x0F) * 10) + (*pToken & 0x0F);
1801 TRACE_(d3d_hw_shader)("ps.%lu.%lu;\n", (*pToken >> 8) & 0x0F, (*pToken & 0x0F));
1803 /* Each release of pixel shaders has had different numbers of temp registers */
1804 switch (version) {
1805 case 10:
1806 case 11:
1807 case 12:
1808 case 13:
1809 case 14: numTemps=12;
1810 numConstants=8;
1811 strcpy(tmpLine, "!!ARBfp1.0");
1812 break;
1813 case 20: numTemps=12;
1814 numConstants=8;
1815 strcpy(tmpLine, "!!ARBfp2.0");
1816 FIXME_(d3d_hw_shader)("No work done yet to support ps2.0 in hw\n");
1817 break;
1818 case 30: numTemps=32;
1819 numConstants=8;
1820 strcpy(tmpLine, "!!ARBfp3.0");
1821 FIXME_(d3d_hw_shader)("No work done yet to support ps3.0 in hw\n");
1822 break;
1823 default:
1824 numTemps=12;
1825 numConstants=8;
1826 strcpy(tmpLine, "!!ARBfp1.0");
1827 FIXME_(d3d_hw_shader)("Unrecognized pixel shader version!\n");
1829 addline(&lineNum, pgmStr, tmpLine);
1831 for(i = 0; i < 6; i++) {
1832 sprintf(tmpLine, "TEMP T%lu;", i);
1833 addline(&lineNum, pgmStr, tmpLine);
1835 for(i = 0; i < 6; i++) {
1836 sprintf(tmpLine, "TEMP R%lu;", i);
1837 addline(&lineNum, pgmStr, tmpLine);
1840 sprintf(tmpLine, "TEMP TMP;");
1841 addline(&lineNum, pgmStr, tmpLine);
1842 sprintf(tmpLine, "TEMP TMP2;");
1843 addline(&lineNum, pgmStr, tmpLine);
1844 sprintf(tmpLine, "TEMP TA;");
1845 addline(&lineNum, pgmStr, tmpLine);
1846 sprintf(tmpLine, "TEMP TB;");
1847 addline(&lineNum, pgmStr, tmpLine);
1848 sprintf(tmpLine, "TEMP TC;");
1849 addline(&lineNum, pgmStr, tmpLine);
1851 strcpy(tmpLine, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };");
1852 addline(&lineNum, pgmStr, tmpLine);
1853 strcpy(tmpLine, "PARAM coefmul = { 2, 4, 8, 16 };");
1854 addline(&lineNum, pgmStr, tmpLine);
1855 strcpy(tmpLine, "PARAM one = { 1.0, 1.0, 1.0, 1.0 };");
1856 addline(&lineNum, pgmStr, tmpLine);
1858 for(i = 0; i < 4; i++) {
1859 sprintf(tmpLine, "MOV T%lu, fragment.texcoord[%lu];", i, i);
1860 addline(&lineNum, pgmStr, tmpLine);
1863 ++pToken;
1864 continue;
1866 if (pshader_is_comment_token(*pToken)) { /** comment */
1867 DWORD comment_len = (*pToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
1868 ++pToken;
1869 /*TRACE("comment[%ld] ;%s\n", comment_len, (char*)pToken);*/
1870 pToken += comment_len;
1871 continue;
1873 code = *pToken;
1874 pInstr = pToken;
1875 curOpcode = pshader_program_get_opcode(code, version);
1876 ++pToken;
1877 if (NULL == curOpcode) {
1878 /* unkown current opcode ... */
1879 while (*pToken & 0x80000000) {
1880 TRACE("unrecognized opcode: %08lx\n", *pToken);
1881 ++pToken;
1883 } else {
1884 autoparam = 1;
1885 saturate = FALSE;
1886 /* Build opcode for GL vertex_program */
1887 switch (curOpcode->opcode) {
1888 case D3DSIO_NOP:
1889 case D3DSIO_PHASE:
1890 continue;
1891 case D3DSIO_DEF:
1893 DWORD reg = *pToken & 0x00001FFF;
1894 sprintf(tmpLine, "PARAM C%lu = { %f, %f, %f, %f };", reg,
1895 *((const float*)(pToken+1)),
1896 *((const float*)(pToken+2)),
1897 *((const float*)(pToken+3)),
1898 *((const float*)(pToken+4)) );
1899 addline(&lineNum, pgmStr, tmpLine);
1900 constants[reg] = 1;
1901 autoparam = 0;
1902 pToken+=5;
1904 break;
1905 case D3DSIO_TEXKILL:
1906 strcpy(tmpLine, "KIL");
1907 break;
1908 case D3DSIO_TEX:
1910 char tmp[20];
1911 get_write_mask(*pToken, tmp);
1912 if (version != 14) {
1913 DWORD reg = *pToken & 0x00001FFF;
1914 sprintf(tmpLine,"TEX T%lu%s, T%lu, texture[%lu], 2D;", reg, tmp, reg, reg);
1915 addline(&lineNum, pgmStr, tmpLine);
1916 autoparam = 0;
1917 pToken++;
1918 } else {
1919 char line[256];
1920 char reg[20];
1921 DWORD reg1 = *pToken & 0x00001FFF;
1922 DWORD reg2 = *(pToken+1) & 0x00001FFF;
1923 if (gen_input_modifier_line(*(pToken+1), 0, reg, line))
1924 addline(&lineNum, pgmStr, line);
1925 sprintf(tmpLine,"TEX R%lu%s, %s, texture[%lu], 2D;", reg1, tmp, reg, reg2);
1926 addline(&lineNum, pgmStr, tmpLine);
1927 autoparam = 0;
1928 pToken += 2;
1931 break;
1932 case D3DSIO_TEXCOORD:
1934 char tmp[20];
1935 get_write_mask(*pToken, tmp);
1936 if (version != 14) {
1937 DWORD reg = *pToken & 0x00001FFF;
1938 sprintf(tmpLine, "MOV T%lu%s, fragment.texcoord[%lu];", reg, tmp, reg);
1939 addline(&lineNum, pgmStr, tmpLine);
1940 autoparam = 0;
1941 pToken++;
1942 } else {
1943 DWORD reg1 = *pToken & 0x00001FFF;
1944 DWORD reg2 = *(pToken+1) & 0x00001FFF;
1945 sprintf(tmpLine, "MOV R%lu%s, fragment.texcoord[%lu];", reg1, tmp, reg2);
1946 addline(&lineNum, pgmStr, tmpLine);
1947 autoparam = 0;
1948 pToken += 2;
1951 break;
1952 case D3DSIO_TEXM3x2PAD:
1954 DWORD reg = *pToken & 0x00001FFF;
1955 char buf[50];
1956 if (gen_input_modifier_line(*(pToken+1), 0, buf, tmpLine))
1957 addline(&lineNum, pgmStr, tmpLine);
1958 sprintf(tmpLine, "DP3 TMP.x, T%lu, %s;", reg, buf);
1959 addline(&lineNum, pgmStr, tmpLine);
1960 autoparam = 0;
1961 pToken += 2;
1963 break;
1964 case D3DSIO_TEXM3x2TEX:
1966 DWORD reg = *pToken & 0x00001FFF;
1967 char buf[50];
1968 if (gen_input_modifier_line(*(pToken+1), 0, buf, tmpLine))
1969 addline(&lineNum, pgmStr, tmpLine);
1970 sprintf(tmpLine, "DP3 TMP.y, T%lu, %s;", reg, buf);
1971 addline(&lineNum, pgmStr, tmpLine);
1972 sprintf(tmpLine, "TEX T%lu, TMP, texture[%lu], 2D;", reg, reg);
1973 addline(&lineNum, pgmStr, tmpLine);
1974 autoparam = 0;
1975 pToken += 2;
1977 break;
1978 case D3DSIO_TEXREG2AR:
1980 DWORD reg1 = *pToken & 0x00001FFF;
1981 DWORD reg2 = *(pToken+1) & 0x00001FFF;
1982 sprintf(tmpLine, "MOV TMP.r, T%lu.a;", reg2);
1983 addline(&lineNum, pgmStr, tmpLine);
1984 sprintf(tmpLine, "MOV TMP.g, T%lu.r;", reg2);
1985 addline(&lineNum, pgmStr, tmpLine);
1986 sprintf(tmpLine, "TEX T%lu, TMP, texture[%lu], 2D;", reg1, reg1);
1987 addline(&lineNum, pgmStr, tmpLine);
1988 autoparam = 0;
1989 pToken+=2;
1991 break;
1992 case D3DSIO_TEXREG2GB:
1994 DWORD reg1 = *pToken & 0x00001FFF;
1995 DWORD reg2 = *(pToken+1) & 0x00001FFF;
1996 sprintf(tmpLine, "MOV TMP.r, T%lu.g;", reg2);
1997 addline(&lineNum, pgmStr, tmpLine);
1998 sprintf(tmpLine, "MOV TMP.g, T%lu.b;", reg2);
1999 addline(&lineNum, pgmStr, tmpLine);
2000 sprintf(tmpLine, "TEX T%lu, TMP, texture[%lu], 2D;", reg1, reg1);
2001 addline(&lineNum, pgmStr, tmpLine);
2002 autoparam = 0;
2003 pToken+=2;
2005 break;
2006 case D3DSIO_TEXBEM:
2008 DWORD reg1 = *pToken & 0x00001FFF;
2009 DWORD reg2 = *(pToken+1) & 0x00001FFF;
2010 /* FIXME: Should apply the BUMPMAPENV matrix */
2011 sprintf(tmpLine, "ADD TMP.rg, fragment.texcoord[%lu], T%lu;", reg1, reg2);
2012 addline(&lineNum, pgmStr, tmpLine);
2013 sprintf(tmpLine, "TEX T%lu, TMP, texture[%lu], 2D;", reg1, reg1);
2014 addline(&lineNum, pgmStr, tmpLine);
2015 autoparam = 0;
2016 pToken+=2;
2018 break;
2019 case D3DSIO_TEXM3x3PAD:
2021 DWORD reg = *pToken & 0x00001FFF;
2022 char buf[50];
2023 if (gen_input_modifier_line(*(pToken+1), 0, buf, tmpLine))
2024 addline(&lineNum, pgmStr, tmpLine);
2025 sprintf(tmpLine, "DP3 TMP.%c, T%lu, %s;", 'x'+row, reg, buf);
2026 addline(&lineNum, pgmStr, tmpLine);
2027 tcw[row++] = reg;
2028 autoparam = 0;
2029 pToken += 2;
2031 break;
2032 case D3DSIO_TEXM3x3TEX:
2034 DWORD reg = *pToken & 0x00001FFF;
2035 char buf[50];
2036 if (gen_input_modifier_line(*(pToken+1), 0, buf, tmpLine))
2037 addline(&lineNum, pgmStr, tmpLine);
2038 sprintf(tmpLine, "DP3 TMP.z, T%lu, %s;", reg, buf);
2039 addline(&lineNum, pgmStr, tmpLine);
2040 /* Cubemap textures will be more used than 3D ones. */
2041 sprintf(tmpLine, "TEX T%lu, TMP, texture[%lu], CUBE;", reg, reg);
2042 addline(&lineNum, pgmStr, tmpLine);
2043 row = 0;
2044 autoparam = 0;
2045 pToken += 2;
2047 case D3DSIO_TEXM3x3VSPEC:
2049 DWORD reg = *pToken & 0x00001FFF;
2050 char buf[50];
2051 if (gen_input_modifier_line(*(pToken+1), 0, buf, tmpLine))
2052 addline(&lineNum, pgmStr, tmpLine);
2053 sprintf(tmpLine, "DP3 TMP.z, T%lu, %s;", reg, buf);
2054 addline(&lineNum, pgmStr, tmpLine);
2055 /* Construct the eye-ray vector from w coordinates */
2056 sprintf(tmpLine, "MOV TMP2.x, fragment.texcoord[%lu].w;", tcw[0]);
2057 addline(&lineNum, pgmStr, tmpLine);
2058 sprintf(tmpLine, "MOV TMP2.y, fragment.texcoord[%lu].w;", tcw[1]);
2059 addline(&lineNum, pgmStr, tmpLine);
2060 sprintf(tmpLine, "MOV TMP2.z, fragment.texcoord[%lu].w;", reg);
2061 addline(&lineNum, pgmStr, tmpLine);
2062 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
2063 sprintf(tmpLine, "DP3 TMP.w, TMP, TMP2;");
2064 addline(&lineNum, pgmStr, tmpLine);
2065 sprintf(tmpLine, "MUL TMP, TMP.w, TMP;");
2066 addline(&lineNum, pgmStr, tmpLine);
2067 sprintf(tmpLine, "MAD TMP, coefmul.x, TMP, -TMP2;");
2068 addline(&lineNum, pgmStr, tmpLine);
2069 /* Cubemap textures will be more used than 3D ones. */
2070 sprintf(tmpLine, "TEX T%lu, TMP, texture[%lu], CUBE;", reg, reg);
2071 addline(&lineNum, pgmStr, tmpLine);
2072 row = 0;
2073 autoparam = 0;
2074 pToken += 2;
2076 break;
2077 case D3DSIO_TEXM3x3SPEC:
2079 DWORD reg = *pToken & 0x00001FFF;
2080 DWORD reg3 = *(pToken+2) & 0x00001FFF;
2081 char buf[50];
2082 if (gen_input_modifier_line(*(pToken+1), 0, buf, tmpLine))
2083 addline(&lineNum, pgmStr, tmpLine);
2084 sprintf(tmpLine, "DP3 TMP.z, T%lu, %s;", reg, buf);
2085 addline(&lineNum, pgmStr, tmpLine);
2086 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
2087 sprintf(tmpLine, "DP3 TMP.w, TMP, C[%lu];", reg3);
2088 addline(&lineNum, pgmStr, tmpLine);
2089 sprintf(tmpLine, "MUL TMP, TMP.w, TMP;");
2090 addline(&lineNum, pgmStr, tmpLine);
2091 sprintf(tmpLine, "MAD TMP, coefmul.x, TMP, -C[%lu];", reg3);
2092 addline(&lineNum, pgmStr, tmpLine);
2093 /* Cubemap textures will be more used than 3D ones. */
2094 sprintf(tmpLine, "TEX T%lu, TMP, texture[%lu], CUBE;", reg, reg);
2095 addline(&lineNum, pgmStr, tmpLine);
2096 row = 0;
2097 autoparam = 0;
2098 pToken += 3;
2100 break;
2101 case D3DSIO_CND:
2102 break;
2103 case D3DSIO_CMP:
2104 break;
2105 case D3DSIO_MOV:
2106 strcpy(tmpLine, "MOV");
2107 break;
2108 case D3DSIO_MUL:
2109 strcpy(tmpLine, "MUL");
2110 break;
2111 case D3DSIO_DP3:
2112 strcpy(tmpLine, "DP3");
2113 break;
2114 case D3DSIO_MAD:
2115 strcpy(tmpLine, "MAD");
2116 break;
2117 case D3DSIO_ADD:
2118 strcpy(tmpLine, "ADD");
2119 break;
2120 case D3DSIO_SUB:
2121 strcpy(tmpLine, "SUB");
2122 break;
2123 case D3DSIO_LRP:
2124 strcpy(tmpLine, "LRP");
2125 break;
2126 default:
2127 FIXME_(d3d_hw_shader)("Can't handle opcode %s in hwShader\n", curOpcode->name);
2129 if (0 != (*pToken & D3DSP_DSTMOD_MASK)) {
2130 DWORD mask = *pToken & D3DSP_DSTMOD_MASK;
2131 switch (mask) {
2132 case D3DSPDM_SATURATE: saturate = TRUE; break;
2133 default:
2134 TRACE("_unhandled_modifier(0x%08lx)", mask);
2137 if (autoparam && (curOpcode->num_params > 0)) {
2138 char regs[3][50];
2139 char operands[4][100];
2140 char tmp[256];
2141 char swzstring[20];
2142 int saturate = 0;
2143 /* Generate lines that handle input modifier computation */
2144 for (i = 1; i < curOpcode->num_params; i++) {
2145 if (gen_input_modifier_line(*(pToken+i), i-1, regs[i-1], tmp))
2146 addline(&lineNum, pgmStr, tmp);
2148 /* Handle saturation only when no shift is present in the output modifier */
2149 if ((*pToken & D3DSPDM_SATURATE) && (0 == (*pToken & D3DSP_DSTSHIFT_MASK)))
2150 saturate = 1;
2151 /* Handle output register */
2152 get_register_name(*pToken, tmp);
2153 strcpy(operands[0], tmp);
2154 get_write_mask(*pToken, tmp);
2155 strcat(operands[0], tmp);
2156 /* Handle input registers */
2157 for (i = 1; i < curOpcode->num_params; i++) {
2158 strcpy(operands[i], regs[i-1]);
2159 get_input_register_swizzle(*(pToken+i), swzstring);
2160 strcat(operands[i], swzstring);
2162 if (curOpcode->opcode == D3DSIO_CMP) {
2163 sprintf(tmpLine, "CMP%s %s, %s, %s, %s;", (saturate ? "_SAT" : ""), operands[0], operands[1], operands[3], operands[2]);
2164 } else if (curOpcode->opcode == D3DSIO_CND) {
2165 sprintf(tmpLine, "ADD TMP, -%s, coefdiv.x;", operands[1]);
2166 addline(&lineNum, pgmStr, tmpLine);
2167 sprintf(tmpLine, "CMP%s %s, TMP, %s, %s;", (saturate ? "_SAT" : ""), operands[0], operands[2], operands[3]);
2168 } else {
2169 if (saturate)
2170 strcat(tmpLine, "_SAT");
2171 strcat(tmpLine, " ");
2172 strcat(tmpLine, operands[0]);
2173 for (i = 1; i < curOpcode->num_params; i++) {
2174 strcat(tmpLine, ", ");
2175 strcat(tmpLine, operands[i]);
2177 strcat(tmpLine,";");
2179 addline(&lineNum, pgmStr, tmpLine);
2180 pToken += curOpcode->num_params;
2182 if (curOpcode->num_params > 0) {
2183 DWORD param = *(pInstr+1);
2184 if (0 != (param & D3DSP_DSTSHIFT_MASK)) {
2185 /* Generate a line that handle the output modifier computation */
2186 char regstr[100];
2187 char write_mask[20];
2188 DWORD shift = (param & D3DSP_DSTSHIFT_MASK) >> D3DSP_DSTSHIFT_SHIFT;
2189 get_register_name(param, regstr);
2190 get_write_mask(param, write_mask);
2191 gen_output_modifier_line(saturate, write_mask, shift, regstr, tmpLine);
2192 addline(&lineNum, pgmStr, tmpLine);
2197 strcpy(tmpLine, "MOV result.color, R0;");
2198 addline(&lineNum, pgmStr, tmpLine);
2200 strcpy(tmpLine, "END");
2201 addline(&lineNum, pgmStr, tmpLine);
2204 /* Create the hw shader */
2205 GL_EXTCALL(glGenProgramsARB(1, &pshader->prgId));
2206 TRACE_(d3d_hw_shader)("Creating a hw pixel shader, prg=%d\n", pshader->prgId);
2208 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pshader->prgId));
2210 /* Create the program and check for errors */
2211 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(pgmStr), pgmStr));
2212 if (glGetError() == GL_INVALID_OPERATION) {
2213 GLint errPos;
2214 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
2215 FIXME_(d3d_hw_shader)("HW PixelShader Error at position: %d\n%s\n", errPos, glGetString(GL_PROGRAM_ERROR_STRING_ARB));
2216 pshader->prgId = -1;
2219 HeapFree(GetProcessHeap(), 0, pgmStr);
2222 inline static VOID IDirect3DPixelShaderImpl_ParseProgram(IDirect3DPixelShaderImpl* pshader, CONST DWORD* pFunction) {
2223 const DWORD* pToken = pFunction;
2224 const SHADER_OPCODE* curOpcode = NULL;
2225 DWORD code;
2226 DWORD len = 0;
2227 DWORD i;
2228 int version = 0;
2230 if (NULL != pToken) {
2231 while (D3DPS_END() != *pToken) {
2232 if (pshader_is_version_token(*pToken)) { /** version */
2233 TRACE("ps.%lu.%lu\n", (*pToken >> 8) & 0x0F, (*pToken & 0x0F));
2234 version = (((*pToken >> 8) & 0x0F) * 10) + (*pToken & 0x0F);
2235 ++pToken;
2236 ++len;
2237 continue;
2239 if (pshader_is_comment_token(*pToken)) { /** comment */
2240 DWORD comment_len = (*pToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
2241 ++pToken;
2242 /*TRACE("comment[%ld] ;%s\n", comment_len, (char*)pToken);*/
2243 pToken += comment_len;
2244 len += comment_len + 1;
2245 continue;
2247 code = *pToken;
2248 curOpcode = pshader_program_get_opcode(code, version);
2249 ++pToken;
2250 ++len;
2251 if (NULL == curOpcode) {
2252 /* unkown current opcode ... */
2253 while (*pToken & 0x80000000) {
2254 TRACE("unrecognized opcode: %08lx\n", *pToken);
2255 ++pToken;
2256 ++len;
2258 } else {
2259 TRACE(" ");
2260 pshader_program_dump_opcode(curOpcode, code, *pToken);
2261 if (curOpcode->num_params > 0) {
2262 pshader_program_dump_param(*pToken, 0);
2263 ++pToken;
2264 ++len;
2265 for (i = 1; i < curOpcode->num_params; ++i) {
2266 TRACE(", ");
2267 if (D3DSIO_DEF != code) {
2268 pshader_program_dump_param(*pToken, 1);
2269 } else {
2270 TRACE("%f", *((const float*) pToken));
2272 ++pToken;
2273 ++len;
2276 TRACE("\n");
2278 pshader->functionLength = (len + 1) * sizeof(DWORD);
2280 } else {
2281 pshader->functionLength = 1; /* no Function defined use fixed function vertex processing */
2284 if (NULL != pFunction) {
2285 IDirect3DPixelShaderImpl_GenerateProgramArbHW(pshader, pFunction);
2288 if (NULL != pFunction) {
2289 pshader->function = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, pshader->functionLength);
2290 memcpy(pshader->function, pFunction, pshader->functionLength);
2291 } else {
2292 pshader->function = NULL;
2296 HRESULT WINAPI IDirect3DDeviceImpl_CreatePixelShader(IDirect3DDevice8Impl* This, CONST DWORD* pFunction, IDirect3DPixelShaderImpl** ppPixelShader) {
2297 IDirect3DPixelShaderImpl* object;
2299 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DPixelShaderImpl));
2300 if (NULL == object) {
2301 *ppPixelShader = NULL;
2302 return D3DERR_OUTOFVIDEOMEMORY;
2304 /*object->lpVtbl = &Direct3DPixelShader9_Vtbl;*/
2305 object->device = This;
2306 object->ref = 1;
2308 object->data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PSHADERDATA8));
2310 IDirect3DPixelShaderImpl_ParseProgram(object, pFunction);
2312 *ppPixelShader = object;
2313 return D3D_OK;
2316 HRESULT WINAPI IDirect3DPixelShaderImpl_GetFunction(IDirect3DPixelShaderImpl* This, VOID* pData, UINT* pSizeOfData) {
2317 if (NULL == pData) {
2318 *pSizeOfData = This->functionLength;
2319 return D3D_OK;
2321 if (*pSizeOfData < This->functionLength) {
2322 *pSizeOfData = This->functionLength;
2323 return D3DERR_MOREDATA;
2325 if (NULL == This->function) { /* no function defined */
2326 TRACE("(%p) : GetFunction no User Function defined using NULL to %p\n", This, pData);
2327 (*(DWORD **) pData) = NULL;
2328 } else {
2329 TRACE("(%p) : GetFunction copying to %p\n", This, pData);
2330 memcpy(pData, This->function, This->functionLength);
2332 return D3D_OK;
2335 HRESULT WINAPI IDirect3DPixelShaderImpl_SetConstantF(IDirect3DPixelShaderImpl* This, UINT StartRegister, CONST FLOAT* pConstantData, UINT Vector4fCount) {
2336 if (StartRegister + Vector4fCount > D3D8_VSHADER_MAX_CONSTANTS) {
2337 return D3DERR_INVALIDCALL;
2339 if (NULL == This->data) { /* temporary while datas not supported */
2340 FIXME("(%p) : PixelShader_SetConstant not fully supported yet\n", This);
2341 return D3DERR_INVALIDCALL;
2343 memcpy(&This->data->C[StartRegister], pConstantData, Vector4fCount * 4 * sizeof(FLOAT));
2344 return D3D_OK;
2347 HRESULT WINAPI IDirect3DPixelShaderImpl_GetConstantF(IDirect3DPixelShaderImpl* This, UINT StartRegister, FLOAT* pConstantData, UINT Vector4fCount) {
2348 if (StartRegister + Vector4fCount > D3D8_VSHADER_MAX_CONSTANTS) {
2349 return D3DERR_INVALIDCALL;
2351 if (NULL == This->data) { /* temporary while datas not supported */
2352 return D3DERR_INVALIDCALL;
2354 memcpy(pConstantData, &This->data->C[StartRegister], Vector4fCount * 4 * sizeof(FLOAT));
2355 return D3D_OK;
2359 /**********************************************************************************************************************************************
2360 **********************************************************************************************************************************************
2361 **********************************************************************************************************************************************
2362 **********************************************************************************************************************************************
2363 **********************************************************************************************************************************************/
2365 /***********************************************************************
2366 * ValidateVertexShader (D3D8.@)
2368 * PARAMS
2369 * toto result?
2371 BOOL WINAPI ValidateVertexShader(LPVOID pFunction, int param1, int param2, LPVOID toto)
2373 FIXME("(void): stub: pFunction %p, param1 %d, param2 %d, result? %p\n", pFunction, param1, param2, toto);
2374 return 0;
2377 /***********************************************************************
2378 * ValidatePixelShader (D3D8.@)
2380 * PARAMS
2381 * toto result?
2383 BOOL WINAPI ValidatePixelShader(LPVOID pFunction, int param1, int param2, LPVOID toto)
2385 FIXME("(void): stub: pFunction %p, param1 %d, param2 %d, result? %p\n", pFunction, param1, param2, toto);
2386 return TRUE;