gallium: Have pipe_buffer_* receive a pipe_screen instead of a pipe_context.
[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 "pipe/p_inlines.h"
31 #include "pipe/p_winsys.h"
32 #include "draw/draw_context.h"
33 #include "tgsi/tgsi_parse.h"
34
35 #include "cell_context.h"
36 #include "cell_state.h"
37
38
39
40 /** cast wrapper */
41 static INLINE struct cell_fragment_shader_state *
42 cell_fragment_shader_state(void *shader)
43 {
44 return (struct cell_fragment_shader_state *) shader;
45 }
46
47
48 /** cast wrapper */
49 static INLINE struct cell_vertex_shader_state *
50 cell_vertex_shader_state(void *shader)
51 {
52 return (struct cell_vertex_shader_state *) shader;
53 }
54
55
56
57 static void *
58 cell_create_fs_state(struct pipe_context *pipe,
59 const struct pipe_shader_state *templ)
60 {
61 /*struct cell_context *cell = cell_context(pipe);*/
62 struct cell_fragment_shader_state *cfs;
63
64 cfs = CALLOC_STRUCT(cell_fragment_shader_state);
65 if (!cfs)
66 return NULL;
67
68 cfs->shader.tokens = tgsi_dup_tokens(templ->tokens);
69 if (!cfs->shader.tokens) {
70 FREE(cfs);
71 return NULL;
72 }
73
74 tgsi_scan_shader(templ->tokens, &cfs->info);
75
76 return cfs;
77 }
78
79
80 static void
81 cell_bind_fs_state(struct pipe_context *pipe, void *fs)
82 {
83 struct cell_context *cell = cell_context(pipe);
84
85 cell->fs = cell_fragment_shader_state(fs);
86
87 cell->dirty |= CELL_NEW_FS;
88 }
89
90
91 static void
92 cell_delete_fs_state(struct pipe_context *pipe, void *fs)
93 {
94 struct cell_fragment_shader_state *cfs = cell_fragment_shader_state(fs);
95
96 FREE((void *) cfs->shader.tokens);
97 FREE(cfs);
98 }
99
100
101 static void *
102 cell_create_vs_state(struct pipe_context *pipe,
103 const struct pipe_shader_state *templ)
104 {
105 struct cell_context *cell = cell_context(pipe);
106 struct cell_vertex_shader_state *cvs;
107
108 cvs = CALLOC_STRUCT(cell_vertex_shader_state);
109 if (!cvs)
110 return NULL;
111
112 cvs->shader.tokens = tgsi_dup_tokens(templ->tokens);
113 if (!cvs->shader.tokens) {
114 FREE(cvs);
115 return NULL;
116 }
117
118 tgsi_scan_shader(templ->tokens, &cvs->info);
119
120 cvs->draw_data = draw_create_vertex_shader(cell->draw, &cvs->shader);
121 if (cvs->draw_data == NULL) {
122 FREE( (void *) cvs->shader.tokens );
123 FREE( cvs );
124 return NULL;
125 }
126
127 return cvs;
128 }
129
130
131 static void
132 cell_bind_vs_state(struct pipe_context *pipe, void *vs)
133 {
134 struct cell_context *cell = cell_context(pipe);
135
136 cell->vs = cell_vertex_shader_state(vs);
137
138 draw_bind_vertex_shader(cell->draw,
139 (cell->vs ? cell->vs->draw_data : NULL));
140
141 cell->dirty |= CELL_NEW_VS;
142 }
143
144
145 static void
146 cell_delete_vs_state(struct pipe_context *pipe, void *vs)
147 {
148 struct cell_context *cell = cell_context(pipe);
149 struct cell_vertex_shader_state *cvs = cell_vertex_shader_state(vs);
150
151 draw_delete_vertex_shader(cell->draw, cvs->draw_data);
152 FREE( (void *) cvs->shader.tokens );
153 FREE( cvs );
154 }
155
156
157 static void
158 cell_set_constant_buffer(struct pipe_context *pipe,
159 uint shader, uint index,
160 const struct pipe_constant_buffer *buf)
161 {
162 struct cell_context *cell = cell_context(pipe);
163 struct pipe_winsys *ws = pipe->winsys;
164
165 assert(shader < PIPE_SHADER_TYPES);
166 assert(index == 0);
167
168 /* note: reference counting */
169 winsys_buffer_reference(ws,
170 &cell->constants[shader].buffer,
171 buf->buffer);
172 cell->constants[shader].size = buf->size;
173
174 cell->dirty |= CELL_NEW_CONSTANTS;
175 }
176
177
178 void
179 cell_init_shader_functions(struct cell_context *cell)
180 {
181 cell->pipe.create_fs_state = cell_create_fs_state;
182 cell->pipe.bind_fs_state = cell_bind_fs_state;
183 cell->pipe.delete_fs_state = cell_delete_fs_state;
184
185 cell->pipe.create_vs_state = cell_create_vs_state;
186 cell->pipe.bind_vs_state = cell_bind_vs_state;
187 cell->pipe.delete_vs_state = cell_delete_vs_state;
188
189 cell->pipe.set_constant_buffer = cell_set_constant_buffer;
190 }