Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / drivers / softpipe / sp_state_shader.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_texture.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 "draw/draw_gs.h"
39 #include "tgsi/tgsi_dump.h"
40 #include "tgsi/tgsi_exec.h"
41 #include "tgsi/tgsi_scan.h"
42 #include "tgsi/tgsi_parse.h"
43
44
45 static void *
46 softpipe_create_fs_state(struct pipe_context *pipe,
47                          const struct pipe_shader_state *templ)
48 {
49    struct softpipe_context *softpipe = softpipe_context(pipe);
50    struct sp_fragment_shader *state;
51    unsigned i;
52
53    /* debug */
54    if (softpipe->dump_fs) 
55       tgsi_dump(templ->tokens, 0);
56
57    /* codegen */
58    state = softpipe_create_fs_sse( softpipe, templ );
59    if (!state) {
60       state = softpipe_create_fs_exec( softpipe, templ );
61    }
62
63    if (!state)
64       return NULL;
65
66    /* draw's fs state */
67    state->draw_shader = draw_create_fragment_shader(softpipe->draw, templ);
68    if (!state->draw_shader) {
69       state->delete( state );
70       return NULL;
71    }
72
73    /* get/save the summary info for this shader */
74    tgsi_scan_shader(templ->tokens, &state->info);
75
76    for (i = 0; i < state->info.num_properties; ++i) {
77       if (state->info.properties[i].name == TGSI_PROPERTY_FS_COORD_ORIGIN)
78          state->origin_lower_left = state->info.properties[i].data[0];
79       else if (state->info.properties[i].name == TGSI_PROPERTY_FS_COORD_PIXEL_CENTER)
80          state->pixel_center_integer = state->info.properties[i].data[0];
81       else if (state->info.properties[i].name == TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
82          state->color0_writes_all_cbufs = state->info.properties[i].data[0];
83    }
84
85    return state;
86 }
87
88
89 static void
90 softpipe_bind_fs_state(struct pipe_context *pipe, void *fs)
91 {
92    struct softpipe_context *softpipe = softpipe_context(pipe);
93
94    if (softpipe->fs == fs)
95       return;
96
97    draw_flush(softpipe->draw);
98
99    softpipe->fs = fs;
100
101    draw_bind_fragment_shader(softpipe->draw,
102                              (softpipe->fs ? softpipe->fs->draw_shader : NULL));
103
104    softpipe->dirty |= SP_NEW_FS;
105 }
106
107
108 static void
109 softpipe_delete_fs_state(struct pipe_context *pipe, void *fs)
110 {
111    struct softpipe_context *softpipe = softpipe_context(pipe);
112    struct sp_fragment_shader *state = fs;
113
114    assert(fs != softpipe_context(pipe)->fs);
115
116    if (softpipe->fs_machine->Tokens == state->shader.tokens) {
117       /* unbind the shader from the tgsi executor if we're
118        * deleting it.
119        */
120       tgsi_exec_machine_bind_shader(softpipe->fs_machine, NULL, 0, NULL);
121    }
122
123    draw_delete_fragment_shader(softpipe->draw, state->draw_shader);
124
125    state->delete( state );
126 }
127
128
129 static void *
130 softpipe_create_vs_state(struct pipe_context *pipe,
131                          const struct pipe_shader_state *templ)
132 {
133    struct softpipe_context *softpipe = softpipe_context(pipe);
134    struct sp_vertex_shader *state;
135
136    state = CALLOC_STRUCT(sp_vertex_shader);
137    if (state == NULL ) 
138       goto fail;
139
140    /* copy shader tokens, the ones passed in will go away.
141     */
142    state->shader.tokens = tgsi_dup_tokens(templ->tokens);
143    if (state->shader.tokens == NULL)
144       goto fail;
145
146    state->draw_data = draw_create_vertex_shader(softpipe->draw, templ);
147    if (state->draw_data == NULL) 
148       goto fail;
149
150    state->max_sampler = state->draw_data->info.file_max[TGSI_FILE_SAMPLER];
151
152    return state;
153
154 fail:
155    if (state) {
156       FREE( (void *)state->shader.tokens );
157       FREE( state->draw_data );
158       FREE( state );
159    }
160    return NULL;
161 }
162
163
164 static void
165 softpipe_bind_vs_state(struct pipe_context *pipe, void *vs)
166 {
167    struct softpipe_context *softpipe = softpipe_context(pipe);
168
169    softpipe->vs = (struct sp_vertex_shader *) vs;
170
171    draw_bind_vertex_shader(softpipe->draw,
172                            (softpipe->vs ? softpipe->vs->draw_data : NULL));
173
174    softpipe->dirty |= SP_NEW_VS;
175 }
176
177
178 static void
179 softpipe_delete_vs_state(struct pipe_context *pipe, void *vs)
180 {
181    struct softpipe_context *softpipe = softpipe_context(pipe);
182
183    struct sp_vertex_shader *state = (struct sp_vertex_shader *) vs;
184
185    draw_delete_vertex_shader(softpipe->draw, state->draw_data);
186    FREE( (void *)state->shader.tokens );
187    FREE( state );
188 }
189
190
191 static void *
192 softpipe_create_gs_state(struct pipe_context *pipe,
193                          const struct pipe_shader_state *templ)
194 {
195    struct softpipe_context *softpipe = softpipe_context(pipe);
196    struct sp_geometry_shader *state;
197
198    state = CALLOC_STRUCT(sp_geometry_shader);
199    if (state == NULL )
200       goto fail;
201
202    /* debug */
203    if (softpipe->dump_gs)
204       tgsi_dump(templ->tokens, 0);
205
206    /* copy shader tokens, the ones passed in will go away.
207     */
208    state->shader.tokens = tgsi_dup_tokens(templ->tokens);
209    if (state->shader.tokens == NULL)
210       goto fail;
211
212    state->draw_data = draw_create_geometry_shader(softpipe->draw, templ);
213    if (state->draw_data == NULL)
214       goto fail;
215
216    state->max_sampler = state->draw_data->info.file_max[TGSI_FILE_SAMPLER];
217
218    return state;
219
220 fail:
221    if (state) {
222       FREE( (void *)state->shader.tokens );
223       FREE( state->draw_data );
224       FREE( state );
225    }
226    return NULL;
227 }
228
229
230 static void
231 softpipe_bind_gs_state(struct pipe_context *pipe, void *gs)
232 {
233    struct softpipe_context *softpipe = softpipe_context(pipe);
234
235    softpipe->gs = (struct sp_geometry_shader *)gs;
236
237    draw_bind_geometry_shader(softpipe->draw,
238                              (softpipe->gs ? softpipe->gs->draw_data : NULL));
239
240    softpipe->dirty |= SP_NEW_GS;
241 }
242
243
244 static void
245 softpipe_delete_gs_state(struct pipe_context *pipe, void *gs)
246 {
247    struct softpipe_context *softpipe = softpipe_context(pipe);
248
249    struct sp_geometry_shader *state =
250       (struct sp_geometry_shader *)gs;
251
252    draw_delete_geometry_shader(softpipe->draw,
253                                (state) ? state->draw_data : 0);
254    FREE(state);
255 }
256
257
258 static void
259 softpipe_set_constant_buffer(struct pipe_context *pipe,
260                              uint shader, uint index,
261                              struct pipe_resource *constants)
262 {
263    struct softpipe_context *softpipe = softpipe_context(pipe);
264    unsigned size = constants ? constants->width0 : 0;
265    const void *data = constants ? softpipe_resource(constants)->data : NULL;
266
267    assert(shader < PIPE_SHADER_TYPES);
268
269    draw_flush(softpipe->draw);
270
271    /* note: reference counting */
272    pipe_resource_reference(&softpipe->constants[shader][index], constants);
273
274    if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
275       draw_set_mapped_constant_buffer(softpipe->draw, shader, index, data, size);
276    }
277
278    softpipe->mapped_constants[shader][index] = data;
279    softpipe->const_buffer_size[shader][index] = size;
280
281    softpipe->dirty |= SP_NEW_CONSTANTS;
282 }
283
284
285 void
286 softpipe_init_shader_funcs(struct pipe_context *pipe)
287 {
288    pipe->create_fs_state = softpipe_create_fs_state;
289    pipe->bind_fs_state   = softpipe_bind_fs_state;
290    pipe->delete_fs_state = softpipe_delete_fs_state;
291
292    pipe->create_vs_state = softpipe_create_vs_state;
293    pipe->bind_vs_state   = softpipe_bind_vs_state;
294    pipe->delete_vs_state = softpipe_delete_vs_state;
295
296    pipe->create_gs_state = softpipe_create_gs_state;
297    pipe->bind_gs_state   = softpipe_bind_gs_state;
298    pipe->delete_gs_state = softpipe_delete_gs_state;
299
300    pipe->set_constant_buffer = softpipe_set_constant_buffer;
301 }