d3dx9: Support some flow control instructions in the shader assembler.
[wine] / dlls / d3dx9_36 / asmparser.c
1 /*
2  * Direct3D asm shader parser
3  *
4  * Copyright 2008 Stefan Dösinger
5  * Copyright 2009 Matteo Bruni
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25 #include "wine/debug.h"
26
27 #include "d3dx9_36_private.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(asmshader);
30 WINE_DECLARE_DEBUG_CHANNEL(parsed_shader);
31
32
33 /****************************************************************
34  * Common(non-version specific) shader parser control code      *
35  ****************************************************************/
36
37 static void asmparser_end(struct asm_parser *This) {
38     TRACE("Finalizing shader\n");
39 }
40
41 static void asmparser_instr(struct asm_parser *This, DWORD opcode,
42                             DWORD mod, DWORD shift,
43                             BWRITER_COMPARISON_TYPE comp,
44                             const struct shader_reg *dst,
45                             const struct src_regs *srcs, int expectednsrcs) {
46     struct instruction *instr;
47     unsigned int i;
48     BOOL firstreg = TRUE;
49     unsigned int src_count = srcs ? srcs->count : 0;
50
51     if(!This->shader) return;
52
53     TRACE_(parsed_shader)("%s%s ", debug_print_opcode(opcode),
54                           debug_print_dstmod(mod));
55     if(dst) {
56         TRACE_(parsed_shader)("%s", debug_print_dstreg(dst, This->shader->type));
57         firstreg = FALSE;
58     }
59     for(i = 0; i < src_count; i++) {
60         if(!firstreg) TRACE_(parsed_shader)(", ");
61         else firstreg = FALSE;
62         TRACE_(parsed_shader)("%s", debug_print_srcreg(&srcs->reg[i],
63                                                        This->shader->type));
64     }
65     TRACE_(parsed_shader)("\n");
66
67     if(src_count != expectednsrcs) {
68         asmparser_message(This, "Line %u: Wrong number of source registers\n", This->line_no);
69         set_parse_status(This, PARSE_ERR);
70         return;
71     }
72
73     instr = alloc_instr(src_count);
74     if(!instr) {
75         ERR("Error allocating memory for the instruction\n");
76         set_parse_status(This, PARSE_ERR);
77         return;
78     }
79
80     instr->opcode = opcode;
81     instr->dstmod = mod;
82     instr->shift = shift;
83     instr->comptype = comp;
84     if(dst) This->funcs->dstreg(This, instr, dst);
85     for(i = 0; i < src_count; i++) {
86         This->funcs->srcreg(This, instr, i, &srcs->reg[i]);
87     }
88
89     if(!add_instruction(This->shader, instr)) {
90         ERR("Out of memory\n");
91         set_parse_status(This, PARSE_ERR);
92     }
93 }
94
95 static void asmparser_srcreg_vs_3(struct asm_parser *This,
96                                   struct instruction *instr, int num,
97                                   const struct shader_reg *src) {
98     memcpy(&instr->src[num], src, sizeof(*src));
99 }
100
101 static void asmparser_dstreg_vs_3(struct asm_parser *This,
102                                   struct instruction *instr,
103                                   const struct shader_reg *dst) {
104     memcpy(&instr->dst, dst, sizeof(*dst));
105     instr->has_dst = TRUE;
106 }
107
108 static void asmparser_predicate_unsupported(struct asm_parser *This,
109                                             const struct shader_reg *predicate) {
110     asmparser_message(This, "Line %u: Predicate not supported in < VS 2.0 or PS 2.x\n", This->line_no);
111     set_parse_status(This, PARSE_ERR);
112 }
113
114 static void asmparser_coissue_unsupported(struct asm_parser *This) {
115     asmparser_message(This, "Line %u: Coissue is only supported in pixel shaders versions <= 1.4\n", This->line_no);
116     set_parse_status(This, PARSE_ERR);
117 }
118
119 static const struct asmparser_backend parser_vs_3 = {
120     asmparser_dstreg_vs_3,
121     asmparser_srcreg_vs_3,
122
123     asmparser_predicate_unsupported,
124     asmparser_coissue_unsupported,
125
126     asmparser_end,
127
128     asmparser_instr,
129 };
130
131 void create_vs30_parser(struct asm_parser *ret) {
132     TRACE_(parsed_shader)("vs_3_0\n");
133
134     ret->shader = asm_alloc(sizeof(*ret->shader));
135     if(!ret->shader) {
136         ERR("Failed to allocate memory for the shader\n");
137         set_parse_status(ret, PARSE_ERR);
138         return;
139     }
140
141     ret->shader->type = ST_VERTEX;
142     ret->shader->version = BWRITERVS_VERSION(3, 0);
143     ret->funcs = &parser_vs_3;
144 }