shader.c 84.5 KB
Newer Older
Raphael Junqueira's avatar
Raphael Junqueira committed
1 2 3
/*
 * shaders implementation
 *
4
 * Copyright 2002-2004 Raphael Junqueira
5 6
 * Copyright 2004 Jason Edmeades
 * Copyright 2004 Christian Costa
Raphael Junqueira's avatar
Raphael Junqueira committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

23 24
#include "config.h"

25 26
#include <math.h>
#include <stdarg.h>
27
#include <stdio.h>
28

Raphael Junqueira's avatar
Raphael Junqueira committed
29 30 31 32 33 34 35 36
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "wingdi.h"
#include "wine/debug.h"

#include "d3d8_private.h"

37
WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
38
WINE_DECLARE_DEBUG_CHANNEL(d3d_hw_shader);
Raphael Junqueira's avatar
Raphael Junqueira committed
39

40 41
/* Shader debugging - Change the following line to enable debugging of software
      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 46
#else 
# define VSTRACE(A) 
47
# define TRACE_VSVECTOR(name)
48 49
#endif

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/**
 * DirectX9 SDK download
 *  http://msdn.microsoft.com/library/default.asp?url=/downloads/list/directx.asp
 *
 * Exploring D3DX
 *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndrive/html/directx07162002.asp
 *
 * Using Vertex Shaders
 *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndrive/html/directx02192001.asp
 *
 * Dx9 New
 *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/whatsnew.asp
 *
 * Dx9 Shaders
 *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader2_0/VertexShader2_0.asp
 *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader2_0/Instructions/Instructions.asp
 *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/GettingStarted/VertexDeclaration/VertexDeclaration.asp
 *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader3_0/VertexShader3_0.asp
 *
 * Dx9 D3DX
 *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/advancedtopics/VertexPipe/matrixstack/matrixstack.asp
 *
 * FVF
73
 *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/GettingStarted/VertexFormats/vformats.asp
74 75 76 77 78 79
 *
 * NVIDIA: DX8 Vertex Shader to NV Vertex Program
 *  http://developer.nvidia.com/view.asp?IO=vstovp
 *
 * NVIDIA: Memory Management with VAR
 *  http://developer.nvidia.com/view.asp?IO=var_memory_management 
80
 */
Raphael Junqueira's avatar
Raphael Junqueira committed
81

82 83 84
typedef void (*shader_fct_t)();

typedef struct SHADER_OPCODE {
85
  CONST WORD    opcode;
86 87 88
  const char*   name;
  CONST UINT    num_params;
  shader_fct_t  soft_fct;
89 90
  DWORD         min_version;
  DWORD         max_version;
91 92
} SHADER_OPCODE;

93 94
/*******************************
 * vshader functions software VM
Raphael Junqueira's avatar
Raphael Junqueira committed
95 96
 */

