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
20 #include "wine/port.h"
22 #include "d3dx9_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3dx
);
54 PRESHADER_OP_DOTSWIZ6
,
55 PRESHADER_OP_DOTSWIZ8
,
58 typedef double (*pres_op_func
)(double *args
, int n
);
60 static double to_signed_nan(double v
)
64 ULONG64 ulong64_value
;
72 return isnan(v
) ? signed_nan
.double_value
: v
;
75 static double pres_mov(double *args
, int n
) {return args
[0];}
76 static double pres_add(double *args
, int n
) {return args
[0] + args
[1];}
77 static double pres_mul(double *args
, int n
) {return args
[0] * args
[1];}
78 static double pres_dot(double *args
, int n
)
84 for (i
= 0; i
< n
; ++i
)
85 sum
+= args
[i
] * args
[i
+ n
];
89 static double pres_dotswiz6(double *args
, int n
)
91 return pres_dot(args
, 3);
94 static double pres_dotswiz8(double *args
, int n
)
96 return pres_dot(args
, 4);
99 static double pres_neg(double *args
, int n
) {return -args
[0];}
100 static double pres_rcp(double *args
, int n
) {return 1.0 / args
[0];}
101 static double pres_lt(double *args
, int n
) {return args
[0] < args
[1] ? 1.0 : 0.0;}
102 static double pres_ge(double *args
, int n
) {return args
[0] >= args
[1] ? 1.0 : 0.0;}
103 static double pres_frc(double *args
, int n
) {return args
[0] - floor(args
[0]);}
104 static double pres_min(double *args
, int n
) {return fmin(args
[0], args
[1]);}
105 static double pres_max(double *args
, int n
) {return fmax(args
[0], args
[1]);}
106 static double pres_cmp(double *args
, int n
) {return args
[0] >= 0.0 ? args
[1] : args
[2];}
107 static double pres_sin(double *args
, int n
) {return sin(args
[0]);}
108 static double pres_cos(double *args
, int n
) {return cos(args
[0]);}
109 static double pres_rsq(double *args
, int n
)
117 return 1.0 / sqrt(v
);
119 static double pres_exp(double *args
, int n
) {return pow(2.0, args
[0]);}
120 static double pres_log(double *args
, int n
)
131 return log(v
) / log(2);
134 static double pres_asin(double *args
, int n
) {return to_signed_nan(asin(args
[0]));}
135 static double pres_acos(double *args
, int n
) {return to_signed_nan(acos(args
[0]));}
136 static double pres_atan(double *args
, int n
) {return atan(args
[0]);}
137 static double pres_atan2(double *args
, int n
) {return atan2(args
[0], args
[1]);}
139 /* According to the test results 'div' operation always returns 0. Compiler does not seem to ever
140 * generate it, using rcp + mul instead, so probably it is not implemented in native d3dx. */
141 static double pres_div(double *args
, int n
) {return 0.0;}
143 #define PRES_OPCODE_MASK 0x7ff00000
144 #define PRES_OPCODE_SHIFT 20
145 #define PRES_SCALAR_FLAG 0x80000000
146 #define PRES_NCOMP_MASK 0x0000ffff
148 #define FOURCC_PRES 0x53455250
149 #define FOURCC_CLIT 0x54494c43
150 #define FOURCC_FXLC 0x434c5846
151 #define FOURCC_PRSI 0x49535250
152 #define PRES_SIGN 0x46580000
158 unsigned int input_count
;
163 static const struct op_info pres_op_info
[] =
165 {0x000, "nop", 0, 0, NULL
}, /* PRESHADER_OP_NOP */
166 {0x100, "mov", 1, 0, pres_mov
}, /* PRESHADER_OP_MOV */
167 {0x101, "neg", 1, 0, pres_neg
}, /* PRESHADER_OP_NEG */
168 {0x103, "rcp", 1, 0, pres_rcp
}, /* PRESHADER_OP_RCP */
169 {0x104, "frc", 1, 0, pres_frc
}, /* PRESHADER_OP_FRC */
170 {0x105, "exp", 1, 0, pres_exp
}, /* PRESHADER_OP_EXP */
171 {0x106, "log", 1, 0, pres_log
}, /* PRESHADER_OP_LOG */
172 {0x107, "rsq", 1, 0, pres_rsq
}, /* PRESHADER_OP_RSQ */
173 {0x108, "sin", 1, 0, pres_sin
}, /* PRESHADER_OP_SIN */
174 {0x109, "cos", 1, 0, pres_cos
}, /* PRESHADER_OP_COS */
175 {0x10a, "asin", 1, 0, pres_asin
}, /* PRESHADER_OP_ASIN */
176 {0x10b, "acos", 1, 0, pres_acos
}, /* PRESHADER_OP_ACOS */
177 {0x10c, "atan", 1, 0, pres_atan
}, /* PRESHADER_OP_ATAN */
178 {0x200, "min", 2, 0, pres_min
}, /* PRESHADER_OP_MIN */
179 {0x201, "max", 2, 0, pres_max
}, /* PRESHADER_OP_MAX */
180 {0x202, "lt", 2, 0, pres_lt
}, /* PRESHADER_OP_LT */
181 {0x203, "ge", 2, 0, pres_ge
}, /* PRESHADER_OP_GE */
182 {0x204, "add", 2, 0, pres_add
}, /* PRESHADER_OP_ADD */
183 {0x205, "mul", 2, 0, pres_mul
}, /* PRESHADER_OP_MUL */
184 {0x206, "atan2", 2, 0, pres_atan2
}, /* PRESHADER_OP_ATAN2 */
185 {0x208, "div", 2, 0, pres_div
}, /* PRESHADER_OP_DIV */
186 {0x300, "cmp", 3, 0, pres_cmp
}, /* PRESHADER_OP_CMP */
187 {0x500, "dot", 2, 1, pres_dot
}, /* PRESHADER_OP_DOT */
188 {0x70e, "d3ds_dotswiz", 6, 0, pres_dotswiz6
}, /* PRESHADER_OP_DOTSWIZ6 */
189 {0x70e, "d3ds_dotswiz", 8, 0, pres_dotswiz8
}, /* PRESHADER_OP_DOTSWIZ8 */
203 unsigned int component_size
;
204 enum pres_value_type type
;
208 {sizeof(double), PRES_VT_DOUBLE
}, /* PRES_REGTAB_IMMED */
209 {sizeof(float), PRES_VT_FLOAT
}, /* PRES_REGTAB_CONST */
210 {sizeof(float), PRES_VT_FLOAT
}, /* PRES_REGTAB_OCONST */
211 {sizeof(BOOL
), PRES_VT_BOOL
}, /* PRES_REGTAB_OBCONST */
212 {sizeof(int), PRES_VT_INT
, }, /* PRES_REGTAB_OICONST */
213 /* TODO: use double precision for 64 bit */
214 {sizeof(float), PRES_VT_FLOAT
} /* PRES_REGTAB_TEMP */
217 static const char *table_symbol
[] =
219 "imm", "c", "oc", "ob", "oi", "r", "(null)",
222 static const enum pres_reg_tables pres_regset2table
[] =
224 PRES_REGTAB_OBCONST
, /* D3DXRS_BOOL */
225 PRES_REGTAB_OICONST
, /* D3DXRS_INT4 */
226 PRES_REGTAB_CONST
, /* D3DXRS_FLOAT4 */
227 PRES_REGTAB_COUNT
, /* D3DXRS_SAMPLER */
230 static const enum pres_reg_tables shad_regset2table
[] =
232 PRES_REGTAB_OBCONST
, /* D3DXRS_BOOL */
233 PRES_REGTAB_OICONST
, /* D3DXRS_INT4 */
234 PRES_REGTAB_OCONST
, /* D3DXRS_FLOAT4 */
235 PRES_REGTAB_COUNT
, /* D3DXRS_SAMPLER */
240 enum pres_reg_tables table
;
241 /* offset is component index, not register index, e. g.
242 offset for component c3.y is 13 (3 * 4 + 1) */
246 struct d3dx_pres_operand
248 struct d3dx_pres_reg reg
;
249 struct d3dx_pres_reg index_reg
;
252 #define MAX_INPUTS_COUNT 8
257 /* first input argument is scalar,
258 scalar component is propagated */
260 unsigned int component_count
;
261 struct d3dx_pres_operand inputs
[MAX_INPUTS_COUNT
];
262 struct d3dx_pres_operand output
;
265 struct const_upload_info
268 unsigned int major
, minor
;
269 unsigned int major_stride
;
270 unsigned int major_count
;
272 unsigned int minor_remainder
;
275 static enum pres_value_type
table_type_from_param_type(D3DXPARAMETER_TYPE type
)
280 return PRES_VT_FLOAT
;
286 FIXME("Unsupported type %u.\n", type
);
287 return PRES_VT_COUNT
;
291 static unsigned int get_reg_offset(unsigned int table
, unsigned int offset
)
293 return table
== PRES_REGTAB_OBCONST
? offset
: offset
>> 2;
296 static unsigned int get_offset_reg(unsigned int table
, unsigned int reg_idx
)
298 return table
== PRES_REGTAB_OBCONST
? reg_idx
: reg_idx
<< 2;
301 static unsigned int get_reg_components(unsigned int table
)
303 return get_offset_reg(table
, 1);
306 #define PRES_BITMASK_BLOCK_SIZE (sizeof(unsigned int) * 8)
308 static HRESULT
regstore_alloc_table(struct d3dx_regstore
*rs
, unsigned int table
)
312 size
= get_offset_reg(table
, rs
->table_sizes
[table
]) * table_info
[table
].component_size
;
315 rs
->tables
[table
] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
);
316 if (!rs
->tables
[table
])
317 return E_OUTOFMEMORY
;
322 static void regstore_free_tables(struct d3dx_regstore
*rs
)
326 for (i
= 0; i
< PRES_REGTAB_COUNT
; ++i
)
328 HeapFree(GetProcessHeap(), 0, rs
->tables
[i
]);
332 static void regstore_set_values(struct d3dx_regstore
*rs
, unsigned int table
, const void *data
,
333 unsigned int start_offset
, unsigned int count
)
335 BYTE
*dst
= rs
->tables
[table
];
336 const BYTE
*src
= data
;
339 dst
+= start_offset
* table_info
[table
].component_size
;
340 size
= count
* table_info
[table
].component_size
;
341 assert((src
< dst
&& size
<= dst
- src
) || (src
> dst
&& size
<= src
- dst
));
342 memcpy(dst
, src
, size
);
345 static double regstore_get_double(struct d3dx_regstore
*rs
, unsigned int table
, unsigned int offset
)
349 p
= (BYTE
*)rs
->tables
[table
] + table_info
[table
].component_size
* offset
;
350 switch (table_info
[table
].type
)
357 FIXME("Unexpected preshader input from table %u.\n", table
);
362 static void regstore_set_double(struct d3dx_regstore
*rs
, unsigned int table
, unsigned int offset
, double v
)
366 p
= (BYTE
*)rs
->tables
[table
] + table_info
[table
].component_size
* offset
;
367 switch (table_info
[table
].type
)
369 case PRES_VT_FLOAT
: *(float *)p
= v
; break;
370 case PRES_VT_DOUBLE
: *(double *)p
= v
; break;
371 case PRES_VT_INT
: *(int *)p
= lrint(v
); break;
372 case PRES_VT_BOOL
: *(BOOL
*)p
= !!v
; break;
374 FIXME("Bad type %u.\n", table_info
[table
].type
);
379 static void dump_bytecode(void *data
, unsigned int size
)
381 unsigned int *bytecode
= (unsigned int *)data
;
382 unsigned int i
, j
, n
;
384 size
/= sizeof(*bytecode
);
388 n
= min(size
- i
, 8);
389 for (j
= 0; j
< n
; ++j
)
390 TRACE("0x%08x,", bytecode
[i
+ j
]);
396 static unsigned int *find_bytecode_comment(unsigned int *ptr
, unsigned int count
,
397 unsigned int fourcc
, unsigned int *size
)
399 /* Provide at least one value in comment section on non-NULL return. */
400 while (count
> 2 && (*ptr
& 0xffff) == 0xfffe)
402 unsigned int section_size
;
404 section_size
= (*ptr
>> 16);
405 if (!section_size
|| section_size
+ 1 > count
)
407 if (*(ptr
+ 1) == fourcc
)
409 *size
= section_size
;
412 count
-= section_size
+ 1;
413 ptr
+= section_size
+ 1;
418 static unsigned int *parse_pres_reg(unsigned int *ptr
, struct d3dx_pres_reg
*reg
)
420 static const enum pres_reg_tables reg_table
[8] =
422 PRES_REGTAB_COUNT
, PRES_REGTAB_IMMED
, PRES_REGTAB_CONST
, PRES_REGTAB_COUNT
,
423 PRES_REGTAB_OCONST
, PRES_REGTAB_OBCONST
, PRES_REGTAB_OICONST
, PRES_REGTAB_TEMP
426 if (*ptr
>= ARRAY_SIZE(reg_table
) || reg_table
[*ptr
] == PRES_REGTAB_COUNT
)
428 FIXME("Unsupported register table %#x.\n", *ptr
);
432 reg
->table
= reg_table
[*ptr
++];
433 reg
->offset
= *ptr
++;
437 static unsigned int *parse_pres_arg(unsigned int *ptr
, unsigned int count
, struct d3dx_pres_operand
*opr
)
439 if (count
< 3 || (*ptr
&& count
< 5))
441 WARN("Byte code buffer ends unexpectedly, count %u.\n", count
);
449 FIXME("Unknown relative addressing flag, word %#x.\n", *ptr
);
452 ptr
= parse_pres_reg(ptr
+ 1, &opr
->index_reg
);
458 opr
->index_reg
.table
= PRES_REGTAB_COUNT
;
462 ptr
= parse_pres_reg(ptr
, &opr
->reg
);
464 if (opr
->reg
.table
== PRES_REGTAB_OBCONST
)
465 opr
->reg
.offset
/= 4;
469 static unsigned int *parse_pres_ins(unsigned int *ptr
, unsigned int count
, struct d3dx_pres_ins
*ins
)
471 unsigned int ins_code
, ins_raw
;
472 unsigned int input_count
;
477 WARN("Byte code buffer ends unexpectedly.\n");
482 ins_code
= (ins_raw
& PRES_OPCODE_MASK
) >> PRES_OPCODE_SHIFT
;
483 ins
->component_count
= ins_raw
& PRES_NCOMP_MASK
;
484 ins
->scalar_op
= !!(ins_raw
& PRES_SCALAR_FLAG
);
486 if (ins
->component_count
< 1 || ins
->component_count
> 4)
488 FIXME("Unsupported number of components %u.\n", ins
->component_count
);
491 input_count
= *ptr
++;
493 for (i
= 0; i
< ARRAY_SIZE(pres_op_info
); ++i
)
494 if (ins_code
== pres_op_info
[i
].opcode
&& input_count
== pres_op_info
[i
].input_count
)
496 if (i
== ARRAY_SIZE(pres_op_info
))
498 FIXME("Unknown opcode %#x, input_count %u, raw %#x.\n", ins_code
, input_count
, ins_raw
);
502 if (input_count
> ARRAY_SIZE(ins
->inputs
))
504 FIXME("Actual input args count %u exceeds inputs array size, instruction %s.\n", input_count
,
505 pres_op_info
[i
].mnem
);
508 for (i
= 0; i
< input_count
; ++i
)
512 p
= parse_pres_arg(ptr
, count
, &ins
->inputs
[i
]);
518 ptr
= parse_pres_arg(ptr
, count
, &ins
->output
);
519 if (ins
->output
.index_reg
.table
!= PRES_REGTAB_COUNT
)
521 FIXME("Relative addressing in output register not supported.\n");
524 if (get_reg_offset(ins
->output
.reg
.table
, ins
->output
.reg
.offset
525 + (pres_op_info
[ins
->op
].func_all_comps
? 0 : ins
->component_count
- 1))
526 != get_reg_offset(ins
->output
.reg
.table
, ins
->output
.reg
.offset
))
528 FIXME("Instructions outputting multiple registers are not supported.\n");
534 static HRESULT
get_ctab_constant_desc(ID3DXConstantTable
*ctab
, D3DXHANDLE hc
, D3DXCONSTANT_DESC
*desc
,
535 WORD
*constantinfo_reserved
)
537 const struct ctab_constant
*constant
= d3dx_shader_get_ctab_constant(ctab
, hc
);
541 FIXME("Could not get constant desc.\n");
542 if (constantinfo_reserved
)
543 *constantinfo_reserved
= 0;
544 return D3DERR_INVALIDCALL
;
546 *desc
= constant
->desc
;
547 if (constantinfo_reserved
)
548 *constantinfo_reserved
= constant
->constantinfo_reserved
;
552 static void get_const_upload_info(struct d3dx_const_param_eval_output
*const_set
,
553 struct const_upload_info
*info
)
555 struct d3dx_parameter
*param
= const_set
->param
;
556 unsigned int table
= const_set
->table
;
558 info
->transpose
= (const_set
->constant_class
== D3DXPC_MATRIX_COLUMNS
&& param
->class == D3DXPC_MATRIX_ROWS
)
559 || (param
->class == D3DXPC_MATRIX_COLUMNS
&& const_set
->constant_class
== D3DXPC_MATRIX_ROWS
);
560 if (const_set
->constant_class
== D3DXPC_MATRIX_COLUMNS
)
562 info
->major
= param
->columns
;
563 info
->minor
= param
->rows
;
567 info
->major
= param
->rows
;
568 info
->minor
= param
->columns
;
571 if (get_reg_components(table
) == 1)
573 unsigned int const_length
= get_offset_reg(table
, const_set
->register_count
);
575 info
->major_stride
= info
->minor
;
576 info
->major_count
= const_length
/ info
->major_stride
;
577 info
->minor_remainder
= const_length
% info
->major_stride
;
581 info
->major_stride
= get_reg_components(table
);
582 info
->major_count
= const_set
->register_count
;
583 info
->minor_remainder
= 0;
585 info
->count
= info
->major_count
* info
->minor
+ info
->minor_remainder
;
588 #define INITIAL_CONST_SET_SIZE 16
590 static HRESULT
append_const_set(struct d3dx_const_tab
*const_tab
, struct d3dx_const_param_eval_output
*set
)
592 if (const_tab
->const_set_count
>= const_tab
->const_set_size
)
594 unsigned int new_size
;
595 struct d3dx_const_param_eval_output
*new_alloc
;
597 if (!const_tab
->const_set_size
)
599 new_size
= INITIAL_CONST_SET_SIZE
;
600 new_alloc
= HeapAlloc(GetProcessHeap(), 0, sizeof(*const_tab
->const_set
) * new_size
);
603 ERR("Out of memory.\n");
604 return E_OUTOFMEMORY
;
609 new_size
= const_tab
->const_set_size
* 2;
610 new_alloc
= HeapReAlloc(GetProcessHeap(), 0, const_tab
->const_set
,
611 sizeof(*const_tab
->const_set
) * new_size
);
614 ERR("Out of memory.\n");
615 return E_OUTOFMEMORY
;
618 const_tab
->const_set
= new_alloc
;
619 const_tab
->const_set_size
= new_size
;
621 const_tab
->const_set
[const_tab
->const_set_count
++] = *set
;
625 static void append_pres_const_sets_for_shader_input(struct d3dx_const_tab
*const_tab
,
626 struct d3dx_preshader
*pres
)
629 struct d3dx_const_param_eval_output const_set
= {NULL
};
631 for (i
= 0; i
< pres
->ins_count
; ++i
)
633 const struct d3dx_pres_ins
*ins
= &pres
->ins
[i
];
634 const struct d3dx_pres_reg
*reg
= &ins
->output
.reg
;
636 if (reg
->table
== PRES_REGTAB_TEMP
)
639 const_set
.register_index
= get_reg_offset(reg
->table
, reg
->offset
);
640 const_set
.register_count
= 1;
641 const_set
.table
= reg
->table
;
642 const_set
.constant_class
= D3DXPC_FORCE_DWORD
;
643 const_set
.element_count
= 1;
644 append_const_set(const_tab
, &const_set
);
648 static int compare_const_set(const void *a
, const void *b
)
650 const struct d3dx_const_param_eval_output
*r1
= a
;
651 const struct d3dx_const_param_eval_output
*r2
= b
;
653 if (r1
->table
!= r2
->table
)
654 return r1
->table
- r2
->table
;
655 return r1
->register_index
- r2
->register_index
;
658 static HRESULT
merge_const_set_entries(struct d3dx_const_tab
*const_tab
,
659 struct d3dx_parameter
*param
, unsigned int index
)
661 unsigned int i
, start_index
= index
;
663 enum pres_reg_tables current_table
;
664 unsigned int current_start_offset
, element_count
;
665 struct d3dx_const_param_eval_output
*first_const
;
667 if (!const_tab
->const_set_count
)
670 while (index
< const_tab
->const_set_count
- 1)
672 first_const
= &const_tab
->const_set
[index
];
673 current_data
= first_const
->param
->data
;
674 current_table
= first_const
->table
;
675 current_start_offset
= get_offset_reg(current_table
, first_const
->register_index
);
677 for (i
= index
; i
< const_tab
->const_set_count
; ++i
)
679 struct d3dx_const_param_eval_output
*const_set
= &const_tab
->const_set
[i
];
680 unsigned int count
= get_offset_reg(const_set
->table
,
681 const_set
->register_count
* const_set
->element_count
);
682 unsigned int start_offset
= get_offset_reg(const_set
->table
, const_set
->register_index
);
684 if (!(const_set
->table
== current_table
&& current_start_offset
== start_offset
685 && const_set
->direct_copy
== first_const
->direct_copy
686 && current_data
== const_set
->param
->data
687 && (const_set
->direct_copy
|| (first_const
->param
->type
== const_set
->param
->type
688 && first_const
->param
->class == const_set
->param
->class
689 && first_const
->param
->columns
== const_set
->param
->columns
690 && first_const
->param
->rows
== const_set
->param
->rows
691 && first_const
->register_count
== const_set
->register_count
692 && (i
== const_tab
->const_set_count
- 1
693 || first_const
->param
->element_count
== const_set
->param
->element_count
)))))
696 current_start_offset
+= count
;
697 current_data
+= const_set
->direct_copy
? count
: const_set
->param
->rows
698 * const_set
->param
->columns
* const_set
->element_count
;
699 element_count
+= const_set
->element_count
;
704 TRACE("Merging %u child parameters for %s, not merging %u, direct_copy %#x.\n", i
- index
,
705 debugstr_a(param
->name
), const_tab
->const_set_count
- i
, first_const
->direct_copy
);
707 first_const
->element_count
= element_count
;
708 if (first_const
->direct_copy
)
710 first_const
->element_count
= 1;
711 if (index
== start_index
712 && !(param
->type
== D3DXPT_VOID
&& param
->class == D3DXPC_STRUCT
))
714 if (table_type_from_param_type(param
->type
) == PRES_VT_COUNT
)
715 return D3DERR_INVALIDCALL
;
716 first_const
->param
= param
;
718 first_const
->register_count
= get_reg_offset(current_table
, current_start_offset
)
719 - first_const
->register_index
;
721 memmove(&const_tab
->const_set
[index
+ 1], &const_tab
->const_set
[i
],
722 sizeof(*const_tab
->const_set
) * (const_tab
->const_set_count
- i
));
723 const_tab
->const_set_count
-= i
- index
- 1;
727 TRACE("Not merging %u child parameters for %s, direct_copy %#x.\n",
728 const_tab
->const_set_count
- i
, debugstr_a(param
->name
), first_const
->direct_copy
);
735 static HRESULT
init_set_constants_param(struct d3dx_const_tab
*const_tab
, ID3DXConstantTable
*ctab
,
736 D3DXHANDLE hc
, struct d3dx_parameter
*param
)
738 D3DXCONSTANT_DESC desc
;
739 unsigned int const_count
, param_count
, i
;
741 struct d3dx_const_param_eval_output const_set
;
742 struct const_upload_info info
;
743 enum pres_value_type table_type
;
746 if (FAILED(get_ctab_constant_desc(ctab
, hc
, &desc
, NULL
)))
747 return D3DERR_INVALIDCALL
;
749 if (param
->element_count
)
751 param_count
= param
->element_count
;
752 const_count
= desc
.Elements
;
757 if (desc
.Elements
> 1)
759 FIXME("Unexpected number of constant elements %u.\n", desc
.Elements
);
760 return D3DERR_INVALIDCALL
;
762 param_count
= param
->member_count
;
763 const_count
= desc
.StructMembers
;
766 if (const_count
!= param_count
)
768 FIXME("Number of elements or struct members differs between parameter (%u) and constant (%u).\n",
769 param_count
, const_count
);
770 return D3DERR_INVALIDCALL
;
774 HRESULT ret
= D3D_OK
;
775 D3DXHANDLE hc_element
;
776 unsigned int index
= const_tab
->const_set_count
;
778 for (i
= 0; i
< const_count
; ++i
)
781 hc_element
= ID3DXConstantTable_GetConstantElement(ctab
, hc
, i
);
783 hc_element
= ID3DXConstantTable_GetConstant(ctab
, hc
, i
);
786 FIXME("Could not get constant.\n");
787 hr
= D3DERR_INVALIDCALL
;
791 hr
= init_set_constants_param(const_tab
, ctab
, hc_element
, ¶m
->members
[i
]);
798 return merge_const_set_entries(const_tab
, param
, index
);
801 TRACE("Constant %s, rows %u, columns %u, class %u, bytes %u.\n",
802 debugstr_a(desc
.Name
), desc
.Rows
, desc
.Columns
, desc
.Class
, desc
.Bytes
);
803 TRACE("Parameter %s, rows %u, columns %u, class %u, flags %#x, bytes %u.\n",
804 debugstr_a(param
->name
), param
->rows
, param
->columns
, param
->class,
805 param
->flags
, param
->bytes
);
807 const_set
.element_count
= 1;
808 const_set
.param
= param
;
809 const_set
.constant_class
= desc
.Class
;
810 if (desc
.RegisterSet
>= ARRAY_SIZE(shad_regset2table
))
812 FIXME("Unknown register set %u.\n", desc
.RegisterSet
);
813 return D3DERR_INVALIDCALL
;
815 const_set
.register_index
= desc
.RegisterIndex
;
816 const_set
.table
= const_tab
->regset2table
[desc
.RegisterSet
];
817 if (const_set
.table
>= PRES_REGTAB_COUNT
)
819 ERR("Unexpected register set %u.\n", desc
.RegisterSet
);
820 return D3DERR_INVALIDCALL
;
822 assert(table_info
[const_set
.table
].component_size
== sizeof(unsigned int));
823 assert(param
->bytes
/ (param
->rows
* param
->columns
) == sizeof(unsigned int));
824 const_set
.register_count
= desc
.RegisterCount
;
825 table_type
= table_info
[const_set
.table
].type
;
826 get_const_upload_info(&const_set
, &info
);
829 TRACE("%s has zero count, skipping.\n", debugstr_a(param
->name
));
833 if (table_type_from_param_type(param
->type
) == PRES_VT_COUNT
)
834 return D3DERR_INVALIDCALL
;
836 const_set
.direct_copy
= table_type_from_param_type(param
->type
) == table_type
837 && !info
.transpose
&& info
.minor
== info
.major_stride
838 && info
.count
== get_offset_reg(const_set
.table
, const_set
.register_count
)
839 && info
.count
* sizeof(unsigned int) <= param
->bytes
;
840 if (info
.minor_remainder
&& !const_set
.direct_copy
&& !info
.transpose
)
841 FIXME("Incomplete last row for not transposed matrix which cannot be directly copied, parameter %s.\n",
842 debugstr_a(param
->name
));
844 if (info
.major_count
> info
.major
845 || (info
.major_count
== info
.major
&& info
.minor_remainder
))
847 WARN("Constant dimensions exceed parameter size.\n");
848 return D3DERR_INVALIDCALL
;
851 if (FAILED(hr
= append_const_set(const_tab
, &const_set
)))
857 static HRESULT
get_constants_desc(unsigned int *byte_code
, struct d3dx_const_tab
*out
,
858 struct d3dx9_base_effect
*base
, const char **skip_constants
,
859 unsigned int skip_constants_count
, struct d3dx_preshader
*pres
)
861 ID3DXConstantTable
*ctab
;
862 D3DXCONSTANT_DESC
*cdesc
;
863 struct d3dx_parameter
**inputs_param
;
864 D3DXCONSTANTTABLE_DESC desc
;
869 hr
= D3DXGetShaderConstantTable(byte_code
, &ctab
);
870 if (FAILED(hr
) || !ctab
)
872 TRACE("Could not get CTAB data, hr %#x.\n", hr
);
873 /* returning OK, shaders and preshaders without CTAB are valid */
876 if (FAILED(hr
= ID3DXConstantTable_GetDesc(ctab
, &desc
)))
878 FIXME("Could not get CTAB desc, hr %#x.\n", hr
);
882 out
->inputs
= cdesc
= HeapAlloc(GetProcessHeap(), 0, sizeof(*cdesc
) * desc
.Constants
);
883 out
->inputs_param
= inputs_param
= HeapAlloc(GetProcessHeap(), 0, sizeof(*inputs_param
) * desc
.Constants
);
884 if (!cdesc
|| !inputs_param
)
890 for (i
= 0; i
< desc
.Constants
; ++i
)
892 unsigned int index
= out
->input_count
;
893 WORD constantinfo_reserved
;
895 hc
= ID3DXConstantTable_GetConstant(ctab
, NULL
, i
);
898 FIXME("Null constant handle.\n");
901 if (FAILED(hr
= get_ctab_constant_desc(ctab
, hc
, &cdesc
[index
], &constantinfo_reserved
)))
903 inputs_param
[index
] = get_parameter_by_name(base
, NULL
, cdesc
[index
].Name
);
904 if (!inputs_param
[index
])
906 WARN("Could not find parameter %s in effect.\n", cdesc
[index
].Name
);
909 if (cdesc
[index
].Class
== D3DXPC_OBJECT
)
911 TRACE("Object %s, parameter %p.\n", cdesc
[index
].Name
, inputs_param
[index
]);
912 if (cdesc
[index
].RegisterSet
!= D3DXRS_SAMPLER
|| inputs_param
[index
]->class != D3DXPC_OBJECT
913 || !is_param_type_sampler(inputs_param
[index
]->type
))
915 WARN("Unexpected object type, constant %s.\n", debugstr_a(cdesc
[index
].Name
));
916 hr
= D3DERR_INVALIDCALL
;
919 if (max(inputs_param
[index
]->element_count
, 1) < cdesc
[index
].RegisterCount
)
921 WARN("Register count exceeds parameter size, constant %s.\n", debugstr_a(cdesc
[index
].Name
));
922 hr
= D3DERR_INVALIDCALL
;
926 if (!is_top_level_parameter(inputs_param
[index
]))
928 WARN("Expected top level parameter '%s'.\n", debugstr_a(cdesc
[index
].Name
));
933 for (j
= 0; j
< skip_constants_count
; ++j
)
935 if (!strcmp(cdesc
[index
].Name
, skip_constants
[j
]))
937 if (!constantinfo_reserved
)
939 WARN("skip_constants parameter %s is not register bound.\n",
941 hr
= D3DERR_INVALIDCALL
;
944 TRACE("Skipping constant %s.\n", cdesc
[index
].Name
);
948 if (j
< skip_constants_count
)
951 if (inputs_param
[index
]->class == D3DXPC_OBJECT
)
953 if (FAILED(hr
= init_set_constants_param(out
, ctab
, hc
, inputs_param
[index
])))
957 append_pres_const_sets_for_shader_input(out
, pres
);
958 if (out
->const_set_count
)
960 struct d3dx_const_param_eval_output
*new_alloc
;
962 qsort(out
->const_set
, out
->const_set_count
, sizeof(*out
->const_set
), compare_const_set
);
965 while (i
< out
->const_set_count
- 1)
967 if (out
->const_set
[i
].constant_class
== D3DXPC_FORCE_DWORD
968 && out
->const_set
[i
+ 1].constant_class
== D3DXPC_FORCE_DWORD
969 && out
->const_set
[i
].table
== out
->const_set
[i
+ 1].table
970 && out
->const_set
[i
].register_index
+ out
->const_set
[i
].register_count
971 >= out
->const_set
[i
+ 1].register_index
)
973 assert(out
->const_set
[i
].register_index
+ out
->const_set
[i
].register_count
974 <= out
->const_set
[i
+ 1].register_index
+ 1);
975 out
->const_set
[i
].register_count
= out
->const_set
[i
+ 1].register_index
+ 1
976 - out
->const_set
[i
].register_index
;
977 memmove(&out
->const_set
[i
+ 1], &out
->const_set
[i
+ 2], sizeof(out
->const_set
[i
])
978 * (out
->const_set_count
- i
- 2));
979 --out
->const_set_count
;
987 new_alloc
= HeapReAlloc(GetProcessHeap(), 0, out
->const_set
,
988 sizeof(*out
->const_set
) * out
->const_set_count
);
991 out
->const_set
= new_alloc
;
992 out
->const_set_size
= out
->const_set_count
;
996 WARN("Out of memory.\n");
1000 ID3DXConstantTable_Release(ctab
);
1004 static void update_table_size(unsigned int *table_sizes
, unsigned int table
, unsigned int max_register
)
1006 if (table
< PRES_REGTAB_COUNT
)
1007 table_sizes
[table
] = max(table_sizes
[table
], max_register
+ 1);
1010 static void update_table_sizes_consts(unsigned int *table_sizes
, struct d3dx_const_tab
*ctab
)
1012 unsigned int i
, table
, max_register
;
1014 for (i
= 0; i
< ctab
->input_count
; ++i
)
1016 if (!ctab
->inputs
[i
].RegisterCount
)
1018 max_register
= ctab
->inputs
[i
].RegisterIndex
+ ctab
->inputs
[i
].RegisterCount
- 1;
1019 table
= ctab
->regset2table
[ctab
->inputs
[i
].RegisterSet
];
1020 update_table_size(table_sizes
, table
, max_register
);
1024 static void dump_arg(struct d3dx_regstore
*rs
, const struct d3dx_pres_operand
*arg
, int component_count
)
1026 static const char *xyzw_str
= "xyzw";
1027 unsigned int i
, table
;
1029 table
= arg
->reg
.table
;
1030 if (table
== PRES_REGTAB_IMMED
&& arg
->index_reg
.table
== PRES_REGTAB_COUNT
)
1033 for (i
= 0; i
< component_count
; ++i
)
1034 TRACE(i
< component_count
- 1 ? "%.16e, " : "%.16e",
1035 ((double *)rs
->tables
[PRES_REGTAB_IMMED
])[arg
->reg
.offset
+ i
]);
1040 if (arg
->index_reg
.table
== PRES_REGTAB_COUNT
)
1042 TRACE("%s%u.", table_symbol
[table
], get_reg_offset(table
, arg
->reg
.offset
));
1046 unsigned int index_reg
;
1048 index_reg
= get_reg_offset(arg
->index_reg
.table
, arg
->index_reg
.offset
);
1049 TRACE("%s[%u + %s%u.%c].", table_symbol
[table
], get_reg_offset(table
, arg
->reg
.offset
),
1050 table_symbol
[arg
->index_reg
.table
], index_reg
,
1051 xyzw_str
[arg
->index_reg
.offset
- get_offset_reg(arg
->index_reg
.table
, index_reg
)]);
1053 for (i
= 0; i
< component_count
; ++i
)
1054 TRACE("%c", xyzw_str
[(arg
->reg
.offset
+ i
) % 4]);
1058 static void dump_registers(struct d3dx_const_tab
*ctab
)
1060 unsigned int table
, i
;
1062 for (i
= 0; i
< ctab
->input_count
; ++i
)
1064 table
= ctab
->regset2table
[ctab
->inputs
[i
].RegisterSet
];
1065 TRACE("// %-12s %s%-4u %u\n", ctab
->inputs_param
[i
] ? ctab
->inputs_param
[i
]->name
: "(nil)",
1066 table_symbol
[table
], ctab
->inputs
[i
].RegisterIndex
, ctab
->inputs
[i
].RegisterCount
);
1070 static void dump_ins(struct d3dx_regstore
*rs
, const struct d3dx_pres_ins
*ins
)
1074 TRACE("%s ", pres_op_info
[ins
->op
].mnem
);
1075 dump_arg(rs
, &ins
->output
, pres_op_info
[ins
->op
].func_all_comps
? 1 : ins
->component_count
);
1076 for (i
= 0; i
< pres_op_info
[ins
->op
].input_count
; ++i
)
1079 dump_arg(rs
, &ins
->inputs
[i
], ins
->scalar_op
&& !i
? 1 : ins
->component_count
);
1084 static void dump_preshader(struct d3dx_preshader
*pres
)
1086 unsigned int i
, immediate_count
= pres
->regs
.table_sizes
[PRES_REGTAB_IMMED
] * 4;
1087 const double *immediates
= pres
->regs
.tables
[PRES_REGTAB_IMMED
];
1089 if (immediate_count
)
1090 TRACE("// Immediates:\n");
1091 for (i
= 0; i
< immediate_count
; ++i
)
1095 TRACE("%.8e", immediates
[i
]);
1101 TRACE("// Preshader registers:\n");
1102 dump_registers(&pres
->inputs
);
1103 TRACE("preshader\n");
1104 for (i
= 0; i
< pres
->ins_count
; ++i
)
1105 dump_ins(&pres
->regs
, &pres
->ins
[i
]);
1108 static HRESULT
parse_preshader(struct d3dx_preshader
*pres
, unsigned int *ptr
, unsigned int count
, struct d3dx9_base_effect
*base
)
1111 unsigned int i
, j
, const_count
;
1114 unsigned int saved_word
;
1115 unsigned int section_size
;
1117 TRACE("Preshader version %#x.\n", *ptr
& 0xffff);
1121 WARN("Unexpected end of byte code buffer.\n");
1122 return D3DXERR_INVALIDDATA
;
1125 p
= find_bytecode_comment(ptr
+ 1, count
- 1, FOURCC_CLIT
, §ion_size
);
1129 if (const_count
> (section_size
- 1) / (sizeof(double) / sizeof(unsigned int)))
1131 WARN("Byte code buffer ends unexpectedly.\n");
1132 return D3DXERR_INVALIDDATA
;
1134 dconst
= (double *)p
;
1141 TRACE("%u double constants.\n", const_count
);
1143 p
= find_bytecode_comment(ptr
+ 1, count
- 1, FOURCC_FXLC
, §ion_size
);
1146 WARN("Could not find preshader code.\n");
1149 pres
->ins_count
= *p
++;
1151 if (pres
->ins_count
> UINT_MAX
/ sizeof(*pres
->ins
))
1153 WARN("Invalid instruction count %u.\n", pres
->ins_count
);
1154 return D3DXERR_INVALIDDATA
;
1156 TRACE("%u instructions.\n", pres
->ins_count
);
1157 pres
->ins
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*pres
->ins
) * pres
->ins_count
);
1159 return E_OUTOFMEMORY
;
1160 for (i
= 0; i
< pres
->ins_count
; ++i
)
1162 unsigned int *ptr_next
;
1164 ptr_next
= parse_pres_ins(p
, section_size
, &pres
->ins
[i
]);
1166 return D3DXERR_INVALIDDATA
;
1167 section_size
-= ptr_next
- p
;
1171 pres
->inputs
.regset2table
= pres_regset2table
;
1175 hr
= get_constants_desc(ptr
, &pres
->inputs
, base
, NULL
, 0, NULL
);
1180 if (const_count
% get_reg_components(PRES_REGTAB_IMMED
))
1182 FIXME("const_count %u is not a multiple of %u.\n", const_count
,
1183 get_reg_components(PRES_REGTAB_IMMED
));
1184 return D3DXERR_INVALIDDATA
;
1186 pres
->regs
.table_sizes
[PRES_REGTAB_IMMED
] = get_reg_offset(PRES_REGTAB_IMMED
, const_count
);
1188 update_table_sizes_consts(pres
->regs
.table_sizes
, &pres
->inputs
);
1189 for (i
= 0; i
< pres
->ins_count
; ++i
)
1191 for (j
= 0; j
< pres_op_info
[pres
->ins
[i
].op
].input_count
; ++j
)
1193 enum pres_reg_tables table
;
1194 unsigned int reg_idx
;
1196 if (pres
->ins
[i
].inputs
[j
].index_reg
.table
== PRES_REGTAB_COUNT
)
1198 unsigned int last_component_index
= pres
->ins
[i
].scalar_op
&& !j
? 0
1199 : pres
->ins
[i
].component_count
- 1;
1201 table
= pres
->ins
[i
].inputs
[j
].reg
.table
;
1202 reg_idx
= get_reg_offset(table
, pres
->ins
[i
].inputs
[j
].reg
.offset
1203 + last_component_index
);
1207 table
= pres
->ins
[i
].inputs
[j
].index_reg
.table
;
1208 reg_idx
= get_reg_offset(table
, pres
->ins
[i
].inputs
[j
].index_reg
.offset
);
1210 if (reg_idx
>= pres
->regs
.table_sizes
[table
])
1212 FIXME("Out of bounds register index, i %u, j %u, table %u, reg_idx %u.\n",
1213 i
, j
, table
, reg_idx
);
1214 return D3DXERR_INVALIDDATA
;
1217 update_table_size(pres
->regs
.table_sizes
, pres
->ins
[i
].output
.reg
.table
,
1218 get_reg_offset(pres
->ins
[i
].output
.reg
.table
, pres
->ins
[i
].output
.reg
.offset
));
1220 if (FAILED(regstore_alloc_table(&pres
->regs
, PRES_REGTAB_IMMED
)))
1221 return E_OUTOFMEMORY
;
1222 regstore_set_values(&pres
->regs
, PRES_REGTAB_IMMED
, dconst
, 0, const_count
);
1227 HRESULT
d3dx_create_param_eval(struct d3dx9_base_effect
*base_effect
, void *byte_code
, unsigned int byte_code_size
,
1228 D3DXPARAMETER_TYPE type
, struct d3dx_param_eval
**peval_out
, ULONG64
*version_counter
,
1229 const char **skip_constants
, unsigned int skip_constants_count
)
1231 struct d3dx_param_eval
*peval
;
1232 unsigned int *ptr
, *shader_ptr
= NULL
;
1235 unsigned int count
, pres_size
;
1238 TRACE("base_effect %p, byte_code %p, byte_code_size %u, type %u, peval_out %p.\n",
1239 base_effect
, byte_code
, byte_code_size
, type
, peval_out
);
1241 count
= byte_code_size
/ sizeof(unsigned int);
1242 if (!byte_code
|| !count
)
1248 peval
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*peval
));
1251 ret
= E_OUTOFMEMORY
;
1254 peval
->version_counter
= version_counter
;
1256 peval
->param_type
= type
;
1259 case D3DXPT_VERTEXSHADER
:
1260 case D3DXPT_PIXELSHADER
:
1267 peval
->shader_inputs
.regset2table
= shad_regset2table
;
1269 ptr
= (unsigned int *)byte_code
;
1272 if ((*ptr
& 0xfffe0000) != 0xfffe0000)
1274 FIXME("Invalid shader signature %#x.\n", *ptr
);
1275 ret
= D3DXERR_INVALIDDATA
;
1278 TRACE("Shader version %#x.\n", *ptr
& 0xffff);
1280 ptr
= find_bytecode_comment(ptr
+ 1, count
- 1, FOURCC_PRES
, &pres_size
);
1282 TRACE("No preshader found.\n");
1289 if (ptr
&& FAILED(ret
= parse_preshader(&peval
->pres
, ptr
, pres_size
, base_effect
)))
1291 FIXME("Failed parsing preshader, byte code for analysis follows.\n");
1292 dump_bytecode(byte_code
, byte_code_size
);
1298 if (FAILED(ret
= get_constants_desc(shader_ptr
, &peval
->shader_inputs
, base_effect
,
1299 skip_constants
, skip_constants_count
, &peval
->pres
)))
1301 TRACE("Could not get shader constant table, hr %#x.\n", ret
);
1304 update_table_sizes_consts(peval
->pres
.regs
.table_sizes
, &peval
->shader_inputs
);
1307 for (i
= PRES_REGTAB_FIRST_SHADER
; i
< PRES_REGTAB_COUNT
; ++i
)
1309 if (FAILED(ret
= regstore_alloc_table(&peval
->pres
.regs
, i
)))
1315 dump_bytecode(byte_code
, byte_code_size
);
1316 dump_preshader(&peval
->pres
);
1319 TRACE("// Shader registers:\n");
1320 dump_registers(&peval
->shader_inputs
);
1324 TRACE("Created parameter evaluator %p.\n", *peval_out
);
1328 WARN("Error creating parameter evaluator.\n");
1330 dump_bytecode(byte_code
, byte_code_size
);
1332 d3dx_free_param_eval(peval
);
1337 static void d3dx_free_const_tab(struct d3dx_const_tab
*ctab
)
1339 HeapFree(GetProcessHeap(), 0, ctab
->inputs
);
1340 HeapFree(GetProcessHeap(), 0, ctab
->inputs_param
);
1341 HeapFree(GetProcessHeap(), 0, ctab
->const_set
);
1344 static void d3dx_free_preshader(struct d3dx_preshader
*pres
)
1346 HeapFree(GetProcessHeap(), 0, pres
->ins
);
1348 regstore_free_tables(&pres
->regs
);
1349 d3dx_free_const_tab(&pres
->inputs
);
1352 void d3dx_free_param_eval(struct d3dx_param_eval
*peval
)
1354 TRACE("peval %p.\n", peval
);
1359 d3dx_free_preshader(&peval
->pres
);
1360 d3dx_free_const_tab(&peval
->shader_inputs
);
1361 HeapFree(GetProcessHeap(), 0, peval
);
1364 static void pres_int_from_float(void *out
, const void *in
, unsigned int count
)
1367 const float *in_float
= in
;
1370 for (i
= 0; i
< count
; ++i
)
1371 out_int
[i
] = in_float
[i
];
1374 static void pres_bool_from_value(void *out
, const void *in
, unsigned int count
)
1377 const DWORD
*in_dword
= in
;
1378 BOOL
*out_bool
= out
;
1380 for (i
= 0; i
< count
; ++i
)
1381 out_bool
[i
] = !!in_dword
[i
];
1384 static void pres_float_from_int(void *out
, const void *in
, unsigned int count
)
1387 const int *in_int
= in
;
1388 float *out_float
= out
;
1390 for (i
= 0; i
< count
; ++i
)
1391 out_float
[i
] = in_int
[i
];
1394 static void pres_float_from_bool(void *out
, const void *in
, unsigned int count
)
1397 const BOOL
*in_bool
= in
;
1398 float *out_float
= out
;
1400 for (i
= 0; i
< count
; ++i
)
1401 out_float
[i
] = !!in_bool
[i
];
1404 static void pres_int_from_bool(void *out
, const void *in
, unsigned int count
)
1407 const float *in_bool
= in
;
1410 for (i
= 0; i
< count
; ++i
)
1411 out_int
[i
] = !!in_bool
[i
];
1414 static void regstore_set_data(struct d3dx_regstore
*rs
, unsigned int table
,
1415 unsigned int offset
, const unsigned int *in
, unsigned int count
, enum pres_value_type param_type
)
1417 typedef void (*conv_func
)(void *out
, const void *in
, unsigned int count
);
1418 static const conv_func set_const_funcs
[PRES_VT_COUNT
][PRES_VT_COUNT
] =
1420 {NULL
, NULL
, pres_int_from_float
, pres_bool_from_value
},
1421 {NULL
, NULL
, NULL
, NULL
},
1422 {pres_float_from_int
, NULL
, NULL
, pres_bool_from_value
},
1423 {pres_float_from_bool
, NULL
, pres_int_from_bool
, NULL
}
1425 enum pres_value_type table_type
= table_info
[table
].type
;
1427 if (param_type
== table_type
)
1429 regstore_set_values(rs
, table
, in
, offset
, count
);
1433 set_const_funcs
[param_type
][table_type
]((unsigned int *)rs
->tables
[table
] + offset
, in
, count
);
1436 static HRESULT
set_constants_device(ID3DXEffectStateManager
*manager
, struct IDirect3DDevice9
*device
,
1437 D3DXPARAMETER_TYPE type
, enum pres_reg_tables table
, void *ptr
,
1438 unsigned int start
, unsigned int count
)
1440 if (type
== D3DXPT_VERTEXSHADER
)
1444 case PRES_REGTAB_OCONST
:
1445 return SET_D3D_STATE_(manager
, device
, SetVertexShaderConstantF
, start
, ptr
, count
);
1446 case PRES_REGTAB_OICONST
:
1447 return SET_D3D_STATE_(manager
, device
, SetVertexShaderConstantI
, start
, ptr
, count
);
1448 case PRES_REGTAB_OBCONST
:
1449 return SET_D3D_STATE_(manager
, device
, SetVertexShaderConstantB
, start
, ptr
, count
);
1451 FIXME("Unexpected register table %u.\n", table
);
1452 return D3DERR_INVALIDCALL
;
1455 else if (type
== D3DXPT_PIXELSHADER
)
1459 case PRES_REGTAB_OCONST
:
1460 return SET_D3D_STATE_(manager
, device
, SetPixelShaderConstantF
, start
, ptr
, count
);
1461 case PRES_REGTAB_OICONST
:
1462 return SET_D3D_STATE_(manager
, device
, SetPixelShaderConstantI
, start
, ptr
, count
);
1463 case PRES_REGTAB_OBCONST
:
1464 return SET_D3D_STATE_(manager
, device
, SetPixelShaderConstantB
, start
, ptr
, count
);
1466 FIXME("Unexpected register table %u.\n", table
);
1467 return D3DERR_INVALIDCALL
;
1472 FIXME("Unexpected parameter type %u.\n", type
);
1473 return D3DERR_INVALIDCALL
;
1477 static HRESULT
set_constants(struct d3dx_regstore
*rs
, struct d3dx_const_tab
*const_tab
,
1478 ULONG64 new_update_version
, ID3DXEffectStateManager
*manager
, struct IDirect3DDevice9
*device
,
1479 D3DXPARAMETER_TYPE type
, BOOL device_update_all
, BOOL pres_dirty
)
1481 unsigned int const_idx
;
1482 unsigned int current_start
= 0, current_count
= 0;
1483 enum pres_reg_tables current_table
= PRES_REGTAB_COUNT
;
1484 BOOL update_device
= manager
|| device
;
1485 HRESULT hr
, result
= D3D_OK
;
1486 ULONG64 update_version
= const_tab
->update_version
;
1488 for (const_idx
= 0; const_idx
< const_tab
->const_set_count
; ++const_idx
)
1490 struct d3dx_const_param_eval_output
*const_set
= &const_tab
->const_set
[const_idx
];
1491 enum pres_reg_tables table
= const_set
->table
;
1492 struct d3dx_parameter
*param
= const_set
->param
;
1493 unsigned int element
, i
, j
, start_offset
;
1494 struct const_upload_info info
;
1496 enum pres_value_type param_type
;
1498 if (!(param
&& is_param_dirty(param
, update_version
)))
1502 start_offset
= get_offset_reg(table
, const_set
->register_index
);
1503 if (const_set
->direct_copy
)
1505 regstore_set_values(rs
, table
, data
, start_offset
,
1506 get_offset_reg(table
, const_set
->register_count
));
1509 param_type
= table_type_from_param_type(param
->type
);
1510 if (const_set
->constant_class
== D3DXPC_SCALAR
|| const_set
->constant_class
== D3DXPC_VECTOR
)
1512 unsigned int count
= max(param
->rows
, param
->columns
);
1514 if (count
>= get_reg_components(table
))
1516 regstore_set_data(rs
, table
, start_offset
, data
,
1517 count
* const_set
->element_count
, param_type
);
1521 for (element
= 0; element
< const_set
->element_count
; ++element
)
1522 regstore_set_data(rs
, table
, start_offset
+ get_offset_reg(table
, element
),
1523 &data
[element
* count
], count
, param_type
);
1527 get_const_upload_info(const_set
, &info
);
1528 for (element
= 0; element
< const_set
->element_count
; ++element
)
1530 unsigned int *out
= (unsigned int *)rs
->tables
[table
] + start_offset
;
1532 /* Store reshaped but (possibly) not converted yet data temporarily in the same constants buffer.
1533 * All the supported types of parameters and table values have the same size. */
1536 for (i
= 0; i
< info
.major_count
; ++i
)
1537 for (j
= 0; j
< info
.minor
; ++j
)
1538 out
[i
* info
.major_stride
+ j
] = data
[i
+ j
* info
.major
];
1540 for (j
= 0; j
< info
.minor_remainder
; ++j
)
1541 out
[i
* info
.major_stride
+ j
] = data
[i
+ j
* info
.major
];
1545 for (i
= 0; i
< info
.major_count
; ++i
)
1546 for (j
= 0; j
< info
.minor
; ++j
)
1547 out
[i
* info
.major_stride
+ j
] = data
[i
* info
.minor
+ j
];
1549 start_offset
+= get_offset_reg(table
, const_set
->register_count
);
1550 data
+= param
->rows
* param
->columns
;
1552 start_offset
= get_offset_reg(table
, const_set
->register_index
);
1553 if (table_info
[table
].type
!= param_type
)
1554 regstore_set_data(rs
, table
, start_offset
, (unsigned int *)rs
->tables
[table
] + start_offset
,
1555 get_offset_reg(table
, const_set
->register_count
) * const_set
->element_count
, param_type
);
1557 const_tab
->update_version
= new_update_version
;
1561 for (const_idx
= 0; const_idx
< const_tab
->const_set_count
; ++const_idx
)
1563 struct d3dx_const_param_eval_output
*const_set
= &const_tab
->const_set
[const_idx
];
1565 if (device_update_all
|| (const_set
->param
1566 ? is_param_dirty(const_set
->param
, update_version
) : pres_dirty
))
1568 enum pres_reg_tables table
= const_set
->table
;
1570 if (table
== current_table
&& current_start
+ current_count
== const_set
->register_index
)
1572 current_count
+= const_set
->register_count
* const_set
->element_count
;
1578 if (FAILED(hr
= set_constants_device(manager
, device
, type
, current_table
,
1579 (DWORD
*)rs
->tables
[current_table
]
1580 + get_offset_reg(current_table
, current_start
), current_start
, current_count
)))
1583 current_table
= table
;
1584 current_start
= const_set
->register_index
;
1585 current_count
= const_set
->register_count
* const_set
->element_count
;
1591 if (FAILED(hr
= set_constants_device(manager
, device
, type
, current_table
,
1592 (DWORD
*)rs
->tables
[current_table
]
1593 + get_offset_reg(current_table
, current_start
), current_start
, current_count
)))
1599 static double exec_get_reg_value(struct d3dx_regstore
*rs
, enum pres_reg_tables table
, unsigned int offset
)
1601 return regstore_get_double(rs
, table
, offset
);
1604 static double exec_get_arg(struct d3dx_regstore
*rs
, const struct d3dx_pres_operand
*opr
, unsigned int comp
)
1606 unsigned int offset
, base_index
, reg_index
, table
;
1608 table
= opr
->reg
.table
;
1610 if (opr
->index_reg
.table
== PRES_REGTAB_COUNT
)
1613 base_index
= lrint(exec_get_reg_value(rs
, opr
->index_reg
.table
, opr
->index_reg
.offset
));
1615 offset
= get_offset_reg(table
, base_index
) + opr
->reg
.offset
+ comp
;
1616 reg_index
= get_reg_offset(table
, offset
);
1618 if (reg_index
>= rs
->table_sizes
[table
])
1620 unsigned int wrap_size
;
1622 if (table
== PRES_REGTAB_CONST
)
1624 /* As it can be guessed from tests, offset into floating constant table is wrapped
1625 * to the nearest power of 2 and not to the actual table size. */
1626 for (wrap_size
= 1; wrap_size
< rs
->table_sizes
[table
]; wrap_size
<<= 1)
1631 wrap_size
= rs
->table_sizes
[table
];
1633 WARN("Wrapping register index %u, table %u, wrap_size %u, table size %u.\n",
1634 reg_index
, table
, wrap_size
, rs
->table_sizes
[table
]);
1635 reg_index
%= wrap_size
;
1637 if (reg_index
>= rs
->table_sizes
[table
])
1640 offset
= get_offset_reg(table
, reg_index
) + offset
% get_reg_components(table
);
1643 return exec_get_reg_value(rs
, table
, offset
);
1646 static void exec_set_arg(struct d3dx_regstore
*rs
, const struct d3dx_pres_reg
*reg
,
1647 unsigned int comp
, double res
)
1649 regstore_set_double(rs
, reg
->table
, reg
->offset
+ comp
, res
);
1652 #define ARGS_ARRAY_SIZE 8
1653 static HRESULT
execute_preshader(struct d3dx_preshader
*pres
)
1655 unsigned int i
, j
, k
;
1656 double args
[ARGS_ARRAY_SIZE
];
1659 for (i
= 0; i
< pres
->ins_count
; ++i
)
1661 const struct d3dx_pres_ins
*ins
;
1662 const struct op_info
*oi
;
1664 ins
= &pres
->ins
[i
];
1665 oi
= &pres_op_info
[ins
->op
];
1666 if (oi
->func_all_comps
)
1668 if (oi
->input_count
* ins
->component_count
> ARGS_ARRAY_SIZE
)
1670 FIXME("Too many arguments (%u) for one instruction.\n", oi
->input_count
* ins
->component_count
);
1673 for (k
= 0; k
< oi
->input_count
; ++k
)
1674 for (j
= 0; j
< ins
->component_count
; ++j
)
1675 args
[k
* ins
->component_count
+ j
] = exec_get_arg(&pres
->regs
, &ins
->inputs
[k
],
1676 ins
->scalar_op
&& !k
? 0 : j
);
1677 res
= oi
->func(args
, ins
->component_count
);
1679 /* only 'dot' instruction currently falls here */
1680 exec_set_arg(&pres
->regs
, &ins
->output
.reg
, 0, res
);
1684 for (j
= 0; j
< ins
->component_count
; ++j
)
1686 for (k
= 0; k
< oi
->input_count
; ++k
)
1687 args
[k
] = exec_get_arg(&pres
->regs
, &ins
->inputs
[k
], ins
->scalar_op
&& !k
? 0 : j
);
1688 res
= oi
->func(args
, ins
->component_count
);
1689 exec_set_arg(&pres
->regs
, &ins
->output
.reg
, j
, res
);
1696 static BOOL
is_const_tab_input_dirty(struct d3dx_const_tab
*ctab
, ULONG64 update_version
)
1700 if (update_version
== ULONG64_MAX
)
1701 update_version
= ctab
->update_version
;
1702 for (i
= 0; i
< ctab
->input_count
; ++i
)
1704 if (is_top_level_param_dirty(top_level_parameter_from_parameter(ctab
->inputs_param
[i
]),
1711 BOOL
is_param_eval_input_dirty(struct d3dx_param_eval
*peval
, ULONG64 update_version
)
1713 return is_const_tab_input_dirty(&peval
->pres
.inputs
, update_version
)
1714 || is_const_tab_input_dirty(&peval
->shader_inputs
, update_version
);
1717 HRESULT
d3dx_evaluate_parameter(struct d3dx_param_eval
*peval
, const struct d3dx_parameter
*param
,
1722 unsigned int elements
, elements_param
, elements_table
;
1725 TRACE("peval %p, param %p, param_value %p.\n", peval
, param
, param_value
);
1727 if (is_const_tab_input_dirty(&peval
->pres
.inputs
, ULONG64_MAX
))
1729 set_constants(&peval
->pres
.regs
, &peval
->pres
.inputs
,
1730 next_update_version(peval
->version_counter
),
1731 NULL
, NULL
, peval
->param_type
, FALSE
, FALSE
);
1733 if (FAILED(hr
= execute_preshader(&peval
->pres
)))
1737 elements_table
= get_offset_reg(PRES_REGTAB_OCONST
, peval
->pres
.regs
.table_sizes
[PRES_REGTAB_OCONST
]);
1738 elements_param
= param
->bytes
/ sizeof(unsigned int);
1739 elements
= min(elements_table
, elements_param
);
1740 oc
= (float *)peval
->pres
.regs
.tables
[PRES_REGTAB_OCONST
];
1741 for (i
= 0; i
< elements
; ++i
)
1742 set_number((unsigned int *)param_value
+ i
, param
->type
, oc
+ i
, D3DXPT_FLOAT
);
1746 HRESULT
d3dx_param_eval_set_shader_constants(ID3DXEffectStateManager
*manager
, struct IDirect3DDevice9
*device
,
1747 struct d3dx_param_eval
*peval
, BOOL update_all
)
1750 struct d3dx_preshader
*pres
= &peval
->pres
;
1751 struct d3dx_regstore
*rs
= &pres
->regs
;
1752 ULONG64 new_update_version
= next_update_version(peval
->version_counter
);
1753 BOOL pres_dirty
= FALSE
;
1755 TRACE("device %p, peval %p, param_type %u.\n", device
, peval
, peval
->param_type
);
1757 if (is_const_tab_input_dirty(&pres
->inputs
, ULONG64_MAX
))
1759 set_constants(rs
, &pres
->inputs
, new_update_version
,
1760 NULL
, NULL
, peval
->param_type
, FALSE
, FALSE
);
1761 if (FAILED(hr
= execute_preshader(pres
)))
1766 return set_constants(rs
, &peval
->shader_inputs
, new_update_version
,
1767 manager
, device
, peval
->param_type
, update_all
, pres_dirty
);