r600g: use the same upload buffer for vertices, indices, and constants
[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 #if 0
63 sprintf(dname, "gallium-%08d.bof", dc);
64 if (dc < 20) {
65 r600_context_dump_bof(&rctx->ctx, dname);
66 R600_ERR("dumped %s\n", dname);
67 }
68 dc++;
69 #endif
70 r600_context_flush(&rctx->ctx);
71
72 /* XXX This shouldn't be really necessary, but removing it breaks some tests.
73 * Needless buffer reallocations may significantly increase memory consumption,
74 * so getting rid of this call is important. */
75 u_upload_flush(rctx->vbuf_mgr->uploader);
76 }
77
78 static void r600_destroy_context(struct pipe_context *context)
79 {
80 struct r600_pipe_context *rctx = (struct r600_pipe_context *)context;
81
82 rctx->context.delete_depth_stencil_alpha_state(&rctx->context, rctx->custom_dsa_flush);
83
84 r600_context_fini(&rctx->ctx);
85
86 util_blitter_destroy(rctx->blitter);
87
88 for (int i = 0; i < R600_PIPE_NSTATES; i++) {
89 free(rctx->states[i]);
90 }
91
92 u_vbuf_mgr_destroy(rctx->vbuf_mgr);
93
94 FREE(rctx);
95 }
96
97 static struct pipe_context *r600_create_context(struct pipe_screen *screen, void *priv)
98 {
99 struct r600_pipe_context *rctx = CALLOC_STRUCT(r600_pipe_context);
100 struct r600_screen* rscreen = (struct r600_screen *)screen;
101 enum chip_class class;
102
103 if (rctx == NULL)
104 return NULL;
105 rctx->context.winsys = rscreen->screen.winsys;
106 rctx->context.screen = screen;
107 rctx->context.priv = priv;
108 rctx->context.destroy = r600_destroy_context;
109 rctx->context.flush = r600_flush;
110
111 /* Easy accessing of screen/winsys. */
112 rctx->screen = rscreen;
113 rctx->radeon = rscreen->radeon;
114 rctx->family = r600_get_family(rctx->radeon);
115
116 r600_init_blit_functions(rctx);
117 r600_init_query_functions(rctx);
118 r600_init_context_resource_functions(rctx);
119 r600_init_surface_functions(rctx);
120 rctx->context.draw_vbo = r600_draw_vbo;
121
122 switch (r600_get_family(rctx->radeon)) {
123 case CHIP_R600:
124 case CHIP_RV610:
125 case CHIP_RV630:
126 case CHIP_RV670:
127 case CHIP_RV620:
128 case CHIP_RV635:
129 case CHIP_RS780:
130 case CHIP_RS880:
131 case CHIP_RV770:
132 case CHIP_RV730:
133 case CHIP_RV710:
134 case CHIP_RV740:
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 case CHIP_PALM:
148 case CHIP_BARTS:
149 case CHIP_TURKS:
150 case CHIP_CAICOS:
151 evergreen_init_state_functions(rctx);
152 if (evergreen_context_init(&rctx->ctx, rctx->radeon)) {
153 r600_destroy_context(&rctx->context);
154 return NULL;
155 }
156 evergreen_init_config(rctx);
157 break;
158 default:
159 R600_ERR("unsupported family %d\n", r600_get_family(rctx->radeon));
160 r600_destroy_context(&rctx->context);
161 return NULL;
162 }
163
164 rctx->vbuf_mgr = u_vbuf_mgr_create(&rctx->context, 1024 * 1024, 256,
165 PIPE_BIND_VERTEX_BUFFER |
166 PIPE_BIND_INDEX_BUFFER |
167 PIPE_BIND_CONSTANT_BUFFER,
168 U_VERTEX_FETCH_BYTE_ALIGNED);
169 if (!rctx->vbuf_mgr) {
170 r600_destroy_context(&rctx->context);
171 return NULL;
172 }
173
174 rctx->blitter = util_blitter_create(&rctx->context);
175 if (rctx->blitter == NULL) {
176 FREE(rctx);
177 return NULL;
178 }
179
180 class = r600_get_family_class(rctx->radeon);
181 if (class == R600 || class == R700)
182 rctx->custom_dsa_flush = r600_create_db_flush_dsa(rctx);
183 else
184 rctx->custom_dsa_flush = evergreen_create_db_flush_dsa(rctx);
185
186 return &rctx->context;
187 }
188
189 /*
190 * pipe_screen
191 */
192 static const char* r600_get_vendor(struct pipe_screen* pscreen)
193 {
194 return "X.Org";
195 }
196
197 static const char *r600_get_family_name(enum radeon_family family)
198 {
199 switch(family) {
200 case CHIP_R600: return "AMD R600";
201 case CHIP_RV610: return "AMD RV610";
202 case CHIP_RV630: return "AMD RV630";
203 case CHIP_RV670: return "AMD RV670";
204 case CHIP_RV620: return "AMD RV620";
205 case CHIP_RV635: return "AMD RV635";
206 case CHIP_RS780: return "AMD RS780";
207 case CHIP_RS880: return "AMD RS880";
208 case CHIP_RV770: return "AMD RV770";
209 case CHIP_RV730: return "AMD RV730";
210 case CHIP_RV710: return "AMD RV710";
211 case CHIP_RV740: return "AMD RV740";
212 case CHIP_CEDAR: return "AMD CEDAR";
213 case CHIP_REDWOOD: return "AMD REDWOOD";
214 case CHIP_JUNIPER: return "AMD JUNIPER";
215 case CHIP_CYPRESS: return "AMD CYPRESS";
216 case CHIP_HEMLOCK: return "AMD HEMLOCK";
217 case CHIP_PALM: return "AMD PALM";
218 case CHIP_BARTS: return "AMD BARTS";
219 case CHIP_TURKS: return "AMD TURKS";
220 case CHIP_CAICOS: return "AMD CAICOS";
221 default: return "AMD unknown";
222 }
223 }
224
225 static const char* r600_get_name(struct pipe_screen* pscreen)
226 {
227 struct r600_screen *rscreen = (struct r600_screen *)pscreen;
228 enum radeon_family family = r600_get_family(rscreen->radeon);
229
230 return r600_get_family_name(family);
231 }
232
233 static int r600_get_param(struct pipe_screen* pscreen, enum pipe_cap param)
234 {
235 struct r600_screen *rscreen = (struct r600_screen *)pscreen;
236 enum radeon_family family = r600_get_family(rscreen->radeon);
237
238 switch (param) {
239 /* Supported features (boolean caps). */
240 case PIPE_CAP_NPOT_TEXTURES:
241 case PIPE_CAP_TWO_SIDED_STENCIL:
242 case PIPE_CAP_GLSL:
243 case PIPE_CAP_DUAL_SOURCE_BLEND:
244 case PIPE_CAP_ANISOTROPIC_FILTER:
245 case PIPE_CAP_POINT_SPRITE:
246 case PIPE_CAP_OCCLUSION_QUERY:
247 case PIPE_CAP_TEXTURE_SHADOW_MAP:
248 case PIPE_CAP_TEXTURE_MIRROR_CLAMP:
249 case PIPE_CAP_TEXTURE_MIRROR_REPEAT:
250 case PIPE_CAP_BLEND_EQUATION_SEPARATE:
251 case PIPE_CAP_SM3:
252 case PIPE_CAP_TEXTURE_SWIZZLE:
253 case PIPE_CAP_INDEP_BLEND_ENABLE:
254 case PIPE_CAP_DEPTHSTENCIL_CLEAR_SEPARATE:
255 case PIPE_CAP_DEPTH_CLAMP:
256 case PIPE_CAP_SHADER_STENCIL_EXPORT:
257 return 1;
258
259 /* Unsupported features (boolean caps). */
260 case PIPE_CAP_STREAM_OUTPUT:
261 case PIPE_CAP_PRIMITIVE_RESTART:
262 case PIPE_CAP_INDEP_BLEND_FUNC: /* FIXME allow this */
263 case PIPE_CAP_INSTANCED_DRAWING:
264 case PIPE_CAP_ARRAY_TEXTURES:
265 return 0;
266
267 /* Texturing. */
268 case PIPE_CAP_MAX_TEXTURE_2D_LEVELS:
269 case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
270 case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
271 if (family >= CHIP_CEDAR)
272 return 15;
273 else
274 return 14;
275 case PIPE_CAP_MAX_VERTEX_TEXTURE_UNITS:
276 /* FIXME allow this once infrastructure is there */
277 return 16;
278 case PIPE_CAP_MAX_TEXTURE_IMAGE_UNITS:
279 case PIPE_CAP_MAX_COMBINED_SAMPLERS:
280 return 16;
281
282 /* Render targets. */
283 case PIPE_CAP_MAX_RENDER_TARGETS:
284 /* FIXME some r6xx are buggy and can only do 4 */
285 return 8;
286
287 /* Fragment coordinate conventions. */
288 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
289 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
290 return 1;
291 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT:
292 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
293 return 0;
294
295 /* Timer queries, present when the clock frequency is non zero. */
296 case PIPE_CAP_TIMER_QUERY:
297 return r600_get_clock_crystal_freq(rscreen->radeon) != 0;
298
299 default:
300 R600_ERR("r600: unknown param %d\n", param);
301 return 0;
302 }
303 }
304
305 static float r600_get_paramf(struct pipe_screen* pscreen, enum pipe_cap param)
306 {
307 struct r600_screen *rscreen = (struct r600_screen *)pscreen;
308 enum radeon_family family = r600_get_family(rscreen->radeon);
309
310 switch (param) {
311 case PIPE_CAP_MAX_LINE_WIDTH:
312 case PIPE_CAP_MAX_LINE_WIDTH_AA:
313 case PIPE_CAP_MAX_POINT_WIDTH:
314 case PIPE_CAP_MAX_POINT_WIDTH_AA:
315 if (family >= CHIP_CEDAR)
316 return 16384.0f;
317 else
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 R600_MAX_CONST_BUFFERS;
366 case PIPE_SHADER_CAP_MAX_PREDS:
367 return 0; /* FIXME */
368 case PIPE_SHADER_CAP_TGSI_CONT_SUPPORTED:
369 return 1;
370 case PIPE_SHADER_CAP_INDIRECT_INPUT_ADDR:
371 case PIPE_SHADER_CAP_INDIRECT_OUTPUT_ADDR:
372 case PIPE_SHADER_CAP_INDIRECT_TEMP_ADDR:
373 case PIPE_SHADER_CAP_INDIRECT_CONST_ADDR:
374 return 1;
375 case PIPE_SHADER_CAP_SUBROUTINES:
376 return 0;
377 default:
378 return 0;
379 }
380 }
381
382 static boolean r600_is_format_supported(struct pipe_screen* screen,
383 enum pipe_format format,
384 enum pipe_texture_target target,
385 unsigned sample_count,
386 unsigned usage,
387 unsigned geom_flags)
388 {
389 unsigned retval = 0;
390 if (target >= PIPE_MAX_TEXTURE_TYPES) {
391 R600_ERR("r600: unsupported texture type %d\n", target);
392 return FALSE;
393 }
394
395 /* Multisample */
396 if (sample_count > 1)
397 return FALSE;
398
399 if ((usage & PIPE_BIND_SAMPLER_VIEW) &&
400 r600_is_sampler_format_supported(format)) {
401 retval |= PIPE_BIND_SAMPLER_VIEW;
402 }
403
404 if ((usage & (PIPE_BIND_RENDER_TARGET |
405 PIPE_BIND_DISPLAY_TARGET |
406 PIPE_BIND_SCANOUT |
407 PIPE_BIND_SHARED)) &&
408 r600_is_colorbuffer_format_supported(format)) {
409 retval |= usage &
410 (PIPE_BIND_RENDER_TARGET |
411 PIPE_BIND_DISPLAY_TARGET |
412 PIPE_BIND_SCANOUT |
413 PIPE_BIND_SHARED);
414 }
415
416 if ((usage & PIPE_BIND_DEPTH_STENCIL) &&
417 r600_is_zs_format_supported(format)) {
418 retval |= PIPE_BIND_DEPTH_STENCIL;
419 }
420
421 if (usage & PIPE_BIND_VERTEX_BUFFER) {
422 struct r600_screen *rscreen = (struct r600_screen *)screen;
423 enum radeon_family family = r600_get_family(rscreen->radeon);
424
425 if (r600_is_vertex_format_supported(format, family)) {
426 retval |= PIPE_BIND_VERTEX_BUFFER;
427 }
428 }
429
430 if (usage & PIPE_BIND_TRANSFER_READ)
431 retval |= PIPE_BIND_TRANSFER_READ;
432 if (usage & PIPE_BIND_TRANSFER_WRITE)
433 retval |= PIPE_BIND_TRANSFER_WRITE;
434
435 return retval == usage;
436 }
437
438 static void r600_destroy_screen(struct pipe_screen* pscreen)
439 {
440 struct r600_screen *rscreen = (struct r600_screen *)pscreen;
441
442 if (rscreen == NULL)
443 return;
444
445 radeon_decref(rscreen->radeon);
446
447 FREE(rscreen);
448 }
449
450
451 struct pipe_screen *r600_screen_create(struct radeon *radeon)
452 {
453 struct r600_screen *rscreen;
454
455 rscreen = CALLOC_STRUCT(r600_screen);
456 if (rscreen == NULL) {
457 return NULL;
458 }
459
460 rscreen->radeon = radeon;
461 rscreen->screen.winsys = (struct pipe_winsys*)radeon;
462 rscreen->screen.destroy = r600_destroy_screen;
463 rscreen->screen.get_name = r600_get_name;
464 rscreen->screen.get_vendor = r600_get_vendor;
465 rscreen->screen.get_param = r600_get_param;
466 rscreen->screen.get_shader_param = r600_get_shader_param;
467 rscreen->screen.get_paramf = r600_get_paramf;
468 rscreen->screen.is_format_supported = r600_is_format_supported;
469 rscreen->screen.context_create = r600_create_context;
470 r600_init_screen_resource_functions(&rscreen->screen);
471
472 rscreen->tiling_info = r600_get_tiling_info(radeon);
473
474 return &rscreen->screen;
475 }