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