1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "draw/draw_context.h"
27 #include "util/u_inlines.h"
28 #include "util/u_math.h"
29 #include "util/u_memory.h"
30 #include "util/u_bitmask.h"
31 #include "tgsi/tgsi_parse.h"
32 #include "tgsi/tgsi_text.h"
33
34 #include "svga_context.h"
35 #include "svga_hw_reg.h"
36 #include "svga_cmd.h"
37 #include "svga_debug.h"
38 #include "svga_shader.h"
39 #include "svga_streamout.h"
40
41
42 /**
43 * Substitute a debug shader.
44 */
45 static const struct tgsi_token *
substitute_vs(unsigned shader_id,const struct tgsi_token * old_tokens)46 substitute_vs(unsigned shader_id, const struct tgsi_token *old_tokens)
47 {
48 #if 0
49 if (shader_id == 12) {
50 static struct tgsi_token tokens[300];
51
52 const char *text =
53 "VERT\n"
54 "DCL IN[0]\n"
55 "DCL IN[1]\n"
56 "DCL IN[2]\n"
57 "DCL OUT[0], POSITION\n"
58 "DCL TEMP[0..4]\n"
59 "IMM FLT32 { 1.0000, 1.0000, 1.0000, 1.0000 }\n"
60 "IMM FLT32 { 0.45, 1.0000, 1.0000, 1.0000 }\n"
61 "IMM FLT32 { 1.297863, 0.039245, 0.035993, 0.035976}\n"
62 "IMM FLT32 { -0.019398, 1.696131, -0.202151, -0.202050 }\n"
63 "IMM FLT32 { 0.051711, -0.348713, -0.979204, -0.978714 }\n"
64 "IMM FLT32 { 0.000000, 0.000003, 139.491577, 141.421356 }\n"
65 "DCL CONST[0..7]\n"
66 "DCL CONST[9..16]\n"
67 " MOV TEMP[2], IMM[0]\n"
68
69 " MOV TEMP[2].xyz, IN[2]\n"
70 " MOV TEMP[2].xyz, IN[0]\n"
71 " MOV TEMP[2].xyz, IN[1]\n"
72
73 " MUL TEMP[1], IMM[3], TEMP[2].yyyy\n"
74 " MAD TEMP[3], IMM[2], TEMP[2].xxxx, TEMP[1]\n"
75 " MAD TEMP[1], IMM[4], TEMP[2].zzzz, TEMP[3]\n"
76 " MAD TEMP[4], IMM[5], TEMP[2].wwww, TEMP[1]\n"
77
78 " MOV OUT[0], TEMP[4]\n"
79 " END\n";
80
81 if (!tgsi_text_translate(text,
82 tokens,
83 ARRAY_SIZE(tokens)))
84 {
85 assert(0);
86 return NULL;
87 }
88
89 return tokens;
90 }
91 #endif
92
93 return old_tokens;
94 }
95
96
97 static void *
svga_create_vs_state(struct pipe_context * pipe,const struct pipe_shader_state * templ)98 svga_create_vs_state(struct pipe_context *pipe,
99 const struct pipe_shader_state *templ)
100 {
101 struct svga_context *svga = svga_context(pipe);
102 struct svga_vertex_shader *vs = CALLOC_STRUCT(svga_vertex_shader);
103
104 if (!vs)
105 return NULL;
106
107 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CREATEVS);
108
109 /* substitute a debug shader?
110 */
111 vs->base.tokens = tgsi_dup_tokens(substitute_vs(svga->debug.shader_id,
112 templ->tokens));
113
114 /* Collect basic info that we'll need later:
115 */
116 tgsi_scan_shader(vs->base.tokens, &vs->base.info);
117
118 {
119 /* Need to do construct a new template in case we substitued a
120 * debug shader.
121 */
122 struct pipe_shader_state tmp2 = *templ;
123 tmp2.tokens = vs->base.tokens;
124 vs->draw_shader = draw_create_vertex_shader(svga->swtnl.draw, &tmp2);
125 }
126
127 vs->base.id = svga->debug.shader_id++;
128
129 vs->generic_outputs = svga_get_generic_outputs_mask(&vs->base.info);
130
131 /* check for any stream output declarations */
132 if (templ->stream_output.num_outputs) {
133 vs->base.stream_output = svga_create_stream_output(svga, &vs->base,
134 &templ->stream_output);
135 }
136
137 SVGA_STATS_TIME_POP(svga_sws(svga));
138 return vs;
139 }
140
141
142 static void
svga_bind_vs_state(struct pipe_context * pipe,void * shader)143 svga_bind_vs_state(struct pipe_context *pipe, void *shader)
144 {
145 struct svga_vertex_shader *vs = (struct svga_vertex_shader *)shader;
146 struct svga_context *svga = svga_context(pipe);
147
148 if (vs == svga->curr.vs)
149 return;
150
151 /* If the currently bound vertex shader has a generated geometry shader,
152 * then unbind the geometry shader before binding a new vertex shader.
153 * We need to unbind the geometry shader here because there is no
154 * pipe_shader associated with the generated geometry shader.
155 */
156 if (svga->curr.vs != NULL && svga->curr.vs->gs != NULL)
157 svga->pipe.bind_gs_state(&svga->pipe, NULL);
158
159 svga->curr.vs = vs;
160 svga->dirty |= SVGA_NEW_VS;
161 }
162
163
164 static void
svga_delete_vs_state(struct pipe_context * pipe,void * shader)165 svga_delete_vs_state(struct pipe_context *pipe, void *shader)
166 {
167 struct svga_context *svga = svga_context(pipe);
168 struct svga_vertex_shader *vs = (struct svga_vertex_shader *)shader;
169 struct svga_vertex_shader *next_vs;
170 struct svga_shader_variant *variant, *tmp;
171
172 svga_hwtnl_flush_retry(svga);
173
174 assert(vs->base.parent == NULL);
175
176 while (vs) {
177 next_vs = (struct svga_vertex_shader *)vs->base.next;
178
179 /* Check if there is a generated geometry shader to go with this
180 * vertex shader. If there is, then delete the geometry shader as well.
181 */
182 if (vs->gs != NULL) {
183 svga->pipe.delete_gs_state(&svga->pipe, vs->gs);
184 }
185
186 if (vs->base.stream_output != NULL)
187 svga_delete_stream_output(svga, vs->base.stream_output);
188
189 draw_delete_vertex_shader(svga->swtnl.draw, vs->draw_shader);
190
191 for (variant = vs->base.variants; variant; variant = tmp) {
192 tmp = variant->next;
193
194 /* Check if deleting currently bound shader */
195 if (variant == svga->state.hw_draw.vs) {
196 SVGA_RETRY(svga, svga_set_shader(svga, SVGA3D_SHADERTYPE_VS, NULL));
197 svga->state.hw_draw.vs = NULL;
198 }
199
200 svga_destroy_shader_variant(svga, variant);
201 }
202
203 FREE((void *)vs->base.tokens);
204 FREE(vs);
205 vs = next_vs;
206 }
207 }
208
209
210 void
svga_init_vs_functions(struct svga_context * svga)211 svga_init_vs_functions(struct svga_context *svga)
212 {
213 svga->pipe.create_vs_state = svga_create_vs_state;
214 svga->pipe.bind_vs_state = svga_bind_vs_state;
215 svga->pipe.delete_vs_state = svga_delete_vs_state;
216 }
217
218