Merge remote-tracking branch 'origin/master' into pipe-video
[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_format.h"
34 #include <util/u_format_s3tc.h>
35 #include <util/u_transfer.h>
36 #include <util/u_surface.h>
37 #include <util/u_pack_color.h>
38 #include <util/u_memory.h>
39 #include <util/u_inlines.h>
40 #include "util/u_upload_mgr.h"
41 #include "os/os_time.h"
42 #include <pipebuffer/pb_buffer.h>
43 #include "r600.h"
44 #include "r600d.h"
45 #include "r600_resource.h"
46 #include "r600_shader.h"
47 #include "r600_pipe.h"
48 #include "r600_state_inlines.h"
49 #include "r600_video_context.h"
50
51 /*
52 * pipe_context
53 */
54 static struct r600_fence *r600_create_fence(struct r600_pipe_context *ctx)
55 {
56 struct r600_fence *fence = NULL;
57
58 if (!ctx->fences.bo) {
59 /* Create the shared buffer object */
60 ctx->fences.bo = r600_bo(ctx->radeon, 4096, 0, 0, 0);
61 if (!ctx->fences.bo) {
62 R600_ERR("r600: failed to create bo for fence objects\n");
63 return NULL;
64 }
65 ctx->fences.data = r600_bo_map(ctx->radeon, ctx->fences.bo, PB_USAGE_UNSYNCHRONIZED, NULL);
66 }
67
68 if (!LIST_IS_EMPTY(&ctx->fences.pool)) {
69 struct r600_fence *entry;
70
71 /* Try to find a freed fence that has been signalled */
72 LIST_FOR_EACH_ENTRY(entry, &ctx->fences.pool, head) {
73 if (ctx->fences.data[entry->index] != 0) {
74 LIST_DELINIT(&entry->head);
75 fence = entry;
76 break;
77 }
78 }
79 }
80
81 if (!fence) {
82 /* Allocate a new fence */
83 struct r600_fence_block *block;
84 unsigned index;
85
86 if ((ctx->fences.next_index + 1) >= 1024) {
87 R600_ERR("r600: too many concurrent fences\n");
88 return NULL;
89 }
90
91 index = ctx->fences.next_index++;
92
93 if (!(index % FENCE_BLOCK_SIZE)) {
94 /* Allocate a new block */
95 block = CALLOC_STRUCT(r600_fence_block);
96 if (block == NULL)
97 return NULL;
98
99 LIST_ADD(&block->head, &ctx->fences.blocks);
100 } else {
101 block = LIST_ENTRY(struct r600_fence_block, ctx->fences.blocks.next, head);
102 }
103
104 fence = &block->fences[index % FENCE_BLOCK_SIZE];
105 fence->ctx = ctx;
106 fence->index = index;
107 }
108
109 pipe_reference_init(&fence->reference, 1);
110
111 ctx->fences.data[fence->index] = 0;
112 r600_context_emit_fence(&ctx->ctx, ctx->fences.bo, fence->index, 1);
113 return fence;
114 }
115
116 static void r600_flush(struct pipe_context *ctx,
117 struct pipe_fence_handle **fence)
118 {
119 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
120 struct r600_fence **rfence = (struct r600_fence**)fence;
121
122 #if 0
123 static int dc = 0;
124 char dname[256];
125 #endif
126
127 if (rfence)
128 *rfence = r600_create_fence(rctx);
129
130 if (!rctx->ctx.pm4_cdwords)
131 return;
132
133 #if 0
134 sprintf(dname, "gallium-%08d.bof", dc);
135 if (dc < 20) {
136 r600_context_dump_bof(&rctx->ctx, dname);
137 R600_ERR("dumped %s\n", dname);
138 }
139 dc++;
140 #endif
141 r600_context_flush(&rctx->ctx);
142
143 /* XXX This shouldn't be really necessary, but removing it breaks some tests.
144 * Needless buffer reallocations may significantly increase memory consumption,
145 * so getting rid of this call is important. */
146 u_upload_flush(rctx->vbuf_mgr->uploader);
147 }
148
149 static void r600_update_num_contexts(struct r600_screen *rscreen, int diff)
150 {
151 pipe_mutex_lock(rscreen->mutex_num_contexts);
152 if (diff > 0) {
153 rscreen->num_contexts++;
154
155 if (rscreen->num_contexts > 1)
156 util_slab_set_thread_safety(&rscreen->pool_buffers,
157 UTIL_SLAB_MULTITHREADED);
158 } else {
159 rscreen->num_contexts--;
160
161 if (rscreen->num_contexts <= 1)
162 util_slab_set_thread_safety(&rscreen->pool_buffers,
163 UTIL_SLAB_SINGLETHREADED);
164 }
165 pipe_mutex_unlock(rscreen->mutex_num_contexts);
166 }
167
168 static void r600_destroy_context(struct pipe_context *context)
169 {
170 struct r600_pipe_context *rctx = (struct r600_pipe_context *)context;
171
172 rctx->context.delete_depth_stencil_alpha_state(&rctx->context, rctx->custom_dsa_flush);
173 util_unreference_framebuffer_state(&rctx->framebuffer);
174
175 r600_context_fini(&rctx->ctx);
176
177 util_blitter_destroy(rctx->blitter);
178
179 for (int i = 0; i < R600_PIPE_NSTATES; i++) {
180 free(rctx->states[i]);
181 }
182
183 u_vbuf_mgr_destroy(rctx->vbuf_mgr);
184 util_slab_destroy(&rctx->pool_transfers);
185
186 if (rctx->fences.bo) {
187 struct r600_fence_block *entry, *tmp;
188
189 LIST_FOR_EACH_ENTRY_SAFE(entry, tmp, &rctx->fences.blocks, head) {
190 LIST_DEL(&entry->head);
191 FREE(entry);
192 }
193
194 r600_bo_unmap(rctx->radeon, rctx->fences.bo);
195 r600_bo_reference(rctx->radeon, &rctx->fences.bo, NULL);
196 }
197
198 r600_update_num_contexts(rctx->screen, -1);
199
200 FREE(rctx);
201 }
202
203 static struct pipe_context *r600_create_context(struct pipe_screen *screen, void *priv)
204 {
205 struct r600_pipe_context *rctx = CALLOC_STRUCT(r600_pipe_context);
206 struct r600_screen* rscreen = (struct r600_screen *)screen;
207 enum chip_class class;
208
209 if (rctx == NULL)
210 return NULL;
211
212 r600_update_num_contexts(rscreen, 1);
213
214 rctx->context.winsys = rscreen->screen.winsys;
215 rctx->context.screen = screen;
216 rctx->context.priv = priv;
217 rctx->context.destroy = r600_destroy_context;
218 rctx->context.flush = r600_flush;
219
220 /* Easy accessing of screen/winsys. */
221 rctx->screen = rscreen;
222 rctx->radeon = rscreen->radeon;
223 rctx->family = r600_get_family(rctx->radeon);
224
225 rctx->fences.bo = NULL;
226 rctx->fences.data = NULL;
227 rctx->fences.next_index = 0;
228 LIST_INITHEAD(&rctx->fences.pool);
229 LIST_INITHEAD(&rctx->fences.blocks);
230
231 r600_init_blit_functions(rctx);
232 r600_init_query_functions(rctx);
233 r600_init_context_resource_functions(rctx);
234 r600_init_surface_functions(rctx);
235 rctx->context.draw_vbo = r600_draw_vbo;
236
237 switch (r600_get_family(rctx->radeon)) {
238 case CHIP_R600:
239 case CHIP_RV610:
240 case CHIP_RV630:
241 case CHIP_RV670:
242 case CHIP_RV620:
243 case CHIP_RV635:
244 case CHIP_RS780:
245 case CHIP_RS880:
246 case CHIP_RV770:
247 case CHIP_RV730:
248 case CHIP_RV710:
249 case CHIP_RV740:
250 r600_init_state_functions(rctx);
251 if (r600_context_init(&rctx->ctx, rctx->radeon)) {
252 r600_destroy_context(&rctx->context);
253 return NULL;
254 }
255 r600_init_config(rctx);
256 break;
257 case CHIP_CEDAR:
258 case CHIP_REDWOOD:
259 case CHIP_JUNIPER:
260 case CHIP_CYPRESS:
261 case CHIP_HEMLOCK:
262 case CHIP_PALM:
263 case CHIP_SUMO:
264 case CHIP_SUMO2:
265 case CHIP_BARTS:
266 case CHIP_TURKS:
267 case CHIP_CAICOS:
268 case CHIP_CAYMAN:
269 evergreen_init_state_functions(rctx);
270 if (evergreen_context_init(&rctx->ctx, rctx->radeon)) {
271 r600_destroy_context(&rctx->context);
272 return NULL;
273 }
274 evergreen_init_config(rctx);
275 break;
276 default:
277 R600_ERR("unsupported family %d\n", r600_get_family(rctx->radeon));
278 r600_destroy_context(&rctx->context);
279 return NULL;
280 }
281
282 util_slab_create(&rctx->pool_transfers,
283 sizeof(struct pipe_transfer), 64,
284 UTIL_SLAB_SINGLETHREADED);
285
286 rctx->vbuf_mgr = u_vbuf_mgr_create(&rctx->context, 1024 * 1024, 256,
287 PIPE_BIND_VERTEX_BUFFER |
288 PIPE_BIND_INDEX_BUFFER |
289 PIPE_BIND_CONSTANT_BUFFER,
290 U_VERTEX_FETCH_DWORD_ALIGNED);
291 if (!rctx->vbuf_mgr) {
292 r600_destroy_context(&rctx->context);
293 return NULL;
294 }
295
296 rctx->blitter = util_blitter_create(&rctx->context);
297 if (rctx->blitter == NULL) {
298 r600_destroy_context(&rctx->context);
299 return NULL;
300 }
301
302 class = r600_get_family_class(rctx->radeon);
303 if (class == R600 || class == R700)
304 rctx->custom_dsa_flush = r600_create_db_flush_dsa(rctx);
305 else
306 rctx->custom_dsa_flush = evergreen_create_db_flush_dsa(rctx);
307
308 return &rctx->context;
309 }
310
311 /*
312 * pipe_screen
313 */
314 static const char* r600_get_vendor(struct pipe_screen* pscreen)
315 {
316 return "X.Org";
317 }
318
319 static const char *r600_get_family_name(enum radeon_family family)
320 {
321 switch(family) {
322 case CHIP_R600: return "AMD R600";
323 case CHIP_RV610: return "AMD RV610";
324 case CHIP_RV630: return "AMD RV630";
325 case CHIP_RV670: return "AMD RV670";
326 case CHIP_RV620: return "AMD RV620";
327 case CHIP_RV635: return "AMD RV635";
328 case CHIP_RS780: return "AMD RS780";
329 case CHIP_RS880: return "AMD RS880";
330 case CHIP_RV770: return "AMD RV770";
331 case CHIP_RV730: return "AMD RV730";
332 case CHIP_RV710: return "AMD RV710";
333 case CHIP_RV740: return "AMD RV740";
334 case CHIP_CEDAR: return "AMD CEDAR";
335 case CHIP_REDWOOD: return "AMD REDWOOD";
336 case CHIP_JUNIPER: return "AMD JUNIPER";
337 case CHIP_CYPRESS: return "AMD CYPRESS";
338 case CHIP_HEMLOCK: return "AMD HEMLOCK";
339 case CHIP_PALM: return "AMD PALM";
340 case CHIP_SUMO: return "AMD SUMO";
341 case CHIP_SUMO2: return "AMD SUMO2";
342 case CHIP_BARTS: return "AMD BARTS";
343 case CHIP_TURKS: return "AMD TURKS";
344 case CHIP_CAICOS: return "AMD CAICOS";
345 case CHIP_CAYMAN: return "AMD CAYMAN";
346 default: return "AMD unknown";
347 }
348 }
349
350 static const char* r600_get_name(struct pipe_screen* pscreen)
351 {
352 struct r600_screen *rscreen = (struct r600_screen *)pscreen;
353 enum radeon_family family = r600_get_family(rscreen->radeon);
354
355 return r600_get_family_name(family);
356 }
357
358 static int r600_get_param(struct pipe_screen* pscreen, enum pipe_cap param)
359 {
360 struct r600_screen *rscreen = (struct r600_screen *)pscreen;
361 enum radeon_family family = r600_get_family(rscreen->radeon);
362
363 switch (param) {
364 /* Supported features (boolean caps). */
365 case PIPE_CAP_NPOT_TEXTURES:
366 case PIPE_CAP_TWO_SIDED_STENCIL:
367 case PIPE_CAP_GLSL:
368 case PIPE_CAP_DUAL_SOURCE_BLEND:
369 case PIPE_CAP_ANISOTROPIC_FILTER:
370 case PIPE_CAP_POINT_SPRITE:
371 case PIPE_CAP_OCCLUSION_QUERY:
372 case PIPE_CAP_TEXTURE_SHADOW_MAP:
373 case PIPE_CAP_TEXTURE_MIRROR_CLAMP:
374 case PIPE_CAP_TEXTURE_MIRROR_REPEAT:
375 case PIPE_CAP_BLEND_EQUATION_SEPARATE:
376 case PIPE_CAP_SM3:
377 case PIPE_CAP_TEXTURE_SWIZZLE:
378 case PIPE_CAP_DEPTHSTENCIL_CLEAR_SEPARATE:
379 case PIPE_CAP_DEPTH_CLAMP:
380 case PIPE_CAP_SHADER_STENCIL_EXPORT:
381 case PIPE_CAP_VERTEX_ELEMENT_INSTANCE_DIVISOR:
382 case PIPE_CAP_MIXED_COLORBUFFER_FORMATS:
383 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
384 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
385 return 1;
386
387 /* Supported except the original R600. */
388 case PIPE_CAP_INDEP_BLEND_ENABLE:
389 case PIPE_CAP_INDEP_BLEND_FUNC:
390 /* R600 doesn't support per-MRT blends */
391 return family == CHIP_R600 ? 0 : 1;
392
393 /* Supported on Evergreen. */
394 case PIPE_CAP_SEAMLESS_CUBE_MAP:
395 case PIPE_CAP_SEAMLESS_CUBE_MAP_PER_TEXTURE:
396 return family >= CHIP_CEDAR ? 1 : 0;
397
398 /* Unsupported features. */
399 case PIPE_CAP_STREAM_OUTPUT:
400 case PIPE_CAP_PRIMITIVE_RESTART:
401 case PIPE_CAP_FRAGMENT_COLOR_CLAMP_CONTROL:
402 case PIPE_CAP_TGSI_INSTANCEID:
403 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT:
404 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
405 return 0;
406
407 case PIPE_CAP_ARRAY_TEXTURES:
408 /* fix once the CS checker upstream is fixed */
409 return debug_get_bool_option("R600_ARRAY_TEXTURE", FALSE);
410
411 /* Texturing. */
412 case PIPE_CAP_MAX_TEXTURE_2D_LEVELS:
413 case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
414 case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
415 if (family >= CHIP_CEDAR)
416 return 15;
417 else
418 return 14;
419 case PIPE_CAP_MAX_VERTEX_TEXTURE_UNITS:
420 case PIPE_CAP_MAX_TEXTURE_IMAGE_UNITS:
421 return 16;
422 case PIPE_CAP_MAX_COMBINED_SAMPLERS:
423 return 32;
424
425 /* Render targets. */
426 case PIPE_CAP_MAX_RENDER_TARGETS:
427 /* FIXME some r6xx are buggy and can only do 4 */
428 return 8;
429
430 /* Timer queries, present when the clock frequency is non zero. */
431 case PIPE_CAP_TIMER_QUERY:
432 return r600_get_clock_crystal_freq(rscreen->radeon) != 0;
433
434 default:
435 R600_ERR("r600: unknown param %d\n", param);
436 return 0;
437 }
438 }
439
440 static float r600_get_paramf(struct pipe_screen* pscreen, enum pipe_cap param)
441 {
442 struct r600_screen *rscreen = (struct r600_screen *)pscreen;
443 enum radeon_family family = r600_get_family(rscreen->radeon);
444
445 switch (param) {
446 case PIPE_CAP_MAX_LINE_WIDTH:
447 case PIPE_CAP_MAX_LINE_WIDTH_AA:
448 case PIPE_CAP_MAX_POINT_WIDTH:
449 case PIPE_CAP_MAX_POINT_WIDTH_AA:
450 if (family >= CHIP_CEDAR)
451 return 16384.0f;
452 else
453 return 8192.0f;
454 case PIPE_CAP_MAX_TEXTURE_ANISOTROPY:
455 return 16.0f;
456 case PIPE_CAP_MAX_TEXTURE_LOD_BIAS:
457 return 16.0f;
458 default:
459 R600_ERR("r600: unsupported paramf %d\n", param);
460 return 0.0f;
461 }
462 }
463
464 static int r600_get_shader_param(struct pipe_screen* pscreen, unsigned shader, enum pipe_shader_cap param)
465 {
466 switch(shader)
467 {
468 case PIPE_SHADER_FRAGMENT:
469 case PIPE_SHADER_VERTEX:
470 break;
471 case PIPE_SHADER_GEOMETRY:
472 /* TODO: support and enable geometry programs */
473 return 0;
474 default:
475 /* TODO: support tessellation on Evergreen */
476 return 0;
477 }
478
479 /* TODO: all these should be fixed, since r600 surely supports much more! */
480 switch (param) {
481 case PIPE_SHADER_CAP_MAX_INSTRUCTIONS:
482 case PIPE_SHADER_CAP_MAX_ALU_INSTRUCTIONS:
483 case PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS:
484 case PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS:
485 return 16384;
486 case PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH:
487 return 8; /* FIXME */
488 case PIPE_SHADER_CAP_MAX_INPUTS:
489 if(shader == PIPE_SHADER_FRAGMENT)
490 return 10;
491 else
492 return 16;
493 case PIPE_SHADER_CAP_MAX_TEMPS:
494 return 256; /* Max native temporaries. */
495 case PIPE_SHADER_CAP_MAX_ADDRS:
496 /* FIXME Isn't this equal to TEMPS? */
497 return 1; /* Max native address registers */
498 case PIPE_SHADER_CAP_MAX_CONSTS:
499 return R600_MAX_CONST_BUFFER_SIZE;
500 case PIPE_SHADER_CAP_MAX_CONST_BUFFERS:
501 return R600_MAX_CONST_BUFFERS;
502 case PIPE_SHADER_CAP_MAX_PREDS:
503 return 0; /* FIXME */
504 case PIPE_SHADER_CAP_TGSI_CONT_SUPPORTED:
505 return 1;
506 case PIPE_SHADER_CAP_INDIRECT_INPUT_ADDR:
507 case PIPE_SHADER_CAP_INDIRECT_OUTPUT_ADDR:
508 case PIPE_SHADER_CAP_INDIRECT_TEMP_ADDR:
509 case PIPE_SHADER_CAP_INDIRECT_CONST_ADDR:
510 return 1;
511 case PIPE_SHADER_CAP_SUBROUTINES:
512 return 0;
513 default:
514 return 0;
515 }
516 }
517
518 static boolean r600_is_format_supported(struct pipe_screen* screen,
519 enum pipe_format format,
520 enum pipe_texture_target target,
521 unsigned sample_count,
522 unsigned usage)
523 {
524 unsigned retval = 0;
525 if (target >= PIPE_MAX_TEXTURE_TYPES) {
526 R600_ERR("r600: unsupported texture type %d\n", target);
527 return FALSE;
528 }
529
530 if (!util_format_is_supported(format, usage))
531 return FALSE;
532
533 /* Multisample */
534 if (sample_count > 1)
535 return FALSE;
536
537 if ((usage & PIPE_BIND_SAMPLER_VIEW) &&
538 r600_is_sampler_format_supported(screen, format)) {
539 retval |= PIPE_BIND_SAMPLER_VIEW;
540 }
541
542 if ((usage & (PIPE_BIND_RENDER_TARGET |
543 PIPE_BIND_DISPLAY_TARGET |
544 PIPE_BIND_SCANOUT |
545 PIPE_BIND_SHARED)) &&
546 r600_is_colorbuffer_format_supported(format)) {
547 retval |= usage &
548 (PIPE_BIND_RENDER_TARGET |
549 PIPE_BIND_DISPLAY_TARGET |
550 PIPE_BIND_SCANOUT |
551 PIPE_BIND_SHARED);
552 }
553
554 if ((usage & PIPE_BIND_DEPTH_STENCIL) &&
555 r600_is_zs_format_supported(format)) {
556 retval |= PIPE_BIND_DEPTH_STENCIL;
557 }
558
559 if (usage & PIPE_BIND_VERTEX_BUFFER) {
560 struct r600_screen *rscreen = (struct r600_screen *)screen;
561 enum radeon_family family = r600_get_family(rscreen->radeon);
562
563 if (r600_is_vertex_format_supported(format, family)) {
564 retval |= PIPE_BIND_VERTEX_BUFFER;
565 }
566 }
567
568 if (usage & PIPE_BIND_TRANSFER_READ)
569 retval |= PIPE_BIND_TRANSFER_READ;
570 if (usage & PIPE_BIND_TRANSFER_WRITE)
571 retval |= PIPE_BIND_TRANSFER_WRITE;
572
573 return retval == usage;
574 }
575
576 static void r600_destroy_screen(struct pipe_screen* pscreen)
577 {
578 struct r600_screen *rscreen = (struct r600_screen *)pscreen;
579
580 if (rscreen == NULL)
581 return;
582
583 radeon_decref(rscreen->radeon);
584
585 util_slab_destroy(&rscreen->pool_buffers);
586 pipe_mutex_destroy(rscreen->mutex_num_contexts);
587 FREE(rscreen);
588 }
589
590 static void r600_fence_reference(struct pipe_screen *pscreen,
591 struct pipe_fence_handle **ptr,
592 struct pipe_fence_handle *fence)
593 {
594 struct r600_fence **oldf = (struct r600_fence**)ptr;
595 struct r600_fence *newf = (struct r600_fence*)fence;
596
597 if (pipe_reference(&(*oldf)->reference, &newf->reference)) {
598 struct r600_pipe_context *ctx = (*oldf)->ctx;
599 LIST_ADDTAIL(&(*oldf)->head, &ctx->fences.pool);
600 }
601
602 *ptr = fence;
603 }
604
605 static boolean r600_fence_signalled(struct pipe_screen *pscreen,
606 struct pipe_fence_handle *fence)
607 {
608 struct r600_fence *rfence = (struct r600_fence*)fence;
609 struct r600_pipe_context *ctx = rfence->ctx;
610
611 return ctx->fences.data[rfence->index];
612 }
613
614 static boolean r600_fence_finish(struct pipe_screen *pscreen,
615 struct pipe_fence_handle *fence,
616 uint64_t timeout)
617 {
618 struct r600_fence *rfence = (struct r600_fence*)fence;
619 struct r600_pipe_context *ctx = rfence->ctx;
620 int64_t start_time = 0;
621 unsigned spins = 0;
622
623 if (timeout != PIPE_TIMEOUT_INFINITE) {
624 start_time = os_time_get();
625
626 /* Convert to microseconds. */
627 timeout /= 1000;
628 }
629
630 while (ctx->fences.data[rfence->index] == 0) {
631 if (++spins % 256)
632 continue;
633 #ifdef PIPE_OS_UNIX
634 sched_yield();
635 #else
636 os_time_sleep(10);
637 #endif
638 if (timeout != PIPE_TIMEOUT_INFINITE &&
639 os_time_get() - start_time >= timeout) {
640 return FALSE;
641 }
642 }
643
644 return TRUE;
645 }
646
647 struct pipe_screen *r600_screen_create(struct radeon *radeon)
648 {
649 struct r600_screen *rscreen;
650
651 rscreen = CALLOC_STRUCT(r600_screen);
652 if (rscreen == NULL) {
653 return NULL;
654 }
655
656 rscreen->radeon = radeon;
657 rscreen->screen.winsys = (struct pipe_winsys*)radeon;
658 rscreen->screen.destroy = r600_destroy_screen;
659 rscreen->screen.get_name = r600_get_name;
660 rscreen->screen.get_vendor = r600_get_vendor;
661 rscreen->screen.get_param = r600_get_param;
662 rscreen->screen.get_shader_param = r600_get_shader_param;
663 rscreen->screen.get_paramf = r600_get_paramf;
664 rscreen->screen.is_format_supported = r600_is_format_supported;
665 rscreen->screen.context_create = r600_create_context;
666 rscreen->screen.video_context_create = r600_video_create;
667 rscreen->screen.fence_reference = r600_fence_reference;
668 rscreen->screen.fence_signalled = r600_fence_signalled;
669 rscreen->screen.fence_finish = r600_fence_finish;
670 r600_init_screen_resource_functions(&rscreen->screen);
671
672 rscreen->tiling_info = r600_get_tiling_info(radeon);
673 util_format_s3tc_init();
674
675 util_slab_create(&rscreen->pool_buffers,
676 sizeof(struct r600_resource_buffer), 64,
677 UTIL_SLAB_SINGLETHREADED);
678
679 pipe_mutex_init(rscreen->mutex_num_contexts);
680
681 return &rscreen->screen;
682 }