Merge branch '7.8'
[mesa.git] / src / gallium / drivers / softpipe / sp_state_fs.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "sp_context.h"
29 #include "sp_state.h"
30 #include "sp_fs.h"
31 #include "sp_buffer.h"
32
33 #include "pipe/p_defines.h"
34 #include "util/u_memory.h"
35 #include "util/u_inlines.h"
36 #include "draw/draw_context.h"
37 #include "draw/draw_vs.h"
38 #include "tgsi/tgsi_dump.h"
39 #include "tgsi/tgsi_scan.h"
40 #include "tgsi/tgsi_parse.h"
41
42
43 void *
44 softpipe_create_fs_state(struct pipe_context *pipe,
45 const struct pipe_shader_state *templ)
46 {
47 struct softpipe_context *softpipe = softpipe_context(pipe);
48 struct sp_fragment_shader *state;
49 unsigned i;
50
51 /* debug */
52 if (softpipe->dump_fs)
53 tgsi_dump(templ->tokens, 0);
54
55 /* codegen */
56 state = softpipe_create_fs_sse( softpipe, templ );
57 if (!state) {
58 state = softpipe_create_fs_exec( softpipe, templ );
59 }
60
61 assert(state);
62
63 /* get/save the summary info for this shader */
64 tgsi_scan_shader(templ->tokens, &state->info);
65
66 for (i = 0; i < state->info.num_properties; ++i) {
67 if (state->info.properties[i].name == TGSI_PROPERTY_FS_COORD_ORIGIN)
68 state->origin_lower_left = state->info.properties[i].data[0];
69 else if (state->info.properties[i].name == TGSI_PROPERTY_FS_COORD_PIXEL_CENTER)
70 state->pixel_center_integer = state->info.properties[i].data[0];
71 }
72
73 return state;
74 }
75
76
77 void
78 softpipe_bind_fs_state(struct pipe_context *pipe, void *fs)
79 {
80 struct softpipe_context *softpipe = softpipe_context(pipe);
81
82 draw_flush(softpipe->draw);
83
84 if (softpipe->fs == fs)
85 return;
86
87 draw_flush(softpipe->draw);
88
89 softpipe->fs = fs;
90
91 softpipe->dirty |= SP_NEW_FS;
92 }
93
94
95 void
96 softpipe_delete_fs_state(struct pipe_context *pipe, void *fs)
97 {
98 struct sp_fragment_shader *state = fs;
99
100 assert(fs != softpipe_context(pipe)->fs);
101
102 state->delete( state );
103 }
104
105
106 void *
107 softpipe_create_vs_state(struct pipe_context *pipe,
108 const struct pipe_shader_state *templ)
109 {
110 struct softpipe_context *softpipe = softpipe_context(pipe);
111 struct sp_vertex_shader *state;
112
113 state = CALLOC_STRUCT(sp_vertex_shader);
114 if (state == NULL )
115 goto fail;
116
117 /* copy shader tokens, the ones passed in will go away.
118 */
119 state->shader.tokens = tgsi_dup_tokens(templ->tokens);
120 if (state->shader.tokens == NULL)
121 goto fail;
122
123 state->draw_data = draw_create_vertex_shader(softpipe->draw, templ);
124 if (state->draw_data == NULL)
125 goto fail;
126
127 state->max_sampler = state->draw_data->info.file_max[TGSI_FILE_SAMPLER];
128
129 return state;
130
131 fail:
132 if (state) {
133 FREE( (void *)state->shader.tokens );
134 FREE( state->draw_data );
135 FREE( state );
136 }
137 return NULL;
138 }
139
140
141 void
142 softpipe_bind_vs_state(struct pipe_context *pipe, void *vs)
143 {
144 struct softpipe_context *softpipe = softpipe_context(pipe);
145
146 softpipe->vs = (struct sp_vertex_shader *) vs;
147
148 draw_bind_vertex_shader(softpipe->draw,
149 (softpipe->vs ? softpipe->vs->draw_data : NULL));
150
151 softpipe->dirty |= SP_NEW_VS;
152 }
153
154
155 void
156 softpipe_delete_vs_state(struct pipe_context *pipe, void *vs)
157 {
158 struct softpipe_context *softpipe = softpipe_context(pipe);
159
160 struct sp_vertex_shader *state = (struct sp_vertex_shader *) vs;
161
162 draw_delete_vertex_shader(softpipe->draw, state->draw_data);
163 FREE( (void *)state->shader.tokens );
164 FREE( state );
165 }
166
167 void
168 softpipe_set_constant_buffer(struct pipe_context *pipe,
169 uint shader, uint index,
170 struct pipe_buffer *constants)
171 {
172 struct softpipe_context *softpipe = softpipe_context(pipe);
173 unsigned size = constants ? constants->size : 0;
174 const void *data = constants ? softpipe_buffer(constants)->data : NULL;
175
176 assert(shader < PIPE_SHADER_TYPES);
177 assert(index == 0);
178
179 draw_flush(softpipe->draw);
180
181 /* note: reference counting */
182 pipe_buffer_reference(&softpipe->constants[shader][index], constants);
183
184 if(shader == PIPE_SHADER_VERTEX) {
185 draw_set_mapped_constant_buffer(softpipe->draw, PIPE_SHADER_VERTEX, index,
186 data, size);
187 }
188
189 softpipe->mapped_constants[shader][index] = data;
190 softpipe->dirty |= SP_NEW_CONSTANTS;
191 }
192
193
194 void *
195 softpipe_create_gs_state(struct pipe_context *pipe,
196 const struct pipe_shader_state *templ)
197 {
198 struct softpipe_context *softpipe = softpipe_context(pipe);
199 struct sp_geometry_shader *state;
200
201 state = CALLOC_STRUCT(sp_geometry_shader);
202 if (state == NULL )
203 goto fail;
204
205 /* debug */
206 if (softpipe->dump_gs)
207 tgsi_dump(templ->tokens, 0);
208
209 /* copy shader tokens, the ones passed in will go away.
210 */
211 state->shader.tokens = tgsi_dup_tokens(templ->tokens);
212 if (state->shader.tokens == NULL)
213 goto fail;
214
215 state->draw_data = draw_create_geometry_shader(softpipe->draw, templ);
216 if (state->draw_data == NULL)
217 goto fail;
218
219 return state;
220
221 fail:
222 if (state) {
223 FREE( (void *)state->shader.tokens );
224 FREE( state->draw_data );
225 FREE( state );
226 }
227 return NULL;
228 }
229
230
231 void
232 softpipe_bind_gs_state(struct pipe_context *pipe, void *gs)
233 {
234 struct softpipe_context *softpipe = softpipe_context(pipe);
235
236 softpipe->gs = (struct sp_geometry_shader *)gs;
237
238 draw_bind_geometry_shader(softpipe->draw,
239 (softpipe->gs ? softpipe->gs->draw_data : NULL));
240
241 softpipe->dirty |= SP_NEW_GS;
242 }
243
244
245 void
246 softpipe_delete_gs_state(struct pipe_context *pipe, void *gs)
247 {
248 struct softpipe_context *softpipe = softpipe_context(pipe);
249
250 struct sp_geometry_shader *state =
251 (struct sp_geometry_shader *)gs;
252
253 draw_delete_geometry_shader(softpipe->draw,
254 (state) ? state->draw_data : 0);
255 FREE(state);
256 }