Merge branch 'nouveau-gallium-0.1' into darktama-gallium-0.1
[mesa.git] / src / mesa / pipe / i915simple / i915_context.c
1 /**************************************************************************
2 *
3 * Copyright 2003 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 "i915_context.h"
29 #include "i915_winsys.h"
30 #include "i915_state.h"
31 #include "i915_batch.h"
32 #include "i915_texture.h"
33 #include "i915_reg.h"
34
35 #include "pipe/draw/draw_context.h"
36 #include "pipe/p_defines.h"
37 #include "pipe/p_winsys.h"
38 #include "pipe/p_util.h"
39
40
41 /**
42 * Query format support.
43 * If we find texture and drawable support differs, add a selector
44 * parameter or another function.
45 */
46 static boolean
47 i915_is_format_supported( struct pipe_context *pipe,
48 enum pipe_format format )
49 {
50 #if 0
51 /* XXX: This is broken -- rewrite if still needed. */
52 static const unsigned tex_supported[] = {
53 PIPE_FORMAT_R8G8B8A8_UNORM,
54 PIPE_FORMAT_A8R8G8B8_UNORM,
55 PIPE_FORMAT_R5G6B5_UNORM,
56 PIPE_FORMAT_U_L8,
57 PIPE_FORMAT_U_A8,
58 PIPE_FORMAT_U_I8,
59 PIPE_FORMAT_U_L8_A8,
60 PIPE_FORMAT_YCBCR,
61 PIPE_FORMAT_YCBCR_REV,
62 PIPE_FORMAT_S8Z24_UNORM,
63 };
64
65
66 /* Actually a lot more than this - add later:
67 */
68 static const unsigned render_supported[] = {
69 PIPE_FORMAT_A8R8G8B8_UNORM,
70 PIPE_FORMAT_R5G6B5_UNORM,
71 };
72
73 /*
74 */
75 static const unsigned z_stencil_supported[] = {
76 PIPE_FORMAT_Z16_UNORM,
77 PIPE_FORMAT_Z32_UNORM,
78 PIPE_FORMAT_S8Z24_UNORM,
79 };
80
81 switch (type) {
82 case PIPE_RENDER_FORMAT:
83 *numFormats = Elements(render_supported);
84 return render_supported;
85
86 case PIPE_TEX_FORMAT:
87 *numFormats = Elements(tex_supported);
88 return render_supported;
89
90 case PIPE_Z_STENCIL_FORMAT:
91 *numFormats = Elements(render_supported);
92 return render_supported;
93
94 default:
95 *numFormats = 0;
96 return NULL;
97 }
98 #else
99 switch( format ) {
100 case PIPE_FORMAT_A8R8G8B8_UNORM:
101 case PIPE_FORMAT_R5G6B5_UNORM:
102 case PIPE_FORMAT_S8Z24_UNORM:
103 return TRUE;
104 default:
105 return FALSE;
106 };
107 #endif
108 }
109
110
111 static int
112 i915_get_param(struct pipe_context *pipe, int param)
113 {
114 switch (param) {
115 case PIPE_CAP_MAX_TEXTURE_IMAGE_UNITS:
116 return 8;
117 case PIPE_CAP_NPOT_TEXTURES:
118 return 1;
119 case PIPE_CAP_TWO_SIDED_STENCIL:
120 return 1;
121 case PIPE_CAP_GLSL:
122 return 0;
123 case PIPE_CAP_S3TC:
124 return 0;
125 case PIPE_CAP_ANISOTROPIC_FILTER:
126 return 0;
127 case PIPE_CAP_POINT_SPRITE:
128 return 0;
129 case PIPE_CAP_MAX_RENDER_TARGETS:
130 return 1;
131 case PIPE_CAP_OCCLUSION_QUERY:
132 return 0;
133 case PIPE_CAP_TEXTURE_SHADOW_MAP:
134 return 1;
135 case PIPE_CAP_MAX_TEXTURE_2D_LEVELS:
136 return 11; /* max 1024x1024 */
137 case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
138 return 8; /* max 128x128x128 */
139 case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
140 return 11; /* max 1024x1024 */
141 default:
142 return 0;
143 }
144 }
145
146
147 static float
148 i915_get_paramf(struct pipe_context *pipe, int param)
149 {
150 switch (param) {
151 case PIPE_CAP_MAX_LINE_WIDTH:
152 /* fall-through */
153 case PIPE_CAP_MAX_LINE_WIDTH_AA:
154 return 7.5;
155
156 case PIPE_CAP_MAX_POINT_WIDTH:
157 /* fall-through */
158 case PIPE_CAP_MAX_POINT_WIDTH_AA:
159 return 255.0;
160
161 case PIPE_CAP_MAX_TEXTURE_ANISOTROPY:
162 return 4.0;
163
164 case PIPE_CAP_MAX_TEXTURE_LOD_BIAS:
165 return 16.0;
166
167 default:
168 return 0;
169 }
170 }
171
172
173 static void i915_destroy( struct pipe_context *pipe )
174 {
175 struct i915_context *i915 = i915_context( pipe );
176
177 draw_destroy( i915->draw );
178
179 FREE( i915 );
180 }
181
182
183
184 static void
185 i915_begin_query(struct pipe_context *pipe, struct pipe_query_object *q)
186 {
187 /* should never be called */
188 assert(0);
189 }
190
191
192 static void
193 i915_end_query(struct pipe_context *pipe, struct pipe_query_object *q)
194 {
195 /* should never be called */
196 assert(0);
197 }
198
199
200 static boolean
201 i915_draw_elements( struct pipe_context *pipe,
202 struct pipe_buffer_handle *indexBuffer,
203 unsigned indexSize,
204 unsigned prim, unsigned start, unsigned count)
205 {
206 struct i915_context *i915 = i915_context( pipe );
207 struct draw_context *draw = i915->draw;
208 unsigned i;
209
210 if (i915->dirty)
211 i915_update_derived( i915 );
212
213 /*
214 * Map vertex buffers
215 */
216 for (i = 0; i < PIPE_ATTRIB_MAX; i++) {
217 if (i915->vertex_buffer[i].buffer) {
218 void *buf
219 = pipe->winsys->buffer_map(pipe->winsys,
220 i915->vertex_buffer[i].buffer,
221 PIPE_BUFFER_FLAG_READ);
222 draw_set_mapped_vertex_buffer(draw, i, buf);
223 }
224 }
225 /* Map index buffer, if present */
226 if (indexBuffer) {
227 void *mapped_indexes
228 = pipe->winsys->buffer_map(pipe->winsys, indexBuffer,
229 PIPE_BUFFER_FLAG_READ);
230 draw_set_mapped_element_buffer(draw, indexSize, mapped_indexes);
231 }
232 else {
233 /* no index/element buffer */
234 draw_set_mapped_element_buffer(draw, 0, NULL);
235 }
236
237 /* Map feedback buffers if enabled */
238 if (i915->feedback.enabled) {
239 const uint n = i915->feedback.interleaved ? 1 : i915->feedback.num_attribs;
240 for (i = 0; i < n; i++) {
241 void *ptr = pipe->winsys->buffer_map(pipe->winsys,
242 i915->feedback_buffer[i].buffer,
243 PIPE_BUFFER_FLAG_WRITE);
244 draw_set_mapped_feedback_buffer(draw, i, ptr,
245 i915->feedback_buffer[i].size);
246 }
247 }
248
249
250 draw_set_mapped_constant_buffer(draw,
251 i915->current.constants[PIPE_SHADER_VERTEX]);
252
253 /* draw! */
254 draw_arrays(i915->draw, prim, start, count);
255
256 /*
257 * unmap vertex/index buffers
258 */
259 for (i = 0; i < PIPE_ATTRIB_MAX; i++) {
260 if (i915->vertex_buffer[i].buffer) {
261 pipe->winsys->buffer_unmap(pipe->winsys, i915->vertex_buffer[i].buffer);
262 draw_set_mapped_vertex_buffer(draw, i, NULL);
263 }
264 }
265 if (indexBuffer) {
266 pipe->winsys->buffer_unmap(pipe->winsys, indexBuffer);
267 draw_set_mapped_element_buffer(draw, 0, NULL);
268 }
269
270 /* Unmap feedback buffers if enabled */
271 if (i915->feedback.enabled) {
272 const uint n = i915->feedback.interleaved ? 1 : i915->feedback.num_attribs;
273 for (i = 0; i < n; i++) {
274 pipe->winsys->buffer_unmap(pipe->winsys,
275 i915->feedback_buffer[i].buffer);
276 draw_set_mapped_feedback_buffer(draw, i, NULL, 0);
277 }
278 }
279
280 return TRUE;
281 }
282
283
284 static boolean i915_draw_arrays( struct pipe_context *pipe,
285 unsigned prim, unsigned start, unsigned count)
286 {
287 return i915_draw_elements(pipe, NULL, 0, prim, start, count);
288 }
289
290
291
292 struct pipe_context *i915_create( struct pipe_winsys *pipe_winsys,
293 struct i915_winsys *i915_winsys,
294 unsigned pci_id )
295 {
296 struct i915_context *i915;
297 unsigned is_i945 = 0;
298
299 switch (pci_id) {
300 case PCI_CHIP_I915_G:
301 case PCI_CHIP_I915_GM:
302 break;
303
304 case PCI_CHIP_I945_G:
305 case PCI_CHIP_I945_GM:
306 case PCI_CHIP_I945_GME:
307 case PCI_CHIP_G33_G:
308 case PCI_CHIP_Q33_G:
309 case PCI_CHIP_Q35_G:
310 is_i945 = 1;
311 break;
312
313 default:
314 pipe_winsys->printf(pipe_winsys,
315 "%s: unknown pci id 0x%x, cannot create context\n",
316 __FUNCTION__, pci_id);
317 return NULL;
318 }
319
320 i915 = CALLOC_STRUCT(i915_context);
321 if (i915 == NULL)
322 return NULL;
323
324 i915->winsys = i915_winsys;
325 i915->pipe.winsys = pipe_winsys;
326
327 i915->pipe.destroy = i915_destroy;
328 i915->pipe.is_format_supported = i915_is_format_supported;
329 i915->pipe.get_param = i915_get_param;
330 i915->pipe.get_paramf = i915_get_paramf;
331
332 i915->pipe.clear = i915_clear;
333
334 i915->pipe.begin_query = i915_begin_query;
335 i915->pipe.end_query = i915_end_query;
336
337 i915->pipe.draw_arrays = i915_draw_arrays;
338 i915->pipe.draw_elements = i915_draw_elements;
339
340 /*
341 * Create drawing context and plug our rendering stage into it.
342 */
343 i915->draw = draw_create();
344 assert(i915->draw);
345 if (GETENV("I915_VBUF")) {
346 draw_set_rasterize_stage(i915->draw, i915_draw_vbuf_stage(i915));
347 }
348 else {
349 draw_set_rasterize_stage(i915->draw, i915_draw_render_stage(i915));
350 }
351
352 i915_init_surface_functions(i915);
353 i915_init_state_functions(i915);
354 i915_init_flush_functions(i915);
355 i915_init_string_functions(i915);
356
357 i915->pci_id = pci_id;
358 i915->flags.is_i945 = is_i945;
359
360 i915->pipe.texture_create = i915_texture_create;
361 i915->pipe.texture_release = i915_texture_release;
362
363 i915->dirty = ~0;
364 i915->hardware_dirty = ~0;
365
366 /* Batch stream debugging is a bit hacked up at the moment:
367 */
368 i915->batch_start = NULL;
369
370 /*
371 * XXX we could plug GL selection/feedback into the drawing pipeline
372 * by specifying a different setup/render stage.
373 */
374
375 return &i915->pipe;
376 }
377