r600g: Destroy the blitter.
[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_INDEP_BLEND_FUNC: /* FIXME allow this */
277 return 0;
278
279 /* Texturing. */
280 case PIPE_CAP_MAX_TEXTURE_2D_LEVELS:
281 case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
282 case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
283 return 14;
284 case PIPE_CAP_MAX_VERTEX_TEXTURE_UNITS:
285 /* FIXME allow this once infrastructure is there */
286 return 16;
287 case PIPE_CAP_MAX_TEXTURE_IMAGE_UNITS:
288 case PIPE_CAP_MAX_COMBINED_SAMPLERS:
289 return 16;
290
291 /* Render targets. */
292 case PIPE_CAP_MAX_RENDER_TARGETS:
293 /* FIXME some r6xx are buggy and can only do 4 */
294 return 8;
295
296 /* Fragment coordinate conventions. */
297 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
298 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
299 return 1;
300 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT:
301 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
302 return 0;
303
304 default:
305 R600_ERR("r600: unknown param %d\n", param);
306 return 0;
307 }
308 }
309
310 static float r600_get_paramf(struct pipe_screen* pscreen, enum pipe_cap param)
311 {
312 switch (param) {
313 case PIPE_CAP_MAX_LINE_WIDTH:
314 case PIPE_CAP_MAX_LINE_WIDTH_AA:
315 case PIPE_CAP_MAX_POINT_WIDTH:
316 case PIPE_CAP_MAX_POINT_WIDTH_AA:
317 return 8192.0f;
318 case PIPE_CAP_MAX_TEXTURE_ANISOTROPY:
319 return 16.0f;
320 case PIPE_CAP_MAX_TEXTURE_LOD_BIAS:
321 return 16.0f;
322 default:
323 R600_ERR("r600: unsupported paramf %d\n", param);
324 return 0.0f;
325 }
326 }
327
328 static int r600_get_shader_param(struct pipe_screen* pscreen, unsigned shader, enum pipe_shader_cap param)
329 {
330 switch(shader)
331 {
332 case PIPE_SHADER_FRAGMENT:
333 case PIPE_SHADER_VERTEX:
334 break;
335 case PIPE_SHADER_GEOMETRY:
336 /* TODO: support and enable geometry programs */
337 return 0;
338 default:
339 /* TODO: support tessellation on Evergreen */
340 return 0;
341 }
342
343 /* TODO: all these should be fixed, since r600 surely supports much more! */
344 switch (param) {
345 case PIPE_SHADER_CAP_MAX_INSTRUCTIONS:
346 case PIPE_SHADER_CAP_MAX_ALU_INSTRUCTIONS:
347 case PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS:
348 case PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS:
349 return 16384;
350 case PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH:
351 return 8; /* FIXME */
352 case PIPE_SHADER_CAP_MAX_INPUTS:
353 if(shader == PIPE_SHADER_FRAGMENT)
354 return 10;
355 else
356 return 16;
357 case PIPE_SHADER_CAP_MAX_TEMPS:
358 return 256; //max native temporaries
359 case PIPE_SHADER_CAP_MAX_ADDRS:
360 return 1; //max native address registers/* FIXME Isn't this equal to TEMPS? */
361 case PIPE_SHADER_CAP_MAX_CONSTS:
362 return 256; //max native parameters
363 case PIPE_SHADER_CAP_MAX_CONST_BUFFERS:
364 return 1;
365 case PIPE_SHADER_CAP_MAX_PREDS:
366 return 0; /* FIXME */
367 case PIPE_SHADER_CAP_TGSI_CONT_SUPPORTED:
368 return 1;
369 default:
370 return 0;
371 }
372 }
373
374 static boolean r600_is_format_supported(struct pipe_screen* screen,
375 enum pipe_format format,
376 enum pipe_texture_target target,
377 unsigned sample_count,
378 unsigned usage,
379 unsigned geom_flags)
380 {
381 unsigned retval = 0;
382 if (target >= PIPE_MAX_TEXTURE_TYPES) {
383 R600_ERR("r600: unsupported texture type %d\n", target);
384 return FALSE;
385 }
386
387 /* Multisample */
388 if (sample_count > 1)
389 return FALSE;
390
391 if ((usage & PIPE_BIND_SAMPLER_VIEW) &&
392 r600_is_sampler_format_supported(format)) {
393 retval |= PIPE_BIND_SAMPLER_VIEW;
394 }
395
396 if ((usage & (PIPE_BIND_RENDER_TARGET |
397 PIPE_BIND_DISPLAY_TARGET |
398 PIPE_BIND_SCANOUT |
399 PIPE_BIND_SHARED)) &&
400 r600_is_colorbuffer_format_supported(format)) {
401 retval |= usage &
402 (PIPE_BIND_RENDER_TARGET |
403 PIPE_BIND_DISPLAY_TARGET |
404 PIPE_BIND_SCANOUT |
405 PIPE_BIND_SHARED);
406 }
407
408 if ((usage & PIPE_BIND_DEPTH_STENCIL) &&
409 r600_is_zs_format_supported(format)) {
410 retval |= PIPE_BIND_DEPTH_STENCIL;
411 }
412
413 if ((usage & PIPE_BIND_VERTEX_BUFFER) &&
414 r600_is_vertex_format_supported(format))
415 retval |= PIPE_BIND_VERTEX_BUFFER;
416
417 if (usage & PIPE_BIND_TRANSFER_READ)
418 retval |= PIPE_BIND_TRANSFER_READ;
419 if (usage & PIPE_BIND_TRANSFER_WRITE)
420 retval |= PIPE_BIND_TRANSFER_WRITE;
421
422 return retval == usage;
423 }
424
425 static void r600_destroy_screen(struct pipe_screen* pscreen)
426 {
427 struct r600_screen *rscreen = (struct r600_screen *)pscreen;
428
429 if (rscreen == NULL)
430 return;
431 FREE(rscreen);
432 }
433
434
435 struct pipe_screen *r600_screen_create(struct radeon *radeon)
436 {
437 struct r600_screen *rscreen;
438
439 rscreen = CALLOC_STRUCT(r600_screen);
440 if (rscreen == NULL) {
441 return NULL;
442 }
443
444 rscreen->radeon = radeon;
445 rscreen->screen.winsys = (struct pipe_winsys*)radeon;
446 rscreen->screen.destroy = r600_destroy_screen;
447 rscreen->screen.get_name = r600_get_name;
448 rscreen->screen.get_vendor = r600_get_vendor;
449 rscreen->screen.get_param = r600_get_param;
450 rscreen->screen.get_shader_param = r600_get_shader_param;
451 rscreen->screen.get_paramf = r600_get_paramf;
452 rscreen->screen.is_format_supported = r600_is_format_supported;
453 rscreen->screen.context_create = r600_create_context;
454 r600_init_screen_texture_functions(&rscreen->screen);
455 r600_init_screen_resource_functions(&rscreen->screen);
456
457 rscreen->tiling_info = r600_get_tiling_info(radeon);
458
459 return &rscreen->screen;
460 }