97
void vshader_add(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
Raphael Junqueira's avatar
Raphael Junqueira committed
98 99 100 101
  d->x = s0->x + s1->x;
  d->y = s0->y + s1->y;
  d->z = s0->z + s1->z;
  d->w = s0->w + s1->w;
102 103
  VSTRACE(("executing add: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
	         s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
Raphael Junqueira's avatar
Raphael Junqueira committed
104 105
}

106
void vshader_dp3(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
Raphael Junqueira's avatar
Raphael Junqueira committed
107
  d->x = d->y = d->z = d->w = s0->x * s1->x + s0->y * s1->y + s0->z * s1->z;
108 109
  VSTRACE(("executing dp3: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
	         s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
Raphael Junqueira's avatar
Raphael Junqueira committed
110 111
}

112
void vshader_dp4(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
Raphael Junqueira's avatar
Raphael Junqueira committed
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 115
  VSTRACE(("executing dp4: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
	  s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
Raphael Junqueira's avatar
Raphael Junqueira committed
116 117
}

118 119
void vshader_dst(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
  d->x = 1.0f;
Raphael Junqueira's avatar
Raphael Junqueira committed
120 121 122
  d->y = s0->y * s1->y;
  d->z = s0->z;
  d->w = s1->w;
123 124
  VSTRACE(("executing dst: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
	  s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
Raphael Junqueira's avatar
Raphael Junqueira committed
125 126
}

127
void vshader_expp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
128 129 130 131 132 133 134 135 136 137 138 139
  union {
    float f;
    DWORD d;
  } tmp;

  tmp.f = floorf(s0->w);
  d->x  = powf(2.0f, tmp.f);
  d->y  = s0->w - tmp.f;

  tmp.f = powf(2.0f, s0->w);
  tmp.d &= 0xFFFFFF00U;
  d->z  = tmp.f;
140
  d->w  = 1.0f;
141 142
  VSTRACE(("executing exp: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
                s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
Raphael Junqueira's avatar
Raphael Junqueira committed
143 144
}

145 146 147 148 149
void vshader_lit(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
  d->x = 1.0f;
  d->y = (0.0f < s0->x) ? s0->x : 0.0f;
  d->z = (0.0f < s0->x && 0.0f < s0->y) ? powf(s0->y, s0->w) : 0.0f;
  d->w = 1.0f;
150 151
  VSTRACE(("executing lit: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
	         s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
Raphael Junqueira's avatar
Raphael Junqueira committed
152 153
}

154 155
void vshader_logp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
  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 158
  VSTRACE(("executing logp: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
	         s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
Raphael Junqueira's avatar
Raphael Junqueira committed
159 160
}

161
void vshader_mad(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1, D3DSHADERVECTOR* s2) {
Raphael Junqueira's avatar
Raphael Junqueira committed
162 163 164 165
  d->x = s0->x * s1->x + s2->x;
  d->y = s0->y * s1->y + s2->y;
  d->z = s0->z * s1->z + s2->z;
  d->w = s0->w * s1->w + s2->w;
166 167
  VSTRACE(("executing mad: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) s2=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
	  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));
Raphael Junqueira's avatar
Raphael Junqueira committed
168 169
}

170
void vshader_max(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
Raphael Junqueira's avatar
Raphael Junqueira committed
171 172 173 174
  d->x = (s0->x >= s1->x) ? s0->x : s1->x;
  d->y = (s0->y >= s1->y) ? s0->y : s1->y;
  d->z = (s0->z >= s1->z) ? s0->z : s1->z;
  d->w = (s0->w >= s1->w) ? s0->w : s1->w;
175 176
  VSTRACE(("executing max: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
	  s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
Raphael Junqueira's avatar
Raphael Junqueira committed
177 178
}

179
void vshader_min(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
Raphael Junqueira's avatar
Raphael Junqueira committed
180 181 182 183
  d->x = (s0->x < s1->x) ? s0->x : s1->x;
  d->y = (s0->y < s1->y) ? s0->y : s1->y;
  d->z = (s0->z < s1->z) ? s0->z : s1->z;
  d->w = (s0->w < s1->w) ? s0->w : s1->w;
184 185
  VSTRACE(("executing min: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
	  s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
Raphael Junqueira's avatar
Raphael Junqueira committed
186 187
}

188
void vshader_mov(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
Raphael Junqueira's avatar
Raphael Junqueira committed
189 190 191 192
  d->x = s0->x;
  d->y = s0->y;
  d->z = s0->z;
  d->w = s0->w;
193 194
  VSTRACE(("executing mov: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
	  s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
Raphael Junqueira's avatar
Raphael Junqueira committed
195 196
}

197
void vshader_mul(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
Raphael Junqueira's avatar
Raphael Junqueira committed
198 199 200 201
  d->x = s0->x * s1->x;
  d->y = s0->y * s1->y;
  d->z = s0->z * s1->z;
  d->w = s0->w * s1->w;
202 203
  VSTRACE(("executing mul: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
	  s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
Raphael Junqueira's avatar
Raphael Junqueira committed
204 205 206 207 208 209
}

void vshader_nop(void) {
  /* 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 213
  VSTRACE(("executing rcp: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
	  s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
Raphael Junqueira's avatar
Raphael Junqueira committed
214 215
}

216 217
void vshader_rsq(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
  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 220
  VSTRACE(("executing rsq: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
	  s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
Raphael Junqueira's avatar
Raphael Junqueira committed
221 222
}

223 224 225 226 227
void vshader_sge(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
  d->x = (s0->x >= s1->x) ? 1.0f : 0.0f;
  d->y = (s0->y >= s1->y) ? 1.0f : 0.0f;
  d->z = (s0->z >= s1->z) ? 1.0f : 0.0f;
  d->w = (s0->w >= s1->w) ? 1.0f : 0.0f;
228 229
  VSTRACE(("executing sge: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
	  s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
Raphael Junqueira's avatar
Raphael Junqueira committed
230 231
}

232 233 234 235 236
void vshader_slt(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
  d->x = (s0->x < s1->x) ? 1.0f : 0.0f;
  d->y = (s0->y < s1->y) ? 1.0f : 0.0f;
  d->z = (s0->z < s1->z) ? 1.0f : 0.0f;
  d->w = (s0->w < s1->w) ? 1.0f : 0.0f;
237 238
  VSTRACE(("executing slt: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
	  s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
Raphael Junqueira's avatar
Raphael Junqueira committed
239 240
}

241
void vshader_sub(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
Raphael Junqueira's avatar
Raphael Junqueira committed
242 243 244 245
  d->x = s0->x - s1->x;
  d->y = s0->y - s1->y;
  d->z = s0->z - s1->z;
  d->w = s0->w - s1->w;
246 247
  VSTRACE(("executing sub: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
	  s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
Raphael Junqueira's avatar
Raphael Junqueira committed
248 249
}

250 251 252 253 254 255
/**
 * Version 1.1 specific
 */

void vshader_exp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
  d->x = d->y = d->z = d->w = powf(2.0f, s0->w);
256 257
  VSTRACE(("executing exp: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
	  s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
258 259 260 261
}

void vshader_log(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
  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 264
  VSTRACE(("executing log: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
	  s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
265 266 267 268 269 270 271
}

void vshader_frc(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
  d->x = s0->x - floorf(s0->x);
  d->y = s0->y - floorf(s0->y);
  d->z = 0.0f;
  d->w = 1.0f;
272 273
  VSTRACE(("executing frc: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
	  s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
}

typedef FLOAT D3DMATRIX44[4][4];
typedef FLOAT D3DMATRIX43[4][3];
typedef FLOAT D3DMATRIX34[4][4];
typedef FLOAT D3DMATRIX33[4][3];
typedef FLOAT D3DMATRIX32[4][2];

void vshader_m4x4(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, /*D3DSHADERVECTOR* mat1*/ D3DMATRIX44 mat) {
  /*
   * BuGGY CODE: here only if cast not work for copy/paste
  D3DSHADERVECTOR* mat2 = mat1 + 1;
  D3DSHADERVECTOR* mat3 = mat1 + 2;
  D3DSHADERVECTOR* mat4 = mat1 + 3; 
  d->x = mat1->x * s0->x + mat2->x * s0->y + mat3->x * s0->z + mat4->x * s0->w;
  d->y = mat1->y * s0->x + mat2->y * s0->y + mat3->y * s0->z + mat4->y * s0->w;
  d->z = mat1->z * s0->x + mat2->z * s0->y + mat3->z * s0->z + mat4->z * s0->w;
  d->w = mat1->w * s0->x + mat2->w * s0->y + mat3->w * s0->z + mat4->w * s0->w;
  */
  d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z + mat[0][3] * s0->w;
  d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z + mat[1][3] * s0->w;
  d->z = mat[2][0] * s0->x + mat[2][1] * s0->y + mat[2][2] * s0->z + mat[2][3] * s0->w;
  d->w = mat[3][0] * s0->x + mat[3][1] * s0->y + mat[3][2] * s0->z + mat[3][3] * s0->w;
297 298 299 300
  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));
  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));
  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));
  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));
301 302 303 304 305 306 307
}

void vshader_m4x3(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DMATRIX43 mat) {
  d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z + mat[0][3] * s0->w;
  d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z + mat[1][3] * s0->w;
  d->z = mat[2][0] * s0->x + mat[2][1] * s0->y + mat[2][2] * s0->z + mat[2][3] * s0->w;
  d->w = 1.0f;
308 309 310 311
  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));
  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));
  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));
  VSTRACE(("executing m4x3(4):                            (%f)       (%f) \n", s0->w, d->w));
312 313 314 315
}

void vshader_m3x4(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DMATRIX34 mat) {
  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 318
  d->z = mat[2][0] * s0->x + mat[2][1] * s0->y + mat[2][2] * s0->z;
  d->w = mat[3][0] * s0->x + mat[3][1] * s0->y + mat[3][2] * s0->z;
319 320 321 322
  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));
  VSTRACE(("executing m3x4(2): mat=(%f, %f, %f)       (%f)       (%f) \n", mat[1][0], mat[1][1], mat[1][2], s0->y, d->y));
  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));
  VSTRACE(("executing m3x4(4): mat=(%f, %f, %f)       (%f)       (%f) \n", mat[3][0], mat[3][1], mat[3][2], s0->w, d->w));
323 324 325
}

void vshader_m3x3(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DMATRIX33 mat) {
326 327
  d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z;
  d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z;
328 329
  d->z = mat[2][0] * s0->x + mat[2][1] * s0->y + mat[2][2] * s0->z;
  d->w = 1.0f;
330 331 332 333
  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));
  VSTRACE(("executing m3x3(2): mat=(%f, %f, %f)       (%f)       (%f) \n", mat[1][0], mat[1][1], mat[1][2], s0->y, d->y));
  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));
  VSTRACE(("executing m3x3(4):                                       (%f) \n", d->w));
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
}

void vshader_m3x2(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DMATRIX32 mat) {
  FIXME("check\n");
  d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z;
  d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z;
  d->z = 0.0f;
  d->w = 1.0f;
}

/**
 * Version 2.0 specific
 */
void vshader_lrp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1, D3DSHADERVECTOR* s2, D3DSHADERVECTOR* s3) {
  d->x = s0->x * (s1->x - s2->x) + s2->x; 
  d->y = s0->y * (s1->y - s2->y) + s2->y; 
  d->z = s0->z * (s1->z - s2->z) + s2->z; 
  d->w = s0->w * (s1->w - s2->w) + s2->x; 
}

Raphael Junqueira's avatar
Raphael Junqueira committed
354 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
Raphael Junqueira's avatar
Raphael Junqueira committed
357
 */
358
static CONST SHADER_OPCODE vshader_ins [] = {
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
  {D3DSIO_NOP,  "NOP",  0, vshader_nop, 0, 0},
  {D3DSIO_MOV,  "MOV",  2, vshader_mov, 0, 0},
  {D3DSIO_ADD,  "ADD",  3, vshader_add, 0, 0},
  {D3DSIO_SUB,  "SUB",  3, vshader_sub, 0, 0},
  {D3DSIO_MAD,  "MAD",  4, vshader_mad, 0, 0},
  {D3DSIO_MUL,  "MUL",  3, vshader_mul, 0, 0},
  {D3DSIO_RCP,  "RCP",  2, vshader_rcp, 0, 0},
  {D3DSIO_RSQ,  "RSQ",  2, vshader_rsq, 0, 0},
  {D3DSIO_DP3,  "DP3",  3, vshader_dp3, 0, 0},
  {D3DSIO_DP4,  "DP4",  3, vshader_dp4, 0, 0},
  {D3DSIO_MIN,  "MIN",  3, vshader_min, 0, 0},
  {D3DSIO_MAX,  "MAX",  3, vshader_max, 0, 0},
  {D3DSIO_SLT,  "SLT",  3, vshader_slt, 0, 0},
  {D3DSIO_SGE,  "SGE",  3, vshader_sge, 0, 0},
  {D3DSIO_EXP,  "EXP",  2, vshader_exp, 0, 0},
  {D3DSIO_LOG,  "LOG",  2, vshader_log, 0, 0},
  {D3DSIO_LIT,  "LIT",  2, vshader_lit, 0, 0},
  {D3DSIO_DST,  "DST",  3, vshader_dst, 0, 0},
  {D3DSIO_LRP,  "LRP",  5, vshader_lrp, 0, 0},
  {D3DSIO_FRC,  "FRC",  2, vshader_frc, 0, 0},
  {D3DSIO_M4x4, "M4X4", 3, vshader_m4x4, 0, 0},
  {D3DSIO_M4x3, "M4X3", 3, vshader_m4x3, 0, 0},
  {D3DSIO_M3x4, "M3X4", 3, vshader_m3x4, 0, 0},
  {D3DSIO_M3x3, "M3X3", 3, vshader_m3x3, 0, 0},
  {D3DSIO_M3x2, "M3X2", 3, vshader_m3x2, 0, 0},
384
  /** FIXME: use direct access so add the others opcodes as stubs */
385 386
  {D3DSIO_EXPP, "EXPP", 2, vshader_expp, 0, 0},
  {D3DSIO_LOGP, "LOGP", 2, vshader_logp, 0, 0},
387

388
  {0, NULL, 0, NULL, 0, 0}
389 390 391
};


392
inline static const SHADER_OPCODE* vshader_program_get_opcode(const DWORD code) {
393
  DWORD i = 0;
394
  /** TODO: use dichotomic search */
395 396 397 398 399 400
  while (NULL != vshader_ins[i].name) {
    if ((code & D3DSI_OPCODE_MASK) == vshader_ins[i].opcode) {
      return &vshader_ins[i];
    }
    ++i;
  }
Raphael Junqueira's avatar
Raphael Junqueira committed
401 402 403
  return NULL;
}

404 405 406 407 408 409 410 411
inline static BOOL vshader_is_version_token(DWORD token) {
  return 0xFFFE0000 == (token & 0xFFFE0000);
}

inline static BOOL vshader_is_comment_token(DWORD token) {
  return D3DSIO_COMMENT == (token & D3DSI_OPCODE_MASK);
}

412
inline static void vshader_program_dump_param(const DWORD param, int input) {
413 414 415 416 417 418
  static const char* rastout_reg_names[] = { "oPos", "oFog", "oPts" };
  static const char swizzle_reg_chars[] = "xyzw";

  DWORD reg = param & 0x00001FFF;
  DWORD regtype = ((param & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT);

419
  if ((param & D3DSP_SRCMOD_MASK) == D3DSPSM_NEG) TRACE("-");
420 421 422
  
  switch (regtype << D3DSP_REGTYPE_SHIFT) {
  case D3DSPR_TEMP:
423
    TRACE("R[%lu]", reg);
424 425
    break;
  case D3DSPR_INPUT:
426
    TRACE("V[%lu]", reg);
427 428
    break;
  case D3DSPR_CONST:
429
    TRACE("C[%s%lu]", (param & D3DVS_ADDRMODE_RELATIVE) ? "a0.x + " : "", reg);
430 431
    break;
  case D3DSPR_ADDR: /*case D3DSPR_TEXTURE:*/
432
    TRACE("a[%lu]", reg);
433 434
    break;
  case D3DSPR_RASTOUT:
435
    TRACE("%s", rastout_reg_names[reg]);
436 437
    break;
  case D3DSPR_ATTROUT:
438
    TRACE("oD[%lu]", reg);
439 440
    break;
  case D3DSPR_TEXCRDOUT:
441
    TRACE("oT[%lu]", reg);
442 443 444 445 446 447 448 449
    break;
  default:
    break;
  }

  if (!input) {
    /** operand output */
    if ((param & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
450 451 452 453
      if (param & D3DSP_WRITEMASK_0) TRACE(".x");
      if (param & D3DSP_WRITEMASK_1) TRACE(".y");
      if (param & D3DSP_WRITEMASK_2) TRACE(".z");
      if (param & D3DSP_WRITEMASK_3) TRACE(".w");
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    }
  } else {
    /** operand input */
    DWORD swizzle = (param & D3DVS_SWIZZLE_MASK) >> D3DVS_SWIZZLE_SHIFT;
    DWORD swizzle_x = swizzle & 0x03;
    DWORD swizzle_y = (swizzle >> 2) & 0x03;
    DWORD swizzle_z = (swizzle >> 4) & 0x03;
    DWORD swizzle_w = (swizzle >> 6) & 0x03;
    /**
     * swizzle bits fields:
     *  WWZZYYXX
     */
    if ((D3DVS_NOSWIZZLE >> D3DVS_SWIZZLE_SHIFT) != swizzle) { /* ! D3DVS_NOSWIZZLE == 0xE4 << D3DVS_SWIZZLE_SHIFT */
      if (swizzle_x == swizzle_y && 
	  swizzle_x == swizzle_z && 
	  swizzle_x == swizzle_w) {
470
	TRACE(".%c", swizzle_reg_chars[swizzle_x]);
471
      } else {
472
	TRACE(".%c%c%c%c", 
473 474 475 476 477 478 479 480 481
		swizzle_reg_chars[swizzle_x], 
		swizzle_reg_chars[swizzle_y], 
		swizzle_reg_chars[swizzle_z], 
		swizzle_reg_chars[swizzle_w]);
      }
    }
  }
}

482 483 484 485
inline static void vshader_program_add_param(const DWORD param, int input, char *hwLine) {
  /*static const char* rastout_reg_names[] = { "oPos", "oFog", "oPts" }; */
  static const char* hwrastout_reg_names[] = { "result.position", "result.fogcoord", "result.pointsize" };
  static const char swizzle_reg_chars[] = "xyzw";
486

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
  DWORD reg = param & 0x00001FFF;
  DWORD regtype = ((param & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT);
  char  tmpReg[255];

  if ((param & D3DSP_SRCMOD_MASK) == D3DSPSM_NEG) {
      strcat(hwLine, " -");
  } else {
      strcat(hwLine, " ");
  }
  
  switch (regtype << D3DSP_REGTYPE_SHIFT) {
  case D3DSPR_TEMP:
    sprintf(tmpReg, "T%lu", reg);
    strcat(hwLine, tmpReg);
    break;
  case D3DSPR_INPUT:
    sprintf(tmpReg, "vertex.attrib[%lu]", reg);
    strcat(hwLine, tmpReg);
    break;
  case D3DSPR_CONST:
507
    sprintf(tmpReg, "C[%s%lu]", (param & D3DVS_ADDRMODE_RELATIVE) ? "A0.x + " : "", reg);
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    strcat(hwLine, tmpReg);
    break;
  case D3DSPR_ADDR: /*case D3DSPR_TEXTURE:*/
    sprintf(tmpReg, "A%lu", reg);
    strcat(hwLine, tmpReg);
    break;
  case D3DSPR_RASTOUT:
    sprintf(tmpReg, "%s", hwrastout_reg_names[reg]);
    strcat(hwLine, tmpReg);
    break;
  case D3DSPR_ATTROUT:
    if (reg==0) {
       strcat(hwLine, "result.color.primary");
    } else {
       strcat(hwLine, "result.color.secondary");
    }
    break;
  case D3DSPR_TEXCRDOUT:
    sprintf(tmpReg, "result.texcoord[%lu]", reg);
    strcat(hwLine, tmpReg);
    break;
  default:
    break;
  }

  if (!input) {
    /** operand output */
    if ((param & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
      strcat(hwLine, ".");
      if (param & D3DSP_WRITEMASK_0) {
          strcat(hwLine, "x");
      }
      if (param & D3DSP_WRITEMASK_1) {
          strcat(hwLine, "y");
      }
      if (param & D3DSP_WRITEMASK_2) {
          strcat(hwLine, "z");
      }
      if (param & D3DSP_WRITEMASK_3) {
          strcat(hwLine, "w");
      }
    }
  } else {
    /** operand input */
    DWORD swizzle = (param & D3DVS_SWIZZLE_MASK) >> D3DVS_SWIZZLE_SHIFT;
    DWORD swizzle_x = swizzle & 0x03;
    DWORD swizzle_y = (swizzle >> 2) & 0x03;
    DWORD swizzle_z = (swizzle >> 4) & 0x03;
    DWORD swizzle_w = (swizzle >> 6) & 0x03;
    /**
     * swizzle bits fields:
     *  WWZZYYXX
     */
    if ((D3DVS_NOSWIZZLE >> D3DVS_SWIZZLE_SHIFT) != swizzle) { /* ! D3DVS_NOSWIZZLE == 0xE4 << D3DVS_SWIZZLE_SHIFT */
      if (swizzle_x == swizzle_y && 
	  swizzle_x == swizzle_z && 
	  swizzle_x == swizzle_w) {
        sprintf(tmpReg, ".%c", swizzle_reg_chars[swizzle_x]);
        strcat(hwLine, tmpReg);
      } else {
        sprintf(tmpReg, ".%c%c%c%c", 
		swizzle_reg_chars[swizzle_x], 
		swizzle_reg_chars[swizzle_y], 
		swizzle_reg_chars[swizzle_z], 
		swizzle_reg_chars[swizzle_w]);
        strcat(hwLine, tmpReg);
      }
    }
  }
577 578
}

579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
DWORD MacroExpansion[4*4];

int ExpandMxMacro(DWORD macro_opcode, const DWORD* args) {
  int i;
  int nComponents = 0;
  DWORD opcode =0;
  switch(macro_opcode) {
    case D3DSIO_M4x4:
      nComponents = 4;
      opcode = D3DSIO_DP4;
      break;
    case D3DSIO_M4x3:
      nComponents = 3;
      opcode = D3DSIO_DP4;
      break;
    case D3DSIO_M3x4:
      nComponents = 4;
      opcode = D3DSIO_DP3;
      break;
    case D3DSIO_M3x3:
      nComponents = 3;
      opcode = D3DSIO_DP3;
      break;
    case D3DSIO_M3x2:
      nComponents = 2;
      opcode = D3DSIO_DP3;
      break;
    default:
      break;
  }
  for (i = 0; i < nComponents; i++) {
    MacroExpansion[i*4+0] = opcode;
    MacroExpansion[i*4+1] = ((*args) & ~D3DSP_WRITEMASK_ALL)|(D3DSP_WRITEMASK_0<<i);
    MacroExpansion[i*4+2] = *(args+1);
    MacroExpansion[i*4+3] = (*(args+2))+i;
  }
  return nComponents;
}	

Raphael Junqueira's avatar
Raphael Junqueira committed
618 619 620
/**
 * Function parser ...
 */
621 622 623 624 625 626
inline static VOID IDirect3DVertexShaderImpl_GenerateProgramArbHW(IDirect3DVertexShaderImpl* vshader, CONST DWORD* pFunction) {
  const DWORD* pToken = pFunction;
  const DWORD* pSavedToken = NULL;
  const SHADER_OPCODE* curOpcode = NULL;
  int nRemInstr = -1;
  DWORD i;
627
  unsigned lineNum = 0;
628 629
  char *pgmStr = NULL;
  char  tmpLine[255];
630 631 632 633
  DWORD nUseAddressRegister = 0;
  DWORD nUseTempRegister = 0;
  DWORD regtype;
  DWORD reg;
634 635 636 637
  IDirect3DDevice8Impl* This = vshader->device;

  pgmStr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 65535); /* 64kb should be enough */

638 639 640
  /**
   * First pass to determine what we need to declare:
   *  - Temporary variables
641
   *  - Address variables
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
   */ 
  if (NULL != pToken) {
    while (D3DVS_END() != *pToken) {
      if (vshader_is_version_token(*pToken)) {
	/** skip version */
	++pToken;
	continue;
      } 
      if (vshader_is_comment_token(*pToken)) { /** comment */
	DWORD comment_len = (*pToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
	++pToken;
	pToken += comment_len;
	continue;
      }
      curOpcode = vshader_program_get_opcode(*pToken);
      ++pToken;
      if (NULL == curOpcode) {
	while (*pToken & 0x80000000) {
	  /* skip unrecognized opcode */
	  ++pToken;
	}
      } else {
	if (curOpcode->num_params > 0) {
	  regtype = ((((*pToken) & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT) << D3DSP_REGTYPE_SHIFT);
	  reg = ((*pToken)  & 0x00001FFF);
	  /** we should validate GL_MAX_PROGRAM_ADDRESS_REGISTERS_AR limits here */
	  if (D3DSPR_ADDR == regtype && nUseAddressRegister <= reg) nUseAddressRegister = reg + 1;
	  /** we should validate GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB limits here */
	  if (D3DSPR_TEMP == regtype && nUseTempRegister <= reg) nUseTempRegister = reg + 1;
	  ++pToken;
	  for (i = 1; i < curOpcode->num_params; ++i) {
	    regtype = ((((*pToken) & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT) << D3DSP_REGTYPE_SHIFT);
	    reg = ((*pToken)  & 0x00001FFF);
	    /** we should validate GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB limits here */
	    if (D3DSPR_TEMP == regtype && nUseTempRegister <= reg) nUseTempRegister = reg + 1;
	    ++pToken;
	  }
	}
      }
    }
  }

  /** second pass, now generate */
  pToken = pFunction;

687
  if (NULL != pToken) {
688
    while (1) {
689 690 691 692 693 694
      tmpLine[0] = 0;

      if ((nRemInstr >= 0) && (--nRemInstr == -1))
        /* Macro is finished, continue normal path */ 
        pToken = pSavedToken;

695 696 697
      if (D3DVS_END() == *pToken)
        break;

698 699 700 701 702
      if (vshader_is_version_token(*pToken)) { /** version */

        /* Extract version *10 into integer value (ie. 1.0 == 10, 1.1==11 etc */
        int version = (((*pToken >> 8) & 0x0F) * 10) + (*pToken & 0x0F);
        int numTemps;
703
        int numConstants;
704

705
	TRACE_(d3d_hw_shader)("vs.%lu.%lu;\n", (*pToken >> 8) & 0x0F, (*pToken & 0x0F));
706 707 708 709 710

        /* Each release of vertex shaders has had different numbers of temp registers */
        switch (version) {
        case 10:
        case 11: numTemps=12; 
711
                 numConstants=96;
712
                 strcpy(tmpLine, "!!ARBvp1.0\n");
Francois Gouget's avatar
Francois Gouget committed
713
                 TRACE_(d3d_hw_shader)("GL HW (%u) : %s", strlen(pgmStr), tmpLine); /* Don't add \n to this line as already in tmpLine */
714 715
                 break;
        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");
Francois Gouget's avatar
Francois Gouget committed
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 722
        case 30: numTemps=32; 
                 numConstants=256;
723
                 strcpy(tmpLine, "!!ARBvp3.0\n");
724
                 FIXME_(d3d_hw_shader)("No work done yet to support vs3.0 in hw\n");
Francois Gouget's avatar
Francois Gouget committed
725
                 TRACE_(d3d_hw_shader)("GL HW (%u) : %s", strlen(pgmStr), tmpLine); /* Don't add \n to this line as already in tmpLine */
726 727 728
                 break;
        default:
                 numTemps=12;
729
                 numConstants=96;
730
                 strcpy(tmpLine, "!!ARBvp1.0\n");
731
                 FIXME_(d3d_hw_shader)("Unrecognized vertex shader version!\n");
732 733
        }
        strcat(pgmStr,tmpLine);
734
	++lineNum;
735

736
        for (i = 0; i < nUseTempRegister/*we should check numTemps here*/; i++) {
737
            sprintf(tmpLine, "TEMP T%ld;\n", i);
738
	    ++lineNum;
Francois Gouget's avatar
Francois Gouget committed
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 741
            strcat(pgmStr,tmpLine);
        }
742 743 744
	for (i = 0; i < nUseAddressRegister; i++) {
            sprintf(tmpLine, "ADDRESS A%ld;\n", i);
	    ++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 747
            strcat(pgmStr,tmpLine);
	}
748 749
	/* Due to the dynamic constants binding mechanism, we need to declare
	 * all the constants for relative addressing. */
750 751 752 753 754 755 756
	/* Mesa supports nly 95 constants for VS1.X although we should have at least 96. */
	if (This->direct3d8->gl_info.gl_vendor == VENDOR_MESA || 
	    This->direct3d8->gl_info.gl_vendor == VENDOR_WINE) {
	  numConstants = 95;
	}
	sprintf(tmpLine, "PARAM C[%d] = { program.env[0..%d] };\n", numConstants, numConstants-1);
	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;
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774

	++pToken;
	continue;
      } 
      if (vshader_is_comment_token(*pToken)) { /** comment */
	DWORD comment_len = (*pToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
	++pToken;
	/*TRACE("comment[%ld] ;%s\n", comment_len, (char*)pToken);*/
	pToken += comment_len;
	continue;
      }
      curOpcode = vshader_program_get_opcode(*pToken);
      ++pToken;
      if (NULL == curOpcode) {
	/* unkown current opcode ... */
	while (*pToken & 0x80000000) {
775
	  TRACE_(d3d_hw_shader)("unrecognized opcode: %08lx\n", *pToken);
776 777 778 779 780
	  ++pToken;
	}
      } else {
        /* Build opcode for GL vertex_program */
        switch (curOpcode->opcode) {
781 782
        case D3DSIO_NOP: 
            continue;
783
        case D3DSIO_MOV:
784 785 786 787 788 789 790
	    /* Address registers must be loaded with the ARL instruction */
	    if (((*pToken) & D3DSP_REGTYPE_MASK) == D3DSPR_ADDR) {
		if (0 < nUseAddressRegister) {
		    strcpy(tmpLine, "ARL");
		    break;
		} else
		   FIXME_(d3d_hw_shader)("Try to load an undeclared address register!\n");
791
	    }
792
	    /* fall through */
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
        case D3DSIO_ADD: 
        case D3DSIO_SUB: 
        case D3DSIO_MAD: 
        case D3DSIO_MUL: 
        case D3DSIO_RCP: 
        case D3DSIO_RSQ: 
        case D3DSIO_DP3: 
        case D3DSIO_DP4: 
        case D3DSIO_MIN: 
        case D3DSIO_MAX: 
        case D3DSIO_SLT: 
        case D3DSIO_SGE:
        case D3DSIO_LIT: 
        case D3DSIO_DST:
        case D3DSIO_FRC:
            strcpy(tmpLine, curOpcode->name); 
            break;

        case D3DSIO_EXPP:
            strcpy(tmpLine, "EXP"); 
            break;
        case D3DSIO_LOGP:
            strcpy(tmpLine, "LOG"); 
            break;
        case D3DSIO_EXP: 
            strcpy(tmpLine, "EX2"); 
            break;
        case D3DSIO_LOG: 
            strcpy(tmpLine, "LG2"); 
            break;

        case D3DSIO_M4x4:
        case D3DSIO_M4x3:
        case D3DSIO_M3x4:
        case D3DSIO_M3x3:
        case D3DSIO_M3x2:
            /* Expand the macro and get number of generated instruction */
            nRemInstr = ExpandMxMacro(curOpcode->opcode, pToken);
            /* Save point to next instruction */
            pSavedToken = pToken + 3;
            /* Execute expanded macro */
            pToken = MacroExpansion;
            continue;

        default:
Francois Gouget's avatar
Francois Gouget committed
838
            FIXME_(d3d_hw_shader)("Can't handle opcode %s in hwShader\n", curOpcode->name);
839 840 841 842 843 844 845 846 847 848 849 850 851
        }

	if (curOpcode->num_params > 0) {
	  vshader_program_add_param(*pToken, 0, tmpLine);
          
	  ++pToken;
	  for (i = 1; i < curOpcode->num_params; ++i) {
            strcat(tmpLine, ",");
	    vshader_program_add_param(*pToken, 1, tmpLine);
	    ++pToken;
	  }
	}
        strcat(tmpLine,";\n");
852
	++lineNum;
Francois Gouget's avatar
Francois Gouget committed
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 855 856 857
        strcat(pgmStr, tmpLine);
      }
    }
    strcpy(tmpLine, "END\n"); 
858
    ++lineNum;
Francois Gouget's avatar
Francois Gouget committed
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 861 862 863 864
    strcat(pgmStr, tmpLine);
  }
  
  /*  Create the hw shader */
  GL_EXTCALL(glGenProgramsARB(1, &vshader->prgId));
865
  TRACE_(d3d_hw_shader)("Creating a hw vertex shader, prg=%d\n", vshader->prgId);
866 867 868 869 870

  GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vshader->prgId));

  /* Create the program and check for errors */
  GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(pgmStr), pgmStr));
871 872 873 874 875
  if (glGetError() == GL_INVALID_OPERATION) {
    GLint errPos;
    glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
    FIXME_(d3d_hw_shader)("HW VertexShader Error at position: %d\n%s\n", errPos, glGetString(GL_PROGRAM_ERROR_STRING_ARB));
    vshader->prgId = -1;
876 877 878 879 880 881
  }
  
  HeapFree(GetProcessHeap(), 0, pgmStr);
}

inline static VOID IDirect3DVertexShaderImpl_ParseProgram(IDirect3DVertexShaderImpl* vshader, CONST DWORD* pFunction, int useHW) {
882
  const DWORD* pToken = pFunction;
883
  const SHADER_OPCODE* curOpcode = NULL;
884 885 886 887 888
  DWORD len = 0;  
  DWORD i;

  if (NULL != pToken) {
    while (D3DVS_END() != *pToken) {
889
      if (vshader_is_version_token(*pToken)) { /** version */
890
	TRACE("vs.%lu.%lu\n", (*pToken >> 8) & 0x0F, (*pToken & 0x0F));
891 892 893 894 895 896 897
	++pToken;
	++len;
	continue;
      } 
      if (vshader_is_comment_token(*pToken)) { /** comment */
	DWORD comment_len = (*pToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
	++pToken;
898
	/*TRACE("comment[%ld] ;%s\n", comment_len, (char*)pToken);*/
899 900 901 902
	pToken += comment_len;
	len += comment_len + 1;
	continue;
      }
903 904 905 906 907 908
      curOpcode = vshader_program_get_opcode(*pToken);
      ++pToken;
      ++len;
      if (NULL == curOpcode) {
	/* unkown current opcode ... */
	while (*pToken & 0x80000000) {
909
	  TRACE("unrecognized opcode: %08lx\n", *pToken);
910 911 912 913
	  ++pToken;
	  ++len;
	}
      } else {
914
	TRACE("%s ", curOpcode->name);
915 916 917 918 919
	if (curOpcode->num_params > 0) {
	  vshader_program_dump_param(*pToken, 0);
	  ++pToken;
	  ++len;
	  for (i = 1; i < curOpcode->num_params; ++i) {
920
	    TRACE(", ");
921 922 923 924 925
	    vshader_program_dump_param(*pToken, 1);
	    ++pToken;
	    ++len;
	  }
	}
926
	TRACE("\n");
927 928
      }
    }
929
    vshader->functionLength = (len + 1) * sizeof(DWORD);
930 931 932
  } else {
    vshader->functionLength = 1; /* no Function defined use fixed function vertex processing */
  }
933

934
  /* Generate HW shader in needed */
935
  if (useHW && NULL != pFunction) {
936
    IDirect3DVertexShaderImpl_GenerateProgramArbHW(vshader, pFunction);
937 938
  }

939
  /* copy the function ... because it will certainly be released by application */
940 941 942 943 944 945 946 947 948 949
  if (NULL != pFunction) {
    vshader->function = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, vshader->functionLength);
    memcpy(vshader->function, pFunction, vshader->functionLength);
  } else {
    vshader->function = NULL;
  }
}

HRESULT WINAPI IDirect3DDeviceImpl_CreateVertexShader(IDirect3DDevice8Impl* This, CONST DWORD* pFunction, DWORD Usage, IDirect3DVertexShaderImpl** ppVertexShader) {
  IDirect3DVertexShaderImpl* object;
950 951
  int useHW;
  
952 953 954 955 956 957 958 959 960 961
  object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DVertexShaderImpl));
  if (NULL == object) {
    *ppVertexShader = NULL;
    return D3DERR_OUTOFVIDEOMEMORY;
  }
  /*object->lpVtbl = &Direct3DVextexShader9_Vtbl;*/
  object->device = This; /* FIXME: AddRef(This) */
  object->ref = 1;
  
  object->usage = Usage;
962
  object->data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(VSHADERDATA8));
963 964 965 966 967 968

  useHW = (((vs_mode == VS_HW) && GL_SUPPORT(ARB_VERTEX_PROGRAM)) &&
            This->devType != D3DDEVTYPE_REF &&
            object->usage != D3DUSAGE_SOFTWAREPROCESSING);
  
  IDirect3DVertexShaderImpl_ParseProgram(object, pFunction, useHW);
969 970 971

  *ppVertexShader = object;
  return D3D_OK;
Raphael Junqueira's avatar
Raphael Junqueira committed
972 973
}

974
BOOL IDirect3DVertexShaderImpl_ExecuteHAL(IDirect3DVertexShaderImpl* vshader, VSHADERINPUTDATA8* input, VSHADEROUTPUTDATA8* output) {
Raphael Junqueira's avatar
Raphael Junqueira committed
975
  /** 
976 977
   * TODO: use the NV_vertex_program (or 1_1) extension 
   *  and specifics vendors (ARB_vertex_program??) variants for it 
Raphael Junqueira's avatar
Raphael Junqueira committed
978 979 980 981
   */
  return TRUE;
}

982
HRESULT WINAPI IDirect3DVertexShaderImpl_ExecuteSW(IDirect3DVertexShaderImpl* vshader, VSHADERINPUTDATA8* input, VSHADEROUTPUTDATA8* output) {
Raphael Junqueira's avatar
Raphael Junqueira committed
983
  /** Vertex Shader Temporary Registers */
984 985 986
  D3DSHADERVECTOR R[12];
  /*D3DSHADERSCALAR A0;*/
  D3DSHADERVECTOR A[1];
Raphael Junqueira's avatar
Raphael Junqueira committed
987
  /** temporary Vector for modifier management */
988 989
  D3DSHADERVECTOR d;
  D3DSHADERVECTOR s[3];
Raphael Junqueira's avatar
Raphael Junqueira committed
990 991
  /** parser datas */
  const DWORD* pToken = vshader->function;
992
  const SHADER_OPCODE* curOpcode = NULL;
993
  /** functions parameters */
994 995
  D3DSHADERVECTOR* p[4];
  D3DSHADERVECTOR* p_send[4];
996 997
  DWORD i;

998
  /** init temporary register */
999
  memset(R, 0, 12 * sizeof(D3DSHADERVECTOR));
1000

1001
  /* vshader_program_parse(vshader); */
1002
#if 0 /* Must not be 1 in cvs */
1003
  TRACE("Input:\n");
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
  TRACE_VSVECTOR(vshader->data->C[0]);
  TRACE_VSVECTOR(vshader->data->C[1]);
  TRACE_VSVECTOR(vshader->data->C[2]);
  TRACE_VSVECTOR(vshader->data->C[3]);
  TRACE_VSVECTOR(vshader->data->C[4]);
  TRACE_VSVECTOR(vshader->data->C[5]);
  TRACE_VSVECTOR(vshader->data->C[6]);
  TRACE_VSVECTOR(vshader->data->C[7]);
  TRACE_VSVECTOR(vshader->data->C[8]);
  TRACE_VSVECTOR(vshader->data->C[64]);
  TRACE_VSVECTOR(input->V[D3DVSDE_POSITION]);
  TRACE_VSVECTOR(input->V[D3DVSDE_BLENDWEIGHT]);
  TRACE_VSVECTOR(input->V[D3DVSDE_BLENDINDICES]);
  TRACE_VSVECTOR(input->V[D3DVSDE_NORMAL]);
  TRACE_VSVECTOR(input->V[D3DVSDE_PSIZE]);
  TRACE_VSVECTOR(input->V[D3DVSDE_DIFFUSE]);
  TRACE_VSVECTOR(input->V[D3DVSDE_SPECULAR]);
  TRACE_VSVECTOR(input->V[D3DVSDE_TEXCOORD0]);
  TRACE_VSVECTOR(input->V[D3DVSDE_TEXCOORD1]);
1023
#endif
1024

1025
  TRACE_VSVECTOR(vshader->data->C[64]);
1026

Raphael Junqueira's avatar
Raphael Junqueira committed
1027 1028 1029
  /* the first dword is the version tag */
  /* TODO: parse it */
  
1030 1031 1032
  if (vshader_is_version_token(*pToken)) { /** version */
    ++pToken;
  }
1033
  while (D3DVS_END() != *pToken) {
1034 1035 1036 1037 1038 1039
    if (vshader_is_comment_token(*pToken)) { /** comment */
      DWORD comment_len = (*pToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
      ++pToken;
      pToken += comment_len;
      continue ;
    }
1040
    curOpcode = vshader_program_get_opcode(*pToken);
Raphael Junqueira's avatar
Raphael Junqueira committed
1041 1042
    ++pToken;
    if (NULL == curOpcode) {
1043
      i = 0;
Raphael Junqueira's avatar
Raphael Junqueira committed
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));
1048
	}
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 1054 1055 1056
	++pToken;
      }
      /*return FALSE;*/
    } 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 1060 1061 1062 1063 1064
	for (i = 0; i < curOpcode->num_params; ++i) {
	  DWORD reg = pToken[i] & 0x00001FFF;
	  DWORD regtype = ((pToken[i] & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT);

	  switch (regtype << D3DSP_REGTYPE_SHIFT) {
	  case D3DSPR_TEMP:
1065
	    /*TRACE("p[%d]=R[%d]\n", i, reg);*/
1066 1067 1068
	    p[i] = &R[reg];
	    break;
	  case D3DSPR_INPUT:
1069
	    /*TRACE("p[%d]=V[%s]\n", i, VertexShaderDeclRegister[reg]);*/
1070 1071 1072
	    p[i] = &input->V[reg];
	    break;
	  case D3DSPR_CONST:
1073
	    if (pToken[i] & D3DVS_ADDRMODE_RELATIVE) {
1074 1075 1076 1077 1078 1079
	      p[i] = &vshader->data->C[(DWORD) A[0].x + reg];
	    } else {
	      p[i] = &vshader->data->C[reg];
	    }
	    break;
	  case D3DSPR_ADDR: /*case D3DSPR_TEXTURE:*/
1080 1081 1082 1083
	    if (0 != reg) {
	      ERR("cannot handle address registers != a0, forcing use of a0\n");
	      reg = 0;
	    }
1084
	    /*TRACE("p[%d]=A[%d]\n", i, reg);*/
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
	    p[i] = &A[reg];
	    break;
	  case D3DSPR_RASTOUT:
	    switch (reg) {
	    case D3DSRO_POSITION:
	      p[i] = &output->oPos;
	      break;
	    case D3DSRO_FOG:
	      p[i] = &output->oFog;
	      break;
	    case D3DSRO_POINT_SIZE:
	      p[i] = &output->oPts;
	      break;
	    }
	    break;
	  case D3DSPR_ATTROUT:
1101
	    /*TRACE("p[%d]=oD[%d]\n", i, reg);*/
1102 1103 1104
	    p[i] = &output->oD[reg];
	    break;
	  case D3DSPR_TEXCRDOUT:
1105
	    /*TRACE("p[%d]=oT[%d]\n", i, reg);*/
1106 1107 1108 1109 1110 1111
	    p[i] = &output->oT[reg];
	    break;
	  default:
	    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);
1115 1116

	    if (!isNegative && (D3DVS_NOSWIZZLE >> D3DVS_SWIZZLE_SHIFT) == swizzle) {
1117
	      /*TRACE("p[%d] not swizzled\n", i);*/
1118 1119
	      p_send[i] = p[i];
	    } else {
1120 1121 1122 1123
	      DWORD swizzle_x = swizzle & 0x03;
	      DWORD swizzle_y = (swizzle >> 2) & 0x03;
	      DWORD swizzle_z = (swizzle >> 4) & 0x03;
	      DWORD swizzle_w = (swizzle >> 6) & 0x03;
1124
	      /*TRACE("p[%d] swizzled\n", i);*/
1125
	      float* tt = (float*) p[i];
1126 1127 1128 1129
	      s[i].x = (isNegative) ? -tt[swizzle_x] : tt[swizzle_x];
	      s[i].y = (isNegative) ? -tt[swizzle_y] : tt[swizzle_y];
	      s[i].z = (isNegative) ? -tt[swizzle_z] : tt[swizzle_z];
	      s[i].w = (isNegative) ? -tt[swizzle_w] : tt[swizzle_w];
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
	      p_send[i] = &s[i];
	    }
	  } else { /* output reg */
	    if ((pToken[i] & D3DSP_WRITEMASK_ALL) == D3DSP_WRITEMASK_ALL) {
	      p_send[i] = p[i];
	    } else {
	      p_send[i] = &d; /* to be post-processed for modifiers management */
	    }
	  }
	}      
      }

      switch (curOpcode->num_params) {	
      case 0:
	curOpcode->soft_fct();
	break;
      case 1:
	curOpcode->soft_fct(p_send[0]);
	break;
      case 2:
	curOpcode->soft_fct(p_send[0], p_send[1]);
	break;
      case 3:
	curOpcode->soft_fct(p_send[0], p_send[1], p_send[2]);
	break;
      case 4:
	curOpcode->soft_fct(p_send[0], p_send[1], p_send[2], p_send[3]);
	break;
1158 1159 1160
      case 5:
	curOpcode->soft_fct(p_send[0], p_send[1], p_send[2], p_send[3], p_send[4]);
	break;
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
      default:
	ERR("%s too many params: %u\n", curOpcode->name, curOpcode->num_params);
      }

      /* check if output reg modifier post-process */
      if (curOpcode->num_params > 0 && (pToken[0] & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
	if (pToken[0] & D3DSP_WRITEMASK_0) p[0]->x = d.x; 
	if (pToken[0] & D3DSP_WRITEMASK_1) p[0]->y = d.y; 
	if (pToken[0] & D3DSP_WRITEMASK_2) p[0]->z = d.z; 
	if (pToken[0] & D3DSP_WRITEMASK_3) p[0]->w = d.w; 
      }
      
1173
#if 0
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
      TRACE_VSVECTOR(output->oPos);
      TRACE_VSVECTOR(output->oD[0]);
      TRACE_VSVECTOR(output->oD[1]);
      TRACE_VSVECTOR(output->oT[0]);
      TRACE_VSVECTOR(output->oT[1]);
      TRACE_VSVECTOR(R[0]);
      TRACE_VSVECTOR(R[1]);
      TRACE_VSVECTOR(R[2]);
      TRACE_VSVECTOR(R[3]);
      TRACE_VSVECTOR(R[4]);
      TRACE_VSVECTOR(R[5]);
1185
#endif
1186

1187 1188
      /* to next opcode token */
      pToken += curOpcode->num_params;
Raphael Junqueira's avatar
Raphael Junqueira committed
1189
    }
1190
#if 0
1191
    TRACE("End of current instruction:\n");
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
    TRACE_VSVECTOR(output->oPos);
    TRACE_VSVECTOR(output->oD[0]);
    TRACE_VSVECTOR(output->oD[1]);
    TRACE_VSVECTOR(output->oT[0]);
    TRACE_VSVECTOR(output->oT[1]);
    TRACE_VSVECTOR(R[0]);
    TRACE_VSVECTOR(R[1]);
    TRACE_VSVECTOR(R[2]);
    TRACE_VSVECTOR(R[3]);
    TRACE_VSVECTOR(R[4]);
    TRACE_VSVECTOR(R[5]);
1203
#endif
Raphael Junqueira's avatar
Raphael Junqueira committed
1204
  }
