1 /****************************************************************************
2 * Copyright (C) 2015 Intel Corporation. All Rights Reserved.
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:
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
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
22 ***************************************************************************/
24 #include "pipe/p_screen.h"
25 #include "util/u_memory.h"
26 #include "os/os_time.h"
28 #include "swr_context.h"
29 #include "swr_screen.h"
30 #include "swr_fence.h"
32 #if defined(PIPE_CC_MSVC) // portable thread yield
33 #define sched_yield SwitchToThread
37 * Fence callback, called by back-end thread on completion of all rendering up
41 swr_sync_cb(uint64_t userData
, uint64_t userData2
, uint64_t userData3
)
43 struct swr_fence
*fence
= (struct swr_fence
*)userData
;
45 /* Correct value is in SwrSync data, and not the fence write field. */
46 fence
->read
= userData2
;
50 * Submit an existing fence.
53 swr_fence_submit(struct swr_context
*ctx
, struct pipe_fence_handle
*fh
)
55 struct swr_fence
*fence
= swr_fence(fh
);
58 fence
->pending
= TRUE
;
59 SwrSync(ctx
->swrContext
, swr_sync_cb
, (uint64_t)fence
, fence
->write
, 0);
63 * Create a new fence object.
65 struct pipe_fence_handle
*
68 static int fence_id
= 0;
69 struct swr_fence
*fence
= CALLOC_STRUCT(swr_fence
);
73 pipe_reference_init(&fence
->reference
, 1);
74 fence
->id
= fence_id
++;
76 return (struct pipe_fence_handle
*)fence
;
79 /** Destroy a fence. Called when refcount hits zero. */
81 swr_fence_destroy(struct swr_fence
*fence
)
87 * Set ptr = fence, with reference counting
90 swr_fence_reference(struct pipe_screen
*screen
,
91 struct pipe_fence_handle
**ptr
,
92 struct pipe_fence_handle
*f
)
94 struct swr_fence
*fence
= swr_fence(f
);
95 struct swr_fence
*old
;
98 old
= swr_fence(*ptr
);
104 if (pipe_reference(&old
->reference
, &fence
->reference
))
105 swr_fence_destroy(old
);
110 * Wait for the fence to finish.
113 swr_fence_finish(struct pipe_screen
*screen
,
114 struct pipe_context
*ctx
,
115 struct pipe_fence_handle
*fence_handle
,
118 while (!swr_is_fence_done(fence_handle
))
121 swr_fence(fence_handle
)->pending
= FALSE
;
128 swr_get_timestamp(struct pipe_screen
*screen
)
130 return os_time_get_nano();
135 swr_fence_init(struct pipe_screen
*p_screen
)
137 p_screen
->fence_reference
= swr_fence_reference
;
138 p_screen
->fence_finish
= swr_fence_finish
;
139 p_screen
->get_timestamp
= swr_get_timestamp
;
141 /* Create persistant StoreTiles "flush" fence, used to signal completion
142 * of flushing tile state back to resource texture, via StoreTiles. */
143 struct swr_screen
*screen
= swr_screen(p_screen
);
144 screen
->flush_fence
= swr_fence_create();