gk110/ir: add emission for OP_SULDB and OP_SUSTx
[mesa.git] / src / gallium / drivers / swr / swr_fence.cpp
1 /****************************************************************************
2 * Copyright (C) 2015 Intel Corporation. All Rights Reserved.
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * 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 NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 ***************************************************************************/
23
24 #include "pipe/p_screen.h"
25 #include "util/u_memory.h"
26 #include "os/os_time.h"
27
28 #include "swr_context.h"
29 #include "swr_screen.h"
30 #include "swr_fence.h"
31
32 #if defined(PIPE_CC_MSVC) // portable thread yield
33 #define sched_yield SwitchToThread
34 #endif
35
36 /*
37 * Fence callback, called by back-end thread on completion of all rendering up
38 * to SwrSync call.
39 */
40 static void
41 swr_sync_cb(uint64_t userData, uint64_t userData2, uint64_t userData3)
42 {
43 struct swr_fence *fence = (struct swr_fence *)userData;
44
45 /* Correct value is in SwrSync data, and not the fence write field. */
46 fence->read = userData2;
47 }
48
49 /*
50 * Submit an existing fence.
51 */
52 void
53 swr_fence_submit(struct swr_context *ctx, struct pipe_fence_handle *fh)
54 {
55 struct swr_fence *fence = swr_fence(fh);
56
57 fence->write++;
58 fence->pending = TRUE;
59 SwrSync(ctx->swrContext, swr_sync_cb, (uint64_t)fence, fence->write, 0);
60 }
61
62 /*
63 * Create a new fence object.
64 */
65 struct pipe_fence_handle *
66 swr_fence_create()
67 {
68 static int fence_id = 0;
69 struct swr_fence *fence = CALLOC_STRUCT(swr_fence);
70 if (!fence)
71 return NULL;
72
73 pipe_reference_init(&fence->reference, 1);
74 fence->id = fence_id++;
75
76 return (struct pipe_fence_handle *)fence;
77 }
78
79 /** Destroy a fence. Called when refcount hits zero. */
80 static void
81 swr_fence_destroy(struct swr_fence *fence)
82 {
83 FREE(fence);
84 }
85
86 /**
87 * Set ptr = fence, with reference counting
88 */
89 void
90 swr_fence_reference(struct pipe_screen *screen,
91 struct pipe_fence_handle **ptr,
92 struct pipe_fence_handle *f)
93 {
94 struct swr_fence *fence = swr_fence(f);
95 struct swr_fence *old;
96
97 if (likely(ptr)) {
98 old = swr_fence(*ptr);
99 *ptr = f;
100 } else {
101 old = NULL;
102 }
103
104 if (pipe_reference(&old->reference, &fence->reference))
105 swr_fence_destroy(old);
106 }
107
108 static INLINE boolean
109 swr_is_fence_done(struct pipe_fence_handle *fence_handle)
110 {
111 struct swr_fence *fence = swr_fence(fence_handle);
112 return (fence->read == fence->write);
113 }
114
115 /*
116 * Wait for the fence to finish.
117 */
118 boolean
119 swr_fence_finish(struct pipe_screen *screen,
120 struct pipe_fence_handle *fence_handle,
121 uint64_t timeout)
122 {
123 while (!swr_is_fence_done(fence_handle))
124 sched_yield();
125
126 swr_fence(fence_handle)->pending = FALSE;
127
128 return TRUE;
129 }
130
131
132 uint64_t
133 swr_get_timestamp(struct pipe_screen *screen)
134 {
135 return os_time_get_nano();
136 }
137
138
139 void
140 swr_fence_init(struct pipe_screen *p_screen)
141 {
142 p_screen->fence_reference = swr_fence_reference;
143 p_screen->fence_finish = swr_fence_finish;
144 p_screen->get_timestamp = swr_get_timestamp;
145
146 /* Create persistant StoreTiles "flush" fence, used to signal completion
147 * of flushing tile state back to resource texture, via StoreTiles. */
148 struct swr_screen *screen = swr_screen(p_screen);
149 screen->flush_fence = swr_fence_create();
150 }