1205
#if 0 /* Must not be 1 in cvs */
1206
    TRACE("Output:\n");
1207 1208 1209 1210 1211
    TRACE_VSVECTOR(output->oPos);
    TRACE_VSVECTOR(output->oD[0]);
    TRACE_VSVECTOR(output->oD[1]);
    TRACE_VSVECTOR(output->oT[0]);
    TRACE_VSVECTOR(output->oT[1]);
1212
#endif
1213
  return D3D_OK;
Raphael Junqueira's avatar
Raphael Junqueira committed
1214 1215
}

1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
HRESULT WINAPI IDirect3DVertexShaderImpl_GetFunction(IDirect3DVertexShaderImpl* This, VOID* pData, UINT* pSizeOfData) {
  if (NULL == pData) {
    *pSizeOfData = This->functionLength;
    return D3D_OK;
  }
  if (*pSizeOfData < This->functionLength) {
    *pSizeOfData = This->functionLength;
    return D3DERR_MOREDATA;
  }
  if (NULL == This->function) { /* no function defined */
    TRACE("(%p) : GetFunction no User Function defined using NULL to %p\n", This, pData);
    (*(DWORD **) pData) = NULL;
  } else {
    TRACE("(%p) : GetFunction copying to %p\n", This, pData);
    memcpy(pData, This->function, This->functionLength);
1231
  }
1232
  return D3D_OK;
1233 1234
}

