gallium: split transfer_inline_write into buffer and texture callbacks
[mesa.git] / src / gallium / drivers / swr / swr_context.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 "util/u_memory.h"
25 #include "util/u_inlines.h"
26 #include "util/u_format.h"
27
28 extern "C" {
29 #include "util/u_transfer.h"
30 #include "util/u_surface.h"
31 }
32
33 #include "swr_context.h"
34 #include "swr_memory.h"
35 #include "swr_screen.h"
36 #include "swr_resource.h"
37 #include "swr_scratch.h"
38 #include "swr_query.h"
39 #include "swr_fence.h"
40
41 #include "api.h"
42 #include "backend.h"
43
44 static struct pipe_surface *
45 swr_create_surface(struct pipe_context *pipe,
46 struct pipe_resource *pt,
47 const struct pipe_surface *surf_tmpl)
48 {
49 struct pipe_surface *ps;
50
51 ps = CALLOC_STRUCT(pipe_surface);
52 if (ps) {
53 pipe_reference_init(&ps->reference, 1);
54 pipe_resource_reference(&ps->texture, pt);
55 ps->context = pipe;
56 ps->format = surf_tmpl->format;
57 if (pt->target != PIPE_BUFFER) {
58 assert(surf_tmpl->u.tex.level <= pt->last_level);
59 ps->width = u_minify(pt->width0, surf_tmpl->u.tex.level);
60 ps->height = u_minify(pt->height0, surf_tmpl->u.tex.level);
61 ps->u.tex.level = surf_tmpl->u.tex.level;
62 ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
63 ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
64 if (ps->u.tex.first_layer != ps->u.tex.last_layer) {
65 debug_printf("creating surface with multiple layers, rendering "
66 "to first layer only\n");
67 }
68 } else {
69 /* setting width as number of elements should get us correct
70 * renderbuffer width */
71 ps->width = surf_tmpl->u.buf.last_element
72 - surf_tmpl->u.buf.first_element + 1;
73 ps->height = pt->height0;
74 ps->u.buf.first_element = surf_tmpl->u.buf.first_element;
75 ps->u.buf.last_element = surf_tmpl->u.buf.last_element;
76 assert(ps->u.buf.first_element <= ps->u.buf.last_element);
77 assert(ps->u.buf.last_element < ps->width);
78 }
79 }
80 return ps;
81 }
82
83 static void
84 swr_surface_destroy(struct pipe_context *pipe, struct pipe_surface *surf)
85 {
86 assert(surf->texture);
87 struct pipe_resource *resource = surf->texture;
88
89 /* If the resource has been drawn to, store tiles. */
90 swr_store_dirty_resource(pipe, resource, SWR_TILE_RESOLVED);
91
92 pipe_resource_reference(&resource, NULL);
93 FREE(surf);
94 }
95
96
97 static void *
98 swr_transfer_map(struct pipe_context *pipe,
99 struct pipe_resource *resource,
100 unsigned level,
101 unsigned usage,
102 const struct pipe_box *box,
103 struct pipe_transfer **transfer)
104 {
105 struct swr_screen *screen = swr_screen(pipe->screen);
106 struct swr_resource *spr = swr_resource(resource);
107 struct pipe_transfer *pt;
108 enum pipe_format format = resource->format;
109
110 assert(resource);
111 assert(level <= resource->last_level);
112
113 /* If mapping an attached rendertarget, store tiles to surface and set
114 * postStoreTileState to SWR_TILE_INVALID so tiles get reloaded on next use
115 * and nothing needs to be done at unmap. */
116 swr_store_dirty_resource(pipe, resource, SWR_TILE_INVALID);
117
118 if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
119 /* If resource is in use, finish fence before mapping.
120 * Unless requested not to block, then if not done return NULL map */
121 if (usage & PIPE_TRANSFER_DONTBLOCK) {
122 if (swr_is_fence_pending(screen->flush_fence))
123 return NULL;
124 } else {
125 if (spr->status) {
126 /* But, if there's no fence pending, submit one.
127 * XXX: Remove once draw timestamps are finished. */
128 if (!swr_is_fence_pending(screen->flush_fence))
129 swr_fence_submit(swr_context(pipe), screen->flush_fence);
130
131 swr_fence_finish(pipe->screen, screen->flush_fence, 0);
132 swr_resource_unused(resource);
133 }
134 }
135 }
136
137 pt = CALLOC_STRUCT(pipe_transfer);
138 if (!pt)
139 return NULL;
140 pipe_resource_reference(&pt->resource, resource);
141 pt->level = level;
142 pt->box = *box;
143 pt->stride = spr->row_stride[level];
144 pt->layer_stride = spr->img_stride[level];
145
146 /* if we're mapping the depth/stencil, copy in stencil */
147 if (spr->base.format == PIPE_FORMAT_Z24_UNORM_S8_UINT
148 && spr->has_stencil) {
149 for (unsigned i = 0; i < spr->alignedWidth * spr->alignedHeight; i++) {
150 spr->swr.pBaseAddress[4 * i + 3] = spr->secondary.pBaseAddress[i];
151 }
152 } else if (spr->base.format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
153 && spr->has_stencil) {
154 for (unsigned i = 0; i < spr->alignedWidth * spr->alignedHeight; i++) {
155 spr->swr.pBaseAddress[8 * i + 4] = spr->secondary.pBaseAddress[i];
156 }
157 }
158
159 unsigned offset = box->z * pt->layer_stride + box->y * pt->stride
160 + box->x * util_format_get_blocksize(format);
161
162 *transfer = pt;
163
164 return spr->swr.pBaseAddress + offset + spr->mip_offsets[level];
165 }
166
167 static void
168 swr_transfer_unmap(struct pipe_context *pipe, struct pipe_transfer *transfer)
169 {
170 assert(transfer->resource);
171
172 struct swr_resource *res = swr_resource(transfer->resource);
173 /* if we're mapping the depth/stencil, copy out stencil */
174 if (res->base.format == PIPE_FORMAT_Z24_UNORM_S8_UINT
175 && res->has_stencil) {
176 for (unsigned i = 0; i < res->alignedWidth * res->alignedHeight; i++) {
177 res->secondary.pBaseAddress[i] = res->swr.pBaseAddress[4 * i + 3];
178 }
179 } else if (res->base.format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
180 && res->has_stencil) {
181 for (unsigned i = 0; i < res->alignedWidth * res->alignedHeight; i++) {
182 res->secondary.pBaseAddress[i] = res->swr.pBaseAddress[8 * i + 4];
183 }
184 }
185
186 pipe_resource_reference(&transfer->resource, NULL);
187 FREE(transfer);
188 }
189
190
191 static void
192 swr_resource_copy(struct pipe_context *pipe,
193 struct pipe_resource *dst,
194 unsigned dst_level,
195 unsigned dstx,
196 unsigned dsty,
197 unsigned dstz,
198 struct pipe_resource *src,
199 unsigned src_level,
200 const struct pipe_box *src_box)
201 {
202 struct swr_screen *screen = swr_screen(pipe->screen);
203
204 /* If either the src or dst is a renderTarget, store tiles before copy */
205 swr_store_dirty_resource(pipe, src, SWR_TILE_RESOLVED);
206 swr_store_dirty_resource(pipe, dst, SWR_TILE_RESOLVED);
207
208 swr_fence_finish(pipe->screen, screen->flush_fence, 0);
209 swr_resource_unused(src);
210 swr_resource_unused(dst);
211
212 if ((dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER)
213 || (dst->target != PIPE_BUFFER && src->target != PIPE_BUFFER)) {
214 util_resource_copy_region(
215 pipe, dst, dst_level, dstx, dsty, dstz, src, src_level, src_box);
216 return;
217 }
218
219 debug_printf("unhandled swr_resource_copy\n");
220 }
221
222
223 static void
224 swr_blit(struct pipe_context *pipe, const struct pipe_blit_info *blit_info)
225 {
226 struct swr_context *ctx = swr_context(pipe);
227 struct pipe_blit_info info = *blit_info;
228
229 if (blit_info->render_condition_enable && !swr_check_render_cond(pipe))
230 return;
231
232 if (info.src.resource->nr_samples > 1 && info.dst.resource->nr_samples <= 1
233 && !util_format_is_depth_or_stencil(info.src.resource->format)
234 && !util_format_is_pure_integer(info.src.resource->format)) {
235 debug_printf("swr: color resolve unimplemented\n");
236 return;
237 }
238
239 if (util_try_blit_via_copy_region(pipe, &info)) {
240 return; /* done */
241 }
242
243 if (info.mask & PIPE_MASK_S) {
244 debug_printf("swr: cannot blit stencil, skipping\n");
245 info.mask &= ~PIPE_MASK_S;
246 }
247
248 if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {
249 debug_printf("swr: blit unsupported %s -> %s\n",
250 util_format_short_name(info.src.resource->format),
251 util_format_short_name(info.dst.resource->format));
252 return;
253 }
254
255 /* XXX turn off occlusion and streamout queries */
256
257 util_blitter_save_vertex_buffer_slot(ctx->blitter, ctx->vertex_buffer);
258 util_blitter_save_vertex_elements(ctx->blitter, (void *)ctx->velems);
259 util_blitter_save_vertex_shader(ctx->blitter, (void *)ctx->vs);
260 /*util_blitter_save_geometry_shader(ctx->blitter, (void*)ctx->gs);*/
261 util_blitter_save_so_targets(
262 ctx->blitter,
263 ctx->num_so_targets,
264 (struct pipe_stream_output_target **)ctx->so_targets);
265 util_blitter_save_rasterizer(ctx->blitter, (void *)ctx->rasterizer);
266 util_blitter_save_viewport(ctx->blitter, &ctx->viewport);
267 util_blitter_save_scissor(ctx->blitter, &ctx->scissor);
268 util_blitter_save_fragment_shader(ctx->blitter, ctx->fs);
269 util_blitter_save_blend(ctx->blitter, (void *)ctx->blend);
270 util_blitter_save_depth_stencil_alpha(ctx->blitter,
271 (void *)ctx->depth_stencil);
272 util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref);
273 util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask);
274 util_blitter_save_framebuffer(ctx->blitter, &ctx->framebuffer);
275 util_blitter_save_fragment_sampler_states(
276 ctx->blitter,
277 ctx->num_samplers[PIPE_SHADER_FRAGMENT],
278 (void **)ctx->samplers[PIPE_SHADER_FRAGMENT]);
279 util_blitter_save_fragment_sampler_views(
280 ctx->blitter,
281 ctx->num_sampler_views[PIPE_SHADER_FRAGMENT],
282 ctx->sampler_views[PIPE_SHADER_FRAGMENT]);
283 util_blitter_save_render_condition(ctx->blitter,
284 ctx->render_cond_query,
285 ctx->render_cond_cond,
286 ctx->render_cond_mode);
287
288 util_blitter_blit(ctx->blitter, &info);
289 }
290
291
292 static void
293 swr_destroy(struct pipe_context *pipe)
294 {
295 struct swr_context *ctx = swr_context(pipe);
296 struct swr_screen *screen = swr_screen(pipe->screen);
297
298 if (ctx->blitter)
299 util_blitter_destroy(ctx->blitter);
300
301 /* Idle core before deleting context */
302 SwrWaitForIdle(ctx->swrContext);
303
304 for (unsigned i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
305 pipe_surface_reference(&ctx->framebuffer.cbufs[i], NULL);
306 }
307
308 pipe_surface_reference(&ctx->framebuffer.zsbuf, NULL);
309
310 for (unsigned i = 0; i < ARRAY_SIZE(ctx->sampler_views[0]); i++) {
311 pipe_sampler_view_reference(&ctx->sampler_views[PIPE_SHADER_FRAGMENT][i], NULL);
312 }
313
314 for (unsigned i = 0; i < ARRAY_SIZE(ctx->sampler_views[0]); i++) {
315 pipe_sampler_view_reference(&ctx->sampler_views[PIPE_SHADER_VERTEX][i], NULL);
316 }
317
318 if (ctx->swrContext)
319 SwrDestroyContext(ctx->swrContext);
320
321 delete ctx->blendJIT;
322
323 swr_destroy_scratch_buffers(ctx);
324
325 /* Only update screen->pipe if current context is being destroyed */
326 assert(screen);
327 if (screen->pipe == pipe)
328 screen->pipe = NULL;
329
330 FREE(ctx);
331 }
332
333
334 static void
335 swr_render_condition(struct pipe_context *pipe,
336 struct pipe_query *query,
337 boolean condition,
338 uint mode)
339 {
340 struct swr_context *ctx = swr_context(pipe);
341
342 ctx->render_cond_query = query;
343 ctx->render_cond_mode = mode;
344 ctx->render_cond_cond = condition;
345 }
346
347 struct pipe_context *
348 swr_create_context(struct pipe_screen *p_screen, void *priv, unsigned flags)
349 {
350 struct swr_context *ctx = CALLOC_STRUCT(swr_context);
351 ctx->blendJIT =
352 new std::unordered_map<BLEND_COMPILE_STATE, PFN_BLEND_JIT_FUNC>;
353
354 SWR_CREATECONTEXT_INFO createInfo;
355 createInfo.driver = GL;
356 createInfo.privateStateSize = sizeof(swr_draw_context);
357 createInfo.pfnLoadTile = swr_LoadHotTile;
358 createInfo.pfnStoreTile = swr_StoreHotTile;
359 createInfo.pfnClearTile = swr_StoreHotTileClear;
360 ctx->swrContext = SwrCreateContext(&createInfo);
361
362 /* Init Load/Store/ClearTiles Tables */
363 swr_InitMemoryModule();
364
365 InitBackendFuncTables();
366
367 if (ctx->swrContext == NULL)
368 goto fail;
369
370 ctx->pipe.screen = p_screen;
371 ctx->pipe.destroy = swr_destroy;
372 ctx->pipe.priv = priv;
373 ctx->pipe.create_surface = swr_create_surface;
374 ctx->pipe.surface_destroy = swr_surface_destroy;
375 ctx->pipe.transfer_map = swr_transfer_map;
376 ctx->pipe.transfer_unmap = swr_transfer_unmap;
377
378 ctx->pipe.transfer_flush_region = u_default_transfer_flush_region;
379 ctx->pipe.buffer_subdata = u_default_buffer_subdata;
380 ctx->pipe.texture_subdata = u_default_texture_subdata;
381
382 ctx->pipe.resource_copy_region = swr_resource_copy;
383 ctx->pipe.render_condition = swr_render_condition;
384
385 swr_state_init(&ctx->pipe);
386 swr_clear_init(&ctx->pipe);
387 swr_draw_init(&ctx->pipe);
388 swr_query_init(&ctx->pipe);
389
390 ctx->pipe.blit = swr_blit;
391 ctx->blitter = util_blitter_create(&ctx->pipe);
392 if (!ctx->blitter)
393 goto fail;
394
395 swr_init_scratch_buffers(ctx);
396
397 return &ctx->pipe;
398
399 fail:
400 /* Should really validate the init steps and fail gracefully */
401 swr_destroy(&ctx->pipe);
402 return NULL;
403 }