winemac: Fix the logic for checking if a view is already in the intended z-order.
[wine.git] / dlls / d3dx9_36 / preshader.c
blob78f3bdb3922d26bc219838dec4191b3b2abbacec
1 /*
2 * Copyright 2016 Paul Gofman
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
22 #include "d3dx9_private.h"
24 #include <float.h>
26 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
28 enum pres_ops
30 PRESHADER_OP_NOP,
31 PRESHADER_OP_MOV,
32 PRESHADER_OP_ADD,
33 PRESHADER_OP_MUL,
34 PRESHADER_OP_DOT,
35 PRESHADER_OP_NEG,
36 PRESHADER_OP_RCP,
37 PRESHADER_OP_LT,
38 PRESHADER_OP_FRC,
39 PRESHADER_OP_MIN,
40 PRESHADER_OP_MAX,
41 PRESHADER_OP_GE,
42 PRESHADER_OP_CMP,
43 PRESHADER_OP_SIN,
44 PRESHADER_OP_COS,
45 PRESHADER_OP_RSQ,
46 PRESHADER_OP_EXP,
47 PRESHADER_OP_DOTSWIZ6,
48 PRESHADER_OP_DOTSWIZ8,
51 typedef double (*pres_op_func)(double *args, int n);
53 static double pres_mov(double *args, int n) {return args[0];}
54 static double pres_add(double *args, int n) {return args[0] + args[1];}
55 static double pres_mul(double *args, int n) {return args[0] * args[1];}
56 static double pres_dot(double *args, int n)
58 int i;
59 double sum;
61 sum = 0.0;
62 for (i = 0; i < n; ++i)
63 sum += args[i] * args[i + n];
64 return sum;
67 static double pres_dotswiz6(double *args, int n)
69 return pres_dot(args, 3);
72 static double pres_dotswiz8(double *args, int n)
74 return pres_dot(args, 4);
77 static double pres_neg(double *args, int n) {return -args[0];}
78 static double pres_rcp(double *args, int n) {return 1.0 / args[0];}
79 static double pres_lt(double *args, int n) {return args[0] < args[1] ? 1.0 : 0.0;}
80 static double pres_ge(double *args, int n) {return args[0] >= args[1] ? 1.0 : 0.0;}
81 static double pres_frc(double *args, int n) {return args[0] - floor(args[0]);}
82 static double pres_min(double *args, int n) {return fmin(args[0], args[1]);}
83 static double pres_max(double *args, int n) {return fmax(args[0], args[1]);}
84 static double pres_cmp(double *args, int n) {return args[0] < 0.0 ? args[2] : args[1];}
85 static double pres_sin(double *args, int n) {return sin(args[0]);}
86 static double pres_cos(double *args, int n) {return cos(args[0]);}
87 static double pres_rsq(double *args, int n)
89 double v;
91 v = fabs(args[0]);
92 if (v == 0.0)
93 return INFINITY;
94 else
95 return 1.0 / sqrt(v);
97 static double pres_exp(double *args, int n) {return pow(2.0, args[0]);}
99 #define PRES_OPCODE_MASK 0x7ff00000
100 #define PRES_OPCODE_SHIFT 20
101 #define PRES_SCALAR_FLAG 0x80000000
102 #define PRES_NCOMP_MASK 0x0000ffff
104 #define FOURCC_PRES 0x53455250
105 #define FOURCC_CLIT 0x54494c43
106 #define FOURCC_FXLC 0x434c5846
107 #define FOURCC_PRSI 0x49535250
108 #define PRES_SIGN 0x46580000
110 struct op_info
112 unsigned int opcode;
113 char mnem[16];
114 unsigned int input_count;
115 BOOL func_all_comps;
116 pres_op_func func;
119 static const struct op_info pres_op_info[] =
121 {0x000, "nop", 0, 0, NULL }, /* PRESHADER_OP_NOP */
122 {0x100, "mov", 1, 0, pres_mov}, /* PRESHADER_OP_MOV */
123 {0x204, "add", 2, 0, pres_add}, /* PRESHADER_OP_ADD */
124 {0x205, "mul", 2, 0, pres_mul}, /* PRESHADER_OP_MUL */
125 {0x500, "dot", 2, 1, pres_dot}, /* PRESHADER_OP_DOT */
126 {0x101, "neg", 1, 0, pres_neg}, /* PRESHADER_OP_NEG */
127 {0x103, "rcp", 1, 0, pres_rcp}, /* PRESHADER_OP_RCP */
128 {0x202, "lt", 2, 0, pres_lt }, /* PRESHADER_OP_LT */
129 {0x104, "frc", 1, 0, pres_frc}, /* PRESHADER_OP_FRC */
130 {0x200, "min", 2, 0, pres_min}, /* PRESHADER_OP_MIN */
131 {0x201, "max", 2, 0, pres_max}, /* PRESHADER_OP_MAX */
132 {0x203, "ge", 2, 0, pres_ge }, /* PRESHADER_OP_GE */
133 {0x300, "cmp", 3, 0, pres_cmp}, /* PRESHADER_OP_CMP */
134 {0x108, "sin", 1, 0, pres_sin}, /* PRESHADER_OP_SIN */
135 {0x109, "cos", 1, 0, pres_cos}, /* PRESHADER_OP_COS */
136 {0x107, "rsq", 1, 0, pres_rsq}, /* PRESHADER_OP_RSQ */
137 {0x105, "exp", 1, 0, pres_exp}, /* PRESHADER_OP_EXP */
138 {0x70e, "d3ds_dotswiz", 6, 0, pres_dotswiz6}, /* PRESHADER_OP_DOTSWIZ6 */
139 {0x70e, "d3ds_dotswiz", 8, 0, pres_dotswiz8}, /* PRESHADER_OP_DOTSWIZ8 */
142 enum pres_value_type
144 PRES_VT_FLOAT,
145 PRES_VT_DOUBLE,
146 PRES_VT_INT,
147 PRES_VT_BOOL
150 static const struct
152 unsigned int component_size;
153 unsigned int reg_component_count;
154 enum pres_value_type type;
156 table_info[] =
158 {sizeof(double), 1, PRES_VT_DOUBLE}, /* PRES_REGTAB_IMMED */
159 {sizeof(float), 4, PRES_VT_FLOAT }, /* PRES_REGTAB_CONST */
160 {sizeof(float), 4, PRES_VT_FLOAT }, /* PRES_REGTAB_OCONST */
161 {sizeof(BOOL), 1, PRES_VT_BOOL }, /* PRES_REGTAB_OBCONST */
162 {sizeof(int), 4, PRES_VT_INT, }, /* PRES_REGTAB_OICONST */
163 /* TODO: use double precision for 64 bit */
164 {sizeof(float), 4, PRES_VT_FLOAT } /* PRES_REGTAB_TEMP */
167 static const char *table_symbol[] =
169 "imm", "c", "oc", "ob", "oi", "r", "(null)",
172 static const enum pres_reg_tables pres_regset2table[] =
174 PRES_REGTAB_OBCONST, /* D3DXRS_BOOL */
175 PRES_REGTAB_OICONST, /* D3DXRS_INT4 */
176 PRES_REGTAB_CONST, /* D3DXRS_FLOAT4 */
177 PRES_REGTAB_COUNT, /* D3DXRS_SAMPLER */
180 static const enum pres_reg_tables shad_regset2table[] =
182 PRES_REGTAB_OBCONST, /* D3DXRS_BOOL */
183 PRES_REGTAB_OICONST, /* D3DXRS_INT4 */
184 PRES_REGTAB_OCONST, /* D3DXRS_FLOAT4 */
185 PRES_REGTAB_COUNT, /* D3DXRS_SAMPLER */
188 struct d3dx_pres_operand
190 enum pres_reg_tables table;
191 /* offset is component index, not register index, e. g.
192 offset for component c3.y is 13 (3 * 4 + 1) */
193 unsigned int offset;
196 #define MAX_INPUTS_COUNT 8
198 struct d3dx_pres_ins
200 enum pres_ops op;
201 /* first input argument is scalar,
202 scalar component is propagated */
203 BOOL scalar_op;
204 unsigned int component_count;
205 struct d3dx_pres_operand inputs[MAX_INPUTS_COUNT];
206 struct d3dx_pres_operand output;
209 static unsigned int get_reg_offset(unsigned int table, unsigned int offset)
211 return offset / table_info[table].reg_component_count;
214 #define PRES_BITMASK_BLOCK_SIZE (sizeof(unsigned int) * 8)
216 static HRESULT regstore_alloc_table(struct d3dx_regstore *rs, unsigned int table)
218 unsigned int size;
220 size = rs->table_sizes[table] * table_info[table].reg_component_count * table_info[table].component_size;
221 if (size)
223 rs->tables[table] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
224 rs->table_value_set[table] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
225 sizeof(*rs->table_value_set[table]) *
226 ((rs->table_sizes[table] + PRES_BITMASK_BLOCK_SIZE - 1) / PRES_BITMASK_BLOCK_SIZE));
227 if (!rs->tables[table] || !rs->table_value_set[table])
228 return E_OUTOFMEMORY;
230 return D3D_OK;
233 static void regstore_free_tables(struct d3dx_regstore *rs)
235 unsigned int i;
237 for (i = 0; i < PRES_REGTAB_COUNT; ++i)
239 HeapFree(GetProcessHeap(), 0, rs->tables[i]);
240 HeapFree(GetProcessHeap(), 0, rs->table_value_set[i]);
244 static void regstore_set_values(struct d3dx_regstore *rs, unsigned int table, void *data,
245 unsigned int start_offset, unsigned int count)
247 unsigned int block_idx, start, end, start_block, end_block;
249 if (!count)
250 return;
252 memcpy((BYTE *)rs->tables[table] + start_offset * table_info[table].component_size,
253 data, count * table_info[table].component_size);
255 start = get_reg_offset(table, start_offset);
256 start_block = start / PRES_BITMASK_BLOCK_SIZE;
257 start -= start_block * PRES_BITMASK_BLOCK_SIZE;
258 end = get_reg_offset(table, start_offset + count - 1);
259 end_block = end / PRES_BITMASK_BLOCK_SIZE;
260 end = (end_block + 1) * PRES_BITMASK_BLOCK_SIZE - 1 - end;
262 if (start_block == end_block)
264 rs->table_value_set[table][start_block] |= (~0u << start) & (~0u >> end);
266 else
268 rs->table_value_set[table][start_block] |= ~0u << start;
270 for (block_idx = start_block + 1; block_idx < end_block; ++block_idx)
271 rs->table_value_set[table][block_idx] = ~0u;
273 rs->table_value_set[table][end_block] |= ~0u >> end;
277 static unsigned int regstore_is_val_set_reg(struct d3dx_regstore *rs, unsigned int table, unsigned int reg_idx)
279 return rs->table_value_set[table][reg_idx / PRES_BITMASK_BLOCK_SIZE] &
280 (1u << (reg_idx % PRES_BITMASK_BLOCK_SIZE));
283 static double regstore_get_double(struct d3dx_regstore *rs, unsigned int table, unsigned int offset)
285 BYTE *p;
287 p = (BYTE *)rs->tables[table] + table_info[table].component_size * offset;
288 switch (table_info[table].type)
290 case PRES_VT_FLOAT:
291 return *(float *)p;
292 case PRES_VT_DOUBLE:
293 return *(double *)p;
294 default:
295 FIXME("Unexpected preshader input from table %u.\n", table);
296 return NAN;
300 static void regstore_set_double(struct d3dx_regstore *rs, unsigned int table, unsigned int offset, double v)
302 BYTE *p;
303 unsigned int reg_idx;
305 p = (BYTE *)rs->tables[table] + table_info[table].component_size * offset;
306 switch (table_info[table].type)
308 case PRES_VT_FLOAT : *(float *)p = v; break;
309 case PRES_VT_DOUBLE: *(double *)p = v; break;
310 case PRES_VT_INT : *(int *)p = lrint(v); break;
311 case PRES_VT_BOOL : *(BOOL *)p = !!v; break;
313 reg_idx = get_reg_offset(table, offset);
314 rs->table_value_set[table][reg_idx / PRES_BITMASK_BLOCK_SIZE] |=
315 1u << (reg_idx % PRES_BITMASK_BLOCK_SIZE);
318 static void regstore_reset_table(struct d3dx_regstore *rs, unsigned int table)
320 unsigned int size;
322 size = rs->table_sizes[table] * table_info[table].reg_component_count * table_info[table].component_size;
324 memset(rs->tables[table], 0, size);
325 memset(rs->table_value_set[table], 0,
326 sizeof(*rs->table_value_set[table]) *
327 ((rs->table_sizes[table] + PRES_BITMASK_BLOCK_SIZE - 1) / PRES_BITMASK_BLOCK_SIZE));
330 static void dump_bytecode(void *data, unsigned int size)
332 unsigned int *bytecode = (unsigned int *)data;
333 unsigned int i, j, n;
335 size /= sizeof(*bytecode);
336 i = 0;
337 while (i < size)
339 n = min(size - i, 8);
340 for (j = 0; j < n; ++j)
341 TRACE("0x%08x,", bytecode[i + j]);
342 i += n;
343 TRACE("\n");
347 static unsigned int *find_bytecode_comment(unsigned int *ptr, unsigned int count,
348 unsigned int fourcc, unsigned int *size)
350 /* Provide at least one value in comment section on non-NULL return. */
351 while (count > 2 && (*ptr & 0xffff) == 0xfffe)
353 unsigned int section_size;
355 section_size = (*ptr >> 16);
356 if (!section_size || section_size + 1 > count)
357 break;
358 if (*(ptr + 1) == fourcc)
360 *size = section_size;
361 return ptr + 2;
363 count -= section_size + 1;
364 ptr += section_size + 1;
366 return NULL;
369 static unsigned int *parse_pres_arg(unsigned int *ptr, unsigned int count, struct d3dx_pres_operand *opr)
371 static const enum pres_reg_tables reg_table[8] =
373 PRES_REGTAB_COUNT, PRES_REGTAB_IMMED, PRES_REGTAB_CONST, PRES_REGTAB_COUNT,
374 PRES_REGTAB_OCONST, PRES_REGTAB_OBCONST, PRES_REGTAB_OICONST, PRES_REGTAB_TEMP
377 if (count < 3)
379 WARN("Byte code buffer ends unexpectedly.\n");
380 return NULL;
383 if (*ptr)
385 FIXME("Relative addressing not supported yet, word %#x.\n", *ptr);
386 return NULL;
388 ++ptr;
390 if (*ptr >= ARRAY_SIZE(reg_table) || reg_table[*ptr] == PRES_REGTAB_COUNT)
392 FIXME("Unsupported register table %#x.\n", *ptr);
393 return NULL;
395 opr->table = reg_table[*ptr++];
396 opr->offset = *ptr++;
398 if (opr->table == PRES_REGTAB_OBCONST)
399 opr->offset /= 4;
400 return ptr;
403 static unsigned int *parse_pres_ins(unsigned int *ptr, unsigned int count, struct d3dx_pres_ins *ins)
405 unsigned int ins_code, ins_raw;
406 unsigned int input_count;
407 unsigned int i;
409 if (count < 2)
411 WARN("Byte code buffer ends unexpectedly.\n");
412 return NULL;
415 ins_raw = *ptr++;
416 ins_code = (ins_raw & PRES_OPCODE_MASK) >> PRES_OPCODE_SHIFT;
417 ins->component_count = ins_raw & PRES_NCOMP_MASK;
418 ins->scalar_op = !!(ins_raw & PRES_SCALAR_FLAG);
420 if (ins->component_count < 1 || ins->component_count > 4)
422 FIXME("Unsupported number of components %u.\n", ins->component_count);
423 return NULL;
425 input_count = *ptr++;
426 count -= 2;
427 for (i = 0; i < ARRAY_SIZE(pres_op_info); ++i)
428 if (ins_code == pres_op_info[i].opcode && input_count == pres_op_info[i].input_count)
429 break;
430 if (i == ARRAY_SIZE(pres_op_info))
432 FIXME("Unknown opcode %#x, input_count %u, raw %#x.\n", ins_code, input_count, ins_raw);
433 return NULL;
435 ins->op = i;
436 if (input_count > ARRAY_SIZE(ins->inputs))
438 FIXME("Actual input args count %u exceeds inputs array size, instruction %s.\n", input_count,
439 pres_op_info[i].mnem);
440 return NULL;
442 for (i = 0; i < input_count; ++i)
444 unsigned int *p;
446 p = parse_pres_arg(ptr, count, &ins->inputs[i]);
447 if (!p)
448 return NULL;
449 count -= p - ptr;
450 ptr = p;
452 ptr = parse_pres_arg(ptr, count, &ins->output);
453 return ptr;
456 static HRESULT get_ctab_constant_desc(ID3DXConstantTable *ctab, D3DXHANDLE hc, D3DXCONSTANT_DESC *desc)
458 D3DXCONSTANT_DESC buffer[2];
459 HRESULT hr;
460 unsigned int count;
462 count = ARRAY_SIZE(buffer);
463 if (FAILED(hr = ID3DXConstantTable_GetConstantDesc(ctab, hc, buffer, &count)))
465 FIXME("Could not get constant desc, hr %#x.\n", hr);
466 return hr;
468 else if (count != 1)
470 FIXME("Unexpected constant descriptors count %u.\n", count);
471 return D3DERR_INVALIDCALL;
473 *desc = buffer[0];
474 return D3D_OK;
477 static HRESULT get_constants_desc(unsigned int *byte_code, struct d3dx_const_tab *out, struct d3dx9_base_effect *base)
479 ID3DXConstantTable *ctab;
480 D3DXCONSTANT_DESC *cdesc;
481 struct d3dx_parameter **inputs_param;
482 D3DXCONSTANTTABLE_DESC desc;
483 HRESULT hr;
484 D3DXHANDLE hc;
485 unsigned int i;
487 out->inputs = cdesc = NULL;
488 out->ctab = NULL;
489 out->inputs_param = NULL;
490 out->input_count = 0;
491 inputs_param = NULL;
492 hr = D3DXGetShaderConstantTable(byte_code, &ctab);
493 if (FAILED(hr) || !ctab)
495 TRACE("Could not get CTAB data, hr %#x.\n", hr);
496 /* returning OK, shaders and preshaders without CTAB are valid */
497 return D3D_OK;
499 if (FAILED(hr = ID3DXConstantTable_GetDesc(ctab, &desc)))
501 FIXME("Could not get CTAB desc, hr %#x.\n", hr);
502 goto err_out;
505 cdesc = HeapAlloc(GetProcessHeap(), 0, sizeof(*cdesc) * desc.Constants);
506 inputs_param = HeapAlloc(GetProcessHeap(), 0, sizeof(*inputs_param) * desc.Constants);
507 if (!cdesc || !inputs_param)
509 hr = E_OUTOFMEMORY;
510 goto err_out;
513 for (i = 0; i < desc.Constants; ++i)
515 hc = ID3DXConstantTable_GetConstant(ctab, NULL, i);
516 if (!hc)
518 FIXME("Null constant handle.\n");
519 goto err_out;
521 if (FAILED(hr = get_ctab_constant_desc(ctab, hc, &cdesc[i])))
522 goto err_out;
523 inputs_param[i] = get_parameter_by_name(base, NULL, cdesc[i].Name);
524 if (cdesc[i].Class == D3DXPC_OBJECT)
525 TRACE("Object %s, parameter %p.\n", cdesc[i].Name, inputs_param[i]);
526 else if (!inputs_param[i])
527 ERR("Could not find parameter %s in effect.\n", cdesc[i].Name);
529 out->input_count = desc.Constants;
530 out->inputs = cdesc;
531 out->inputs_param = inputs_param;
532 out->ctab = ctab;
533 return D3D_OK;
535 err_out:
536 HeapFree(GetProcessHeap(), 0, cdesc);
537 HeapFree(GetProcessHeap(), 0, inputs_param);
538 if (ctab)
539 ID3DXConstantTable_Release(ctab);
540 return hr;
543 static void update_table_size(unsigned int *table_sizes, unsigned int table, unsigned int max_register)
545 if (table < PRES_REGTAB_COUNT)
546 table_sizes[table] = max(table_sizes[table], max_register + 1);
549 static void update_table_sizes_consts(unsigned int *table_sizes, struct d3dx_const_tab *ctab)
551 unsigned int i, table, max_register;
553 for (i = 0; i < ctab->input_count; ++i)
555 if (!ctab->inputs[i].RegisterCount)
556 continue;
557 max_register = ctab->inputs[i].RegisterIndex + ctab->inputs[i].RegisterCount - 1;
558 table = ctab->regset2table[ctab->inputs[i].RegisterSet];
559 update_table_size(table_sizes, table, max_register);
563 static void dump_arg(struct d3dx_regstore *rs, const struct d3dx_pres_operand *arg, int component_count)
565 static const char *xyzw_str = "xyzw";
566 unsigned int i, table;
568 table = arg->table;
569 if (table == PRES_REGTAB_IMMED)
571 TRACE("(");
572 for (i = 0; i < component_count; ++i)
573 TRACE(i < component_count - 1 ? "%.16e, " : "%.16e",
574 ((double *)rs->tables[PRES_REGTAB_IMMED])[arg->offset + i]);
575 TRACE(")");
577 else
579 TRACE("%s%u.", table_symbol[table], get_reg_offset(table, arg->offset));
580 for (i = 0; i < component_count; ++i)
581 TRACE("%c", xyzw_str[(arg->offset + i) % 4]);
585 static void dump_registers(struct d3dx_const_tab *ctab)
587 unsigned int table, i;
589 for (i = 0; i < ctab->input_count; ++i)
591 table = ctab->regset2table[ctab->inputs[i].RegisterSet];
592 TRACE("// %-12s %s%-4u %u\n", ctab->inputs_param[i] ? ctab->inputs_param[i]->name : "(nil)",
593 table_symbol[table], ctab->inputs[i].RegisterIndex, ctab->inputs[i].RegisterCount);
597 static void dump_ins(struct d3dx_regstore *rs, const struct d3dx_pres_ins *ins)
599 unsigned int i;
601 TRACE(" %s ", pres_op_info[ins->op].mnem);
602 dump_arg(rs, &ins->output, pres_op_info[ins->op].func_all_comps ? 1 : ins->component_count);
603 for (i = 0; i < pres_op_info[ins->op].input_count; ++i)
605 TRACE(", ");
606 dump_arg(rs, &ins->inputs[i], ins->scalar_op && !i ? 1 : ins->component_count);
608 TRACE("\n");
611 static void dump_preshader(struct d3dx_preshader *pres)
613 unsigned int i;
615 TRACE("// Preshader registers:\n");
616 dump_registers(&pres->inputs);
617 TRACE(" preshader\n");
618 for (i = 0; i < pres->ins_count; ++i)
619 dump_ins(&pres->regs, &pres->ins[i]);
622 static HRESULT parse_preshader(struct d3dx_preshader *pres, unsigned int *ptr, unsigned int count, struct d3dx9_base_effect *base)
624 unsigned int *p;
625 unsigned int i, j, const_count;
626 double *dconst;
627 HRESULT hr;
628 unsigned int saved_word;
629 unsigned int section_size;
631 TRACE("Preshader version %#x.\n", *ptr & 0xffff);
633 if (!count)
635 WARN("Unexpected end of byte code buffer.\n");
636 return D3DXERR_INVALIDDATA;
639 p = find_bytecode_comment(ptr + 1, count - 1, FOURCC_CLIT, &section_size);
640 if (p)
642 const_count = *p++;
643 if (const_count > (section_size - 1) / (sizeof(double) / sizeof(unsigned int)))
645 WARN("Byte code buffer ends unexpectedly.\n");
646 return D3DXERR_INVALIDDATA;
648 dconst = (double *)p;
650 else
652 const_count = 0;
653 dconst = NULL;
655 TRACE("%u double constants.\n", const_count);
657 p = find_bytecode_comment(ptr + 1, count - 1, FOURCC_FXLC, &section_size);
658 if (!p)
660 WARN("Could not find preshader code.\n");
661 return D3D_OK;
663 pres->ins_count = *p++;
664 --section_size;
665 if (pres->ins_count > UINT_MAX / sizeof(*pres->ins))
667 WARN("Invalid instruction count %u.\n", pres->ins_count);
668 return D3DXERR_INVALIDDATA;
670 TRACE("%u instructions.\n", pres->ins_count);
671 pres->ins = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pres->ins) * pres->ins_count);
672 if (!pres->ins)
673 return E_OUTOFMEMORY;
674 for (i = 0; i < pres->ins_count; ++i)
676 unsigned int *ptr_next;
678 ptr_next = parse_pres_ins(p, section_size, &pres->ins[i]);
679 if (!ptr_next)
680 return D3DXERR_INVALIDDATA;
681 section_size -= ptr_next - p;
682 p = ptr_next;
685 saved_word = *ptr;
686 *ptr = 0xfffe0000;
687 hr = get_constants_desc(ptr, &pres->inputs, base);
688 *ptr = saved_word;
689 if (FAILED(hr))
690 return hr;
692 pres->inputs.regset2table = pres_regset2table;
694 pres->regs.table_sizes[PRES_REGTAB_IMMED] = const_count;
696 for (i = 0; i < pres->ins_count; ++i)
698 for (j = 0; j < pres_op_info[pres->ins[i].op].input_count; ++j)
699 update_table_size(pres->regs.table_sizes, pres->ins[i].inputs[j].table,
700 get_reg_offset(pres->ins[i].inputs[j].table,
701 pres->ins[i].inputs[j].offset + pres->ins[i].component_count - 1));
703 update_table_size(pres->regs.table_sizes, pres->ins[i].output.table,
704 get_reg_offset(pres->ins[i].output.table,
705 pres->ins[i].output.offset + pres->ins[i].component_count - 1));
707 update_table_sizes_consts(pres->regs.table_sizes, &pres->inputs);
708 if (FAILED(regstore_alloc_table(&pres->regs, PRES_REGTAB_IMMED)))
709 return E_OUTOFMEMORY;
710 regstore_set_values(&pres->regs, PRES_REGTAB_IMMED, dconst, 0, const_count);
712 return D3D_OK;
715 void d3dx_create_param_eval(struct d3dx9_base_effect *base_effect, void *byte_code, unsigned int byte_code_size,
716 D3DXPARAMETER_TYPE type, struct d3dx_param_eval **peval_out)
718 struct d3dx_param_eval *peval;
719 unsigned int *ptr;
720 HRESULT hr;
721 unsigned int i;
722 BOOL shader;
723 unsigned int count, pres_size;
725 TRACE("base_effect %p, byte_code %p, byte_code_size %u, type %u, peval_out %p.\n",
726 base_effect, byte_code, byte_code_size, type, peval_out);
728 count = byte_code_size / sizeof(unsigned int);
729 if (!byte_code || !count)
731 *peval_out = NULL;
732 return;
735 peval = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*peval));
736 if (!peval)
737 goto err_out;
739 peval->param_type = type;
740 switch (type)
742 case D3DXPT_VERTEXSHADER:
743 case D3DXPT_PIXELSHADER:
744 shader = TRUE;
745 break;
746 default:
747 shader = FALSE;
748 break;
750 peval->shader_inputs.regset2table = shad_regset2table;
752 ptr = (unsigned int *)byte_code;
753 if (shader)
755 if ((*ptr & 0xfffe0000) != 0xfffe0000)
757 FIXME("Invalid shader signature %#x.\n", *ptr);
758 goto err_out;
760 TRACE("Shader version %#x.\n", *ptr & 0xffff);
762 if (FAILED(hr = get_constants_desc(ptr, &peval->shader_inputs, base_effect)))
764 FIXME("Could not get shader constant table, hr %#x.\n", hr);
765 goto err_out;
767 update_table_sizes_consts(peval->pres.regs.table_sizes, &peval->shader_inputs);
768 ptr = find_bytecode_comment(ptr + 1, count - 1, FOURCC_PRES, &pres_size);
769 if (!ptr)
770 TRACE("No preshader found.\n");
772 else
774 pres_size = count;
777 if (ptr && FAILED(parse_preshader(&peval->pres, ptr, pres_size, base_effect)))
779 FIXME("Failed parsing preshader, byte code for analysis follows.\n");
780 dump_bytecode(byte_code, byte_code_size);
781 goto err_out;
784 for (i = PRES_REGTAB_FIRST_SHADER; i < PRES_REGTAB_COUNT; ++i)
786 if (FAILED(regstore_alloc_table(&peval->pres.regs, i)))
787 goto err_out;
790 if (TRACE_ON(d3dx))
792 dump_bytecode(byte_code, byte_code_size);
793 dump_preshader(&peval->pres);
794 if (shader)
796 TRACE("// Shader registers:\n");
797 dump_registers(&peval->shader_inputs);
800 *peval_out = peval;
801 TRACE("Created parameter evaluator %p.\n", *peval_out);
802 return;
804 err_out:
805 FIXME("Error creating parameter evaluator.\n");
806 d3dx_free_param_eval(peval);
807 *peval_out = NULL;
810 static void d3dx_free_const_tab(struct d3dx_const_tab *ctab)
812 HeapFree(GetProcessHeap(), 0, ctab->inputs);
813 HeapFree(GetProcessHeap(), 0, ctab->inputs_param);
814 if (ctab->ctab)
815 ID3DXConstantTable_Release(ctab->ctab);
818 static void d3dx_free_preshader(struct d3dx_preshader *pres)
820 HeapFree(GetProcessHeap(), 0, pres->ins);
822 regstore_free_tables(&pres->regs);
823 d3dx_free_const_tab(&pres->inputs);
826 void d3dx_free_param_eval(struct d3dx_param_eval *peval)
828 TRACE("peval %p.\n", peval);
830 if (!peval)
831 return;
833 d3dx_free_preshader(&peval->pres);
834 d3dx_free_const_tab(&peval->shader_inputs);
835 HeapFree(GetProcessHeap(), 0, peval);
838 static HRESULT set_constants_param(struct d3dx_regstore *rs, struct d3dx_const_tab *const_tab,
839 D3DXHANDLE hc, struct d3dx_parameter *param)
841 ID3DXConstantTable *ctab = const_tab->ctab;
842 D3DXCONSTANT_DESC desc;
843 unsigned int const_count, param_count, i, j, n, table, start_offset;
844 unsigned int minor, major, major_stride, param_offset;
845 BOOL transpose, get_element;
847 if (FAILED(get_ctab_constant_desc(ctab, hc, &desc)))
848 return D3DERR_INVALIDCALL;
850 if (param->element_count)
852 param_count = param->element_count;
853 const_count = desc.Elements;
854 get_element = TRUE;
856 else
858 if (desc.Elements > 1)
860 FIXME("Unexpected number of constant elements %u.\n", desc.Elements);
861 return D3DERR_INVALIDCALL;
863 param_count = param->member_count;
864 const_count = desc.StructMembers;
865 get_element = FALSE;
867 if (const_count != param_count)
869 FIXME("Number of elements or struct members differs between parameter (%u) and constant (%u).\n",
870 param_count, const_count);
871 return D3DERR_INVALIDCALL;
873 if (const_count)
875 HRESULT hr, ret;
876 D3DXHANDLE hc_element;
878 ret = D3D_OK;
879 for (i = 0; i < const_count; ++i)
881 if (get_element)
882 hc_element = ID3DXConstantTable_GetConstantElement(ctab, hc, i);
883 else
884 hc_element = ID3DXConstantTable_GetConstant(ctab, hc, i);
885 if (!hc_element)
887 FIXME("Could not get constant.\n");
888 hr = D3DERR_INVALIDCALL;
890 else
892 hr = set_constants_param(rs, const_tab, hc_element, &param->members[i]);
894 if (FAILED(hr))
895 ret = hr;
897 return ret;
900 transpose = (desc.Class == D3DXPC_MATRIX_COLUMNS && param->class == D3DXPC_MATRIX_ROWS)
901 || (param->class == D3DXPC_MATRIX_COLUMNS && desc.Class == D3DXPC_MATRIX_ROWS);
902 if (desc.Class == D3DXPC_MATRIX_COLUMNS)
904 major = param->columns;
905 minor = param->rows;
907 else
909 major = param->rows;
910 minor = param->columns;
913 TRACE("Constant %s, rows %u, columns %u, class %u, bytes %u.\n",
914 debugstr_a(desc.Name), desc.Rows, desc.Columns, desc.Class, desc.Bytes);
915 TRACE("Parameter %s, rows %u, columns %u, class %u, flags %#x, bytes %u, transpose %#x.\n",
916 debugstr_a(param->name), param->rows, param->columns, param->class,
917 param->flags, param->bytes, transpose);
919 if (desc.RegisterSet >= ARRAY_SIZE(shad_regset2table))
921 FIXME("Unknown register set %u.\n", desc.RegisterSet);
922 return D3DERR_INVALIDCALL;
924 table = const_tab->regset2table[desc.RegisterSet];
925 if (table >= PRES_REGTAB_COUNT)
927 ERR("Unexpected register set %u.\n", desc.RegisterSet);
928 return D3DERR_INVALIDCALL;
930 start_offset = desc.RegisterIndex * table_info[table].reg_component_count;
931 major_stride = max(minor, table_info[table].reg_component_count);
932 n = min(major * major_stride,
933 desc.RegisterCount * table_info[table].reg_component_count + major_stride - 1) / major_stride;
934 for (i = 0; i < n; ++i)
936 for (j = 0; j < minor; ++j)
938 unsigned int out;
939 unsigned int *in;
940 unsigned int offset;
942 offset = start_offset + i * major_stride + j;
943 if (offset / table_info[table].reg_component_count >= rs->table_sizes[table])
945 if (table_info[table].reg_component_count != 1)
946 FIXME("Output offset exceeds table size, name %s, component %u.\n", desc.Name, i);
947 break;
949 if (transpose)
950 param_offset = i + j * major;
951 else
952 param_offset = i * minor + j;
953 if (param_offset * sizeof(unsigned int) >= param->bytes)
955 WARN("Parameter data is too short, name %s, component %u.\n", desc.Name, i);
956 break;
959 in = (unsigned int *)param->data + param_offset;
960 /* TODO: store data transfer / convert operation instead of performing an operation
961 from here, to move this to parsing stage */
962 switch (table_info[table].type)
964 case PRES_VT_FLOAT: set_number(&out, D3DXPT_FLOAT, in, param->type); break;
965 case PRES_VT_INT: set_number(&out, D3DXPT_INT, in, param->type); break;
966 case PRES_VT_BOOL: set_number(&out, D3DXPT_BOOL, in, param->type); break;
967 default:
968 FIXME("Unexpected type %#x.\n", table_info[table].type);
969 break;
971 regstore_set_values(rs, table, &out, offset, 1);
975 return D3D_OK;
978 static HRESULT set_constants(struct d3dx_regstore *rs, struct d3dx_const_tab *const_tab)
980 unsigned int i;
981 HRESULT hr, ret;
982 D3DXHANDLE hc;
984 ret = D3D_OK;
985 for (i = 0; i < const_tab->input_count; ++i)
987 if (!const_tab->inputs_param[i] || const_tab->inputs_param[i]->class == D3DXPC_OBJECT)
988 continue;
989 hc = ID3DXConstantTable_GetConstant(const_tab->ctab, NULL, i);
990 if (hc)
992 hr = set_constants_param(rs, const_tab, hc, const_tab->inputs_param[i]);
994 else
996 FIXME("Could not get constant, index %u.\n", i);
997 hr = D3DERR_INVALIDCALL;
999 if (FAILED(hr))
1000 ret = hr;
1002 return ret;
1005 static double exec_get_arg(struct d3dx_regstore *rs, const struct d3dx_pres_ins *ins,
1006 const struct d3dx_pres_operand *opr, unsigned int comp)
1008 if (!regstore_is_val_set_reg(rs, opr->table, (opr->offset + comp) / table_info[opr->table].reg_component_count))
1010 WARN("Using uninitialized input ");
1011 dump_arg(rs, opr, comp);
1012 TRACE(".\n");
1013 dump_ins(rs, ins);
1015 return regstore_get_double(rs, opr->table, opr->offset + comp);
1018 static void exec_set_arg(struct d3dx_regstore *rs, const struct d3dx_pres_operand *opr,
1019 unsigned int comp, double res)
1021 regstore_set_double(rs, opr->table, opr->offset + comp, res);
1024 #define ARGS_ARRAY_SIZE 8
1025 static HRESULT execute_preshader(struct d3dx_preshader *pres)
1027 unsigned int i, j, k;
1028 double args[ARGS_ARRAY_SIZE];
1029 double res;
1031 for (i = 0; i < pres->ins_count; ++i)
1033 const struct d3dx_pres_ins *ins;
1034 const struct op_info *oi;
1036 ins = &pres->ins[i];
1037 oi = &pres_op_info[ins->op];
1038 if (oi->func_all_comps)
1040 if (oi->input_count * ins->component_count > ARGS_ARRAY_SIZE)
1042 FIXME("Too many arguments (%u) for one instruction.\n", oi->input_count * ins->component_count);
1043 return E_FAIL;
1045 for (k = 0; k < oi->input_count; ++k)
1046 for (j = 0; j < ins->component_count; ++j)
1047 args[k * ins->component_count + j] = exec_get_arg(&pres->regs, ins, &ins->inputs[k],
1048 ins->scalar_op && !k ? 0 : j);
1049 res = oi->func(args, ins->component_count);
1051 /* only 'dot' instruction currently falls here */
1052 exec_set_arg(&pres->regs, &ins->output, 0, res);
1054 else
1056 for (j = 0; j < ins->component_count; ++j)
1058 for (k = 0; k < oi->input_count; ++k)
1059 args[k] = exec_get_arg(&pres->regs, ins, &ins->inputs[k], ins->scalar_op && !k ? 0 : j);
1060 res = oi->func(args, ins->component_count);
1061 exec_set_arg(&pres->regs, &ins->output, j, res);
1065 return D3D_OK;
1068 HRESULT d3dx_evaluate_parameter(struct d3dx_param_eval *peval, const struct d3dx_parameter *param, void *param_value)
1070 HRESULT hr;
1071 unsigned int i;
1072 unsigned int elements, elements_param, elements_table;
1073 float *oc;
1075 TRACE("peval %p, param %p, param_value %p.\n", peval, param, param_value);
1077 if (FAILED(hr = set_constants(&peval->pres.regs, &peval->pres.inputs)))
1078 return hr;
1080 if (FAILED(hr = execute_preshader(&peval->pres)))
1081 return hr;
1083 elements_table = table_info[PRES_REGTAB_OCONST].reg_component_count
1084 * peval->pres.regs.table_sizes[PRES_REGTAB_OCONST];
1085 elements_param = param->bytes / sizeof(unsigned int);
1086 elements = min(elements_table, elements_param);
1087 oc = (float *)peval->pres.regs.tables[PRES_REGTAB_OCONST];
1088 for (i = 0; i < elements; ++i)
1089 set_number((unsigned int *)param_value + i, param->type, oc + i, D3DXPT_FLOAT);
1090 return D3D_OK;
1093 static HRESULT set_shader_constants_device(struct IDirect3DDevice9 *device, struct d3dx_regstore *rs,
1094 D3DXPARAMETER_TYPE type, enum pres_reg_tables table)
1096 unsigned int start, count;
1097 void *ptr;
1098 HRESULT hr, result;
1100 result = D3D_OK;
1101 start = 0;
1102 while (start < rs->table_sizes[table])
1104 count = 0;
1105 while (start < rs->table_sizes[table] && !regstore_is_val_set_reg(rs, table, start))
1106 ++start;
1107 while (start + count < rs->table_sizes[table] && regstore_is_val_set_reg(rs, table, start + count))
1108 ++count;
1109 if (!count)
1110 break;
1111 TRACE("Setting %u constants at %u.\n", count, start);
1112 ptr = (BYTE *)rs->tables[table] + start * table_info[table].reg_component_count
1113 * table_info[table].component_size;
1114 if (type == D3DXPT_VERTEXSHADER)
1116 switch(table)
1118 case PRES_REGTAB_OCONST:
1119 hr = IDirect3DDevice9_SetVertexShaderConstantF(device, start, (const float *)ptr, count);
1120 break;
1121 case PRES_REGTAB_OICONST:
1122 hr = IDirect3DDevice9_SetVertexShaderConstantI(device, start, (const int *)ptr, count);
1123 break;
1124 case PRES_REGTAB_OBCONST:
1125 hr = IDirect3DDevice9_SetVertexShaderConstantB(device, start, (const BOOL *)ptr, count);
1126 break;
1127 default:
1128 FIXME("Unexpected register table %u.\n", table);
1129 return D3DERR_INVALIDCALL;
1132 else if (type == D3DXPT_PIXELSHADER)
1134 switch(table)
1136 case PRES_REGTAB_OCONST:
1137 hr = IDirect3DDevice9_SetPixelShaderConstantF(device, start, (const float *)ptr, count);
1138 break;
1139 case PRES_REGTAB_OICONST:
1140 hr = IDirect3DDevice9_SetPixelShaderConstantI(device, start, (const int *)ptr, count);
1141 break;
1142 case PRES_REGTAB_OBCONST:
1143 hr = IDirect3DDevice9_SetPixelShaderConstantB(device, start, (const BOOL *)ptr, count);
1144 break;
1145 default:
1146 FIXME("Unexpected register table %u.\n", table);
1147 return D3DERR_INVALIDCALL;
1150 else
1152 FIXME("Unexpected parameter type %u.\n", type);
1153 return D3DERR_INVALIDCALL;
1156 if (FAILED(hr))
1158 ERR("Setting constants failed, type %u, table %u, hr %#x.\n", type, table, hr);
1159 result = hr;
1161 start += count;
1163 regstore_reset_table(rs, table);
1164 return result;
1167 HRESULT d3dx_param_eval_set_shader_constants(struct IDirect3DDevice9 *device, struct d3dx_param_eval *peval)
1169 static const enum pres_reg_tables set_tables[] =
1170 {PRES_REGTAB_OCONST, PRES_REGTAB_OICONST, PRES_REGTAB_OBCONST};
1171 HRESULT hr, result;
1172 struct d3dx_preshader *pres = &peval->pres;
1173 struct d3dx_regstore *rs = &pres->regs;
1174 unsigned int i;
1176 TRACE("device %p, peval %p, param_type %u.\n", device, peval, peval->param_type);
1178 if (FAILED(hr = set_constants(rs, &pres->inputs)))
1179 return hr;
1180 if (FAILED(hr = execute_preshader(pres)))
1181 return hr;
1182 if (FAILED(hr = set_constants(rs, &peval->shader_inputs)))
1183 return hr;
1185 result = D3D_OK;
1186 for (i = 0; i < ARRAY_SIZE(set_tables); ++i)
1188 if (FAILED(hr = set_shader_constants_device(device, rs, peval->param_type, set_tables[i])))
1189 result = hr;
1191 return result;