1235 1236 1237 1238 1239 1240 1241
HRESULT WINAPI IDirect3DVertexShaderImpl_SetConstantF(IDirect3DVertexShaderImpl* This, UINT StartRegister, CONST FLOAT* pConstantData, UINT Vector4fCount) {
  if (StartRegister + Vector4fCount > D3D8_VSHADER_MAX_CONSTANTS) {
    return D3DERR_INVALIDCALL;
  }
  if (NULL == This->data) { /* temporary while datas not supported */
    FIXME("(%p) : VertexShader_SetConstant not fully supported yet\n", This);
    return D3DERR_INVALIDCALL;
Raphael Junqueira's avatar
Raphael Junqueira committed
1242
  }
1243 1244
  memcpy(&This->data->C[StartRegister], pConstantData, Vector4fCount * 4 * sizeof(FLOAT));
  return D3D_OK;
Raphael Junqueira's avatar
Raphael Junqueira committed
1245 1246
}

1247 1248 1249 1250 1251 1252
HRESULT WINAPI IDirect3DVertexShaderImpl_GetConstantF(IDirect3DVertexShaderImpl* This, UINT StartRegister, FLOAT* pConstantData, UINT Vector4fCount) {
  if (StartRegister + Vector4fCount > D3D8_VSHADER_MAX_CONSTANTS) {
    return D3DERR_INVALIDCALL;
  }
  if (NULL == This->data) { /* temporary while datas not supported */
    return D3DERR_INVALIDCALL;
1253
  }
1254 1255
  memcpy(pConstantData, &This->data->C[StartRegister], Vector4fCount * 4 * sizeof(FLOAT));
  return D3D_OK;
1256 1257 1258
}


