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 "swr_context.h"
25 #include "swr_memory.h"
26 #include "swr_screen.h"
27 #include "swr_resource.h"
28 #include "swr_scratch.h"
29 #include "swr_query.h"
30 #include "swr_fence.h"
32 #include "util/u_memory.h"
33 #include "util/u_inlines.h"
34 #include "util/u_format.h"
35 #include "util/u_atomic.h"
38 #include "util/u_transfer.h"
39 #include "util/u_surface.h"
45 static struct pipe_surface
*
46 swr_create_surface(struct pipe_context
*pipe
,
47 struct pipe_resource
*pt
,
48 const struct pipe_surface
*surf_tmpl
)
50 struct pipe_surface
*ps
;
52 ps
= CALLOC_STRUCT(pipe_surface
);
54 pipe_reference_init(&ps
->reference
, 1);
55 pipe_resource_reference(&ps
->texture
, pt
);
57 ps
->format
= surf_tmpl
->format
;
58 if (pt
->target
!= PIPE_BUFFER
) {
59 assert(surf_tmpl
->u
.tex
.level
<= pt
->last_level
);
60 ps
->width
= u_minify(pt
->width0
, surf_tmpl
->u
.tex
.level
);
61 ps
->height
= u_minify(pt
->height0
, surf_tmpl
->u
.tex
.level
);
62 ps
->u
.tex
.level
= surf_tmpl
->u
.tex
.level
;
63 ps
->u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
64 ps
->u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
66 /* setting width as number of elements should get us correct
67 * renderbuffer width */
68 ps
->width
= surf_tmpl
->u
.buf
.last_element
69 - surf_tmpl
->u
.buf
.first_element
+ 1;
70 ps
->height
= pt
->height0
;
71 ps
->u
.buf
.first_element
= surf_tmpl
->u
.buf
.first_element
;
72 ps
->u
.buf
.last_element
= surf_tmpl
->u
.buf
.last_element
;
73 assert(ps
->u
.buf
.first_element
<= ps
->u
.buf
.last_element
);
74 assert(ps
->u
.buf
.last_element
< ps
->width
);
81 swr_surface_destroy(struct pipe_context
*pipe
, struct pipe_surface
*surf
)
83 assert(surf
->texture
);
84 struct pipe_resource
*resource
= surf
->texture
;
86 /* If the resource has been drawn to, store tiles. */
87 swr_store_dirty_resource(pipe
, resource
, SWR_TILE_RESOLVED
);
89 pipe_resource_reference(&resource
, NULL
);
95 swr_transfer_map(struct pipe_context
*pipe
,
96 struct pipe_resource
*resource
,
99 const struct pipe_box
*box
,
100 struct pipe_transfer
**transfer
)
102 struct swr_screen
*screen
= swr_screen(pipe
->screen
);
103 struct swr_resource
*spr
= swr_resource(resource
);
104 struct pipe_transfer
*pt
;
105 enum pipe_format format
= resource
->format
;
108 assert(level
<= resource
->last_level
);
110 /* If mapping an attached rendertarget, store tiles to surface and set
111 * postStoreTileState to SWR_TILE_INVALID so tiles get reloaded on next use
112 * and nothing needs to be done at unmap. */
113 swr_store_dirty_resource(pipe
, resource
, SWR_TILE_INVALID
);
115 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
116 /* If resource is in use, finish fence before mapping.
117 * Unless requested not to block, then if not done return NULL map */
118 if (usage
& PIPE_TRANSFER_DONTBLOCK
) {
119 if (swr_is_fence_pending(screen
->flush_fence
))
123 /* But, if there's no fence pending, submit one.
124 * XXX: Remove once draw timestamps are finished. */
125 if (!swr_is_fence_pending(screen
->flush_fence
))
126 swr_fence_submit(swr_context(pipe
), screen
->flush_fence
);
128 swr_fence_finish(pipe
->screen
, NULL
, screen
->flush_fence
, 0);
129 swr_resource_unused(resource
);
134 pt
= CALLOC_STRUCT(pipe_transfer
);
137 pipe_resource_reference(&pt
->resource
, resource
);
138 pt
->usage
= (pipe_transfer_usage
)usage
;
141 pt
->stride
= spr
->swr
.pitch
;
142 pt
->layer_stride
= spr
->swr
.qpitch
* spr
->swr
.pitch
;
144 /* if we're mapping the depth/stencil, copy in stencil for the section
147 if (usage
& PIPE_TRANSFER_READ
&& spr
->has_depth
&& spr
->has_stencil
) {
149 for (int z
= box
->z
; z
< box
->z
+ box
->depth
; z
++) {
150 zbase
= (z
* spr
->swr
.qpitch
+ box
->y
) * spr
->swr
.pitch
+
151 spr
->mip_offsets
[level
];
152 sbase
= (z
* spr
->secondary
.qpitch
+ box
->y
) * spr
->secondary
.pitch
+
153 spr
->secondary_mip_offsets
[level
];
154 for (int y
= box
->y
; y
< box
->y
+ box
->height
; y
++) {
155 if (spr
->base
.format
== PIPE_FORMAT_Z24_UNORM_S8_UINT
) {
156 for (int x
= box
->x
; x
< box
->x
+ box
->width
; x
++)
157 spr
->swr
.pBaseAddress
[zbase
+ 4 * x
+ 3] =
158 spr
->secondary
.pBaseAddress
[sbase
+ x
];
159 } else if (spr
->base
.format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
) {
160 for (int x
= box
->x
; x
< box
->x
+ box
->width
; x
++)
161 spr
->swr
.pBaseAddress
[zbase
+ 8 * x
+ 4] =
162 spr
->secondary
.pBaseAddress
[sbase
+ x
];
164 zbase
+= spr
->swr
.pitch
;
165 sbase
+= spr
->secondary
.pitch
;
170 unsigned offset
= box
->z
* pt
->layer_stride
+
171 util_format_get_nblocksy(format
, box
->y
) * pt
->stride
+
172 util_format_get_stride(format
, box
->x
);
176 return spr
->swr
.pBaseAddress
+ offset
+ spr
->mip_offsets
[level
];
180 swr_transfer_flush_region(struct pipe_context
*pipe
,
181 struct pipe_transfer
*transfer
,
182 const struct pipe_box
*flush_box
)
184 assert(transfer
->resource
);
185 assert(transfer
->usage
& PIPE_TRANSFER_WRITE
);
187 struct swr_resource
*spr
= swr_resource(transfer
->resource
);
188 if (!spr
->has_depth
|| !spr
->has_stencil
)
192 struct pipe_box box
= *flush_box
;
193 box
.x
+= transfer
->box
.x
;
194 box
.y
+= transfer
->box
.y
;
195 box
.z
+= transfer
->box
.z
;
196 for (int z
= box
.z
; z
< box
.z
+ box
.depth
; z
++) {
197 zbase
= (z
* spr
->swr
.qpitch
+ box
.y
) * spr
->swr
.pitch
+
198 spr
->mip_offsets
[transfer
->level
];
199 sbase
= (z
* spr
->secondary
.qpitch
+ box
.y
) * spr
->secondary
.pitch
+
200 spr
->secondary_mip_offsets
[transfer
->level
];
201 for (int y
= box
.y
; y
< box
.y
+ box
.height
; y
++) {
202 if (spr
->base
.format
== PIPE_FORMAT_Z24_UNORM_S8_UINT
) {
203 for (int x
= box
.x
; x
< box
.x
+ box
.width
; x
++)
204 spr
->secondary
.pBaseAddress
[sbase
+ x
] =
205 spr
->swr
.pBaseAddress
[zbase
+ 4 * x
+ 3];
206 } else if (spr
->base
.format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
) {
207 for (int x
= box
.x
; x
< box
.x
+ box
.width
; x
++)
208 spr
->secondary
.pBaseAddress
[sbase
+ x
] =
209 spr
->swr
.pBaseAddress
[zbase
+ 8 * x
+ 4];
211 zbase
+= spr
->swr
.pitch
;
212 sbase
+= spr
->secondary
.pitch
;
218 swr_transfer_unmap(struct pipe_context
*pipe
, struct pipe_transfer
*transfer
)
220 assert(transfer
->resource
);
222 struct swr_resource
*spr
= swr_resource(transfer
->resource
);
223 /* if we're mapping the depth/stencil, copy in stencil for the section
226 if (transfer
->usage
& PIPE_TRANSFER_WRITE
&&
227 !(transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
) &&
228 spr
->has_depth
&& spr
->has_stencil
) {
230 u_box_3d(0, 0, 0, transfer
->box
.width
, transfer
->box
.height
,
231 transfer
->box
.depth
, &box
);
232 swr_transfer_flush_region(pipe
, transfer
, &box
);
235 pipe_resource_reference(&transfer
->resource
, NULL
);
241 swr_resource_copy(struct pipe_context
*pipe
,
242 struct pipe_resource
*dst
,
247 struct pipe_resource
*src
,
249 const struct pipe_box
*src_box
)
251 struct swr_screen
*screen
= swr_screen(pipe
->screen
);
253 /* If either the src or dst is a renderTarget, store tiles before copy */
254 swr_store_dirty_resource(pipe
, src
, SWR_TILE_RESOLVED
);
255 swr_store_dirty_resource(pipe
, dst
, SWR_TILE_RESOLVED
);
257 swr_fence_finish(pipe
->screen
, NULL
, screen
->flush_fence
, 0);
258 swr_resource_unused(src
);
259 swr_resource_unused(dst
);
261 if ((dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
)
262 || (dst
->target
!= PIPE_BUFFER
&& src
->target
!= PIPE_BUFFER
)) {
263 util_resource_copy_region(
264 pipe
, dst
, dst_level
, dstx
, dsty
, dstz
, src
, src_level
, src_box
);
268 debug_printf("unhandled swr_resource_copy\n");
273 swr_blit(struct pipe_context
*pipe
, const struct pipe_blit_info
*blit_info
)
275 struct swr_context
*ctx
= swr_context(pipe
);
276 struct pipe_blit_info info
= *blit_info
;
278 if (blit_info
->render_condition_enable
&& !swr_check_render_cond(pipe
))
281 if (info
.src
.resource
->nr_samples
> 1 && info
.dst
.resource
->nr_samples
<= 1
282 && !util_format_is_depth_or_stencil(info
.src
.resource
->format
)
283 && !util_format_is_pure_integer(info
.src
.resource
->format
)) {
284 debug_printf("swr: color resolve unimplemented\n");
288 if (util_try_blit_via_copy_region(pipe
, &info
)) {
292 if (info
.mask
& PIPE_MASK_S
) {
293 debug_printf("swr: cannot blit stencil, skipping\n");
294 info
.mask
&= ~PIPE_MASK_S
;
297 if (!util_blitter_is_blit_supported(ctx
->blitter
, &info
)) {
298 debug_printf("swr: blit unsupported %s -> %s\n",
299 util_format_short_name(info
.src
.resource
->format
),
300 util_format_short_name(info
.dst
.resource
->format
));
304 /* XXX turn off occlusion and streamout queries */
306 util_blitter_save_vertex_buffer_slot(ctx
->blitter
, ctx
->vertex_buffer
);
307 util_blitter_save_vertex_elements(ctx
->blitter
, (void *)ctx
->velems
);
308 util_blitter_save_vertex_shader(ctx
->blitter
, (void *)ctx
->vs
);
309 /*util_blitter_save_geometry_shader(ctx->blitter, (void*)ctx->gs);*/
310 util_blitter_save_so_targets(
313 (struct pipe_stream_output_target
**)ctx
->so_targets
);
314 util_blitter_save_rasterizer(ctx
->blitter
, (void *)ctx
->rasterizer
);
315 util_blitter_save_viewport(ctx
->blitter
, &ctx
->viewport
);
316 util_blitter_save_scissor(ctx
->blitter
, &ctx
->scissor
);
317 util_blitter_save_fragment_shader(ctx
->blitter
, ctx
->fs
);
318 util_blitter_save_blend(ctx
->blitter
, (void *)ctx
->blend
);
319 util_blitter_save_depth_stencil_alpha(ctx
->blitter
,
320 (void *)ctx
->depth_stencil
);
321 util_blitter_save_stencil_ref(ctx
->blitter
, &ctx
->stencil_ref
);
322 util_blitter_save_sample_mask(ctx
->blitter
, ctx
->sample_mask
);
323 util_blitter_save_framebuffer(ctx
->blitter
, &ctx
->framebuffer
);
324 util_blitter_save_fragment_sampler_states(
326 ctx
->num_samplers
[PIPE_SHADER_FRAGMENT
],
327 (void **)ctx
->samplers
[PIPE_SHADER_FRAGMENT
]);
328 util_blitter_save_fragment_sampler_views(
330 ctx
->num_sampler_views
[PIPE_SHADER_FRAGMENT
],
331 ctx
->sampler_views
[PIPE_SHADER_FRAGMENT
]);
332 util_blitter_save_render_condition(ctx
->blitter
,
333 ctx
->render_cond_query
,
334 ctx
->render_cond_cond
,
335 ctx
->render_cond_mode
);
337 util_blitter_blit(ctx
->blitter
, &info
);
342 swr_destroy(struct pipe_context
*pipe
)
344 struct swr_context
*ctx
= swr_context(pipe
);
345 struct swr_screen
*screen
= swr_screen(pipe
->screen
);
348 util_blitter_destroy(ctx
->blitter
);
350 /* Idle core before deleting context */
351 SwrWaitForIdle(ctx
->swrContext
);
353 for (unsigned i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
354 pipe_surface_reference(&ctx
->framebuffer
.cbufs
[i
], NULL
);
357 pipe_surface_reference(&ctx
->framebuffer
.zsbuf
, NULL
);
359 for (unsigned i
= 0; i
< ARRAY_SIZE(ctx
->sampler_views
[0]); i
++) {
360 pipe_sampler_view_reference(&ctx
->sampler_views
[PIPE_SHADER_FRAGMENT
][i
], NULL
);
363 for (unsigned i
= 0; i
< ARRAY_SIZE(ctx
->sampler_views
[0]); i
++) {
364 pipe_sampler_view_reference(&ctx
->sampler_views
[PIPE_SHADER_VERTEX
][i
], NULL
);
368 SwrDestroyContext(ctx
->swrContext
);
370 delete ctx
->blendJIT
;
372 swr_destroy_scratch_buffers(ctx
);
374 /* Only update screen->pipe if current context is being destroyed */
376 if (screen
->pipe
== pipe
)
384 swr_render_condition(struct pipe_context
*pipe
,
385 struct pipe_query
*query
,
389 struct swr_context
*ctx
= swr_context(pipe
);
391 ctx
->render_cond_query
= query
;
392 ctx
->render_cond_mode
= mode
;
393 ctx
->render_cond_cond
= condition
;
397 swr_UpdateStats(HANDLE hPrivateContext
, const SWR_STATS
*pStats
)
399 swr_draw_context
*pDC
= (swr_draw_context
*)hPrivateContext
;
404 struct swr_query_result
*pqr
= (struct swr_query_result
*)pDC
->pStats
;
406 SWR_STATS
*pSwrStats
= &pqr
->core
;
408 pSwrStats
->DepthPassCount
+= pStats
->DepthPassCount
;
409 pSwrStats
->PsInvocations
+= pStats
->PsInvocations
;
410 pSwrStats
->CsInvocations
+= pStats
->CsInvocations
;
414 swr_UpdateStatsFE(HANDLE hPrivateContext
, const SWR_STATS_FE
*pStats
)
416 swr_draw_context
*pDC
= (swr_draw_context
*)hPrivateContext
;
421 struct swr_query_result
*pqr
= (struct swr_query_result
*)pDC
->pStats
;
423 SWR_STATS_FE
*pSwrStats
= &pqr
->coreFE
;
424 p_atomic_add(&pSwrStats
->IaVertices
, pStats
->IaVertices
);
425 p_atomic_add(&pSwrStats
->IaPrimitives
, pStats
->IaPrimitives
);
426 p_atomic_add(&pSwrStats
->VsInvocations
, pStats
->VsInvocations
);
427 p_atomic_add(&pSwrStats
->HsInvocations
, pStats
->HsInvocations
);
428 p_atomic_add(&pSwrStats
->DsInvocations
, pStats
->DsInvocations
);
429 p_atomic_add(&pSwrStats
->GsInvocations
, pStats
->GsInvocations
);
430 p_atomic_add(&pSwrStats
->CInvocations
, pStats
->CInvocations
);
431 p_atomic_add(&pSwrStats
->CPrimitives
, pStats
->CPrimitives
);
432 p_atomic_add(&pSwrStats
->GsPrimitives
, pStats
->GsPrimitives
);
434 for (unsigned i
= 0; i
< 4; i
++) {
435 p_atomic_add(&pSwrStats
->SoPrimStorageNeeded
[i
],
436 pStats
->SoPrimStorageNeeded
[i
]);
437 p_atomic_add(&pSwrStats
->SoNumPrimsWritten
[i
],
438 pStats
->SoNumPrimsWritten
[i
]);
442 struct pipe_context
*
443 swr_create_context(struct pipe_screen
*p_screen
, void *priv
, unsigned flags
)
445 struct swr_context
*ctx
= CALLOC_STRUCT(swr_context
);
447 new std::unordered_map
<BLEND_COMPILE_STATE
, PFN_BLEND_JIT_FUNC
>;
449 SWR_CREATECONTEXT_INFO createInfo
;
450 memset(&createInfo
, 0, sizeof(createInfo
));
451 createInfo
.privateStateSize
= sizeof(swr_draw_context
);
452 createInfo
.pfnLoadTile
= swr_LoadHotTile
;
453 createInfo
.pfnStoreTile
= swr_StoreHotTile
;
454 createInfo
.pfnClearTile
= swr_StoreHotTileClear
;
455 createInfo
.pfnUpdateStats
= swr_UpdateStats
;
456 createInfo
.pfnUpdateStatsFE
= swr_UpdateStatsFE
;
457 ctx
->swrContext
= SwrCreateContext(&createInfo
);
459 /* Init Load/Store/ClearTiles Tables */
460 swr_InitMemoryModule();
462 InitBackendFuncTables();
464 if (ctx
->swrContext
== NULL
)
467 ctx
->pipe
.screen
= p_screen
;
468 ctx
->pipe
.destroy
= swr_destroy
;
469 ctx
->pipe
.priv
= priv
;
470 ctx
->pipe
.create_surface
= swr_create_surface
;
471 ctx
->pipe
.surface_destroy
= swr_surface_destroy
;
472 ctx
->pipe
.transfer_map
= swr_transfer_map
;
473 ctx
->pipe
.transfer_unmap
= swr_transfer_unmap
;
474 ctx
->pipe
.transfer_flush_region
= swr_transfer_flush_region
;
476 ctx
->pipe
.buffer_subdata
= u_default_buffer_subdata
;
477 ctx
->pipe
.texture_subdata
= u_default_texture_subdata
;
479 ctx
->pipe
.resource_copy_region
= swr_resource_copy
;
480 ctx
->pipe
.render_condition
= swr_render_condition
;
482 swr_state_init(&ctx
->pipe
);
483 swr_clear_init(&ctx
->pipe
);
484 swr_draw_init(&ctx
->pipe
);
485 swr_query_init(&ctx
->pipe
);
487 ctx
->pipe
.blit
= swr_blit
;
488 ctx
->blitter
= util_blitter_create(&ctx
->pipe
);
492 swr_init_scratch_buffers(ctx
);
497 /* Should really validate the init steps and fail gracefully */
498 swr_destroy(&ctx
->pipe
);