2 * Copyright 2009 Henri Verbeet for CodeWeavers
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 "wined3d_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader
);
26 #define WINED3D_SM4_INSTRUCTION_MODIFIER (1 << 31)
28 #define WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT 24
29 #define WINED3D_SM4_INSTRUCTION_LENGTH_MASK (0xf << WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT)
31 #define WINED3D_SM4_OPCODE_MASK 0xff
33 #define WINED3D_SM4_REGISTER_MODIFIER (1 << 31)
35 #define WINED3D_SM4_REGISTER_ORDER_SHIFT 20
36 #define WINED3D_SM4_REGISTER_ORDER_MASK (0x3 << WINED3D_SM4_REGISTER_ORDER_SHIFT)
38 #define WINED3D_SM4_REGISTER_TYPE_SHIFT 12
39 #define WINED3D_SM4_REGISTER_TYPE_MASK (0xf << WINED3D_SM4_REGISTER_TYPE_SHIFT)
41 #define WINED3D_SM4_IMMCONST_TYPE_SHIFT 0
42 #define WINED3D_SM4_IMMCONST_TYPE_MASK (0x3 << WINED3D_SM4_IMMCONST_TYPE_SHIFT)
44 #define WINED3D_SM4_WRITEMASK_SHIFT 4
45 #define WINED3D_SM4_WRITEMASK_MASK (0xf << WINED3D_SM4_WRITEMASK_SHIFT)
47 #define WINED3D_SM4_SWIZZLE_SHIFT 4
48 #define WINED3D_SM4_SWIZZLE_MASK (0xff << WINED3D_SM4_SWIZZLE_SHIFT)
50 #define WINED3D_SM4_VERSION_MAJOR(version) (((version) >> 4) & 0xf)
51 #define WINED3D_SM4_VERSION_MINOR(version) (((version) >> 0) & 0xf)
53 enum wined3d_sm4_opcode
55 WINED3D_SM4_OP_ADD
= 0x00,
56 WINED3D_SM4_OP_AND
= 0x01,
57 WINED3D_SM4_OP_BREAK
= 0x02,
58 WINED3D_SM4_OP_BREAKC
= 0x03,
59 WINED3D_SM4_OP_CUT
= 0x09,
60 WINED3D_SM4_OP_DERIV_RTX
= 0x0b,
61 WINED3D_SM4_OP_DERIV_RTY
= 0x0c,
62 WINED3D_SM4_OP_DIV
= 0x0e,
63 WINED3D_SM4_OP_DP3
= 0x10,
64 WINED3D_SM4_OP_DP4
= 0x11,
65 WINED3D_SM4_OP_EMIT
= 0x13,
66 WINED3D_SM4_OP_ENDIF
= 0x15,
67 WINED3D_SM4_OP_ENDLOOP
= 0x16,
68 WINED3D_SM4_OP_EQ
= 0x18,
69 WINED3D_SM4_OP_EXP
= 0x19,
70 WINED3D_SM4_OP_FRC
= 0x1a,
71 WINED3D_SM4_OP_FTOI
= 0x1b,
72 WINED3D_SM4_OP_GE
= 0x1d,
73 WINED3D_SM4_OP_IADD
= 0x1e,
74 WINED3D_SM4_OP_IF
= 0x1f,
75 WINED3D_SM4_OP_IEQ
= 0x20,
76 WINED3D_SM4_OP_IGE
= 0x21,
77 WINED3D_SM4_OP_IMUL
= 0x26,
78 WINED3D_SM4_OP_ITOF
= 0x2b,
79 WINED3D_SM4_OP_LD
= 0x2d,
80 WINED3D_SM4_OP_LOG
= 0x2f,
81 WINED3D_SM4_OP_LOOP
= 0x30,
82 WINED3D_SM4_OP_LT
= 0x31,
83 WINED3D_SM4_OP_MAD
= 0x32,
84 WINED3D_SM4_OP_MIN
= 0x33,
85 WINED3D_SM4_OP_MAX
= 0x34,
86 WINED3D_SM4_OP_MOV
= 0x36,
87 WINED3D_SM4_OP_MOVC
= 0x37,
88 WINED3D_SM4_OP_MUL
= 0x38,
89 WINED3D_SM4_OP_RET
= 0x3e,
90 WINED3D_SM4_OP_ROUND_NI
= 0x41,
91 WINED3D_SM4_OP_RSQ
= 0x44,
92 WINED3D_SM4_OP_SAMPLE
= 0x45,
93 WINED3D_SM4_OP_SAMPLE_LOD
= 0x48,
94 WINED3D_SM4_OP_SAMPLE_GRAD
= 0x49,
95 WINED3D_SM4_OP_SQRT
= 0x4b,
96 WINED3D_SM4_OP_SINCOS
= 0x4d,
97 WINED3D_SM4_OP_UDIV
= 0x4e,
98 WINED3D_SM4_OP_USHR
= 0x55,
99 WINED3D_SM4_OP_UTOF
= 0x56,
100 WINED3D_SM4_OP_XOR
= 0x57,
103 enum wined3d_sm4_register_type
105 WINED3D_SM4_RT_TEMP
= 0x0,
106 WINED3D_SM4_RT_INPUT
= 0x1,
107 WINED3D_SM4_RT_OUTPUT
= 0x2,
108 WINED3D_SM4_RT_IMMCONST
= 0x4,
109 WINED3D_SM4_RT_SAMPLER
= 0x6,
110 WINED3D_SM4_RT_CONSTBUFFER
= 0x8,
111 WINED3D_SM4_RT_NULL
= 0xd,
114 enum wined3d_sm4_immconst_type
116 WINED3D_SM4_IMMCONST_SCALAR
= 0x1,
117 WINED3D_SM4_IMMCONST_VEC4
= 0x2,
120 struct wined3d_sm4_data
122 struct wined3d_shader_version shader_version
;
124 const struct wined3d_shader_signature
*output_signature
;
127 struct wined3d_sm4_opcode_info
129 enum wined3d_sm4_opcode opcode
;
130 enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx
;
137 enum wined3d_sysval_semantic sysval
;
138 enum wined3d_shader_register_type register_type
;
142 static const struct wined3d_sm4_opcode_info opcode_table
[] =
144 {WINED3D_SM4_OP_ADD
, WINED3DSIH_ADD
, 1, 2},
145 {WINED3D_SM4_OP_AND
, WINED3DSIH_AND
, 1, 2},
146 {WINED3D_SM4_OP_BREAK
, WINED3DSIH_BREAK
, 0, 0},
147 {WINED3D_SM4_OP_BREAKC
, WINED3DSIH_BREAKP
, 0, 1},
148 {WINED3D_SM4_OP_CUT
, WINED3DSIH_CUT
, 0, 0},
149 {WINED3D_SM4_OP_DERIV_RTX
, WINED3DSIH_DSX
, 1, 1},
150 {WINED3D_SM4_OP_DERIV_RTY
, WINED3DSIH_DSY
, 1, 1},
151 {WINED3D_SM4_OP_DIV
, WINED3DSIH_DIV
, 1, 2},
152 {WINED3D_SM4_OP_DP3
, WINED3DSIH_DP3
, 1, 2},
153 {WINED3D_SM4_OP_DP4
, WINED3DSIH_DP4
, 1, 2},
154 {WINED3D_SM4_OP_EMIT
, WINED3DSIH_EMIT
, 0, 0},
155 {WINED3D_SM4_OP_ENDIF
, WINED3DSIH_ENDIF
, 0, 0},
156 {WINED3D_SM4_OP_ENDLOOP
, WINED3DSIH_ENDLOOP
, 0, 0},
157 {WINED3D_SM4_OP_EQ
, WINED3DSIH_EQ
, 1, 2},
158 {WINED3D_SM4_OP_EXP
, WINED3DSIH_EXP
, 1, 1},
159 {WINED3D_SM4_OP_FRC
, WINED3DSIH_FRC
, 1, 1},
160 {WINED3D_SM4_OP_FTOI
, WINED3DSIH_FTOI
, 1, 1},
161 {WINED3D_SM4_OP_GE
, WINED3DSIH_GE
, 1, 2},
162 {WINED3D_SM4_OP_IADD
, WINED3DSIH_IADD
, 1, 2},
163 {WINED3D_SM4_OP_IF
, WINED3DSIH_IF
, 0, 1},
164 {WINED3D_SM4_OP_IEQ
, WINED3DSIH_IEQ
, 1, 2},
165 {WINED3D_SM4_OP_IGE
, WINED3DSIH_IGE
, 1, 2},
166 {WINED3D_SM4_OP_IMUL
, WINED3DSIH_IMUL
, 2, 2},
167 {WINED3D_SM4_OP_ITOF
, WINED3DSIH_ITOF
, 1, 1},
168 {WINED3D_SM4_OP_LD
, WINED3DSIH_LD
, 1, 2},
169 {WINED3D_SM4_OP_LOG
, WINED3DSIH_LOG
, 1, 1},
170 {WINED3D_SM4_OP_LOOP
, WINED3DSIH_LOOP
, 0, 0},
171 {WINED3D_SM4_OP_LT
, WINED3DSIH_LT
, 1, 2},
172 {WINED3D_SM4_OP_MAD
, WINED3DSIH_MAD
, 1, 3},
173 {WINED3D_SM4_OP_MIN
, WINED3DSIH_MIN
, 1, 2},
174 {WINED3D_SM4_OP_MAX
, WINED3DSIH_MAX
, 1, 2},
175 {WINED3D_SM4_OP_MOV
, WINED3DSIH_MOV
, 1, 1},
176 {WINED3D_SM4_OP_MOVC
, WINED3DSIH_MOVC
, 1, 3},
177 {WINED3D_SM4_OP_MUL
, WINED3DSIH_MUL
, 1, 2},
178 {WINED3D_SM4_OP_RET
, WINED3DSIH_RET
, 0, 0},
179 {WINED3D_SM4_OP_ROUND_NI
, WINED3DSIH_ROUND_NI
, 1, 1},
180 {WINED3D_SM4_OP_RSQ
, WINED3DSIH_RSQ
, 1, 1},
181 {WINED3D_SM4_OP_SAMPLE
, WINED3DSIH_SAMPLE
, 1, 3},
182 {WINED3D_SM4_OP_SAMPLE_LOD
, WINED3DSIH_SAMPLE_LOD
, 1, 4},
183 {WINED3D_SM4_OP_SAMPLE_GRAD
,WINED3DSIH_SAMPLE_GRAD
, 1, 5},
184 {WINED3D_SM4_OP_SQRT
, WINED3DSIH_SQRT
, 1, 1},
185 {WINED3D_SM4_OP_SINCOS
, WINED3DSIH_SINCOS
, 2, 1},
186 {WINED3D_SM4_OP_UDIV
, WINED3DSIH_UDIV
, 2, 2},
187 {WINED3D_SM4_OP_USHR
, WINED3DSIH_USHR
, 1, 2},
188 {WINED3D_SM4_OP_UTOF
, WINED3DSIH_UTOF
, 1, 1},
189 {WINED3D_SM4_OP_XOR
, WINED3DSIH_XOR
, 1, 2},
192 static const enum wined3d_shader_register_type register_type_table
[] =
194 /* WINED3D_SM4_RT_TEMP */ WINED3DSPR_TEMP
,
195 /* WINED3D_SM4_RT_INPUT */ WINED3DSPR_INPUT
,
196 /* WINED3D_SM4_RT_OUTPUT */ WINED3DSPR_OUTPUT
,
198 /* WINED3D_SM4_RT_IMMCONST */ WINED3DSPR_IMMCONST
,
200 /* WINED3D_SM4_RT_SAMPLER */ WINED3DSPR_SAMPLER
,
201 /* WINED3D_SM4_RT_RESOURCE */ WINED3DSPR_RESOURCE
,
202 /* WINED3D_SM4_RT_CONSTBUFFER */ WINED3DSPR_CONSTBUFFER
,
207 /* WINED3D_SM4_RT_NULL */ WINED3DSPR_NULL
,
210 static const struct sysval_map sysval_map
[] =
212 {WINED3D_SV_DEPTH
, WINED3DSPR_DEPTHOUT
, 0},
213 {WINED3D_SV_TARGET0
, WINED3DSPR_COLOROUT
, 0},
214 {WINED3D_SV_TARGET1
, WINED3DSPR_COLOROUT
, 1},
215 {WINED3D_SV_TARGET2
, WINED3DSPR_COLOROUT
, 2},
216 {WINED3D_SV_TARGET3
, WINED3DSPR_COLOROUT
, 3},
217 {WINED3D_SV_TARGET4
, WINED3DSPR_COLOROUT
, 4},
218 {WINED3D_SV_TARGET5
, WINED3DSPR_COLOROUT
, 5},
219 {WINED3D_SV_TARGET6
, WINED3DSPR_COLOROUT
, 6},
220 {WINED3D_SV_TARGET7
, WINED3DSPR_COLOROUT
, 7},
223 static const struct wined3d_sm4_opcode_info
*get_opcode_info(enum wined3d_sm4_opcode opcode
)
227 for (i
= 0; i
< sizeof(opcode_table
) / sizeof(*opcode_table
); ++i
)
229 if (opcode
== opcode_table
[i
].opcode
) return &opcode_table
[i
];
235 static void map_sysval(enum wined3d_sysval_semantic sysval
, struct wined3d_shader_register
*reg
)
239 for (i
= 0; i
< sizeof(sysval_map
) / sizeof(*sysval_map
); ++i
)
241 if (sysval
== sysval_map
[i
].sysval
)
243 reg
->type
= sysval_map
[i
].register_type
;
244 reg
->idx
= sysval_map
[i
].register_idx
;
249 static void map_register(const struct wined3d_sm4_data
*priv
, struct wined3d_shader_register
*reg
)
251 switch (priv
->shader_version
.type
)
253 case WINED3D_SHADER_TYPE_PIXEL
:
254 if (reg
->type
== WINED3DSPR_OUTPUT
)
257 const struct wined3d_shader_signature
*s
= priv
->output_signature
;
261 ERR("Shader has no output signature, unable to map register.\n");
265 for (i
= 0; i
< s
->element_count
; ++i
)
267 if (s
->elements
[i
].register_idx
== reg
->idx
)
269 map_sysval(s
->elements
[i
].sysval_semantic
, reg
);
281 static void *shader_sm4_init(const DWORD
*byte_code
, const struct wined3d_shader_signature
*output_signature
)
283 struct wined3d_sm4_data
*priv
= HeapAlloc(GetProcessHeap(), 0, sizeof(*priv
));
286 ERR("Failed to allocate private data\n");
290 priv
->output_signature
= output_signature
;
295 static void shader_sm4_free(void *data
)
297 HeapFree(GetProcessHeap(), 0, data
);
300 static void shader_sm4_read_header(void *data
, const DWORD
**ptr
, struct wined3d_shader_version
*shader_version
)
302 struct wined3d_sm4_data
*priv
= data
;
307 version_token
= *(*ptr
)++;
308 TRACE("version: 0x%08x\n", version_token
);
310 TRACE("token count: %u\n", **ptr
);
311 priv
->end
+= *(*ptr
)++;
313 switch (version_token
>> 16)
316 priv
->shader_version
.type
= WINED3D_SHADER_TYPE_PIXEL
;
320 priv
->shader_version
.type
= WINED3D_SHADER_TYPE_VERTEX
;
324 priv
->shader_version
.type
= WINED3D_SHADER_TYPE_GEOMETRY
;
328 FIXME("Unrecognized shader type %#x\n", version_token
>> 16);
330 priv
->shader_version
.major
= WINED3D_SM4_VERSION_MAJOR(version_token
);
331 priv
->shader_version
.minor
= WINED3D_SM4_VERSION_MINOR(version_token
);
333 *shader_version
= priv
->shader_version
;
336 static void shader_sm4_read_opcode(void *data
, const DWORD
**ptr
, struct wined3d_shader_instruction
*ins
,
339 const struct wined3d_sm4_opcode_info
*opcode_info
;
340 DWORD token
= *(*ptr
)++;
341 DWORD opcode
= token
& WINED3D_SM4_OPCODE_MASK
;
343 *param_size
= ((token
& WINED3D_SM4_INSTRUCTION_LENGTH_MASK
) >> WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT
) - 1;
345 opcode_info
= get_opcode_info(opcode
);
348 FIXME("Unrecognized opcode %#x, token 0x%08x\n", opcode
, token
);
349 ins
->handler_idx
= WINED3DSIH_TABLE_SIZE
;
353 ins
->handler_idx
= opcode_info
->handler_idx
;
357 ins
->dst_count
= opcode_info
->dst_count
;
358 ins
->src_count
= opcode_info
->src_count
;
360 if (token
& WINED3D_SM4_INSTRUCTION_MODIFIER
)
362 DWORD modifier
= *(*ptr
)++;
363 FIXME("Skipping modifier 0x%08x.\n", modifier
);
367 static void shader_sm4_read_src_param(void *data
, const DWORD
**ptr
, struct wined3d_shader_src_param
*src_param
,
368 struct wined3d_shader_src_param
*src_rel_addr
)
370 struct wined3d_sm4_data
*priv
= data
;
371 DWORD token
= *(*ptr
)++;
372 enum wined3d_sm4_register_type register_type
;
375 register_type
= (token
& WINED3D_SM4_REGISTER_TYPE_MASK
) >> WINED3D_SM4_REGISTER_TYPE_SHIFT
;
376 if (register_type
>= sizeof(register_type_table
) / sizeof(*register_type_table
))
378 FIXME("Unhandled register type %#x\n", register_type
);
379 src_param
->reg
.type
= WINED3DSPR_TEMP
;
383 src_param
->reg
.type
= register_type_table
[register_type
];
386 if (token
& WINED3D_SM4_REGISTER_MODIFIER
)
388 DWORD modifier
= *(*ptr
)++;
390 /* FIXME: This will probably break down at some point. The SM4
391 * modifiers look like flags, while wined3d currently has an enum
392 * with possible combinations, e.g. WINED3DSPSM_ABSNEG. */
396 src_param
->modifiers
= WINED3DSPSM_NEG
;
400 src_param
->modifiers
= WINED3DSPSM_ABS
;
404 FIXME("Skipping modifier 0x%08x.\n", modifier
);
405 src_param
->modifiers
= WINED3DSPSM_NONE
;
411 src_param
->modifiers
= WINED3DSPSM_NONE
;
414 order
= (token
& WINED3D_SM4_REGISTER_ORDER_MASK
) >> WINED3D_SM4_REGISTER_ORDER_SHIFT
;
416 if (order
< 1) src_param
->reg
.idx
= ~0U;
417 else src_param
->reg
.idx
= *(*ptr
)++;
419 if (order
< 2) src_param
->reg
.array_idx
= ~0U;
420 else src_param
->reg
.array_idx
= *(*ptr
)++;
422 if (order
> 2) FIXME("Unhandled order %u.\n", order
);
424 if (register_type
== WINED3D_SM4_RT_IMMCONST
)
426 enum wined3d_sm4_immconst_type immconst_type
=
427 (token
& WINED3D_SM4_IMMCONST_TYPE_MASK
) >> WINED3D_SM4_IMMCONST_TYPE_SHIFT
;
428 src_param
->swizzle
= WINED3DSP_NOSWIZZLE
;
430 switch(immconst_type
)
432 case WINED3D_SM4_IMMCONST_SCALAR
:
433 src_param
->reg
.immconst_type
= WINED3D_IMMCONST_SCALAR
;
434 memcpy(src_param
->reg
.immconst_data
, *ptr
, 1 * sizeof(DWORD
));
438 case WINED3D_SM4_IMMCONST_VEC4
:
439 src_param
->reg
.immconst_type
= WINED3D_IMMCONST_VEC4
;
440 memcpy(src_param
->reg
.immconst_data
, *ptr
, 4 * sizeof(DWORD
));
445 FIXME("Unhandled immediate constant type %#x\n", immconst_type
);
451 src_param
->swizzle
= (token
& WINED3D_SM4_SWIZZLE_MASK
) >> WINED3D_SM4_SWIZZLE_SHIFT
;
454 src_param
->reg
.rel_addr
= NULL
;
456 map_register(priv
, &src_param
->reg
);
459 static void shader_sm4_read_dst_param(void *data
, const DWORD
**ptr
, struct wined3d_shader_dst_param
*dst_param
,
460 struct wined3d_shader_src_param
*dst_rel_addr
)
462 struct wined3d_sm4_data
*priv
= data
;
463 DWORD token
= *(*ptr
)++;
464 enum wined3d_sm4_register_type register_type
;
467 register_type
= (token
& WINED3D_SM4_REGISTER_TYPE_MASK
) >> WINED3D_SM4_REGISTER_TYPE_SHIFT
;
468 if (register_type
>= sizeof(register_type_table
) / sizeof(*register_type_table
))
470 FIXME("Unhandled register type %#x\n", register_type
);
471 dst_param
->reg
.type
= WINED3DSPR_TEMP
;
475 dst_param
->reg
.type
= register_type_table
[register_type
];
478 order
= (token
& WINED3D_SM4_REGISTER_ORDER_MASK
) >> WINED3D_SM4_REGISTER_ORDER_SHIFT
;
480 if (order
< 1) dst_param
->reg
.idx
= ~0U;
481 else dst_param
->reg
.idx
= *(*ptr
)++;
483 if (order
< 2) dst_param
->reg
.array_idx
= ~0U;
484 else dst_param
->reg
.array_idx
= *(*ptr
)++;
486 if (order
> 2) FIXME("Unhandled order %u.\n", order
);
488 dst_param
->write_mask
= (token
& WINED3D_SM4_WRITEMASK_MASK
) >> WINED3D_SM4_WRITEMASK_SHIFT
;
489 dst_param
->modifiers
= 0;
490 dst_param
->shift
= 0;
491 dst_param
->reg
.rel_addr
= NULL
;
493 map_register(priv
, &dst_param
->reg
);
496 static void shader_sm4_read_semantic(const DWORD
**ptr
, struct wined3d_shader_semantic
*semantic
)
498 FIXME("ptr %p, semantic %p stub!\n", ptr
, semantic
);
501 static void shader_sm4_read_comment(const DWORD
**ptr
, const char **comment
, UINT
*comment_size
)
503 FIXME("ptr %p, comment %p, comment_size %p stub!\n", ptr
, comment
, comment_size
);
507 static BOOL
shader_sm4_is_end(void *data
, const DWORD
**ptr
)
509 struct wined3d_sm4_data
*priv
= data
;
510 return *ptr
== priv
->end
;
513 const struct wined3d_shader_frontend sm4_shader_frontend
=
517 shader_sm4_read_header
,
518 shader_sm4_read_opcode
,
519 shader_sm4_read_src_param
,
520 shader_sm4_read_dst_param
,
521 shader_sm4_read_semantic
,
522 shader_sm4_read_comment
,