1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
/**********************************************************************************************************************************************
 **********************************************************************************************************************************************
 **********************************************************************************************************************************************
 **********************************************************************************************************************************************
 **********************************************************************************************************************************************/

void pshader_texcoord(D3DSHADERVECTOR* d) {
}

void pshader_texkill(D3DSHADERVECTOR* d) {
}

void pshader_tex(D3DSHADERVECTOR* d) {
}

void pshader_texbem(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
}

void pshader_texbeml(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
}

void pshader_texreg2ar(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
}

void pshader_texreg2gb(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
}

void pshader_texm3x2pad(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
}

void pshader_texm3x2tex(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
}

void pshader_texm3x3pad(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
}

void pshader_texm3x3tex(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
}

void pshader_texm3x3diff(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
}

void pshader_texm3x3spec(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
}

void pshader_texm3x3vspec(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
}

void pshader_cnd(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1, D3DSHADERVECTOR* s2) {
}

void pshader_def(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1, D3DSHADERVECTOR* s2, D3DSHADERVECTOR* s3) {
}

void pshader_texreg2rgb(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
}

void pshader_texdp3tex(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
}

void pshader_texm3x2depth(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
}

void pshader_texdp3(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
}

void pshader_texm3x3(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
}

void pshader_texdepth(D3DSHADERVECTOR* d) {
}

void pshader_cmp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1, D3DSHADERVECTOR* s2) {
}

void pshader_bem(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
}

