Merge commit 'origin/gallium-0.1' into gallium-0.1
[mesa.git] / src / gallium / drivers / cell / ppu / cell_context.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 /**
29 * Authors
30 * Brian Paul
31 */
32
33
34 #include <stdio.h>
35
36 #include "pipe/p_defines.h"
37 #include "pipe/p_format.h"
38 #include "pipe/p_util.h"
39 #include "pipe/p_winsys.h"
40 #include "cell/common.h"
41 #include "draw/draw_context.h"
42 #include "draw/draw_private.h"
43 #include "cell_clear.h"
44 #include "cell_context.h"
45 #include "cell_draw_arrays.h"
46 #include "cell_flush.h"
47 #include "cell_render.h"
48 #include "cell_state.h"
49 #include "cell_surface.h"
50 #include "cell_spu.h"
51 #include "cell_texture.h"
52 #include "cell_vbuf.h"
53
54
55
56 static boolean
57 cell_is_format_supported( struct pipe_context *pipe,
58 enum pipe_format format, uint type )
59 {
60 /*struct cell_context *cell = cell_context( pipe );*/
61
62 switch (type) {
63 case PIPE_TEXTURE:
64 /* cell supports all texture formats, XXX for now anyway */
65 return TRUE;
66 case PIPE_SURFACE:
67 /* cell supports all (off-screen) surface formats, XXX for now */
68 return TRUE;
69 default:
70 assert(0);
71 return FALSE;
72 }
73 }
74
75
76 static int cell_get_param(struct pipe_context *pipe, int param)
77 {
78 switch (param) {
79 case PIPE_CAP_MAX_TEXTURE_IMAGE_UNITS:
80 return 8;
81 case PIPE_CAP_NPOT_TEXTURES:
82 return 1;
83 case PIPE_CAP_TWO_SIDED_STENCIL:
84 return 1;
85 case PIPE_CAP_GLSL:
86 return 1;
87 case PIPE_CAP_S3TC:
88 return 0;
89 case PIPE_CAP_ANISOTROPIC_FILTER:
90 return 0;
91 case PIPE_CAP_POINT_SPRITE:
92 return 1;
93 case PIPE_CAP_MAX_RENDER_TARGETS:
94 return 1;
95 case PIPE_CAP_OCCLUSION_QUERY:
96 return 1;
97 case PIPE_CAP_TEXTURE_SHADOW_MAP:
98 return 1;
99 case PIPE_CAP_MAX_TEXTURE_2D_LEVELS:
100 return 12; /* max 2Kx2K */
101 case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
102 return 8; /* max 128x128x128 */
103 case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
104 return 12; /* max 2Kx2K */
105 default:
106 return 0;
107 }
108 }
109
110 static float cell_get_paramf(struct pipe_context *pipe, int param)
111 {
112 switch (param) {
113 case PIPE_CAP_MAX_LINE_WIDTH:
114 /* fall-through */
115 case PIPE_CAP_MAX_LINE_WIDTH_AA:
116 return 255.0; /* arbitrary */
117
118 case PIPE_CAP_MAX_POINT_WIDTH:
119 /* fall-through */
120 case PIPE_CAP_MAX_POINT_WIDTH_AA:
121 return 255.0; /* arbitrary */
122
123 case PIPE_CAP_MAX_TEXTURE_ANISOTROPY:
124 return 0.0;
125
126 case PIPE_CAP_MAX_TEXTURE_LOD_BIAS:
127 return 16.0; /* arbitrary */
128
129 default:
130 return 0;
131 }
132 }
133
134
135 static const char *
136 cell_get_name( struct pipe_context *pipe )
137 {
138 return "Cell";
139 }
140
141 static const char *
142 cell_get_vendor( struct pipe_context *pipe )
143 {
144 return "Tungsten Graphics, Inc.";
145 }
146
147
148
149 static void
150 cell_destroy_context( struct pipe_context *pipe )
151 {
152 struct cell_context *cell = cell_context(pipe);
153
154 cell_spu_exit(cell);
155
156 align_free(cell);
157 }
158
159
160 static struct draw_context *
161 cell_draw_create(struct cell_context *cell)
162 {
163 struct draw_context *draw = draw_create();
164
165 if (getenv("GALLIUM_CELL_VS")) {
166 /* plug in SPU-based vertex transformation code */
167 draw->shader_queue_flush = cell_vertex_shader_queue_flush;
168 draw->driver_private = cell;
169 }
170
171 return draw;
172 }
173
174
175 struct pipe_context *
176 cell_create_context(struct pipe_winsys *winsys, struct cell_winsys *cws)
177 {
178 struct cell_context *cell;
179 uint spu, buf;
180
181 /* some fields need to be 16-byte aligned, so align the whole object */
182 cell = (struct cell_context*) align_malloc(sizeof(struct cell_context), 16);
183 if (!cell)
184 return NULL;
185
186 memset(cell, 0, sizeof(*cell));
187
188 cell->winsys = cws;
189 cell->pipe.winsys = winsys;
190 cell->pipe.destroy = cell_destroy_context;
191
192 /* queries */
193 cell->pipe.is_format_supported = cell_is_format_supported;
194 cell->pipe.get_name = cell_get_name;
195 cell->pipe.get_vendor = cell_get_vendor;
196 cell->pipe.get_param = cell_get_param;
197 cell->pipe.get_paramf = cell_get_paramf;
198
199
200 /* state setters */
201 cell->pipe.create_blend_state = cell_create_blend_state;
202 cell->pipe.bind_blend_state = cell_bind_blend_state;
203 cell->pipe.delete_blend_state = cell_delete_blend_state;
204
205 cell->pipe.create_sampler_state = cell_create_sampler_state;
206 cell->pipe.bind_sampler_state = cell_bind_sampler_state;
207 cell->pipe.delete_sampler_state = cell_delete_sampler_state;
208
209 cell->pipe.create_depth_stencil_alpha_state = cell_create_depth_stencil_alpha_state;
210 cell->pipe.bind_depth_stencil_alpha_state = cell_bind_depth_stencil_alpha_state;
211 cell->pipe.delete_depth_stencil_alpha_state = cell_delete_depth_stencil_alpha_state;
212
213 cell->pipe.create_rasterizer_state = cell_create_rasterizer_state;
214 cell->pipe.bind_rasterizer_state = cell_bind_rasterizer_state;
215 cell->pipe.delete_rasterizer_state = cell_delete_rasterizer_state;
216
217 cell->pipe.create_fs_state = cell_create_fs_state;
218 cell->pipe.bind_fs_state = cell_bind_fs_state;
219 cell->pipe.delete_fs_state = cell_delete_fs_state;
220
221 cell->pipe.create_vs_state = cell_create_vs_state;
222 cell->pipe.bind_vs_state = cell_bind_vs_state;
223 cell->pipe.delete_vs_state = cell_delete_vs_state;
224
225 cell->pipe.set_blend_color = cell_set_blend_color;
226 cell->pipe.set_clip_state = cell_set_clip_state;
227 cell->pipe.set_constant_buffer = cell_set_constant_buffer;
228
229 cell->pipe.set_framebuffer_state = cell_set_framebuffer_state;
230
231 cell->pipe.set_polygon_stipple = cell_set_polygon_stipple;
232 cell->pipe.set_scissor_state = cell_set_scissor_state;
233 cell->pipe.set_viewport_state = cell_set_viewport_state;
234
235 cell->pipe.set_vertex_buffer = cell_set_vertex_buffer;
236 cell->pipe.set_vertex_element = cell_set_vertex_element;
237
238 cell->pipe.draw_arrays = cell_draw_arrays;
239 cell->pipe.draw_elements = cell_draw_elements;
240
241 cell->pipe.clear = cell_clear_surface;
242 cell->pipe.flush = cell_flush;
243
244 /* textures */
245 cell->pipe.texture_create = cell_texture_create;
246 cell->pipe.texture_release = cell_texture_release;
247 cell->pipe.get_tex_surface = cell_get_tex_surface;
248
249 cell->pipe.set_sampler_texture = cell_set_sampler_texture;
250
251 #if 0
252 cell->pipe.begin_query = cell_begin_query;
253 cell->pipe.end_query = cell_end_query;
254 cell->pipe.wait_query = cell_wait_query;
255 #endif
256
257 cell_init_surface_functions(cell);
258
259 cell->draw = cell_draw_create(cell);
260
261 cell_init_vbuf(cell);
262 draw_set_rasterize_stage(cell->draw, cell->vbuf);
263
264 /*
265 * SPU stuff
266 */
267 cell->num_spus = 6;
268
269 cell_start_spus(cell);
270
271 /* init command, vertex/index buffer info */
272 for (buf = 0; buf < CELL_NUM_BUFFERS; buf++) {
273 cell->buffer_size[buf] = 0;
274
275 /* init batch buffer status values,
276 * mark 0th buffer as used, rest as free.
277 */
278 for (spu = 0; spu < cell->num_spus; spu++) {
279 if (buf == 0)
280 cell->buffer_status[spu][buf][0] = CELL_BUFFER_STATUS_USED;
281 else
282 cell->buffer_status[spu][buf][0] = CELL_BUFFER_STATUS_FREE;
283 }
284 }
285
286 return &cell->pipe;
287 }