5d97eed247e1763a947aea6e8eb79b3b9487df83
[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 "main/imports.h" /* CALLOC */
29 #include "i915_context.h"
30 #include "i915_winsys.h"
31 #include "i915_state.h"
32 #include "i915_batch.h"
33 #include "i915_tex_layout.h"
34 #include "i915_reg.h"
35
36 #include "pipe/draw/draw_context.h"
37 #include "pipe/p_defines.h"
38 #include "pipe/p_winsys.h"
39 #include "pipe/p_util.h"
40
41
42 /**
43 * Return list of supported surface/texture formats.
44 * If we find texture and drawable support differs, add a selector
45 * parameter or another function.
46 */
47 static const unsigned *
48 i915_supported_formats(struct pipe_context *pipe,
49 // unsigned type,
50 unsigned *numFormats)
51 {
52 #if 0
53 static const unsigned tex_supported[] = {
54 PIPE_FORMAT_U_R8_G8_B8_A8,
55 PIPE_FORMAT_U_A8_R8_G8_B8,
56 PIPE_FORMAT_U_R5_G6_B5,
57 PIPE_FORMAT_U_L8,
58 PIPE_FORMAT_U_A8,
59 PIPE_FORMAT_U_I8,
60 PIPE_FORMAT_U_L8_A8,
61 PIPE_FORMAT_YCBCR,
62 PIPE_FORMAT_YCBCR_REV,
63 PIPE_FORMAT_S8_Z24,
64 };
65
66
67 /* Actually a lot more than this - add later:
68 */
69 static const unsigned render_supported[] = {
70 PIPE_FORMAT_U_A8_R8_G8_B8,
71 PIPE_FORMAT_U_R5_G6_B5,
72 };
73
74 /*
75 */
76 static const unsigned z_stencil_supported[] = {
77 PIPE_FORMAT_U_Z16,
78 PIPE_FORMAT_U_Z32,
79 PIPE_FORMAT_S8_Z24,
80 };
81
82 switch (type) {
83 case PIPE_RENDER_FORMAT:
84 *numFormats = Elements(render_supported);
85 return render_supported;
86
87 case PIPE_TEX_FORMAT:
88 *numFormats = Elements(tex_supported);
89 return render_supported;
90
91 case PIPE_Z_STENCIL_FORMAT:
92 *numFormats = Elements(render_supported);
93 return render_supported;
94
95 default:
96 *numFormats = 0;
97 return NULL;
98 }
99 #else
100 static const unsigned render_supported[] = {
101 PIPE_FORMAT_U_A8_R8_G8_B8,
102 PIPE_FORMAT_U_R5_G6_B5,
103 PIPE_FORMAT_S8_Z24,
104 };
105 *numFormats = 3;
106 return render_supported;
107 #endif
108 }
109
110
111 /**
112 * We might want to return max texture levels instead...
113 */
114 static void
115 i915_max_texture_size(struct pipe_context *pipe, unsigned textureType,
116 unsigned *maxWidth, unsigned *maxHeight, unsigned *maxDepth)
117 {
118 switch (textureType) {
119 case PIPE_TEXTURE_1D:
120 *maxWidth = 2048;
121 break;
122 case PIPE_TEXTURE_2D:
123 *maxWidth =
124 *maxHeight = 2048;
125 break;
126 case PIPE_TEXTURE_3D:
127 *maxWidth =
128 *maxHeight =
129 *maxDepth = 256;
130 break;
131 case PIPE_TEXTURE_CUBE:
132 *maxWidth =
133 *maxHeight = 2048;
134 break;
135 default:
136 assert(0);
137 }
138 }
139
140
141 static int
142 i915_get_param(struct pipe_context *pipe, int param)
143 {
144 switch (param) {
145 default:
146 return 0;
147 }
148 }
149
150
151 static void i915_destroy( struct pipe_context *pipe )
152 {
153 struct i915_context *i915 = i915_context( pipe );
154
155 draw_destroy( i915->draw );
156
157 free( i915 );
158 }
159
160
161
162 static void
163 i915_begin_query(struct pipe_context *pipe, struct pipe_query_object *q)
164 {
165 /* should never be called */
166 assert(0);
167 }
168
169
170 static void
171 i915_end_query(struct pipe_context *pipe, struct pipe_query_object *q)
172 {
173 /* should never be called */
174 assert(0);
175 }
176
177
178 static boolean i915_draw_elements( struct pipe_context *pipe,
179 struct pipe_buffer_handle *indexBuffer,
180 unsigned indexSize,
181 unsigned prim, unsigned start, unsigned count)
182 {
183 struct i915_context *i915 = i915_context( pipe );
184 struct draw_context *draw = i915->draw;
185 unsigned i;
186
187 if (i915->dirty)
188 i915_update_derived( i915 );
189
190
191 /*
192 * Map vertex buffers
193 */
194 for (i = 0; i < PIPE_ATTRIB_MAX; i++) {
195 if (i915->vertex_buffer[i].buffer) {
196 void *buf
197 = pipe->winsys->buffer_map(pipe->winsys,
198 i915->vertex_buffer[i].buffer,
199 PIPE_BUFFER_FLAG_READ);
200 draw_set_mapped_vertex_buffer(draw, i, buf);
201 }
202 }
203 /* Map index buffer, if present */
204 if (indexBuffer) {
205 void *mapped_indexes
206 = pipe->winsys->buffer_map(pipe->winsys, indexBuffer,
207 PIPE_BUFFER_FLAG_READ);
208 draw_set_mapped_element_buffer(draw, indexSize, mapped_indexes);
209 }
210 else {
211 /* no index/element buffer */
212 draw_set_mapped_element_buffer(draw, 0, NULL);
213 }
214
215 draw_set_mapped_constant_buffer(draw,
216 i915->current.constants[PIPE_SHADER_VERTEX]);
217
218 /* draw! */
219 draw_arrays(i915->draw, prim, start, count);
220
221 /*
222 * unmap vertex/index buffers
223 */
224 for (i = 0; i < PIPE_ATTRIB_MAX; i++) {
225 if (i915->vertex_buffer[i].buffer) {
226 pipe->winsys->buffer_unmap(pipe->winsys, i915->vertex_buffer[i].buffer);
227 draw_set_mapped_vertex_buffer(draw, i, NULL);
228 }
229 }
230 if (indexBuffer) {
231 pipe->winsys->buffer_unmap(pipe->winsys, indexBuffer);
232 draw_set_mapped_element_buffer(draw, 0, NULL);
233 }
234
235 return TRUE;
236 }
237
238
239 static boolean i915_draw_arrays( struct pipe_context *pipe,
240 unsigned prim, unsigned start, unsigned count)
241 {
242 return i915_draw_elements(pipe, NULL, 0, prim, start, count);
243 }
244
245
246
247 struct pipe_context *i915_create( struct pipe_winsys *pipe_winsys,
248 struct i915_winsys *i915_winsys,
249 unsigned pci_id )
250 {
251 struct i915_context *i915;
252 unsigned is_i945 = 0;
253
254 switch (pci_id) {
255 case PCI_CHIP_I915_G:
256 case PCI_CHIP_I915_GM:
257 break;
258
259 case PCI_CHIP_I945_G:
260 case PCI_CHIP_I945_GM:
261 case PCI_CHIP_I945_GME:
262 case PCI_CHIP_G33_G:
263 case PCI_CHIP_Q33_G:
264 case PCI_CHIP_Q35_G:
265 is_i945 = 1;
266 break;
267
268 default:
269 pipe_winsys->printf(pipe_winsys,
270 "%s: unknown pci id 0x%x, cannot create context\n",
271 __FUNCTION__, pci_id);
272 return NULL;
273 }
274
275 i915 = CALLOC_STRUCT(i915_context);
276 if (i915 == NULL)
277 return NULL;
278
279 i915->winsys = i915_winsys;
280 i915->pipe.winsys = pipe_winsys;
281
282 i915->pipe.destroy = i915_destroy;
283 i915->pipe.supported_formats = i915_supported_formats;
284 i915->pipe.max_texture_size = i915_max_texture_size;
285 i915->pipe.get_param = i915_get_param;
286
287 i915->pipe.clear = i915_clear;
288
289 i915->pipe.begin_query = i915_begin_query;
290 i915->pipe.end_query = i915_end_query;
291
292 i915->pipe.draw_arrays = i915_draw_arrays;
293 i915->pipe.draw_elements = i915_draw_elements;
294
295 /*
296 * Create drawing context and plug our rendering stage into it.
297 */
298 i915->draw = draw_create();
299 assert(i915->draw);
300 draw_set_rasterize_stage(i915->draw, i915_draw_render_stage(i915));
301
302 i915_init_region_functions(i915);
303 i915_init_surface_functions(i915);
304 i915_init_state_functions(i915);
305 i915_init_flush_functions(i915);
306 i915_init_string_functions(i915);
307
308 i915->pci_id = pci_id;
309 i915->flags.is_i945 = is_i945;
310
311 if (i915->flags.is_i945)
312 i915->pipe.mipmap_tree_layout = i945_miptree_layout;
313 else
314 i915->pipe.mipmap_tree_layout = i915_miptree_layout;
315
316
317 i915->dirty = ~0;
318 i915->hardware_dirty = ~0;
319
320 /* Batch stream debugging is a bit hacked up at the moment:
321 */
322 i915->batch_start = BEGIN_BATCH(0, 0);
323
324 /*
325 * XXX we could plug GL selection/feedback into the drawing pipeline
326 * by specifying a different setup/render stage.
327 */
328
329 return &i915->pipe;
330 }
331