static CONST SHADER_OPCODE pshader_ins [] = {
  {D3DSIO_NOP,  "nop",  0, vshader_nop, 0, 0},
  {D3DSIO_MOV,  "mov",  2, vshader_mov, 0, 0},
  {D3DSIO_ADD,  "add",  3, vshader_add, 0, 0},
  {D3DSIO_SUB,  "sub",  3, vshader_sub, 0, 0},
  {D3DSIO_MAD,  "mad",  4, vshader_mad, 0, 0},
  {D3DSIO_MUL,  "mul",  3, vshader_mul, 0, 0},
  {D3DSIO_RCP,  "rcp",  2, vshader_rcp, 0, 0},
  {D3DSIO_RSQ,  "rsq",  2, vshader_rsq, 0, 0},
  {D3DSIO_DP3,  "dp3",  3, vshader_dp3, 0, 0},
  {D3DSIO_DP4,  "dp4",  3, vshader_dp4, 0, 0},
  {D3DSIO_MIN,  "min",  3, vshader_min, 0, 0},
  {D3DSIO_MAX,  "max",  3, vshader_max, 0, 0},
  {D3DSIO_SLT,  "slt",  3, vshader_slt, 0, 0},
  {D3DSIO_SGE,  "sge",  3, vshader_sge, 0, 0},
  {D3DSIO_EXP,  "exp",  2, vshader_exp, 0, 0},
  {D3DSIO_LOG,  "log",  2, vshader_log, 0, 0},
  {D3DSIO_LIT,  "lit",  2, vshader_lit, 0, 0},
  {D3DSIO_DST,  "dst",  3, vshader_dst, 0, 0},
1356
  {D3DSIO_LRP,  "lrp",  4, vshader_lrp, 0, 0},
1357 1358 1359 1360 1361 1362 1363
  {D3DSIO_FRC,  "frc",  2, vshader_frc, 0, 0},
  {D3DSIO_M4x4, "m4x4", 3, vshader_m4x4, 0, 0},
  {D3DSIO_M4x3, "m4x3", 3, vshader_m4x3, 0, 0},
  {D3DSIO_M3x4, "m3x4", 3, vshader_m3x4, 0, 0},
  {D3DSIO_M3x3, "m3x3", 3, vshader_m3x3, 0, 0},
  {D3DSIO_M3x2, "m3x2", 3, vshader_m3x2, 0, 0},

1364 1365 1366 1367 1368 1369 1370
  {D3DSIO_TEXCOORD,     "texcoord",     1, pshader_texcoord,     D3DPS_VERSION(1,0), D3DPS_VERSION(1,3)},
  {D3DSIO_TEXCOORD,     "texcrd",       2, pshader_texcoord,     D3DPS_VERSION(1,4), D3DPS_VERSION(1,4)},
  {D3DSIO_TEXKILL,      "texkill",      1, pshader_texkill,      D3DPS_VERSION(1,0), D3DPS_VERSION(1,4)},
  {D3DSIO_TEX,          "tex",          1, pshader_tex,          D3DPS_VERSION(1,0), D3DPS_VERSION(1,3)}, 
  {D3DSIO_TEX,          "texld",        2, pshader_tex,          D3DPS_VERSION(1,4), D3DPS_VERSION(1,4)}, 
  {D3DSIO_TEXBEM,       "texbem",       2, pshader_texbem,       D3DPS_VERSION(1,0), D3DPS_VERSION(1,3)}, 
  {D3DSIO_TEXBEML,      "texbeml",      2, pshader_texbeml,      D3DPS_VERSION(1,0), D3DPS_VERSION(1,3)}, 
1371 1372
  {D3DSIO_TEXREG2AR,    "texreg2ar",    2, pshader_texreg2ar,    D3DPS_VERSION(1,1), D3DPS_VERSION(1,3)}, 
  {D3DSIO_TEXREG2GB,    "texreg2gb",    2, pshader_texreg2gb,    D3DPS_VERSION(1,2), D3DPS_VERSION(1,3)}, 
1373 1374 1375 1376
  {D3DSIO_TEXM3x2PAD,   "texm3x2pad",   2, pshader_texm3x2pad,   D3DPS_VERSION(1,0), D3DPS_VERSION(1,3)}, 
  {D3DSIO_TEXM3x2TEX,   "texm3x2tex",   2, pshader_texm3x2tex,   D3DPS_VERSION(1,0), D3DPS_VERSION(1,3)}, 
  {D3DSIO_TEXM3x3PAD,   "texm3x3pad",   2, pshader_texm3x3pad,   D3DPS_VERSION(1,0), D3DPS_VERSION(1,3)}, 
  {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 1379
  {D3DSIO_TEXM3x3SPEC,  "texm3x3spec",  3, pshader_texm3x3spec,  D3DPS_VERSION(1,0), D3DPS_VERSION(1,3)}, 
  {D3DSIO_TEXM3x3VSPEC, "texm3x3vspec", 2, pshader_texm3x3vspec, D3DPS_VERSION(1,0), D3DPS_VERSION(1,3)}, 
1380 1381 1382 1383 1384

  {D3DSIO_EXPP,         "expp",         2, vshader_expp, 0, 0},
  {D3DSIO_LOGP,         "logp",         2, vshader_logp, 0, 0},

  {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 1387 1388 1389 1390 1391 1392 1393 1394 1395
  {D3DSIO_TEXREG2RGB,   "texbreg2rgb",  2, pshader_texreg2rgb,   D3DPS_VERSION(1,2), D3DPS_VERSION(1,3)}, 
  
  {D3DSIO_TEXDP3TEX,    "texdp3tex",    2, pshader_texdp3tex,    D3DPS_VERSION(1,2), D3DPS_VERSION(1,3)}, 
  {D3DSIO_TEXM3x2DEPTH, "texm3x2depth", 2, pshader_texm3x2depth, D3DPS_VERSION(1,3), D3DPS_VERSION(1,3)}, 
  {D3DSIO_TEXDP3,       "texdp3",       2, pshader_texdp3,       D3DPS_VERSION(1,2), D3DPS_VERSION(1,3)}, 
  {D3DSIO_TEXM3x3,      "texm3x3",      2, pshader_texm3x3,      D3DPS_VERSION(1,2), D3DPS_VERSION(1,3)},
  {D3DSIO_TEXDEPTH,     "texdepth",     1, pshader_texdepth,     D3DPS_VERSION(1,4), D3DPS_VERSION(1,4)},
  {D3DSIO_CMP,          "cmp",          4, pshader_cmp,          D3DPS_VERSION(1,1), D3DPS_VERSION(3,0)}, 
  {D3DSIO_BEM,          "bem",          3, pshader_bem,          D3DPS_VERSION(1,4), D3DPS_VERSION(1,4)}, 

1396 1397
  {D3DSIO_PHASE,  "phase",  0, vshader_nop, 0, 0},

1398 1399 1400
  {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 1405
  /** TODO: use dichotomic search */
  while (NULL != pshader_ins[i].name) {
1406 1407 1408
    if ( ( (code & D3DSI_OPCODE_MASK) == pshader_ins[i].opcode) &&
         ( ( (hex_version >= pshader_ins[i].min_version) && (hex_version <= pshader_ins[i].max_version)) ||
	   ( (pshader_ins[i].min_version == 0) && (pshader_ins[i].max_version == 0) ) ) ) {
1409 1410 1411 1412 1413 1414 1415
      return &pshader_ins[i];
    }
    ++i;
  }
  return NULL;
}

1416 1417 1418 1419 1420 1421 1422 1423
inline static BOOL pshader_is_version_token(DWORD token) {
  return 0xFFFF0000 == (token & 0xFFFF0000);
}

inline static BOOL pshader_is_comment_token(DWORD token) {
  return D3DSIO_COMMENT == (token & D3DSI_OPCODE_MASK);
}

1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
inline static void pshader_program_dump_opcode(const SHADER_OPCODE* curOpcode, const DWORD code, const DWORD output) {
  if (0 != (code & ~D3DSI_OPCODE_MASK)) {
    DWORD mask = (code & ~D3DSI_OPCODE_MASK);
    switch (mask) {
    case 0x40000000:    TRACE("+"); break;
    default:
      TRACE(" unhandled modifier(0x%08lx) ", mask);
    }
  }
  TRACE("%s", curOpcode->name);
  /**
   * normally this is a destination reg modifier 
   * but in pixel shaders asm code its specified as:
   *  dp3_x4 t1.rgba, r1, c1
   * or
   *  dp3_x2_sat r0, t0_bx2, v0_bx2
   * so for better debbuging i use the same norm
   */
  if (0 != (output & D3DSP_DSTSHIFT_MASK)) {
    DWORD shift = (output & D3DSP_DSTSHIFT_MASK) >> D3DSP_DSTSHIFT_SHIFT;
1444
    if (shift < 8) {
1445
      TRACE("_x%u", 1 << shift);
1446 1447
    } else {
      TRACE("_d%u", 1 << (16-shift));
1448
    }
1449
  }
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
  if (0 != (output & D3DSP_DSTMOD_MASK)) {
    DWORD mask = output & D3DSP_DSTMOD_MASK;
    switch (mask) {
    case D3DSPDM_SATURATE: TRACE("_sat"); break;
    default:
      TRACE("_unhandled_modifier(0x%08lx)", mask);
    }
  }
  TRACE(" ");
}

inline static void pshader_program_dump_param(const DWORD param, int input) {
  static const char* rastout_reg_names[] = { "oC0", "oC1", "oC2", "oC3", "oDepth" };
  static const char swizzle_reg_chars[] = "rgba";

  DWORD reg = param & 0x00001FFF;
  DWORD regtype = ((param & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT);

1468 1469 1470 1471 1472 1473 1474 1475
  if (input) {
    if ( ((param & D3DSP_SRCMOD_MASK) == D3DSPSM_NEG) ||
         ((param & D3DSP_SRCMOD_MASK) == D3DSPSM_BIASNEG) ||
         ((param & D3DSP_SRCMOD_MASK) == D3DSPSM_SIGNNEG) ||
         ((param & D3DSP_SRCMOD_MASK) == D3DSPSM_X2NEG) )
      TRACE("-");
    else if ((param & D3DSP_SRCMOD_MASK) == D3DSPSM_COMP)
      TRACE("1-");
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
  }
  
  switch (regtype << D3DSP_REGTYPE_SHIFT) {
  case D3DSPR_TEMP:
    TRACE("R[%lu]", reg);
    break;
  case D3DSPR_INPUT:
    TRACE("V[%lu]", reg);
    break;
  case D3DSPR_CONST:
1486
    TRACE("C[%s%lu]", (param & D3DVS_ADDRMODE_RELATIVE) ? "a0.x + " : "", reg);
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
    break;
  case D3DSPR_TEXTURE: /* case D3DSPR_ADDR: */
    TRACE("t[%lu]", reg);
    break;
  case D3DSPR_RASTOUT:
    TRACE("%s", rastout_reg_names[reg]);
    break;
  case D3DSPR_ATTROUT:
    TRACE("oD[%lu]", reg);
    break;
  case D3DSPR_TEXCRDOUT:
    TRACE("oT[%lu]", reg);
    break;
  default:
    break;
  }

  if (!input) {
    /** operand output */
    /**
     * for better debugging traces it's done into opcode dump code
     * @see pshader_program_dump_opcode
    if (0 != (param & D3DSP_DSTMOD_MASK)) {
      DWORD mask = param & D3DSP_DSTMOD_MASK;
      switch (mask) {
      case D3DSPDM_SATURATE: TRACE("_sat"); break;
      default:
      TRACE("_unhandled_modifier(0x%08lx)", mask);
      }
    }
    if (0 != (param & D3DSP_DSTSHIFT_MASK)) {
      DWORD shift = (param & D3DSP_DSTSHIFT_MASK) >> D3DSP_DSTSHIFT_SHIFT;
      if (shift > 0) {
	TRACE("_x%u", 1 << shift);
      }
    }
    */
    if ((param & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
1525 1526 1527 1528 1529
      TRACE(".");
      if (param & D3DSP_WRITEMASK_0) TRACE("r");
      if (param & D3DSP_WRITEMASK_1) TRACE("g");
      if (param & D3DSP_WRITEMASK_2) TRACE("b");
      if (param & D3DSP_WRITEMASK_3) TRACE("a");
1530 1531 1532 1533 1534 1535 1536 1537
    }
  } else {
    /** operand input */
    DWORD swizzle = (param & D3DSP_SWIZZLE_MASK) >> D3DSP_SWIZZLE_SHIFT;
    DWORD swizzle_x = swizzle & 0x03;
    DWORD swizzle_y = (swizzle >> 2) & 0x03;
    DWORD swizzle_z = (swizzle >> 4) & 0x03;
    DWORD swizzle_w = (swizzle >> 6) & 0x03;
1538

1539 1540 1541 1542 1543 1544 1545 1546
    if (0 != (param & D3DSP_SRCMOD_MASK)) {
      DWORD mask = param & D3DSP_SRCMOD_MASK;
      /*TRACE("_modifier(0x%08lx) ", mask);*/
      switch (mask) {
      case D3DSPSM_NONE:    break;
      case D3DSPSM_NEG:     break;
      case D3DSPSM_BIAS:    TRACE("_bias"); break;
      case D3DSPSM_BIASNEG: TRACE("_bias"); break;
1547 1548 1549
      case D3DSPSM_SIGN:    TRACE("_bx2"); break;
      case D3DSPSM_SIGNNEG: TRACE("_bx2"); break;
      case D3DSPSM_COMP:    break;
1550
      case D3DSPSM_X2:      TRACE("_x2"); break;
1551
      case D3DSPSM_X2NEG:   TRACE("_x2"); break;
1552 1553 1554 1555 1556 1557
      case D3DSPSM_DZ:      TRACE("_dz"); break;
      case D3DSPSM_DW:      TRACE("_dw"); break;
      default:
	TRACE("_unknown(0x%08lx)", mask);
      }
    }
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575

    /**
     * swizzle bits fields:
     *  WWZZYYXX
     */
    if ((D3DSP_NOSWIZZLE >> D3DSP_SWIZZLE_SHIFT) != swizzle) { /* ! D3DVS_NOSWIZZLE == 0xE4 << D3DVS_SWIZZLE_SHIFT */
      if (swizzle_x == swizzle_y && 
	  swizzle_x == swizzle_z && 
	  swizzle_x == swizzle_w) {
	TRACE(".%c", swizzle_reg_chars[swizzle_x]);
      } else {
	TRACE(".%c%c%c%c", 
	      swizzle_reg_chars[swizzle_x], 
	      swizzle_reg_chars[swizzle_y], 
	      swizzle_reg_chars[swizzle_z], 
	      swizzle_reg_chars[swizzle_w]);
      }
    }
1576 1577 1578
  }
}

1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
static int constants[D3D8_PSHADER_MAX_CONSTANTS];

inline static void get_register_name(const DWORD param, char* regstr)
{
  static const char* rastout_reg_names[] = { "oC0", "oC1", "oC2", "oC3", "oDepth" };

  DWORD reg = param & 0x00001FFF;
  DWORD regtype = ((param & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT);

  switch (regtype << D3DSP_REGTYPE_SHIFT) {
  case D3DSPR_TEMP:
    sprintf(regstr, "R%lu", reg);
    break;
  case D3DSPR_INPUT:
    if (reg==0) {
       strcpy(regstr, "fragment.color.primary");
    } else {
       strcpy(regstr, "fragment.color.secondary");
    }
    break;
  case D3DSPR_CONST:
    if (constants[reg])
      sprintf(regstr, "C%lu", reg);
    else
      sprintf(regstr, "program.env[%lu]", reg);
    break;
  case D3DSPR_TEXTURE: /* case D3DSPR_ADDR: */
    sprintf(regstr,"T%lu", reg);
    break;
  case D3DSPR_RASTOUT:
    sprintf(regstr, "%s", rastout_reg_names[reg]);
    break;
  case D3DSPR_ATTROUT:
    sprintf(regstr, "oD[%lu]", reg);
    break;
  case D3DSPR_TEXCRDOUT:
    sprintf(regstr, "oT[%lu]", reg);
    break;
  default:
    break;
  }
}	

inline static void addline(int* lineNum, char* pgm, char* line)
{
  ++(*lineNum);
  TRACE_(d3d_hw_shader)("GL HW (%u, %u) : %s\n", *lineNum, strlen(pgm), line);
  strcat(pgm, line);
  strcat(pgm, "\n");
}

char* shift_tab[] = {
  "dummy",     /*  0 (none) */ 
  "coefmul.x", /*  1 (x2)   */ 
  "coefmul.y", /*  2 (x4)   */ 
  "coefmul.z", /*  3 (x8)   */ 
  "coefmul.w", /*  4 (x16)  */ 
  "dummy",     /*  5 (x32)  */ 
  "dummy",     /*  6 (x64)  */ 
  "dummy",     /*  7 (x128) */ 
  "dummy",     /*  8 (d256) */ 
  "dummy",     /*  9 (d128) */ 
  "dummy",     /* 10 (d64)  */ 
  "dummy",     /* 11 (d32)  */ 
  "coefdiv.w", /* 12 (d16)  */ 
  "coefdiv.z", /* 13 (d8)   */ 
  "coefdiv.y", /* 14 (d4)   */ 
  "coefdiv.x"  /* 15 (d2)   */ 
};

inline static void get_write_mask(const DWORD output_reg, char* write_mask) 
{
  *write_mask = 0;
  if ((output_reg & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
1653 1654 1655 1656 1657
    strcat(write_mask, ".");
    if (output_reg & D3DSP_WRITEMASK_0) strcat(write_mask, "r");
    if (output_reg & D3DSP_WRITEMASK_1) strcat(write_mask, "g");
    if (output_reg & D3DSP_WRITEMASK_2) strcat(write_mask, "b");
    if (output_reg & D3DSP_WRITEMASK_3) strcat(write_mask, "a");
1658
  }
1659 1660
}

1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
inline static void get_input_register_swizzle(const DWORD instr, char* swzstring) 
{
    static const char swizzle_reg_chars[] = "rgba";
    DWORD swizzle = (instr & D3DSP_SWIZZLE_MASK) >> D3DSP_SWIZZLE_SHIFT;
    DWORD swizzle_x = swizzle & 0x03;
    DWORD swizzle_y = (swizzle >> 2) & 0x03;
    DWORD swizzle_z = (swizzle >> 4) & 0x03;
    DWORD swizzle_w = (swizzle >> 6) & 0x03;
    /**
     * swizzle bits fields:
     *  WWZZYYXX
     */
    *swzstring = 0;
    if ((D3DSP_NOSWIZZLE >> D3DSP_SWIZZLE_SHIFT) != swizzle) { /* ! D3DVS_NOSWIZZLE == 0xE4 << D3DVS_SWIZZLE_SHIFT */
      if (swizzle_x == swizzle_y && 
	  swizzle_x == swizzle_z && 
	  swizzle_x == swizzle_w) {
	sprintf(swzstring, ".%c", swizzle_reg_chars[swizzle_x]);
      } else {
	sprintf(swzstring, ".%c%c%c%c", 
	      swizzle_reg_chars[swizzle_x], 
	      swizzle_reg_chars[swizzle_y], 
	      swizzle_reg_chars[swizzle_z], 
	      swizzle_reg_chars[swizzle_w]);
      }
    }
}

inline static void gen_output_modifier_line(int saturate, char* write_mask, int shift, char *regstr, char* line)
{
  /* Generate a line that does the output modifier computation */
  sprintf(line, "MUL%s %s%s, %s, %s;", saturate ? "_SAT" : "", regstr, write_mask, regstr, shift_tab[shift]);
1693 1694
}

1695 1696 1697 1698
inline static int gen_input_modifier_line(const DWORD instr, int tmpreg, char* outregstr, char* line)
{
  /* Generate a line that does the input modifier computation and return the input register to use */
  static char regstr[256];
1699
  static char tmpline[256];
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
  int insert_line;
 
  /* Assume a new line will be added */
  insert_line = 1;
  
  /* Get register name */
  get_register_name(instr, regstr);
  
  switch (instr & D3DSP_SRCMOD_MASK) {
    case D3DSPSM_NONE:
      strcpy(outregstr, regstr);
      insert_line = 0;
      break;
    case D3DSPSM_NEG:
      sprintf(outregstr, "-%s", regstr);
      insert_line = 0;
      break;
    case D3DSPSM_BIAS:
      sprintf(line, "ADD T%c, %s, -coefdiv.x;", 'A' + tmpreg, regstr);
      break;
    case D3DSPSM_BIASNEG:
      sprintf(line, "ADD T%c, -%s, coefdiv.x;", 'A' + tmpreg, regstr);
      break;
    case D3DSPSM_SIGN:
      sprintf(line, "MAD T%c, %s, coefmul.x, -one.x;", 'A' + tmpreg, regstr);
      break;
    case D3DSPSM_SIGNNEG:
      sprintf(line, "MAD T%c, %s, -coefmul.x, one.x;", 'A' + tmpreg, regstr);
      break;
    case D3DSPSM_COMP:
      sprintf(line, "SUB T%c, one.x, %s;", 'A' + tmpreg, regstr);
      break;
    case D3DSPSM_X2:
1733
      sprintf(line, "ADD T%c, %s, %s;", 'A' + tmpreg, regstr, regstr);
1734 1735
      break;
    case D3DSPSM_X2NEG:
1736
      sprintf(line, "ADD T%c, -%s, -%s;", 'A' + tmpreg, regstr, regstr);
1737 1738
      break;
    case D3DSPSM_DZ:
1739 1740 1741 1742 1743
      sprintf(line, "RCP T%c, %s.z;", 'A' + tmpreg, regstr);
      sprintf(tmpline, "MUL T%c, %s, T%c;", 'A' + tmpreg, regstr, 'A' + tmpreg);
      strcat(line, "\n"); /* Hack */
      strcat(line, tmpline);
      break;
1744
    case D3DSPSM_DW:
1745 1746 1747 1748 1749
      sprintf(line, "RCP T%c, %s;", 'A' + tmpreg, regstr);
      sprintf(tmpline, "MUL T%c, %s, T%c;", 'A' + tmpreg, regstr, 'A' + tmpreg);
      strcat(line, "\n"); /* Hack */
      strcat(line, tmpline);
      break;
1750 1751 1752 1753 1754 1755 1756 1757 1758
    default:
      strcpy(outregstr, regstr);
      insert_line = 0;
  }

  if (insert_line) {
    /* Substitute the register name */
    sprintf(outregstr, "T%c", 'A' + tmpreg);
  }
1759

1760 1761
  return insert_line;
}
1762 1763 1764 1765 1766 1767 1768 1769 1770

/**
 * Pixel Shaders
 *
 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/PixelShader1_X/modifiers/sourceregistermodifiers.asp
 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/PixelShader2_0/Registers/Registers.asp
 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/d3d/interfaces/IDirect3DPixelShader9/_IDirect3DPixelShader9.asp
 *
 */
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
inline static VOID IDirect3DPixelShaderImpl_GenerateProgramArbHW(IDirect3DPixelShaderImpl* pshader, CONST DWORD* pFunction) {
  const DWORD* pToken = pFunction;
  const SHADER_OPCODE* curOpcode = NULL;
  const DWORD* pInstr;
  DWORD code;
  DWORD i;
  int autoparam;
  unsigned lineNum = 0;
  char *pgmStr = NULL;
  char  tmpLine[255];
  BOOL saturate;
1782 1783
  int row = 0;
  DWORD tcw[2];
1784
  IDirect3DDevice8Impl* This = pshader->device;
1785
  int version = 0;
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797

  for(i = 0; i < D3D8_PSHADER_MAX_CONSTANTS; i++)
    constants[i] = 0;
  
  pgmStr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 65535); /* 64kb should be enough */
  
  if (NULL != pToken) {
    while (D3DPS_END() != *pToken) { 
      if (pshader_is_version_token(*pToken)) { /** version */
        int numTemps;
        int numConstants;

1798 1799 1800
        /* Extract version *10 into integer value (ie. 1.0 == 10, 1.1==11 etc */
        version = (((*pToken >> 8) & 0x0F) * 10) + (*pToken & 0x0F);

1801 1802 1803 1804
	TRACE_(d3d_hw_shader)("ps.%lu.%lu;\n", (*pToken >> 8) & 0x0F, (*pToken & 0x0F));

        /* Each release of pixel shaders has had different numbers of temp registers */
        switch (version) {
1805 1806 1807 1808 1809
        case 10:
        case 11:
        case 12:
        case 13:
        case 14: numTemps=12;
1810 1811 1812 1813 1814 1815 1816 1817
                 numConstants=8;
                 strcpy(tmpLine, "!!ARBfp1.0");
                 break;
        case 20: numTemps=12;
                 numConstants=8;
                 strcpy(tmpLine, "!!ARBfp2.0");
                 FIXME_(d3d_hw_shader)("No work done yet to support ps2.0 in hw\n");
                 break;
1818
        case 30: numTemps=32;
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
                 numConstants=8;
                 strcpy(tmpLine, "!!ARBfp3.0");
                 FIXME_(d3d_hw_shader)("No work done yet to support ps3.0 in hw\n");
                 break;
        default:
                 numTemps=12;
                 numConstants=8;
                 strcpy(tmpLine, "!!ARBfp1.0");
                 FIXME_(d3d_hw_shader)("Unrecognized pixel shader version!\n");
        }
        addline(&lineNum, pgmStr, tmpLine);

1831
        for(i = 0; i < 6; i++) {
1832 1833 1834
          sprintf(tmpLine, "TEMP T%lu;", i);
          addline(&lineNum, pgmStr, tmpLine);
        }
1835
        for(i = 0; i < 6; i++) {
1836 1837 1838 1839 1840 1841
          sprintf(tmpLine, "TEMP R%lu;", i);
          addline(&lineNum, pgmStr, tmpLine);
        }

        sprintf(tmpLine, "TEMP TMP;");
        addline(&lineNum, pgmStr, tmpLine);
1842 1843
        sprintf(tmpLine, "TEMP TMP2;");
        addline(&lineNum, pgmStr, tmpLine);
1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
        sprintf(tmpLine, "TEMP TA;");
        addline(&lineNum, pgmStr, tmpLine);
        sprintf(tmpLine, "TEMP TB;");
        addline(&lineNum, pgmStr, tmpLine);
        sprintf(tmpLine, "TEMP TC;");
        addline(&lineNum, pgmStr, tmpLine);

        strcpy(tmpLine, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };");
        addline(&lineNum, pgmStr, tmpLine);
        strcpy(tmpLine, "PARAM coefmul = { 2, 4, 8, 16 };");
        addline(&lineNum, pgmStr, tmpLine);
        strcpy(tmpLine, "PARAM one = { 1.0, 1.0, 1.0, 1.0 };");
        addline(&lineNum, pgmStr, tmpLine);

        for(i = 0; i < 4; i++) {
          sprintf(tmpLine, "MOV T%lu, fragment.texcoord[%lu];", i, i);
          addline(&lineNum, pgmStr, tmpLine);
        }

        ++pToken;
        continue;
      } 
      if (pshader_is_comment_token(*pToken)) { /** comment */
        DWORD comment_len = (*pToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
        ++pToken;
        /*TRACE("comment[%ld] ;%s\n", comment_len, (char*)pToken);*/
        pToken += comment_len;
        continue;
      }
      code = *pToken;
      pInstr = pToken;
1875
      curOpcode = pshader_program_get_opcode(code, version);
1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887
      ++pToken;
      if (NULL == curOpcode) {
        /* unkown current opcode ... */
        while (*pToken & 0x80000000) {
          TRACE("unrecognized opcode: %08lx\n", *pToken);
          ++pToken;
        }
      } else {
        autoparam = 1;
        saturate = FALSE;
        /* Build opcode for GL vertex_program */
        switch (curOpcode->opcode) {
1888 1889 1890
        case D3DSIO_NOP: 
        case D3DSIO_PHASE: 
            continue;
1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
        case D3DSIO_DEF:
            {
              DWORD reg = *pToken & 0x00001FFF;
              sprintf(tmpLine, "PARAM C%lu = { %f, %f, %f, %f };", reg,
                              *((float*)(pToken+1)),
                              *((float*)(pToken+2)),
                              *((float*)(pToken+3)),
                              *((float*)(pToken+4)) );
              addline(&lineNum, pgmStr, tmpLine);
              constants[reg] = 1;
              autoparam = 0;
              pToken+=5;
            }
            break;
        case D3DSIO_TEXKILL:
            strcpy(tmpLine, "KIL");
            break;
        case D3DSIO_TEX:
            {
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929
              char tmp[20];
              get_write_mask(*pToken, tmp);
              if (version != 14) {
                DWORD reg = *pToken & 0x00001FFF;
                sprintf(tmpLine,"TEX T%lu%s, T%lu, texture[%lu], 2D;", reg, tmp, reg, reg);
                addline(&lineNum, pgmStr, tmpLine);
                autoparam = 0;
                pToken++;
              } else {
                char line[256];
                char reg[20];
                DWORD reg1 = *pToken & 0x00001FFF;
                DWORD reg2 = *(pToken+1) & 0x00001FFF;
                if (gen_input_modifier_line(*(pToken+1), 0, reg, line))
                  addline(&lineNum, pgmStr, line);
                sprintf(tmpLine,"TEX R%lu%s, %s, texture[%lu], 2D;", reg1, tmp, reg, reg2);
                addline(&lineNum, pgmStr, tmpLine);
                autoparam = 0;
                pToken += 2;
              }
1930 1931 1932 1933
            }
            break;
        case D3DSIO_TEXCOORD:
            {
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949
              char tmp[20];
              get_write_mask(*pToken, tmp);
              if (version != 14) {
                DWORD reg = *pToken & 0x00001FFF;
                sprintf(tmpLine, "MOV T%lu%s, fragment.texcoord[%lu];", reg, tmp, reg);
                addline(&lineNum, pgmStr, tmpLine);
                autoparam = 0;
                pToken++;
              } else {
                DWORD reg1 = *pToken & 0x00001FFF;
                DWORD reg2 = *(pToken+1) & 0x00001FFF;
                sprintf(tmpLine, "MOV R%lu%s, fragment.texcoord[%lu];", reg1, tmp, reg2);
                addline(&lineNum, pgmStr, tmpLine);
                autoparam = 0;
                pToken += 2;
              }
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
            }
            break;
        case D3DSIO_TEXM3x2PAD:
            {
              DWORD reg = *pToken & 0x00001FFF;
              char buf[50];
              if (gen_input_modifier_line(*(pToken+1), 0, buf, tmpLine))
                addline(&lineNum, pgmStr, tmpLine);
              sprintf(tmpLine, "DP3 TMP.x, T%lu, %s;", reg, buf);
              addline(&lineNum, pgmStr, tmpLine);
              autoparam = 0;
              pToken += 2;
            }
            break;
        case D3DSIO_TEXM3x2TEX:
            {
              DWORD reg = *pToken & 0x00001FFF;
              char buf[50];
              if (gen_input_modifier_line(*(pToken+1), 0, buf, tmpLine))
                addline(&lineNum, pgmStr, tmpLine);
              sprintf(tmpLine, "DP3 TMP.y, T%lu, %s;", reg, buf);
              addline(&lineNum, pgmStr, tmpLine);
              sprintf(tmpLine, "TEX T%lu, TMP, texture[%lu], 2D;", reg, reg);
              addline(&lineNum, pgmStr, tmpLine);
              autoparam = 0;
              pToken += 2;
            }
            break;
	case D3DSIO_TEXREG2AR:
            {
              DWORD reg1 = *pToken & 0x00001FFF;
              DWORD reg2 = *(pToken+1) & 0x00001FFF;
              sprintf(tmpLine, "MOV TMP.r, T%lu.a;", reg2);
              addline(&lineNum, pgmStr, tmpLine);
              sprintf(tmpLine, "MOV TMP.g, T%lu.r;", reg2);
              addline(&lineNum, pgmStr, tmpLine);
              sprintf(tmpLine, "TEX T%lu, TMP, texture[%lu], 2D;", reg1, reg1);
              addline(&lineNum, pgmStr, tmpLine);
              autoparam = 0;
              pToken+=2;
            }
            break;
        case D3DSIO_TEXREG2GB:
            {
              DWORD reg1 = *pToken & 0x00001FFF;
              DWORD reg2 = *(pToken+1) & 0x00001FFF;
              sprintf(tmpLine, "MOV TMP.r, T%lu.g;", reg2);
              addline(&lineNum, pgmStr, tmpLine);
              sprintf(tmpLine, "MOV TMP.g, T%lu.b;", reg2);
              addline(&lineNum, pgmStr, tmpLine);
              sprintf(tmpLine, "TEX T%lu, TMP, texture[%lu], 2D;", reg1, reg1);
              addline(&lineNum, pgmStr, tmpLine);
              autoparam = 0;
              pToken+=2;
            }
            break;
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104
	case D3DSIO_TEXBEM:
            {
              DWORD reg1 = *pToken & 0x00001FFF;
              DWORD reg2 = *(pToken+1) & 0x00001FFF;
              /* FIXME: Should apply the BUMPMAPENV matrix */
              sprintf(tmpLine, "ADD TMP.rg, fragment.texcoord[%lu], T%lu;", reg1, reg2);
              addline(&lineNum, pgmStr, tmpLine);
              sprintf(tmpLine, "TEX T%lu, TMP, texture[%lu], 2D;", reg1, reg1);
              addline(&lineNum, pgmStr, tmpLine);
              autoparam = 0;
              pToken+=2;
            }
            break;
        case D3DSIO_TEXM3x3PAD:
            {
              DWORD reg = *pToken & 0x00001FFF;
              char buf[50];
              if (gen_input_modifier_line(*(pToken+1), 0, buf, tmpLine))
                addline(&lineNum, pgmStr, tmpLine);
              sprintf(tmpLine, "DP3 TMP.%c, T%lu, %s;", 'x'+row, reg, buf);
              addline(&lineNum, pgmStr, tmpLine);
              tcw[row++] = reg;
              autoparam = 0;
              pToken += 2;
            }
            break;
        case D3DSIO_TEXM3x3TEX:
            {
              DWORD reg = *pToken & 0x00001FFF;
              char buf[50];
              if (gen_input_modifier_line(*(pToken+1), 0, buf, tmpLine))
                addline(&lineNum, pgmStr, tmpLine);
              sprintf(tmpLine, "DP3 TMP.z, T%lu, %s;", reg, buf);
              addline(&lineNum, pgmStr, tmpLine);
              /* Cubemap textures will be more used than 3D ones. */
              sprintf(tmpLine, "TEX T%lu, TMP, texture[%lu], CUBE;", reg, reg);
              addline(&lineNum, pgmStr, tmpLine);
              row = 0;
              autoparam = 0;
              pToken += 2;
            }
        case D3DSIO_TEXM3x3VSPEC:
            {
              DWORD reg = *pToken & 0x00001FFF;
              char buf[50];
              if (gen_input_modifier_line(*(pToken+1), 0, buf, tmpLine))
                addline(&lineNum, pgmStr, tmpLine);
              sprintf(tmpLine, "DP3 TMP.z, T%lu, %s;", reg, buf);
              addline(&lineNum, pgmStr, tmpLine);
              /* Construct the eye-ray vector from w coordinates */
              sprintf(tmpLine, "MOV TMP2.x, fragment.texcoord[%lu].w;", tcw[0]);
              addline(&lineNum, pgmStr, tmpLine);
              sprintf(tmpLine, "MOV TMP2.y, fragment.texcoord[%lu].w;", tcw[1]);
              addline(&lineNum, pgmStr, tmpLine);
              sprintf(tmpLine, "MOV TMP2.z, fragment.texcoord[%lu].w;", reg);
              addline(&lineNum, pgmStr, tmpLine);
              /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
              sprintf(tmpLine, "DP3 TMP.w, TMP, TMP2;");
              addline(&lineNum, pgmStr, tmpLine);
              sprintf(tmpLine, "MUL TMP, TMP.w, TMP;");
              addline(&lineNum, pgmStr, tmpLine);
              sprintf(tmpLine, "MAD TMP, coefmul.x, TMP, -TMP2;");
              addline(&lineNum, pgmStr, tmpLine);
              /* Cubemap textures will be more used than 3D ones. */
              sprintf(tmpLine, "TEX T%lu, TMP, texture[%lu], CUBE;", reg, reg);
              addline(&lineNum, pgmStr, tmpLine);
              row = 0;
              autoparam = 0;
              pToken += 2;
            }
            break;
        case D3DSIO_TEXM3x3SPEC:
            {
              DWORD reg = *pToken & 0x00001FFF;
              DWORD reg3 = *(pToken+2) & 0x00001FFF;
              char buf[50];
              if (gen_input_modifier_line(*(pToken+1), 0, buf, tmpLine))
                addline(&lineNum, pgmStr, tmpLine);
              sprintf(tmpLine, "DP3 TMP.z, T%lu, %s;", reg, buf);
              addline(&lineNum, pgmStr, tmpLine);
              /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
              sprintf(tmpLine, "DP3 TMP.w, TMP, C[%lu];", reg3);
              addline(&lineNum, pgmStr, tmpLine);
              sprintf(tmpLine, "MUL TMP, TMP.w, TMP;");
              addline(&lineNum, pgmStr, tmpLine);
              sprintf(tmpLine, "MAD TMP, coefmul.x, TMP, -C[%lu];", reg3);
              addline(&lineNum, pgmStr, tmpLine);	      
              /* Cubemap textures will be more used than 3D ones. */
              sprintf(tmpLine, "TEX T%lu, TMP, texture[%lu], CUBE;", reg, reg);
              addline(&lineNum, pgmStr, tmpLine);
              row = 0;
              autoparam = 0;
              pToken += 3;
            }
            break;
        case D3DSIO_CND:
	    break;
        case D3DSIO_CMP:
            break;
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
        case D3DSIO_MOV:
            strcpy(tmpLine, "MOV");
            break;
        case D3DSIO_MUL:
            strcpy(tmpLine, "MUL");
            break;
        case D3DSIO_DP3:
            strcpy(tmpLine, "DP3");
            break;
        case D3DSIO_MAD:
            strcpy(tmpLine, "MAD");
            break;
        case D3DSIO_ADD:
            strcpy(tmpLine, "ADD");
            break;
        case D3DSIO_SUB:
            strcpy(tmpLine, "SUB");
            break;
2123 2124 2125
        case D3DSIO_LRP:
            strcpy(tmpLine, "LRP");
            break;
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
        default:
            FIXME_(d3d_hw_shader)("Can't handle opcode %s in hwShader\n", curOpcode->name);
        }
        if (0 != (*pToken & D3DSP_DSTMOD_MASK)) {
          DWORD mask = *pToken & D3DSP_DSTMOD_MASK;
          switch (mask) {
          case D3DSPDM_SATURATE: saturate = TRUE; break;
          default:
            TRACE("_unhandled_modifier(0x%08lx)", mask);
          }
        }
        if (autoparam && (curOpcode->num_params > 0)) {
          char regs[3][50];
2139
          char operands[4][100];
2140 2141
          char tmp[256];
          char swzstring[20];
2142
          int saturate = 0;
2143 2144 2145 2146 2147 2148 2149
	  /* Generate lines that handle input modifier computation */
          for (i = 1; i < curOpcode->num_params; i++) {
            if (gen_input_modifier_line(*(pToken+i), i-1, regs[i-1], tmp))
              addline(&lineNum, pgmStr, tmp);
          }
	  /* Handle saturation only when no shift is present in the output modifier */
          if ((*pToken & D3DSPDM_SATURATE) && (0 == (*pToken & D3DSP_DSTSHIFT_MASK)))
2150
            saturate = 1;
2151 2152
	  /* Handle output register */
          get_register_name(*pToken, tmp);
2153
          strcpy(operands[0], tmp);
2154
          get_write_mask(*pToken, tmp);
2155
          strcat(operands[0], tmp);
2156 2157
	  /* Handle input registers */
          for (i = 1; i < curOpcode->num_params; i++) {
2158
            strcpy(operands[i], regs[i-1]);
2159
            get_input_register_swizzle(*(pToken+i), swzstring);
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177
            strcat(operands[i], swzstring);
          }
	  if (curOpcode->opcode == D3DSIO_CMP) {
            sprintf(tmpLine, "CMP%s %s, %s, %s, %s;", (saturate ? "_SAT" : ""), operands[0], operands[1], operands[3], operands[2]);
	  } else if (curOpcode->opcode == D3DSIO_CND) {
            sprintf(tmpLine, "ADD TMP, -%s, coefdiv.x;", operands[1]);
            addline(&lineNum, pgmStr, tmpLine);
            sprintf(tmpLine, "CMP%s %s, TMP, %s, %s;", (saturate ? "_SAT" : ""), operands[0], operands[2], operands[3]);
          } else {
            if (saturate)
              strcat(tmpLine, "_SAT");
            strcat(tmpLine, " ");
            strcat(tmpLine, operands[0]);
            for (i = 1; i < curOpcode->num_params; i++) {
              strcat(tmpLine, ", ");
              strcat(tmpLine, operands[i]);
            }
            strcat(tmpLine,";");
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221
          }
          addline(&lineNum, pgmStr, tmpLine);
          pToken += curOpcode->num_params;
        }
        if (curOpcode->num_params > 0) {
          DWORD param = *(pInstr+1);
          if (0 != (param & D3DSP_DSTSHIFT_MASK)) {
            /* Generate a line that handle the output modifier computation */
            char regstr[100];
            char write_mask[20];
            DWORD shift = (param & D3DSP_DSTSHIFT_MASK) >> D3DSP_DSTSHIFT_SHIFT;
            get_register_name(param, regstr);
            get_write_mask(param, write_mask);
            gen_output_modifier_line(saturate, write_mask, shift, regstr, tmpLine);
            addline(&lineNum, pgmStr, tmpLine);
          }
        }
      }
    }
    strcpy(tmpLine, "MOV result.color, R0;");
    addline(&lineNum, pgmStr, tmpLine);

    strcpy(tmpLine, "END");
    addline(&lineNum, pgmStr, tmpLine);
  }

  /*  Create the hw shader */
  GL_EXTCALL(glGenProgramsARB(1, &pshader->prgId));
  TRACE_(d3d_hw_shader)("Creating a hw pixel shader, prg=%d\n", pshader->prgId);

  GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, pshader->prgId));

  /* Create the program and check for errors */
  GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(pgmStr), pgmStr));
  if (glGetError() == GL_INVALID_OPERATION) {
    GLint errPos;
    glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
    FIXME_(d3d_hw_shader)("HW PixelShader Error at position: %d\n%s\n", errPos, glGetString(GL_PROGRAM_ERROR_STRING_ARB));
    pshader->prgId = -1;
  }
  
  HeapFree(GetProcessHeap(), 0, pgmStr);
}

2222 2223 2224 2225 2226 2227
inline static VOID IDirect3DPixelShaderImpl_ParseProgram(IDirect3DPixelShaderImpl* pshader, CONST DWORD* pFunction) {
  const DWORD* pToken = pFunction;
  const SHADER_OPCODE* curOpcode = NULL;
  DWORD code;
  DWORD len = 0;  
  DWORD i;
2228
  int version = 0;
2229 2230 2231 2232 2233

  if (NULL != pToken) {
    while (D3DPS_END() != *pToken) { 
      if (pshader_is_version_token(*pToken)) { /** version */
	TRACE("ps.%lu.%lu\n", (*pToken >> 8) & 0x0F, (*pToken & 0x0F));
2234
	version = (((*pToken >> 8) & 0x0F) * 10) + (*pToken & 0x0F);
2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247
	++pToken;
	++len;
	continue;
      } 
      if (pshader_is_comment_token(*pToken)) { /** comment */
	DWORD comment_len = (*pToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
	++pToken;
	/*TRACE("comment[%ld] ;%s\n", comment_len, (char*)pToken);*/
	pToken += comment_len;
	len += comment_len + 1;
	continue;
      }
      code = *pToken;
2248
      curOpcode = pshader_program_get_opcode(code, version);
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282
      ++pToken;
      ++len;
      if (NULL == curOpcode) {
	/* unkown current opcode ... */
	while (*pToken & 0x80000000) {
	  TRACE("unrecognized opcode: %08lx\n", *pToken);
	  ++pToken;
	  ++len;
	}
      } else {
	TRACE(" ");
	pshader_program_dump_opcode(curOpcode, code, *pToken);
	if (curOpcode->num_params > 0) {
	  pshader_program_dump_param(*pToken, 0);
	  ++pToken;
	  ++len;
	  for (i = 1; i < curOpcode->num_params; ++i) {
	    TRACE(", ");
	    if (D3DSIO_DEF != code) {
	      pshader_program_dump_param(*pToken, 1);
	    } else {
	      TRACE("%f", *((float*) pToken));
	    }
	    ++pToken;
	    ++len;
	  }
	}
	TRACE("\n");
      }
      pshader->functionLength = (len + 1) * sizeof(DWORD);
    } 
  } else {
    pshader->functionLength = 1; /* no Function defined use fixed function vertex processing */
  }
2283 2284 2285 2286 2287

  if (NULL != pFunction) {
    IDirect3DPixelShaderImpl_GenerateProgramArbHW(pshader, pFunction);
  }

2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339
  if (NULL != pFunction) {
    pshader->function = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, pshader->functionLength);
    memcpy(pshader->function, pFunction, pshader->functionLength);
  } else {
    pshader->function = NULL;
  }
}

HRESULT WINAPI IDirect3DDeviceImpl_CreatePixelShader(IDirect3DDevice8Impl* This, CONST DWORD* pFunction, IDirect3DPixelShaderImpl** ppPixelShader) {
  IDirect3DPixelShaderImpl* object;

  object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DPixelShaderImpl));
  if (NULL == object) {
    *ppPixelShader = NULL;
    return D3DERR_OUTOFVIDEOMEMORY;
  }
  /*object->lpVtbl = &Direct3DPixelShader9_Vtbl;*/
  object->device = This;
  object->ref = 1;
  
  object->data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PSHADERDATA8));
    
  IDirect3DPixelShaderImpl_ParseProgram(object, pFunction);

  *ppPixelShader = object;
  return D3D_OK;
}

