Merge commit 'lb2/arb_fragment_coord_conventions'
[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
32 #include "pipe/p_defines.h"
33 #include "util/u_memory.h"
34 #include "draw/draw_context.h"
35 #include "draw/draw_vs.h"
36 #include "tgsi/tgsi_dump.h"
37 #include "tgsi/tgsi_scan.h"
38 #include "tgsi/tgsi_parse.h"
39
40
41 void *
42 softpipe_create_fs_state(struct pipe_context *pipe,
43 const struct pipe_shader_state *templ)
44 {
45 struct softpipe_context *softpipe = softpipe_context(pipe);
46 struct sp_fragment_shader *state;
47 unsigned i;
48
49 /* debug */
50 if (softpipe->dump_fs)
51 tgsi_dump(templ->tokens, 0);
52
53 /* codegen */
54 state = softpipe_create_fs_sse( softpipe, templ );
55 if (!state) {
56 state = softpipe_create_fs_exec( softpipe, templ );
57 }
58
59 assert(state);
60
61 /* get/save the summary info for this shader */
62 tgsi_scan_shader(templ->tokens, &state->info);
63
64 for (i = 0; i < state->info.num_properties; ++i) {
65 if (state->info.properties[i].name == TGSI_PROPERTY_FS_COORD_ORIGIN)
66 state->origin_lower_left = state->info.properties[i].data[0];
67 else if (state->info.properties[i].name == TGSI_PROPERTY_FS_COORD_PIXEL_CENTER)
68 state->pixel_center_integer = state->info.properties[i].data[0];
69 }
70
71 return state;
72 }
73
74
75 void
76 softpipe_bind_fs_state(struct pipe_context *pipe, void *fs)
77 {
78 struct softpipe_context *softpipe = softpipe_context(pipe);
79
80 draw_flush(softpipe->draw);
81
82 if (softpipe->fs == fs)
83 return;
84
85 draw_flush(softpipe->draw);
86
87 softpipe->fs = fs;
88
89 softpipe->dirty |= SP_NEW_FS;
90 }
91
92
93 void
94 softpipe_delete_fs_state(struct pipe_context *pipe, void *fs)
95 {
96 struct sp_fragment_shader *state = fs;
97
98 assert(fs != softpipe_context(pipe)->fs);
99
100 state->delete( state );
101 }
102
103
104 void *
105 softpipe_create_vs_state(struct pipe_context *pipe,
106 const struct pipe_shader_state *templ)
107 {
108 struct softpipe_context *softpipe = softpipe_context(pipe);
109 struct sp_vertex_shader *state;
110
111 state = CALLOC_STRUCT(sp_vertex_shader);
112 if (state == NULL )
113 goto fail;
114
115 /* copy shader tokens, the ones passed in will go away.
116 */
117 state->shader.tokens = tgsi_dup_tokens(templ->tokens);
118 if (state->shader.tokens == NULL)
119 goto fail;
120
121 state->draw_data = draw_create_vertex_shader(softpipe->draw, templ);
122 if (state->draw_data == NULL)
123 goto fail;
124
125 state->max_sampler = state->draw_data->info.file_max[TGSI_FILE_SAMPLER];
126
127 return state;
128
129 fail:
130 if (state) {
131 FREE( (void *)state->shader.tokens );
132 FREE( state->draw_data );
133 FREE( state );
134 }
135 return NULL;
136 }
137
138
139 void
140 softpipe_bind_vs_state(struct pipe_context *pipe, void *vs)
141 {
142 struct softpipe_context *softpipe = softpipe_context(pipe);
143
144 softpipe->vs = (struct sp_vertex_shader *) vs;
145
146 draw_bind_vertex_shader(softpipe->draw,
147 (softpipe->vs ? softpipe->vs->draw_data : NULL));
148
149 softpipe->dirty |= SP_NEW_VS;
150 }
151
152
153 void
154 softpipe_delete_vs_state(struct pipe_context *pipe, void *vs)
155 {
156 struct softpipe_context *softpipe = softpipe_context(pipe);
157
158 struct sp_vertex_shader *state = (struct sp_vertex_shader *) vs;
159
160 draw_delete_vertex_shader(softpipe->draw, state->draw_data);
161 FREE( (void *)state->shader.tokens );
162 FREE( state );
163 }
164
165
166
167 void
168 softpipe_set_constant_buffer(struct pipe_context *pipe,
169 uint shader, uint index,
170 struct pipe_buffer *buf)
171 {
172 struct softpipe_context *softpipe = softpipe_context(pipe);
173
174 assert(shader < PIPE_SHADER_TYPES);
175 assert(index < PIPE_MAX_CONSTANT_BUFFERS);
176
177 draw_flush(softpipe->draw);
178
179 /* note: reference counting */
180 pipe_buffer_reference(&softpipe->constants[shader][index], buf);
181
182 softpipe->dirty |= SP_NEW_CONSTANTS;
183 }
184
185 void *
186 softpipe_create_gs_state(struct pipe_context *pipe,
187 const struct pipe_shader_state *templ)
188 {
189 struct softpipe_context *softpipe = softpipe_context(pipe);
190 struct sp_geometry_shader *state;
191
192 state = CALLOC_STRUCT(sp_geometry_shader);
193 if (state == NULL )
194 goto fail;
195
196 /* debug */
197 if (softpipe->dump_gs)
198 tgsi_dump(templ->tokens, 0);
199
200 /* copy shader tokens, the ones passed in will go away.
201 */
202 state->shader.tokens = tgsi_dup_tokens(templ->tokens);
203 if (state->shader.tokens == NULL)
204 goto fail;
205
206 state->draw_data = draw_create_geometry_shader(softpipe->draw, templ);
207 if (state->draw_data == NULL)
208 goto fail;
209
210 return state;
211
212 fail:
213 if (state) {
214 FREE( (void *)state->shader.tokens );
215 FREE( state->draw_data );
216 FREE( state );
217 }
218 return NULL;
219 }
220
221
222 void
223 softpipe_bind_gs_state(struct pipe_context *pipe, void *gs)
224 {
225 struct softpipe_context *softpipe = softpipe_context(pipe);
226
227 softpipe->gs = (struct sp_geometry_shader *)gs;
228
229 draw_bind_geometry_shader(softpipe->draw,
230 (softpipe->gs ? softpipe->gs->draw_data : NULL));
231
232 softpipe->dirty |= SP_NEW_GS;
233 }
234
235
236 void
237 softpipe_delete_gs_state(struct pipe_context *pipe, void *gs)
238 {
239 struct softpipe_context *softpipe = softpipe_context(pipe);
240
241 struct sp_geometry_shader *state =
242 (struct sp_geometry_shader *)gs;
243
244 draw_delete_geometry_shader(softpipe->draw,
245 (state) ? state->draw_data : 0);
246 FREE(state);
247 }