bc6127d7e7c17983ab848378d51ffbafbfc83c38
[mesa.git] / src / gallium / drivers / cell / ppu / cell_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 "pipe/p_defines.h"
29 #include "util/u_memory.h"
30 #include "util/u_inlines.h"
31 #include "draw/draw_context.h"
32 #include "tgsi/tgsi_parse.h"
33
34 #include "cell_context.h"
35 #include "cell_state.h"
36 #include "cell_gen_fp.h"
37
38
39 /** cast wrapper */
40 static INLINE struct cell_fragment_shader_state *
41 cell_fragment_shader_state(void *shader)
42 {
43 return (struct cell_fragment_shader_state *) shader;
44 }
45
46
47 /** cast wrapper */
48 static INLINE struct cell_vertex_shader_state *
49 cell_vertex_shader_state(void *shader)
50 {
51 return (struct cell_vertex_shader_state *) shader;
52 }
53
54
55 /**
56 * Create fragment shader state.
57 * Called via pipe->create_fs_state()
58 */
59 static void *
60 cell_create_fs_state(struct pipe_context *pipe,
61 const struct pipe_shader_state *templ)
62 {
63 struct cell_context *cell = cell_context(pipe);
64 struct cell_fragment_shader_state *cfs;
65
66 cfs = CALLOC_STRUCT(cell_fragment_shader_state);
67 if (!cfs)
68 return NULL;
69
70 cfs->shader.tokens = tgsi_dup_tokens(templ->tokens);
71 if (!cfs->shader.tokens) {
72 FREE(cfs);
73 return NULL;
74 }
75
76 tgsi_scan_shader(templ->tokens, &cfs->info);
77
78 cell_gen_fragment_program(cell, cfs->shader.tokens, &cfs->code);
79
80 return cfs;
81 }
82
83
84 /**
85 * Called via pipe->bind_fs_state()
86 */
87 static void
88 cell_bind_fs_state(struct pipe_context *pipe, void *fs)
89 {
90 struct cell_context *cell = cell_context(pipe);
91
92 cell->fs = cell_fragment_shader_state(fs);
93
94 cell->dirty |= CELL_NEW_FS;
95 }
96
97
98 /**
99 * Called via pipe->delete_fs_state()
100 */
101 static void
102 cell_delete_fs_state(struct pipe_context *pipe, void *fs)
103 {
104 struct cell_fragment_shader_state *cfs = cell_fragment_shader_state(fs);
105
106 spe_release_func(&cfs->code);
107
108 FREE((void *) cfs->shader.tokens);
109 FREE(cfs);
110 }
111
112
113 /**
114 * Create vertex shader state.
115 * Called via pipe->create_vs_state()
116 */
117 static void *
118 cell_create_vs_state(struct pipe_context *pipe,
119 const struct pipe_shader_state *templ)
120 {
121 struct cell_context *cell = cell_context(pipe);
122 struct cell_vertex_shader_state *cvs;
123
124 cvs = CALLOC_STRUCT(cell_vertex_shader_state);
125 if (!cvs)
126 return NULL;
127
128 cvs->shader.tokens = tgsi_dup_tokens(templ->tokens);
129 if (!cvs->shader.tokens) {
130 FREE(cvs);
131 return NULL;
132 }
133
134 tgsi_scan_shader(templ->tokens, &cvs->info);
135
136 cvs->draw_data = draw_create_vertex_shader(cell->draw, &cvs->shader);
137 if (cvs->draw_data == NULL) {
138 FREE( (void *) cvs->shader.tokens );
139 FREE( cvs );
140 return NULL;
141 }
142
143 return cvs;
144 }
145
146
147 /**
148 * Called via pipe->bind_vs_state()
149 */
150 static void
151 cell_bind_vs_state(struct pipe_context *pipe, void *vs)
152 {
153 struct cell_context *cell = cell_context(pipe);
154
155 cell->vs = cell_vertex_shader_state(vs);
156
157 draw_bind_vertex_shader(cell->draw,
158 (cell->vs ? cell->vs->draw_data : NULL));
159
160 cell->dirty |= CELL_NEW_VS;
161 }
162
163
164 /**
165 * Called via pipe->delete_vs_state()
166 */
167 static void
168 cell_delete_vs_state(struct pipe_context *pipe, void *vs)
169 {
170 struct cell_context *cell = cell_context(pipe);
171 struct cell_vertex_shader_state *cvs = cell_vertex_shader_state(vs);
172
173 draw_delete_vertex_shader(cell->draw, cvs->draw_data);
174 FREE( (void *) cvs->shader.tokens );
175 FREE( cvs );
176 }
177
178
179 /**
180 * Called via pipe->set_constant_buffer()
181 */
182 static void
183 cell_set_constant_buffer(struct pipe_context *pipe,
184 uint shader, uint index,
185 struct pipe_buffer *buf)
186 {
187 struct cell_context *cell = cell_context(pipe);
188
189 assert(shader < PIPE_SHADER_TYPES);
190 assert(index == 0);
191
192 draw_flush(cell->draw);
193
194 /* note: reference counting */
195 pipe_buffer_reference(&cell->constants[shader], buf);
196
197 if (shader == PIPE_SHADER_VERTEX)
198 cell->dirty |= CELL_NEW_VS_CONSTANTS;
199 else if (shader == PIPE_SHADER_FRAGMENT)
200 cell->dirty |= CELL_NEW_FS_CONSTANTS;
201 }
202
203
204 void
205 cell_init_shader_functions(struct cell_context *cell)
206 {
207 cell->pipe.create_fs_state = cell_create_fs_state;
208 cell->pipe.bind_fs_state = cell_bind_fs_state;
209 cell->pipe.delete_fs_state = cell_delete_fs_state;
210
211 cell->pipe.create_vs_state = cell_create_vs_state;
212 cell->pipe.bind_vs_state = cell_bind_vs_state;
213 cell->pipe.delete_vs_state = cell_delete_vs_state;
214
215 cell->pipe.set_constant_buffer = cell_set_constant_buffer;
216 }