d6f3229bed78b1c5d5f346e42a44b4c780238071
[profile/ivi/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 || shader == PIPE_SHADER_GEOMETRY) {
185       draw_set_mapped_constant_buffer(softpipe->draw, shader, index, data, size);
186    }
187
188    softpipe->mapped_constants[shader][index] = data;
189    softpipe->dirty |= SP_NEW_CONSTANTS;
190 }
191
192
193 void *
194 softpipe_create_gs_state(struct pipe_context *pipe,
195                          const struct pipe_shader_state *templ)
196 {
197    struct softpipe_context *softpipe = softpipe_context(pipe);
198    struct sp_geometry_shader *state;
199
200    state = CALLOC_STRUCT(sp_geometry_shader);
201    if (state == NULL )
202       goto fail;
203
204    /* debug */
205    if (softpipe->dump_gs)
206       tgsi_dump(templ->tokens, 0);
207
208    /* copy shader tokens, the ones passed in will go away.
209     */
210    state->shader.tokens = tgsi_dup_tokens(templ->tokens);
211    if (state->shader.tokens == NULL)
212       goto fail;
213
214    state->draw_data = draw_create_geometry_shader(softpipe->draw, templ);
215    if (state->draw_data == NULL)
216       goto fail;
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 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 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 }