94649231cf3bba22bfee3adce99c7e22c0db06c1
[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 uint format )
49 {
50 #if 0
51 /* XXX: This is broken -- rewrite if still needed. */
52 static const unsigned tex_supported[] = {
53 PIPE_FORMAT_U_R8_G8_B8_A8,
54 PIPE_FORMAT_U_A8_R8_G8_B8,
55 PIPE_FORMAT_U_R5_G6_B5,
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_S8_Z24,
63 };
64
65
66 /* Actually a lot more than this - add later:
67 */
68 static const unsigned render_supported[] = {
69 PIPE_FORMAT_U_A8_R8_G8_B8,
70 PIPE_FORMAT_U_R5_G6_B5,
71 };
72
73 /*
74 */
75 static const unsigned z_stencil_supported[] = {
76 PIPE_FORMAT_U_Z16,
77 PIPE_FORMAT_U_Z32,
78 PIPE_FORMAT_S8_Z24,
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_U_A8_R8_G8_B8:
101 case PIPE_FORMAT_U_R5_G6_B5:
102 case PIPE_FORMAT_S8_Z24:
103 return TRUE;
104 };
105 return FALSE;
106 #endif
107 }
108
109
110 static int
111 i915_get_param(struct pipe_context *pipe, int param)
112 {
113 switch (param) {
114 case PIPE_CAP_MAX_TEXTURE_IMAGE_UNITS:
115 return 8;
116 case PIPE_CAP_NPOT_TEXTURES:
117 return 1;
118 case PIPE_CAP_TWO_SIDED_STENCIL:
119 return 1;
120 case PIPE_CAP_GLSL:
121 return 0;
122 case PIPE_CAP_S3TC:
123 return 0;
124 case PIPE_CAP_ANISOTROPIC_FILTER:
125 return 0;
126 case PIPE_CAP_POINT_SPRITE:
127 return 0;
128 case PIPE_CAP_MAX_RENDER_TARGETS:
129 return 1;
130 case PIPE_CAP_OCCLUSION_QUERY:
131 return 0;
132 case PIPE_CAP_TEXTURE_SHADOW_MAP:
133 return 1;
134 case PIPE_CAP_MAX_TEXTURE_2D_LEVELS:
135 return 11; /* max 1024x1024 */
136 case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
137 return 8; /* max 128x128x128 */
138 case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
139 return 11; /* max 1024x1024 */
140 default:
141 return 0;
142 }
143 }
144
145
146 static float
147 i915_get_paramf(struct pipe_context *pipe, int param)
148 {
149 switch (param) {
150 case PIPE_CAP_MAX_LINE_WIDTH:
151 /* fall-through */
152 case PIPE_CAP_MAX_LINE_WIDTH_AA:
153 return 7.5;
154
155 case PIPE_CAP_MAX_POINT_WIDTH:
156 /* fall-through */
157 case PIPE_CAP_MAX_POINT_WIDTH_AA:
158 return 255.0;
159
160 case PIPE_CAP_MAX_TEXTURE_ANISOTROPY:
161 return 4.0;
162
163 case PIPE_CAP_MAX_TEXTURE_LOD_BIAS:
164 return 16.0;
165
166 default:
167 return 0;
168 }
169 }
170
171
172 static void i915_destroy( struct pipe_context *pipe )
173 {
174 struct i915_context *i915 = i915_context( pipe );
175
176 draw_destroy( i915->draw );
177
178 FREE( i915 );
179 }
180
181
182
183 static void
184 i915_begin_query(struct pipe_context *pipe, struct pipe_query_object *q)
185 {
186 /* should never be called */
187 assert(0);
188 }
189
190
191 static void
192 i915_end_query(struct pipe_context *pipe, struct pipe_query_object *q)
193 {
194 /* should never be called */
195 assert(0);
196 }
197
198
199 static boolean
200 i915_draw_elements( struct pipe_context *pipe,
201 struct pipe_buffer_handle *indexBuffer,
202 unsigned indexSize,
203 unsigned prim, unsigned start, unsigned count)
204 {
205 struct i915_context *i915 = i915_context( pipe );
206 struct draw_context *draw = i915->draw;
207 unsigned i;
208
209 if (i915->dirty)
210 i915_update_derived( i915 );
211
212 /*
213 * Map vertex buffers
214 */
215 for (i = 0; i < PIPE_ATTRIB_MAX; i++) {
216 if (i915->vertex_buffer[i].buffer) {
217 void *buf
218 = pipe->winsys->buffer_map(pipe->winsys,
219 i915->vertex_buffer[i].buffer,
220 PIPE_BUFFER_FLAG_READ);
221 draw_set_mapped_vertex_buffer(draw, i, buf);
222 }
223 }
224 /* Map index buffer, if present */
225 if (indexBuffer) {
226 void *mapped_indexes
227 = pipe->winsys->buffer_map(pipe->winsys, indexBuffer,
228 PIPE_BUFFER_FLAG_READ);
229 draw_set_mapped_element_buffer(draw, indexSize, mapped_indexes);
230 }
231 else {
232 /* no index/element buffer */
233 draw_set_mapped_element_buffer(draw, 0, NULL);
234 }
235
236 /* Map feedback buffers if enabled */
237 if (i915->feedback.enabled) {
238 const uint n = i915->feedback.interleaved ? 1 : i915->feedback.num_attribs;
239 for (i = 0; i < n; i++) {
240 void *ptr = pipe->winsys->buffer_map(pipe->winsys,
241 i915->feedback_buffer[i].buffer,
242 PIPE_BUFFER_FLAG_WRITE);
243 draw_set_mapped_feedback_buffer(draw, i, ptr,
244 i915->feedback_buffer[i].size);
245 }
246 }
247
248
249 draw_set_mapped_constant_buffer(draw,
250 i915->current.constants[PIPE_SHADER_VERTEX]);
251
252 /* draw! */
253 draw_arrays(i915->draw, prim, start, count);
254
255 /*
256 * unmap vertex/index buffers
257 */
258 for (i = 0; i < PIPE_ATTRIB_MAX; i++) {
259 if (i915->vertex_buffer[i].buffer) {
260 pipe->winsys->buffer_unmap(pipe->winsys, i915->vertex_buffer[i].buffer);
261 draw_set_mapped_vertex_buffer(draw, i, NULL);
262 }
263 }
264 if (indexBuffer) {
265 pipe->winsys->buffer_unmap(pipe->winsys, indexBuffer);
266 draw_set_mapped_element_buffer(draw, 0, NULL);
267 }
268
269 /* Unmap feedback buffers if enabled */
270 if (i915->feedback.enabled) {
271 const uint n = i915->feedback.interleaved ? 1 : i915->feedback.num_attribs;
272 for (i = 0; i < n; i++) {
273 pipe->winsys->buffer_unmap(pipe->winsys,
274 i915->feedback_buffer[i].buffer);
275 draw_set_mapped_feedback_buffer(draw, i, NULL, 0);
276 }
277 }
278
279 return TRUE;
280 }
281
282
283 static boolean i915_draw_arrays( struct pipe_context *pipe,
284 unsigned prim, unsigned start, unsigned count)
285 {
286 return i915_draw_elements(pipe, NULL, 0, prim, start, count);
287 }
288
289
290
291 struct pipe_context *i915_create( struct pipe_winsys *pipe_winsys,
292 struct i915_winsys *i915_winsys,
293 unsigned pci_id )
294 {
295 struct i915_context *i915;
296 unsigned is_i945 = 0;
297
298 switch (pci_id) {
299 case PCI_CHIP_I915_G:
300 case PCI_CHIP_I915_GM:
301 break;
302
303 case PCI_CHIP_I945_G:
304 case PCI_CHIP_I945_GM:
305 case PCI_CHIP_I945_GME:
306 case PCI_CHIP_G33_G:
307 case PCI_CHIP_Q33_G:
308 case PCI_CHIP_Q35_G:
309 is_i945 = 1;
310 break;
311
312 default:
313 pipe_winsys->printf(pipe_winsys,
314 "%s: unknown pci id 0x%x, cannot create context\n",
315 __FUNCTION__, pci_id);
316 return NULL;
317 }
318
319 i915 = CALLOC_STRUCT(i915_context);
320 if (i915 == NULL)
321 return NULL;
322
323 i915->winsys = i915_winsys;
324 i915->pipe.winsys = pipe_winsys;
325
326 i915->pipe.destroy = i915_destroy;
327 i915->pipe.is_format_supported = i915_is_format_supported;
328 i915->pipe.get_param = i915_get_param;
329 i915->pipe.get_paramf = i915_get_paramf;
330
331 i915->pipe.clear = i915_clear;
332
333 i915->pipe.begin_query = i915_begin_query;
334 i915->pipe.end_query = i915_end_query;
335
336 i915->pipe.draw_arrays = i915_draw_arrays;
337 i915->pipe.draw_elements = i915_draw_elements;
338
339 /*
340 * Create drawing context and plug our rendering stage into it.
341 */
342 i915->draw = draw_create();
343 assert(i915->draw);
344 if (GETENV("I915_VBUF")) {
345 draw_set_rasterize_stage(i915->draw, i915_draw_vbuf_stage(i915));
346 }
347 else {
348 draw_set_rasterize_stage(i915->draw, i915_draw_render_stage(i915));
349 }
350
351 i915_init_region_functions(i915);
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