r600g: We don't support PIPE_CAP_PRIMITIVE_RESTART.
[mesa.git] / src / gallium / drivers / r600 / r600_pipe.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #include <stdio.h>
24 #include <errno.h>
25 #include <pipe/p_defines.h>
26 #include <pipe/p_state.h>
27 #include <pipe/p_context.h>
28 #include <tgsi/tgsi_scan.h>
29 #include <tgsi/tgsi_parse.h>
30 #include <tgsi/tgsi_util.h>
31 #include <util/u_blitter.h>
32 #include <util/u_double_list.h>
33 #include <util/u_transfer.h>
34 #include <util/u_surface.h>
35 #include <util/u_pack_color.h>
36 #include <util/u_memory.h>
37 #include <util/u_inlines.h>
38 #include <util/u_upload_mgr.h>
39 #include <pipebuffer/pb_buffer.h>
40 #include "r600.h"
41 #include "r600d.h"
42 #include "r600_resource.h"
43 #include "r600_shader.h"
44 #include "r600_pipe.h"
45 #include "r600_state_inlines.h"
46
47 /*
48 * pipe_context
49 */
50 static void r600_flush(struct pipe_context *ctx, unsigned flags,
51 struct pipe_fence_handle **fence)
52 {
53 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
54 #if 0
55 static int dc = 0;
56 char dname[256];
57 #endif
58
59 if (!rctx->ctx.pm4_cdwords)
60 return;
61
62 u_upload_flush(rctx->upload_vb);
63 u_upload_flush(rctx->upload_ib);
64
65 #if 0
66 sprintf(dname, "gallium-%08d.bof", dc);
67 if (dc < 20) {
68 r600_context_dump_bof(&rctx->ctx, dname);
69 R600_ERR("dumped %s\n", dname);
70 }
71 dc++;
72 #endif
73 r600_context_flush(&rctx->ctx);
74 }
75
76 static void r600_destroy_context(struct pipe_context *context)
77 {
78 struct r600_pipe_context *rctx = (struct r600_pipe_context *)context;
79
80 r600_context_fini(&rctx->ctx);
81 for (int i = 0; i < R600_PIPE_NSTATES; i++) {
82 free(rctx->states[i]);
83 }
84
85 util_blitter_destroy(rctx->blitter);
86
87 u_upload_destroy(rctx->upload_vb);
88 u_upload_destroy(rctx->upload_ib);
89
90 if (rctx->tran.translate_cache)
91 translate_cache_destroy(rctx->tran.translate_cache);
92
93 FREE(rctx->ps_resource);
94 FREE(rctx->vs_resource);
95 FREE(rctx);
96 }
97
98 static struct pipe_context *r600_create_context(struct pipe_screen *screen, void *priv)
99 {
100 struct r600_pipe_context *rctx = CALLOC_STRUCT(r600_pipe_context);
101 struct r600_screen* rscreen = (struct r600_screen *)screen;
102 enum chip_class class;
103
104 if (rctx == NULL)
105 return NULL;
106 rctx->context.winsys = rscreen->screen.winsys;
107 rctx->context.screen = screen;
108 rctx->context.priv = priv;
109 rctx->context.destroy = r600_destroy_context;
110 rctx->context.flush = r600_flush;
111
112 /* Easy accessing of screen/winsys. */
113 rctx->screen = rscreen;
114 rctx->radeon = rscreen->radeon;
115 rctx->family = r600_get_family(rctx->radeon);
116
117 r600_init_blit_functions(rctx);
118 r600_init_query_functions(rctx);
119 r600_init_context_resource_functions(rctx);
120
121 switch (r600_get_family(rctx->radeon)) {
122 case CHIP_R600:
123 case CHIP_RV610:
124 case CHIP_RV630:
125 case CHIP_RV670:
126 case CHIP_RV620:
127 case CHIP_RV635:
128 case CHIP_RS780:
129 case CHIP_RS880:
130 case CHIP_RV770:
131 case CHIP_RV730:
132 case CHIP_RV710:
133 case CHIP_RV740:
134 rctx->context.draw_vbo = r600_draw_vbo;
135 r600_init_state_functions(rctx);
136 if (r600_context_init(&rctx->ctx, rctx->radeon)) {
137 r600_destroy_context(&rctx->context);
138 return NULL;
139 }
140 r600_init_config(rctx);
141 break;
142 case CHIP_CEDAR:
143 case CHIP_REDWOOD:
144 case CHIP_JUNIPER:
145 case CHIP_CYPRESS:
146 case CHIP_HEMLOCK:
147 rctx->context.draw_vbo = evergreen_draw;
148 evergreen_init_state_functions(rctx);
149 if (evergreen_context_init(&rctx->ctx, rctx->radeon)) {
150 r600_destroy_context(&rctx->context);
151 return NULL;
152 }
153 evergreen_init_config(rctx);
154 break;
155 default:
156 R600_ERR("unsupported family %d\n", r600_get_family(rctx->radeon));
157 r600_destroy_context(&rctx->context);
158 return NULL;
159 }
160
161 rctx->upload_ib = u_upload_create(&rctx->context, 32 * 1024, 16,
162 PIPE_BIND_INDEX_BUFFER);
163 if (rctx->upload_ib == NULL) {
164 r600_destroy_context(&rctx->context);
165 return NULL;
166 }
167
168 rctx->upload_vb = u_upload_create(&rctx->context, 128 * 1024, 16,
169 PIPE_BIND_VERTEX_BUFFER);
170 if (rctx->upload_vb == NULL) {
171 r600_destroy_context(&rctx->context);
172 return NULL;
173 }
174
175 rctx->blitter = util_blitter_create(&rctx->context);
176 if (rctx->blitter == NULL) {
177 FREE(rctx);
178 return NULL;
179 }
180
181 rctx->tran.translate_cache = translate_cache_create();
182 if (rctx->tran.translate_cache == NULL) {
183 FREE(rctx);
184 return NULL;
185 }
186
187 rctx->vs_resource = CALLOC(R600_RESOURCE_ARRAY_SIZE, sizeof(struct r600_pipe_state));
188 if (!rctx->vs_resource) {
189 FREE(rctx);
190 return NULL;
191 }
192
193 rctx->ps_resource = CALLOC(R600_RESOURCE_ARRAY_SIZE, sizeof(struct r600_pipe_state));
194 if (!rctx->ps_resource) {
195 FREE(rctx);
196 return NULL;
197 }
198
199 class = r600_get_family_class(rctx->radeon);
200 if (class == R600 || class == R700)
201 rctx->custom_dsa_flush = r600_create_db_flush_dsa(rctx);
202 else
203 rctx->custom_dsa_flush = evergreen_create_db_flush_dsa(rctx);
204
205 r600_blit_uncompress_depth_ptr = r600_blit_uncompress_depth;
206
207 return &rctx->context;
208 }
209
210 /*
211 * pipe_screen
212 */
213 static const char* r600_get_vendor(struct pipe_screen* pscreen)
214 {
215 return "X.Org";
216 }
217
218 static const char *r600_get_family_name(enum radeon_family family)
219 {
220 switch(family) {
221 case CHIP_R600: return "R600";
222 case CHIP_RV610: return "RV610";
223 case CHIP_RV630: return "RV630";
224 case CHIP_RV670: return "RV670";
225 case CHIP_RV620: return "RV620";
226 case CHIP_RV635: return "RV635";
227 case CHIP_RS780: return "RS780";
228 case CHIP_RS880: return "RS880";
229 case CHIP_RV770: return "RV770";
230 case CHIP_RV730: return "RV730";
231 case CHIP_RV710: return "RV710";
232 case CHIP_RV740: return "RV740";
233 case CHIP_CEDAR: return "CEDAR";
234 case CHIP_REDWOOD: return "REDWOOD";
235 case CHIP_JUNIPER: return "JUNIPER";
236 case CHIP_CYPRESS: return "CYPRESS";
237 case CHIP_HEMLOCK: return "HEMLOCK";
238 default: return "unknown";
239 }
240 }
241
242 static const char* r600_get_name(struct pipe_screen* pscreen)
243 {
244 struct r600_screen *rscreen = (struct r600_screen *)pscreen;
245 enum radeon_family family = r600_get_family(rscreen->radeon);
246
247 return r600_get_family_name(family);
248 }
249
250 static int r600_get_param(struct pipe_screen* pscreen, enum pipe_cap param)
251 {
252 switch (param) {
253 /* Supported features (boolean caps). */
254 case PIPE_CAP_NPOT_TEXTURES:
255 case PIPE_CAP_TWO_SIDED_STENCIL:
256 case PIPE_CAP_GLSL:
257 case PIPE_CAP_DUAL_SOURCE_BLEND:
258 case PIPE_CAP_ANISOTROPIC_FILTER:
259 case PIPE_CAP_POINT_SPRITE:
260 case PIPE_CAP_OCCLUSION_QUERY:
261 case PIPE_CAP_TEXTURE_SHADOW_MAP:
262 case PIPE_CAP_TEXTURE_MIRROR_CLAMP:
263 case PIPE_CAP_TEXTURE_MIRROR_REPEAT:
264 case PIPE_CAP_BLEND_EQUATION_SEPARATE:
265 case PIPE_CAP_SM3:
266 case PIPE_CAP_TEXTURE_SWIZZLE:
267 case PIPE_CAP_INDEP_BLEND_ENABLE:
268 case PIPE_CAP_DEPTHSTENCIL_CLEAR_SEPARATE:
269 case PIPE_CAP_DEPTH_CLAMP:
270 case PIPE_CAP_SHADER_STENCIL_EXPORT:
271 return 1;
272
273 /* Unsupported features (boolean caps). */
274 case PIPE_CAP_TIMER_QUERY:
275 case PIPE_CAP_STREAM_OUTPUT:
276 case PIPE_CAP_PRIMITIVE_RESTART:
277 case PIPE_CAP_INDEP_BLEND_FUNC: /* FIXME allow this */
278 return 0;
279
280 /* Texturing. */
281 case PIPE_CAP_MAX_TEXTURE_2D_LEVELS:
282 case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
283 case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
284 return 14;
285 case PIPE_CAP_MAX_VERTEX_TEXTURE_UNITS:
286 /* FIXME allow this once infrastructure is there */
287 return 16;
288 case PIPE_CAP_MAX_TEXTURE_IMAGE_UNITS:
289 case PIPE_CAP_MAX_COMBINED_SAMPLERS:
290 return 16;
291
292 /* Render targets. */
293 case PIPE_CAP_MAX_RENDER_TARGETS:
294 /* FIXME some r6xx are buggy and can only do 4 */
295 return 8;
296
297 /* Fragment coordinate conventions. */
298 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
299 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
300 return 1;
301 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT:
302 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
303 return 0;
304
305 default:
306 R600_ERR("r600: unknown param %d\n", param);
307 return 0;
308 }
309 }
310
311 static float r600_get_paramf(struct pipe_screen* pscreen, enum pipe_cap param)
312 {
313 switch (param) {
314 case PIPE_CAP_MAX_LINE_WIDTH:
315 case PIPE_CAP_MAX_LINE_WIDTH_AA:
316 case PIPE_CAP_MAX_POINT_WIDTH:
317 case PIPE_CAP_MAX_POINT_WIDTH_AA:
318 return 8192.0f;
319 case PIPE_CAP_MAX_TEXTURE_ANISOTROPY:
320 return 16.0f;
321 case PIPE_CAP_MAX_TEXTURE_LOD_BIAS:
322 return 16.0f;
323 default:
324 R600_ERR("r600: unsupported paramf %d\n", param);
325 return 0.0f;
326 }
327 }
328
329 static int r600_get_shader_param(struct pipe_screen* pscreen, unsigned shader, enum pipe_shader_cap param)
330 {
331 switch(shader)
332 {
333 case PIPE_SHADER_FRAGMENT:
334 case PIPE_SHADER_VERTEX:
335 break;
336 case PIPE_SHADER_GEOMETRY:
337 /* TODO: support and enable geometry programs */
338 return 0;
339 default:
340 /* TODO: support tessellation on Evergreen */
341 return 0;
342 }
343
344 /* TODO: all these should be fixed, since r600 surely supports much more! */
345 switch (param) {
346 case PIPE_SHADER_CAP_MAX_INSTRUCTIONS:
347 case PIPE_SHADER_CAP_MAX_ALU_INSTRUCTIONS:
348 case PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS:
349 case PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS:
350 return 16384;
351 case PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH:
352 return 8; /* FIXME */
353 case PIPE_SHADER_CAP_MAX_INPUTS:
354 if(shader == PIPE_SHADER_FRAGMENT)
355 return 10;
356 else
357 return 16;
358 case PIPE_SHADER_CAP_MAX_TEMPS:
359 return 256; //max native temporaries
360 case PIPE_SHADER_CAP_MAX_ADDRS:
361 return 1; //max native address registers/* FIXME Isn't this equal to TEMPS? */
362 case PIPE_SHADER_CAP_MAX_CONSTS:
363 return 256; //max native parameters
364 case PIPE_SHADER_CAP_MAX_CONST_BUFFERS:
365 return 1;
366 case PIPE_SHADER_CAP_MAX_PREDS:
367 return 0; /* FIXME */
368 case PIPE_SHADER_CAP_TGSI_CONT_SUPPORTED:
369 return 1;
370 default:
371 return 0;
372 }
373 }
374
375 static boolean r600_is_format_supported(struct pipe_screen* screen,
376 enum pipe_format format,
377 enum pipe_texture_target target,
378 unsigned sample_count,
379 unsigned usage,
380 unsigned geom_flags)
381 {
382 unsigned retval = 0;
383 if (target >= PIPE_MAX_TEXTURE_TYPES) {
384 R600_ERR("r600: unsupported texture type %d\n", target);
385 return FALSE;
386 }
387
388 /* Multisample */
389 if (sample_count > 1)
390 return FALSE;
391
392 if ((usage & PIPE_BIND_SAMPLER_VIEW) &&
393 r600_is_sampler_format_supported(format)) {
394 retval |= PIPE_BIND_SAMPLER_VIEW;
395 }
396
397 if ((usage & (PIPE_BIND_RENDER_TARGET |
398 PIPE_BIND_DISPLAY_TARGET |
399 PIPE_BIND_SCANOUT |
400 PIPE_BIND_SHARED)) &&
401 r600_is_colorbuffer_format_supported(format)) {
402 retval |= usage &
403 (PIPE_BIND_RENDER_TARGET |
404 PIPE_BIND_DISPLAY_TARGET |
405 PIPE_BIND_SCANOUT |
406 PIPE_BIND_SHARED);
407 }
408
409 if ((usage & PIPE_BIND_DEPTH_STENCIL) &&
410 r600_is_zs_format_supported(format)) {
411 retval |= PIPE_BIND_DEPTH_STENCIL;
412 }
413
414 if ((usage & PIPE_BIND_VERTEX_BUFFER) &&
415 r600_is_vertex_format_supported(format))
416 retval |= PIPE_BIND_VERTEX_BUFFER;
417
418 if (usage & PIPE_BIND_TRANSFER_READ)
419 retval |= PIPE_BIND_TRANSFER_READ;
420 if (usage & PIPE_BIND_TRANSFER_WRITE)
421 retval |= PIPE_BIND_TRANSFER_WRITE;
422
423 return retval == usage;
424 }
425
426 static void r600_destroy_screen(struct pipe_screen* pscreen)
427 {
428 struct r600_screen *rscreen = (struct r600_screen *)pscreen;
429
430 if (rscreen == NULL)
431 return;
432 FREE(rscreen);
433 }
434
435
436 struct pipe_screen *r600_screen_create(struct radeon *radeon)
437 {
438 struct r600_screen *rscreen;
439
440 rscreen = CALLOC_STRUCT(r600_screen);
441 if (rscreen == NULL) {
442 return NULL;
443 }
444
445 rscreen->radeon = radeon;
446 rscreen->screen.winsys = (struct pipe_winsys*)radeon;
447 rscreen->screen.destroy = r600_destroy_screen;
448 rscreen->screen.get_name = r600_get_name;
449 rscreen->screen.get_vendor = r600_get_vendor;
450 rscreen->screen.get_param = r600_get_param;
451 rscreen->screen.get_shader_param = r600_get_shader_param;
452 rscreen->screen.get_paramf = r600_get_paramf;
453 rscreen->screen.is_format_supported = r600_is_format_supported;
454 rscreen->screen.context_create = r600_create_context;
455 r600_init_screen_texture_functions(&rscreen->screen);
456 r600_init_screen_resource_functions(&rscreen->screen);
457
458 rscreen->tiling_info = r600_get_tiling_info(radeon);
459
460 return &rscreen->screen;
461 }