HRESULT WINAPI IDirect3DPixelShaderImpl_GetFunction(IDirect3DPixelShaderImpl* This, VOID* pData, UINT* pSizeOfData) {
  if (NULL == pData) {
    *pSizeOfData = This->functionLength;
    return D3D_OK;
  }
  if (*pSizeOfData < This->functionLength) {
    *pSizeOfData = This->functionLength;
    return D3DERR_MOREDATA;
  }
  if (NULL == This->function) { /* no function defined */
    TRACE("(%p) : GetFunction no User Function defined using NULL to %p\n", This, pData);
    (*(DWORD **) pData) = NULL;
  } else {
    TRACE("(%p) : GetFunction copying to %p\n", This, pData);
    memcpy(pData, This->function, This->functionLength);
  }
  return D3D_OK;
}

HRESULT WINAPI IDirect3DPixelShaderImpl_SetConstantF(IDirect3DPixelShaderImpl* This, UINT StartRegister, CONST FLOAT* pConstantData, UINT Vector4fCount) {
  if (StartRegister + Vector4fCount > D3D8_VSHADER_MAX_CONSTANTS) {
    return D3DERR_INVALIDCALL;
  }
  if (NULL == This->data) { /* temporary while datas not supported */
2340
    FIXME("(%p) : PixelShader_SetConstant not fully supported yet\n", This);
2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364
    return D3DERR_INVALIDCALL;
  }
  memcpy(&This->data->C[StartRegister], pConstantData, Vector4fCount * 4 * sizeof(FLOAT));
  return D3D_OK;
}

