1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
/****************************************************************************
* Copyright (C) 2015 Intel Corporation. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
***************************************************************************/
#include "util/u_memory.h"
#include "util/u_inlines.h"
#include "util/u_format.h"
extern "C" {
#include "util/u_transfer.h"
#include "util/u_surface.h"
}
#include "swr_context.h"
#include "swr_memory.h"
#include "swr_screen.h"
#include "swr_resource.h"
#include "swr_scratch.h"
#include "swr_query.h"
#include "swr_fence.h"
#include "api.h"
#include "backend.h"
static struct pipe_surface *
swr_create_surface(struct pipe_context *pipe,
struct pipe_resource *pt,
const struct pipe_surface *surf_tmpl)
{
struct pipe_surface *ps;
ps = CALLOC_STRUCT(pipe_surface);
if (ps) {
pipe_reference_init(&ps->reference, 1);
pipe_resource_reference(&ps->texture, pt);
ps->context = pipe;
ps->format = surf_tmpl->format;
if (pt->target != PIPE_BUFFER) {
assert(surf_tmpl->u.tex.level <= pt->last_level);
ps->width = u_minify(pt->width0, surf_tmpl->u.tex.level);
ps->height = u_minify(pt->height0, surf_tmpl->u.tex.level);
ps->u.tex.level = surf_tmpl->u.tex.level;
ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
if (ps->u.tex.first_layer != ps->u.tex.last_layer) {
debug_printf("creating surface with multiple layers, rendering "
"to first layer only\n");
}
} else {
/* setting width as number of elements should get us correct
* renderbuffer width */
ps->width = surf_tmpl->u.buf.last_element
- surf_tmpl->u.buf.first_element + 1;
ps->height = pt->height0;
ps->u.buf.first_element = surf_tmpl->u.buf.first_element;
ps->u.buf.last_element = surf_tmpl->u.buf.last_element;
assert(ps->u.buf.first_element <= ps->u.buf.last_element);
assert(ps->u.buf.last_element < ps->width);
}
}
return ps;
}
static void
swr_surface_destroy(struct pipe_context *pipe, struct pipe_surface *surf)
{
assert(surf->texture);
struct pipe_resource *resource = surf->texture;
/* If the resource has been drawn to, store tiles. */
swr_store_dirty_resource(pipe, resource, SWR_TILE_RESOLVED);
pipe_resource_reference(&resource, NULL);
FREE(surf);
}
static void *
swr_transfer_map(struct pipe_context *pipe,
struct pipe_resource *resource,
unsigned level,
unsigned usage,
const struct pipe_box *box,
struct pipe_transfer **transfer)
{
struct swr_screen *screen = swr_screen(pipe->screen);
struct swr_resource *spr = swr_resource(resource);
struct pipe_transfer *pt;
enum pipe_format format = resource->format;
assert(resource);
assert(level <= resource->last_level);
/* If mapping an attached rendertarget, store tiles to surface and set
* postStoreTileState to SWR_TILE_INVALID so tiles get reloaded on next use
* and nothing needs to be done at unmap. */
swr_store_dirty_resource(pipe, resource, SWR_TILE_INVALID);
if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
/* If resource is in use, finish fence before mapping.
* Unless requested not to block, then if not done return NULL map */
if (usage & PIPE_TRANSFER_DONTBLOCK) {
if (swr_is_fence_pending(screen->flush_fence))
return NULL;
} else {
if (spr->status) {
/* But, if there's no fence pending, submit one.
* XXX: Remove once draw timestamps are finished. */
if (!swr_is_fence_pending(screen->flush_fence))
swr_fence_submit(swr_context(pipe), screen->flush_fence);
swr_fence_finish(pipe->screen, NULL, screen->flush_fence, 0);
swr_resource_unused(resource);
}
}
}
pt = CALLOC_STRUCT(pipe_transfer);
if (!pt)
return NULL;
pipe_resource_reference(&pt->resource, resource);
pt->level = level;
pt->box = *box;
pt->stride = spr->row_stride[level];
pt->layer_stride = spr->img_stride[level];
/* if we're mapping the depth/stencil, copy in stencil */
if (spr->base.format == PIPE_FORMAT_Z24_UNORM_S8_UINT
&& spr->has_stencil) {
for (unsigned i = 0; i < spr->alignedWidth * spr->alignedHeight; i++) {
spr->swr.pBaseAddress[4 * i + 3] = spr->secondary.pBaseAddress[i];
}
} else if (spr->base.format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
&& spr->has_stencil) {
for (unsigned i = 0; i < spr->alignedWidth * spr->alignedHeight; i++) {
spr->swr.pBaseAddress[8 * i + 4] = spr->secondary.pBaseAddress[i];
}
}
unsigned offset = box->z * pt->layer_stride + box->y * pt->stride
+ box->x * util_format_get_blocksize(format);
*transfer = pt;
return spr->swr.pBaseAddress + offset + spr->mip_offsets[level];
}
static void
swr_transfer_unmap(struct pipe_context *pipe, struct pipe_transfer *transfer)
{
assert(transfer->resource);
struct swr_resource *res = swr_resource(transfer->resource);
/* if we're mapping the depth/stencil, copy out stencil */
if (res->base.format == PIPE_FORMAT_Z24_UNORM_S8_UINT
&& res->has_stencil) {
for (unsigned i = 0; i < res->alignedWidth * res->alignedHeight; i++) {
res->secondary.pBaseAddress[i] = res->swr.pBaseAddress[4 * i + 3];
}
} else if (res->base.format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
&& res->has_stencil) {
for (unsigned i = 0; i < res->alignedWidth * res->alignedHeight; i++) {
res->secondary.pBaseAddress[i] = res->swr.pBaseAddress[8 * i + 4];
}
}
pipe_resource_reference(&transfer->resource, NULL);
FREE(transfer);
}
static void
swr_resource_copy(struct pipe_context *pipe,
struct pipe_resource *dst,
unsigned dst_level,
unsigned dstx,
unsigned dsty,
unsigned dstz,
struct pipe_resource *src,
unsigned src_level,
const struct pipe_box *src_box)
{
struct swr_screen *screen = swr_screen(pipe->screen);
/* If either the src or dst is a renderTarget, store tiles before copy */
swr_store_dirty_resource(pipe, src, SWR_TILE_RESOLVED);
swr_store_dirty_resource(pipe, dst, SWR_TILE_RESOLVED);
swr_fence_finish(pipe->screen, NULL, screen->flush_fence, 0);
swr_resource_unused(src);
swr_resource_unused(dst);
if ((dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER)
|| (dst->target != PIPE_BUFFER && src->target != PIPE_BUFFER)) {
util_resource_copy_region(
pipe, dst, dst_level, dstx, dsty, dstz, src, src_level, src_box);
return;
}
debug_printf("unhandled swr_resource_copy\n");
}
static void
swr_blit(struct pipe_context *pipe, const struct pipe_blit_info *blit_info)
{
struct swr_context *ctx = swr_context(pipe);
struct pipe_blit_info info = *blit_info;
if (blit_info->render_condition_enable && !swr_check_render_cond(pipe))
return;
if (info.src.resource->nr_samples > 1 && info.dst.resource->nr_samples <= 1
&& !util_format_is_depth_or_stencil(info.src.resource->format)
&& !util_format_is_pure_integer(info.src.resource->format)) {
debug_printf("swr: color resolve unimplemented\n");
return;
}
if (util_try_blit_via_copy_region(pipe, &info)) {
return; /* done */
}
if (info.mask & PIPE_MASK_S) {
debug_printf("swr: cannot blit stencil, skipping\n");
info.mask &= ~PIPE_MASK_S;
}
if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {
debug_printf("swr: blit unsupported %s -> %s\n",
util_format_short_name(info.src.resource->format),
util_format_short_name(info.dst.resource->format));
return;
}
/* XXX turn off occlusion and streamout queries */
util_blitter_save_vertex_buffer_slot(ctx->blitter, ctx->vertex_buffer);
util_blitter_save_vertex_elements(ctx->blitter, (void *)ctx->velems);
util_blitter_save_vertex_shader(ctx->blitter, (void *)ctx->vs);
/*util_blitter_save_geometry_shader(ctx->blitter, (void*)ctx->gs);*/
util_blitter_save_so_targets(
ctx->blitter,
ctx->num_so_targets,
(struct pipe_stream_output_target **)ctx->so_targets);
util_blitter_save_rasterizer(ctx->blitter, (void *)ctx->rasterizer);
util_blitter_save_viewport(ctx->blitter, &ctx->viewport);
util_blitter_save_scissor(ctx->blitter, &ctx->scissor);
util_blitter_save_fragment_shader(ctx->blitter, ctx->fs);
util_blitter_save_blend(ctx->blitter, (void *)ctx->blend);
util_blitter_save_depth_stencil_alpha(ctx->blitter,
(void *)ctx->depth_stencil);
util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref);
util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask);
util_blitter_save_framebuffer(ctx->blitter, &ctx->framebuffer);
util_blitter_save_fragment_sampler_states(
ctx->blitter,
ctx->num_samplers[PIPE_SHADER_FRAGMENT],
(void **)ctx->samplers[PIPE_SHADER_FRAGMENT]);
util_blitter_save_fragment_sampler_views(
ctx->blitter,
ctx->num_sampler_views[PIPE_SHADER_FRAGMENT],
ctx->sampler_views[PIPE_SHADER_FRAGMENT]);
util_blitter_save_render_condition(ctx->blitter,
ctx->render_cond_query,
ctx->render_cond_cond,
ctx->render_cond_mode);
util_blitter_blit(ctx->blitter, &info);
}
static void
swr_destroy(struct pipe_context *pipe)
{
struct swr_context *ctx = swr_context(pipe);
struct swr_screen *screen = swr_screen(pipe->screen);
if (ctx->blitter)
util_blitter_destroy(ctx->blitter);
/* Idle core before deleting context */
SwrWaitForIdle(ctx->swrContext);
for (unsigned i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
pipe_surface_reference(&ctx->framebuffer.cbufs[i], NULL);
}
pipe_surface_reference(&ctx->framebuffer.zsbuf, NULL);
for (unsigned i = 0; i < ARRAY_SIZE(ctx->sampler_views[0]); i++) {
pipe_sampler_view_reference(&ctx->sampler_views[PIPE_SHADER_FRAGMENT][i], NULL);
}
for (unsigned i = 0; i < ARRAY_SIZE(ctx->sampler_views[0]); i++) {
pipe_sampler_view_reference(&ctx->sampler_views[PIPE_SHADER_VERTEX][i], NULL);
}
if (ctx->swrContext)
SwrDestroyContext(ctx->swrContext);
delete ctx->blendJIT;
swr_destroy_scratch_buffers(ctx);
/* Only update screen->pipe if current context is being destroyed */
assert(screen);
if (screen->pipe == pipe)
screen->pipe = NULL;
FREE(ctx);
}
static void
swr_render_condition(struct pipe_context *pipe,
struct pipe_query *query,
boolean condition,
uint mode)
{
struct swr_context *ctx = swr_context(pipe);
ctx->render_cond_query = query;
ctx->render_cond_mode = mode;
ctx->render_cond_cond = condition;
}
static void
swr_UpdateStats(HANDLE hPrivateContext, const SWR_STATS *pStats)
{
swr_draw_context *pDC = (swr_draw_context*)hPrivateContext;
if (!pDC)
return;
struct swr_context *ctx = (struct swr_context *)pDC->swr_ctx;
SWR_STATS *pSwrStats = &ctx->stats;
pSwrStats->DepthPassCount += pStats->DepthPassCount;
pSwrStats->IaVertices += pStats->IaVertices;
pSwrStats->IaPrimitives += pStats->IaPrimitives;
pSwrStats->VsInvocations += pStats->VsInvocations;
pSwrStats->HsInvocations += pStats->HsInvocations;
pSwrStats->DsInvocations += pStats->DsInvocations;
pSwrStats->GsInvocations += pStats->GsInvocations;
pSwrStats->PsInvocations += pStats->PsInvocations;
pSwrStats->CsInvocations += pStats->CsInvocations;
pSwrStats->CInvocations += pStats->CInvocations;
pSwrStats->CPrimitives += pStats->CPrimitives;
pSwrStats->GsPrimitives += pStats->GsPrimitives;
for (unsigned i = 0; i < 4; i++) {
pSwrStats->SoPrimStorageNeeded[i] += pStats->SoPrimStorageNeeded[i];
pSwrStats->SoNumPrimsWritten[i] += pStats->SoNumPrimsWritten[i];
}
}
struct pipe_context *
swr_create_context(struct pipe_screen *p_screen, void *priv, unsigned flags)
{
struct swr_context *ctx = CALLOC_STRUCT(swr_context);
ctx->blendJIT =
new std::unordered_map<BLEND_COMPILE_STATE, PFN_BLEND_JIT_FUNC>;
SWR_CREATECONTEXT_INFO createInfo;
memset(&createInfo, 0, sizeof(createInfo));
createInfo.driver = GL;
createInfo.privateStateSize = sizeof(swr_draw_context);
createInfo.pfnLoadTile = swr_LoadHotTile;
createInfo.pfnStoreTile = swr_StoreHotTile;
createInfo.pfnClearTile = swr_StoreHotTileClear;
createInfo.pfnUpdateStats = swr_UpdateStats;
ctx->swrContext = SwrCreateContext(&createInfo);
/* Init Load/Store/ClearTiles Tables */
swr_InitMemoryModule();
InitBackendFuncTables();
if (ctx->swrContext == NULL)
goto fail;
ctx->pipe.screen = p_screen;
ctx->pipe.destroy = swr_destroy;
ctx->pipe.priv = priv;
ctx->pipe.create_surface = swr_create_surface;
ctx->pipe.surface_destroy = swr_surface_destroy;
ctx->pipe.transfer_map = swr_transfer_map;
ctx->pipe.transfer_unmap = swr_transfer_unmap;
ctx->pipe.transfer_flush_region = u_default_transfer_flush_region;
ctx->pipe.buffer_subdata = u_default_buffer_subdata;
ctx->pipe.texture_subdata = u_default_texture_subdata;
ctx->pipe.resource_copy_region = swr_resource_copy;
ctx->pipe.render_condition = swr_render_condition;
swr_state_init(&ctx->pipe);
swr_clear_init(&ctx->pipe);
swr_draw_init(&ctx->pipe);
swr_query_init(&ctx->pipe);
ctx->pipe.blit = swr_blit;
ctx->blitter = util_blitter_create(&ctx->pipe);
if (!ctx->blitter)
goto fail;
swr_init_scratch_buffers(ctx);
return &ctx->pipe;
fail:
/* Should really validate the init steps and fail gracefully */
swr_destroy(&ctx->pipe);
return NULL;
}
|