nvfx: fix the temporary copying logic and add asserts
[mesa.git] / src / gallium / drivers / nvfx / nvfx_surface.c
1
2 /**************************************************************************
3 *
4 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 #include "pipe/p_context.h"
30 #include "pipe/p_format.h"
31 #include "util/u_format.h"
32 #include "util/u_math.h"
33 #include "util/u_memory.h"
34 #include "util/u_pack_color.h"
35 #include "util/u_blitter.h"
36
37 #include "nouveau/nouveau_winsys.h"
38 #include "nouveau/nouveau_screen.h"
39 #include "nvfx_context.h"
40 #include "nvfx_screen.h"
41 #include "nvfx_resource.h"
42 #include "nv04_2d.h"
43
44 #include <nouveau/nouveau_bo.h>
45
46 static INLINE void
47 nvfx_region_set_format(struct nv04_region* rgn, enum pipe_format format)
48 {
49 unsigned bits = util_format_get_blocksizebits(format);
50 switch(bits)
51 {
52 case 8:
53 rgn->bpps = 0;
54 break;
55 case 16:
56 rgn->bpps = 1;
57 break;
58 case 32:
59 rgn->bpps = 2;
60 break;
61 default:
62 {
63 int shift;
64 assert(util_is_power_of_two(bits));
65 shift = util_logbase2(bits) - 3;
66 assert(shift >= 2);
67 rgn->bpps = 2;
68 shift -= 2;
69
70 rgn->x = util_format_get_nblocksx(format, rgn->x) << shift;
71 rgn->y = util_format_get_nblocksy(format, rgn->y);
72 }
73 }
74 }
75
76 static INLINE void
77 nvfx_region_fixup_swizzled(struct nv04_region* rgn, unsigned zslice, unsigned width, unsigned height, unsigned depth)
78 {
79 // TODO: move this code to surface creation?
80 if((depth <= 1) && (height <= 1 || width <= 2))
81 rgn->pitch = width << rgn->bpps;
82 else if(depth > 1 && height <= 2 && width <= 2)
83 {
84 rgn->pitch = width << rgn->bpps;
85 rgn->offset += (zslice * width * height) << rgn->bpps;
86 }
87 else
88 {
89 rgn->pitch = 0;
90 rgn->z = zslice;
91 rgn->w = width;
92 rgn->h = height;
93 rgn->d = depth;
94 }
95 }
96
97 static INLINE void
98 nvfx_region_init_for_surface(struct nv04_region* rgn, struct nvfx_surface* surf, unsigned x, unsigned y, bool for_write)
99 {
100 rgn->x = x;
101 rgn->y = y;
102 rgn->z = 0;
103 nvfx_region_set_format(rgn, surf->base.base.format);
104
105 if(surf->temp)
106 {
107 rgn->bo = surf->temp->base.bo;
108 rgn->offset = 0;
109 rgn->pitch = surf->temp->linear_pitch;
110
111 if(for_write)
112 util_dirty_surface_set_dirty(nvfx_surface_get_dirty_surfaces(&surf->base.base), &surf->base);
113 } else {
114 rgn->bo = ((struct nvfx_resource*)surf->base.base.texture)->bo;
115 rgn->offset = surf->base.base.offset;
116 rgn->pitch = surf->pitch;
117
118 if(!(surf->base.base.texture->flags & NVFX_RESOURCE_FLAG_LINEAR))
119 nvfx_region_fixup_swizzled(rgn, surf->base.base.zslice, surf->base.base.width, surf->base.base.height, u_minify(surf->base.base.texture->depth0, surf->base.base.level));
120 }
121 }
122
123 static INLINE void
124 nvfx_region_init_for_subresource(struct nv04_region* rgn, struct pipe_resource* pt, struct pipe_subresource sub, unsigned x, unsigned y, unsigned z, bool for_write)
125 {
126 if(pt->target != PIPE_BUFFER)
127 {
128 struct nvfx_surface* ns = (struct nvfx_surface*)util_surfaces_peek(&((struct nvfx_miptree*)pt)->surfaces, pt, sub.face, sub.level, z);
129 if(ns && util_dirty_surface_is_dirty(&ns->base))
130 {
131 nvfx_region_init_for_surface(rgn, ns, x, y, for_write);
132 return;
133 }
134 }
135
136 rgn->bo = ((struct nvfx_resource*)pt)->bo;
137 rgn->offset = nvfx_subresource_offset(pt, sub.face, sub.level, z);
138 rgn->pitch = nvfx_subresource_pitch(pt, sub.level);
139 rgn->x = x;
140 rgn->y = y;
141 rgn->z = 0;
142
143 nvfx_region_set_format(rgn, pt->format);
144 if(!(pt->flags & NVFX_RESOURCE_FLAG_LINEAR))
145 nvfx_region_fixup_swizzled(rgn, z, u_minify(pt->width0, sub.level), u_minify(pt->height0, sub.level), u_minify(pt->depth0, sub.level));
146 }
147
148 // TODO: actually test this for all formats, it's probably wrong for some...
149
150 static INLINE int
151 nvfx_surface_format(enum pipe_format format)
152 {
153 switch(util_format_get_blocksize(format)) {
154 case 1:
155 return NV04_CONTEXT_SURFACES_2D_FORMAT_Y8;
156 case 2:
157 //return NV04_CONTEXT_SURFACES_2D_FORMAT_Y16;
158 return NV04_CONTEXT_SURFACES_2D_FORMAT_R5G6B5;
159 case 4:
160 //if(format == PIPE_FORMAT_B8G8R8X8_UNORM || format == PIPE_FORMAT_B8G8R8A8_UNORM)
161 return NV04_CONTEXT_SURFACES_2D_FORMAT_A8R8G8B8;
162 //else
163 // return NV04_CONTEXT_SURFACES_2D_FORMAT_Y32;
164 default:
165 return -1;
166 }
167 }
168
169 static INLINE int
170 nv04_scaled_image_format(enum pipe_format format)
171 {
172 switch(util_format_get_blocksize(format)) {
173 case 1:
174 return NV03_SCALED_IMAGE_FROM_MEMORY_COLOR_FORMAT_Y8;
175 case 2:
176 //if(format == PIPE_FORMAT_B5G5R5A1_UNORM)
177 // return NV03_SCALED_IMAGE_FROM_MEMORY_COLOR_FORMAT_A1R5G5B5;
178 //else
179 return NV03_SCALED_IMAGE_FROM_MEMORY_COLOR_FORMAT_R5G6B5;
180 case 4:
181 if(format == PIPE_FORMAT_B8G8R8X8_UNORM)
182 return NV03_SCALED_IMAGE_FROM_MEMORY_COLOR_FORMAT_X8R8G8B8;
183 else
184 return NV03_SCALED_IMAGE_FROM_MEMORY_COLOR_FORMAT_A8R8G8B8;
185 default:
186 return -1;
187 }
188 }
189
190 // don't save index buffer because blitter doesn't setit
191 static struct blitter_context*
192 nvfx_get_blitter(struct pipe_context* pipe, int copy)
193 {
194 struct nvfx_context* nvfx = nvfx_context(pipe);
195
196 assert(nvfx->blitters_in_use < Elements(nvfx->blitter));
197
198 struct blitter_context** pblitter = &nvfx->blitter[nvfx->blitters_in_use++];
199 if(!*pblitter)
200 *pblitter = util_blitter_create(pipe);
201 struct blitter_context* blitter = *pblitter;
202
203 util_blitter_save_blend(blitter, nvfx->blend);
204 util_blitter_save_depth_stencil_alpha(blitter, nvfx->zsa);
205 util_blitter_save_stencil_ref(blitter, &nvfx->stencil_ref);
206 util_blitter_save_rasterizer(blitter, nvfx->rasterizer);
207 util_blitter_save_fragment_shader(blitter, nvfx->fragprog);
208 util_blitter_save_vertex_shader(blitter, nvfx->vertprog);
209 util_blitter_save_viewport(blitter, &nvfx->viewport);
210 util_blitter_save_framebuffer(blitter, &nvfx->framebuffer);
211 util_blitter_save_clip(blitter, &nvfx->clip);
212 util_blitter_save_vertex_elements(blitter, nvfx->vtxelt);
213 util_blitter_save_vertex_buffers(blitter, nvfx->vtxbuf_nr, nvfx->vtxbuf);
214
215 if(copy)
216 {
217 util_blitter_save_fragment_sampler_states(blitter, nvfx->nr_samplers, (void**)nvfx->tex_sampler);
218 util_blitter_save_fragment_sampler_views(blitter, nvfx->nr_textures, nvfx->fragment_sampler_views);
219 }
220
221 return blitter;
222 }
223
224 static inline void
225 nvfx_put_blitter(struct pipe_context* pipe, struct blitter_context* blitter)
226 {
227 struct nvfx_context* nvfx = nvfx_context(pipe);
228 --nvfx->blitters_in_use;
229 assert(nvfx->blitters_in_use >= 0);
230 }
231
232 static unsigned
233 nvfx_region_clone(struct nv04_2d_context* ctx, struct nv04_region* rgn, unsigned w, unsigned h, boolean for_read)
234 {
235 unsigned begin = nv04_region_begin(rgn, w, h);
236 unsigned end = nv04_region_end(rgn, w, h);
237 unsigned size = end - begin;
238 struct nouveau_bo* bo = 0;
239 nouveau_bo_new(rgn->bo->device, NOUVEAU_BO_MAP | NOUVEAU_BO_GART, 256, size, &bo);
240
241 if(for_read || (size > ((w * h) << rgn->bpps)))
242 nv04_memcpy(ctx, bo, 0, rgn->bo, rgn->offset + begin, size);
243
244 rgn->bo = bo;
245 rgn->offset = -begin;
246 return begin;
247 }
248
249 static void
250 nvfx_resource_copy_region(struct pipe_context *pipe,
251 struct pipe_resource *dstr, struct pipe_subresource subdst,
252 unsigned dstx, unsigned dsty, unsigned dstz,
253 struct pipe_resource *srcr, struct pipe_subresource subsrc,
254 unsigned srcx, unsigned srcy, unsigned srcz,
255 unsigned w, unsigned h)
256 {
257 static int copy_threshold = -1;
258 struct nv04_2d_context *ctx = nvfx_screen(pipe->screen)->eng2d;
259 struct nv04_region dst, src;
260 int dst_to_gpu;
261 int src_on_gpu;
262 boolean small;
263 int ret;
264
265 if(!w || !h)
266 return;
267
268 if(copy_threshold < 0)
269 copy_threshold = debug_get_num_option("NOUVEAU_COPY_THRESHOLD", 4);
270
271 dst_to_gpu = dstr->usage != PIPE_USAGE_DYNAMIC && dstr->usage != PIPE_USAGE_STAGING;
272 src_on_gpu = nvfx_resource_on_gpu(srcr);
273
274 nvfx_region_init_for_subresource(&dst, dstr, subdst, dstx, dsty, dstz, TRUE);
275 nvfx_region_init_for_subresource(&src, srcr, subsrc, srcx, srcy, srcz, FALSE);
276 w = util_format_get_stride(dstr->format, w) >> dst.bpps;
277 h = util_format_get_nblocksy(dstr->format, h);
278
279 small = (w * h <= copy_threshold);
280 if((!dst_to_gpu || !src_on_gpu) && small)
281 ret = -1; /* use the CPU */
282 else
283 ret = nv04_region_copy_2d(ctx, &dst, &src, w, h,
284 dstr->target == PIPE_BUFFER ? -1 : nvfx_surface_format(dstr->format),
285 dstr->target == PIPE_BUFFER ? -1 : nv04_scaled_image_format(dstr->format),
286 dst_to_gpu, src_on_gpu);
287 if(!ret)
288 {}
289 else if(ret > 0 && dstr->bind & PIPE_BIND_RENDER_TARGET && srcr->bind & PIPE_BIND_SAMPLER_VIEW)
290 {
291 struct blitter_context* blitter = nvfx_get_blitter(pipe, 1);
292 util_blitter_copy_region(blitter, dstr, subdst, dstx, dsty, dstz, srcr, subsrc, srcx, srcy, srcz, w, h, TRUE);
293 nvfx_put_blitter(pipe, blitter);
294 }
295 else
296 {
297 struct nv04_region dstt = dst;
298 struct nv04_region srct = src;
299 unsigned dstbegin = 0;
300
301 if(!small)
302 {
303 if(src_on_gpu)
304 nvfx_region_clone(ctx, &srct, w, h, TRUE);
305
306 if(dst_to_gpu)
307 dstbegin = nvfx_region_clone(ctx, &dstt, w, h, FALSE);
308 }
309
310 nv04_region_copy_cpu(&dstt, &srct, w, h);
311
312 if(srct.bo != src.bo)
313 nouveau_screen_bo_release(pipe->screen, srct.bo);
314
315 if(dstt.bo != dst.bo)
316 {
317 nv04_memcpy(ctx, dst.bo, dst.offset + dstbegin, dstt.bo, 0, dstt.bo->size);
318 nouveau_screen_bo_release(pipe->screen, dstt.bo);
319 }
320 }
321 }
322
323 static int
324 nvfx_surface_fill(struct pipe_context* pipe, struct pipe_surface *dsts,
325 unsigned dx, unsigned dy, unsigned w, unsigned h, unsigned value)
326 {
327 struct nv04_2d_context *ctx = nvfx_screen(pipe->screen)->eng2d;
328 struct nv04_region dst;
329 int ret;
330 /* Always try to use the GPU right now, if possible
331 * If the user wanted the surface data on the CPU, he would have cleared with memset (hopefully) */
332
333 // we don't care about interior pixel order since we set all them to the same value
334 nvfx_region_init_for_surface(&dst, (struct nvfx_surface*)dsts, dx, dy, TRUE);
335
336 w = util_format_get_stride(dsts->format, w) >> dst.bpps;
337 h = util_format_get_nblocksy(dsts->format, h);
338
339 ret = nv04_region_fill_2d(ctx, &dst, w, h, value);
340 if(ret > 0 && dsts->texture->bind & PIPE_BIND_RENDER_TARGET)
341 return 1;
342 else if(ret)
343 {
344 struct nv04_region dstt = dst;
345 unsigned dstbegin = 0;
346
347 if(nvfx_resource_on_gpu(dsts->texture))
348 dstbegin = nvfx_region_clone(ctx, &dstt, w, h, FALSE);
349
350 nv04_region_fill_cpu(&dstt, w, h, value);
351
352 if(dstt.bo != dst.bo)
353 {
354 nv04_memcpy(ctx, dst.bo, dst.offset + dstbegin, dstt.bo, 0, dstt.bo->size);
355 nouveau_screen_bo_release(pipe->screen, dstt.bo);
356 }
357 }
358
359 return 0;
360 }
361
362
363 void
364 nvfx_screen_surface_takedown(struct pipe_screen *pscreen)
365 {
366 nv04_2d_context_takedown(nvfx_screen(pscreen)->eng2d);
367 nvfx_screen(pscreen)->eng2d = 0;
368 }
369
370 int
371 nvfx_screen_surface_init(struct pipe_screen *pscreen)
372 {
373 struct nv04_2d_context* ctx = nv04_2d_context_init(nouveau_screen(pscreen)->channel);
374 if(!ctx)
375 return -1;
376 nvfx_screen(pscreen)->eng2d = ctx;
377 return 0;
378 }
379
380 static void
381 nvfx_surface_copy_temp(struct pipe_context* pipe, struct pipe_surface* surf, int to_temp)
382 {
383 struct nvfx_surface* ns = (struct nvfx_surface*)surf;
384 struct pipe_subresource tempsr, surfsr;
385 struct nvfx_context* nvfx = nvfx_context(pipe);
386
387 /* temporarily detach the temp, so it isn't used in place of the actual resource */
388 struct nvfx_miptree* temp = ns->temp;
389 ns->temp = 0;
390
391 // TODO: we really should do this validation before setting these variable in draw calls
392 unsigned use_vertex_buffers = nvfx->use_vertex_buffers;
393 boolean use_index_buffer = nvfx->use_index_buffer;
394 unsigned base_vertex = nvfx->base_vertex;
395
396 tempsr.face = 0;
397 tempsr.level = 0;
398 surfsr.face = surf->face;
399 surfsr.level = surf->level;
400
401 if(to_temp)
402 nvfx_resource_copy_region(pipe, &temp->base.base, tempsr, 0, 0, 0, surf->texture, surfsr, 0, 0, surf->zslice, surf->width, surf->height);
403 else
404 nvfx_resource_copy_region(pipe, surf->texture, surfsr, 0, 0, surf->zslice, &temp->base.base, tempsr, 0, 0, 0, surf->width, surf->height);
405
406 /* If this triggers, it probably means we attempted to use the blitter
407 * but failed due to non-renderability of the target.
408 * Obviously, this would lead to infinite recursion if supported. */
409 assert(!ns->temp);
410
411 ns->temp = temp;
412
413 nvfx->use_vertex_buffers = use_vertex_buffers;
414 nvfx->use_index_buffer = use_index_buffer;
415 nvfx->base_vertex = base_vertex;
416
417 nvfx->dirty |= NVFX_NEW_ARRAYS;
418 nvfx->draw_dirty |= NVFX_NEW_ARRAYS;
419 }
420
421 void
422 nvfx_surface_create_temp(struct pipe_context* pipe, struct pipe_surface* surf)
423 {
424 struct nvfx_surface* ns = (struct nvfx_surface*)surf;
425 struct pipe_resource template;
426 memset(&template, 0, sizeof(struct pipe_resource));
427 template.target = PIPE_TEXTURE_2D;
428 template.format = surf->format;
429 template.width0 = surf->width;
430 template.height0 = surf->height;
431 template.depth0 = 1;
432 template.nr_samples = surf->texture->nr_samples;
433 template.flags = NVFX_RESOURCE_FLAG_LINEAR;
434
435 assert(!ns->temp && !util_dirty_surface_is_dirty(&ns->base));
436
437 ns->temp = (struct nvfx_miptree*)nvfx_miptree_create(pipe->screen, &template);
438 nvfx_surface_copy_temp(pipe, surf, 1);
439 }
440
441 void
442 nvfx_surface_flush(struct pipe_context* pipe, struct pipe_surface* surf)
443 {
444 struct nvfx_context* nvfx = (struct nvfx_context*)pipe;
445 struct nvfx_surface* ns = (struct nvfx_surface*)surf;
446 boolean bound = FALSE;
447
448 nvfx_surface_copy_temp(pipe, surf, 0);
449
450 util_dirty_surface_set_clean(nvfx_surface_get_dirty_surfaces(surf), &ns->base);
451
452 if(nvfx->framebuffer.zsbuf == surf)
453 bound = TRUE;
454 else
455 {
456 for(unsigned i = 0; i < nvfx->framebuffer.nr_cbufs; ++i)
457 {
458 if(nvfx->framebuffer.cbufs[i] == surf)
459 {
460 bound = TRUE;
461 break;
462 }
463 }
464 }
465
466 if(!bound)
467 pipe_resource_reference((struct pipe_resource**)&ns->temp, 0);
468 }
469
470 static void
471 nvfx_clear_render_target(struct pipe_context *pipe,
472 struct pipe_surface *dst,
473 const float *rgba,
474 unsigned dstx, unsigned dsty,
475 unsigned width, unsigned height)
476 {
477 union util_color uc;
478 util_pack_color(rgba, dst->format, &uc);
479
480 if(util_format_get_blocksizebits(dst->format) > 32
481 || nvfx_surface_fill(pipe, dst, dstx, dsty, width, height, uc.ui))
482 {
483 // TODO: probably should use hardware clear here instead if possible
484 struct blitter_context* blitter = nvfx_get_blitter(pipe, 0);
485 util_blitter_clear_render_target(blitter, dst, rgba, dstx, dsty, width, height);
486 nvfx_put_blitter(pipe, blitter);
487 }
488 }
489
490 static void
491 nvfx_clear_depth_stencil(struct pipe_context *pipe,
492 struct pipe_surface *dst,
493 unsigned clear_flags,
494 double depth,
495 unsigned stencil,
496 unsigned dstx, unsigned dsty,
497 unsigned width, unsigned height)
498 {
499 if(util_format_get_blocksizebits(dst->format) > 32
500 || nvfx_surface_fill(pipe, dst, dstx, dsty, width, height, util_pack_z_stencil(dst->format, depth, stencil)))
501 {
502 // TODO: probably should use hardware clear here instead if possible
503 struct blitter_context* blitter = nvfx_get_blitter(pipe, 0);
504 util_blitter_clear_depth_stencil(blitter, dst, clear_flags, depth, stencil, dstx, dsty, width, height);
505 nvfx_put_blitter(pipe, blitter);
506 }
507 }
508
509
510 void
511 nvfx_init_surface_functions(struct nvfx_context *nvfx)
512 {
513 nvfx->pipe.resource_copy_region = nvfx_resource_copy_region;
514 nvfx->pipe.clear_render_target = nvfx_clear_render_target;
515 nvfx->pipe.clear_depth_stencil = nvfx_clear_depth_stencil;
516 }