HRESULT WINAPI IDirect3DPixelShaderImpl_GetConstantF(IDirect3DPixelShaderImpl* This, UINT StartRegister, FLOAT* pConstantData, UINT Vector4fCount) {
  if (StartRegister + Vector4fCount > D3D8_VSHADER_MAX_CONSTANTS) {
    return D3DERR_INVALIDCALL;
  }
  if (NULL == This->data) { /* temporary while datas not supported */
    return D3DERR_INVALIDCALL;
  }
  memcpy(pConstantData, &This->data->C[StartRegister], Vector4fCount * 4 * sizeof(FLOAT));
  return D3D_OK;
}


/**********************************************************************************************************************************************
 **********************************************************************************************************************************************
 **********************************************************************************************************************************************
 **********************************************************************************************************************************************
 **********************************************************************************************************************************************/

2365 2366 2367
/***********************************************************************
 *		ValidateVertexShader (D3D8.@)
 */
2368 2369 2370
BOOL WINAPI ValidateVertexShader(LPVOID what) {
  FIXME("(void): stub: %p\n", what);
  return 0;
Raphael Junqueira's avatar
Raphael Junqueira committed
2371 2372
}

2373 2374 2375
/***********************************************************************
 *		ValidatePixelShader (D3D8.@)
 */
2376 2377
BOOL WINAPI ValidatePixelShader(LPVOID what, LPVOID toto) {
  FIXME("(void): stub: %p %p\n", what, toto);
2378
  return TRUE;
Raphael Junqueira's avatar
Raphael Junqueira committed
2379
}