2 /**************************************************************************
4 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
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"
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"
44 #include <nouveau/nouveau_bo.h>
47 nvfx_region_set_format(struct nv04_region
* rgn
, enum pipe_format format
)
49 unsigned bits
= util_format_get_blocksizebits(format
);
60 if(format
== PIPE_FORMAT_B5G5R5X1_UNORM
)
65 if(format
== PIPE_FORMAT_R8G8B8X8_UNORM
|| format
== PIPE_FORMAT_B8G8R8X8_UNORM
)
79 rgn
->x
= util_format_get_nblocksx(format
, rgn
->x
) << shift
;
80 rgn
->y
= util_format_get_nblocksy(format
, rgn
->y
);
86 nvfx_region_init_for_surface(struct nv04_region
* rgn
, struct nvfx_surface
* surf
, unsigned x
, unsigned y
, bool for_write
)
94 rgn
->bo
= surf
->temp
->base
.bo
;
96 rgn
->pitch
= surf
->temp
->linear_pitch
;
99 util_dirty_surface_set_dirty(nvfx_surface_get_dirty_surfaces(&surf
->base
.base
), &surf
->base
);
101 rgn
->bo
= ((struct nvfx_resource
*)surf
->base
.base
.texture
)->bo
;
102 rgn
->offset
= surf
->base
.base
.offset
;
104 if(surf
->base
.base
.texture
->flags
& NVFX_RESOURCE_FLAG_LINEAR
)
105 rgn
->pitch
= surf
->pitch
;
109 rgn
->z
= surf
->base
.base
.zslice
;
110 rgn
->w
= surf
->base
.base
.width
;
111 rgn
->h
= surf
->base
.base
.height
;
112 rgn
->d
= u_minify(surf
->base
.base
.texture
->depth0
, surf
->base
.base
.level
);
116 nvfx_region_set_format(rgn
, surf
->base
.base
.format
);
118 nv04_region_try_to_linearize(rgn
);
122 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
)
124 if(pt
->target
!= PIPE_BUFFER
)
126 struct nvfx_surface
* ns
= (struct nvfx_surface
*)util_surfaces_peek(&((struct nvfx_miptree
*)pt
)->surfaces
, pt
, sub
.face
, sub
.level
, z
);
127 if(ns
&& util_dirty_surface_is_dirty(&ns
->base
))
129 nvfx_region_init_for_surface(rgn
, ns
, x
, y
, for_write
);
134 rgn
->bo
= ((struct nvfx_resource
*)pt
)->bo
;
135 rgn
->offset
= nvfx_subresource_offset(pt
, sub
.face
, sub
.level
, z
);
139 if(pt
->flags
& NVFX_RESOURCE_FLAG_LINEAR
)
141 rgn
->pitch
= nvfx_subresource_pitch(pt
, sub
.level
);
148 rgn
->w
= u_minify(pt
->width0
, sub
.level
);
149 rgn
->h
= u_minify(pt
->height0
, sub
.level
);
150 rgn
->d
= u_minify(pt
->depth0
, sub
.level
);
153 nvfx_region_set_format(rgn
, pt
->format
);
155 nv04_region_try_to_linearize(rgn
);
158 // don't save index buffer because blitter doesn't setit
159 static struct blitter_context
*
160 nvfx_get_blitter(struct pipe_context
* pipe
, int copy
)
162 struct nvfx_context
* nvfx
= nvfx_context(pipe
);
164 assert(nvfx
->blitters_in_use
< Elements(nvfx
->blitter
));
166 struct blitter_context
** pblitter
= &nvfx
->blitter
[nvfx
->blitters_in_use
++];
168 *pblitter
= util_blitter_create(pipe
);
169 struct blitter_context
* blitter
= *pblitter
;
171 util_blitter_save_blend(blitter
, nvfx
->blend
);
172 util_blitter_save_depth_stencil_alpha(blitter
, nvfx
->zsa
);
173 util_blitter_save_stencil_ref(blitter
, &nvfx
->stencil_ref
);
174 util_blitter_save_rasterizer(blitter
, nvfx
->rasterizer
);
175 util_blitter_save_fragment_shader(blitter
, nvfx
->fragprog
);
176 util_blitter_save_vertex_shader(blitter
, nvfx
->vertprog
);
177 util_blitter_save_viewport(blitter
, &nvfx
->viewport
);
178 util_blitter_save_framebuffer(blitter
, &nvfx
->framebuffer
);
179 util_blitter_save_clip(blitter
, &nvfx
->clip
);
180 util_blitter_save_vertex_elements(blitter
, nvfx
->vtxelt
);
181 util_blitter_save_vertex_buffers(blitter
, nvfx
->vtxbuf_nr
, nvfx
->vtxbuf
);
185 util_blitter_save_fragment_sampler_states(blitter
, nvfx
->nr_samplers
, (void**)nvfx
->tex_sampler
);
186 util_blitter_save_fragment_sampler_views(blitter
, nvfx
->nr_textures
, nvfx
->fragment_sampler_views
);
193 nvfx_put_blitter(struct pipe_context
* pipe
, struct blitter_context
* blitter
)
195 struct nvfx_context
* nvfx
= nvfx_context(pipe
);
196 --nvfx
->blitters_in_use
;
197 assert(nvfx
->blitters_in_use
>= 0);
201 nvfx_region_clone(struct nv04_2d_context
* ctx
, struct nv04_region
* rgn
, unsigned w
, unsigned h
, boolean for_read
)
203 unsigned begin
= nv04_region_begin(rgn
, w
, h
);
204 unsigned end
= nv04_region_end(rgn
, w
, h
);
205 unsigned size
= end
- begin
;
206 struct nouveau_bo
* bo
= 0;
207 nouveau_bo_new(rgn
->bo
->device
, NOUVEAU_BO_MAP
| NOUVEAU_BO_GART
, 256, size
, &bo
);
209 if(for_read
|| (size
> ((w
* h
) << rgn
->bpps
)))
210 nv04_memcpy(ctx
, bo
, 0, rgn
->bo
, rgn
->offset
+ begin
, size
);
213 rgn
->offset
= -begin
;
218 nvfx_resource_copy_region(struct pipe_context
*pipe
,
219 struct pipe_resource
*dstr
, struct pipe_subresource subdst
,
220 unsigned dstx
, unsigned dsty
, unsigned dstz
,
221 struct pipe_resource
*srcr
, struct pipe_subresource subsrc
,
222 unsigned srcx
, unsigned srcy
, unsigned srcz
,
223 unsigned w
, unsigned h
)
225 static int copy_threshold
= -1;
226 struct nv04_2d_context
*ctx
= nvfx_screen(pipe
->screen
)->eng2d
;
227 struct nv04_region dst
, src
;
236 if(copy_threshold
< 0)
237 copy_threshold
= debug_get_num_option("NOUVEAU_COPY_THRESHOLD", 4);
239 dst_to_gpu
= dstr
->usage
!= PIPE_USAGE_DYNAMIC
&& dstr
->usage
!= PIPE_USAGE_STAGING
;
240 src_on_gpu
= nvfx_resource_on_gpu(srcr
);
242 nvfx_region_init_for_subresource(&dst
, dstr
, subdst
, dstx
, dsty
, dstz
, TRUE
);
243 nvfx_region_init_for_subresource(&src
, srcr
, subsrc
, srcx
, srcy
, srcz
, FALSE
);
244 w
= util_format_get_stride(dstr
->format
, w
) >> dst
.bpps
;
245 h
= util_format_get_nblocksy(dstr
->format
, h
);
247 small
= (w
* h
<= copy_threshold
);
248 if((!dst_to_gpu
|| !src_on_gpu
) && small
)
249 ret
= -1; /* use the CPU */
251 ret
= nv04_region_copy_2d(ctx
, &dst
, &src
, w
, h
, dst_to_gpu
, src_on_gpu
);
255 && dstr
->bind
& (PIPE_BIND_RENDER_TARGET
| PIPE_BIND_DEPTH_STENCIL
)
256 && srcr
->bind
& PIPE_BIND_SAMPLER_VIEW
)
258 /* this currently works because we hack the bind flags on resource creation to be
259 * the maximum set that the resource type actually supports
261 * TODO: perhaps support reinterpreting the formats
263 struct blitter_context
* blitter
= nvfx_get_blitter(pipe
, 1);
264 util_blitter_copy_region(blitter
, dstr
, subdst
, dstx
, dsty
, dstz
, srcr
, subsrc
, srcx
, srcy
, srcz
, w
, h
, TRUE
);
265 nvfx_put_blitter(pipe
, blitter
);
269 struct nv04_region dstt
= dst
;
270 struct nv04_region srct
= src
;
271 unsigned dstbegin
= 0;
276 nvfx_region_clone(ctx
, &srct
, w
, h
, TRUE
);
279 dstbegin
= nvfx_region_clone(ctx
, &dstt
, w
, h
, FALSE
);
282 nv04_region_copy_cpu(&dstt
, &srct
, w
, h
);
284 if(srct
.bo
!= src
.bo
)
285 nouveau_screen_bo_release(pipe
->screen
, srct
.bo
);
287 if(dstt
.bo
!= dst
.bo
)
289 nv04_memcpy(ctx
, dst
.bo
, dst
.offset
+ dstbegin
, dstt
.bo
, 0, dstt
.bo
->size
);
290 nouveau_screen_bo_release(pipe
->screen
, dstt
.bo
);
296 nvfx_surface_fill(struct pipe_context
* pipe
, struct pipe_surface
*dsts
,
297 unsigned dx
, unsigned dy
, unsigned w
, unsigned h
, unsigned value
)
299 struct nv04_2d_context
*ctx
= nvfx_screen(pipe
->screen
)->eng2d
;
300 struct nv04_region dst
;
302 /* Always try to use the GPU right now, if possible
303 * If the user wanted the surface data on the CPU, he would have cleared with memset (hopefully) */
305 // we don't care about interior pixel order since we set all them to the same value
306 nvfx_region_init_for_surface(&dst
, (struct nvfx_surface
*)dsts
, dx
, dy
, TRUE
);
308 w
= util_format_get_stride(dsts
->format
, w
) >> dst
.bpps
;
309 h
= util_format_get_nblocksy(dsts
->format
, h
);
311 ret
= nv04_region_fill_2d(ctx
, &dst
, w
, h
, value
);
312 if(ret
> 0 && dsts
->texture
->bind
& PIPE_BIND_RENDER_TARGET
)
316 struct nv04_region dstt
= dst
;
317 unsigned dstbegin
= 0;
319 if(nvfx_resource_on_gpu(dsts
->texture
))
320 dstbegin
= nvfx_region_clone(ctx
, &dstt
, w
, h
, FALSE
);
322 nv04_region_fill_cpu(&dstt
, w
, h
, value
);
324 if(dstt
.bo
!= dst
.bo
)
326 nv04_memcpy(ctx
, dst
.bo
, dst
.offset
+ dstbegin
, dstt
.bo
, 0, dstt
.bo
->size
);
327 nouveau_screen_bo_release(pipe
->screen
, dstt
.bo
);
336 nvfx_screen_surface_takedown(struct pipe_screen
*pscreen
)
338 nv04_2d_context_takedown(nvfx_screen(pscreen
)->eng2d
);
339 nvfx_screen(pscreen
)->eng2d
= 0;
343 nvfx_screen_surface_init(struct pipe_screen
*pscreen
)
345 struct nv04_2d_context
* ctx
= nv04_2d_context_init(nouveau_screen(pscreen
)->channel
);
348 nvfx_screen(pscreen
)->eng2d
= ctx
;
353 nvfx_surface_copy_temp(struct pipe_context
* pipe
, struct pipe_surface
* surf
, int to_temp
)
355 struct nvfx_surface
* ns
= (struct nvfx_surface
*)surf
;
356 struct pipe_subresource tempsr
, surfsr
;
357 struct nvfx_context
* nvfx
= nvfx_context(pipe
);
359 /* temporarily detach the temp, so it isn't used in place of the actual resource */
360 struct nvfx_miptree
* temp
= ns
->temp
;
363 // TODO: we really should do this validation before setting these variable in draw calls
364 unsigned use_vertex_buffers
= nvfx
->use_vertex_buffers
;
365 boolean use_index_buffer
= nvfx
->use_index_buffer
;
366 unsigned base_vertex
= nvfx
->base_vertex
;
370 surfsr
.face
= surf
->face
;
371 surfsr
.level
= surf
->level
;
374 nvfx_resource_copy_region(pipe
, &temp
->base
.base
, tempsr
, 0, 0, 0, surf
->texture
, surfsr
, 0, 0, surf
->zslice
, surf
->width
, surf
->height
);
376 nvfx_resource_copy_region(pipe
, surf
->texture
, surfsr
, 0, 0, surf
->zslice
, &temp
->base
.base
, tempsr
, 0, 0, 0, surf
->width
, surf
->height
);
378 /* If this triggers, it probably means we attempted to use the blitter
379 * but failed due to non-renderability of the target.
380 * Obviously, this would lead to infinite recursion if supported. */
385 nvfx
->use_vertex_buffers
= use_vertex_buffers
;
386 nvfx
->use_index_buffer
= use_index_buffer
;
387 nvfx
->base_vertex
= base_vertex
;
389 nvfx
->dirty
|= NVFX_NEW_ARRAYS
;
390 nvfx
->draw_dirty
|= NVFX_NEW_ARRAYS
;
394 nvfx_surface_create_temp(struct pipe_context
* pipe
, struct pipe_surface
* surf
)
396 struct nvfx_surface
* ns
= (struct nvfx_surface
*)surf
;
397 struct pipe_resource
template;
398 memset(&template, 0, sizeof(struct pipe_resource
));
399 template.target
= PIPE_TEXTURE_2D
;
400 template.format
= surf
->format
;
401 template.width0
= surf
->width
;
402 template.height0
= surf
->height
;
404 template.nr_samples
= surf
->texture
->nr_samples
;
405 template.flags
= NVFX_RESOURCE_FLAG_LINEAR
;
407 assert(!ns
->temp
&& !util_dirty_surface_is_dirty(&ns
->base
));
409 ns
->temp
= (struct nvfx_miptree
*)nvfx_miptree_create(pipe
->screen
, &template);
410 nvfx_surface_copy_temp(pipe
, surf
, 1);
414 nvfx_surface_flush(struct pipe_context
* pipe
, struct pipe_surface
* surf
)
416 struct nvfx_context
* nvfx
= (struct nvfx_context
*)pipe
;
417 struct nvfx_surface
* ns
= (struct nvfx_surface
*)surf
;
418 boolean bound
= FALSE
;
420 nvfx_surface_copy_temp(pipe
, surf
, 0);
422 util_dirty_surface_set_clean(nvfx_surface_get_dirty_surfaces(surf
), &ns
->base
);
424 if(nvfx
->framebuffer
.zsbuf
== surf
)
428 for(unsigned i
= 0; i
< nvfx
->framebuffer
.nr_cbufs
; ++i
)
430 if(nvfx
->framebuffer
.cbufs
[i
] == surf
)
439 pipe_resource_reference((struct pipe_resource
**)&ns
->temp
, 0);
443 nvfx_clear_render_target(struct pipe_context
*pipe
,
444 struct pipe_surface
*dst
,
446 unsigned dstx
, unsigned dsty
,
447 unsigned width
, unsigned height
)
450 util_pack_color(rgba
, dst
->format
, &uc
);
452 if(util_format_get_blocksizebits(dst
->format
) > 32
453 || nvfx_surface_fill(pipe
, dst
, dstx
, dsty
, width
, height
, uc
.ui
))
455 // TODO: probably should use hardware clear here instead if possible
456 struct blitter_context
* blitter
= nvfx_get_blitter(pipe
, 0);
457 util_blitter_clear_render_target(blitter
, dst
, rgba
, dstx
, dsty
, width
, height
);
458 nvfx_put_blitter(pipe
, blitter
);
463 nvfx_clear_depth_stencil(struct pipe_context
*pipe
,
464 struct pipe_surface
*dst
,
465 unsigned clear_flags
,
468 unsigned dstx
, unsigned dsty
,
469 unsigned width
, unsigned height
)
471 if(util_format_get_blocksizebits(dst
->format
) > 32
472 || nvfx_surface_fill(pipe
, dst
, dstx
, dsty
, width
, height
, util_pack_z_stencil(dst
->format
, depth
, stencil
)))
474 // TODO: probably should use hardware clear here instead if possible
475 struct blitter_context
* blitter
= nvfx_get_blitter(pipe
, 0);
476 util_blitter_clear_depth_stencil(blitter
, dst
, clear_flags
, depth
, stencil
, dstx
, dsty
, width
, height
);
477 nvfx_put_blitter(pipe
, blitter
);
483 nvfx_init_surface_functions(struct nvfx_context
*nvfx
)
485 nvfx
->pipe
.resource_copy_region
= nvfx_resource_copy_region
;
486 nvfx
->pipe
.clear_render_target
= nvfx_clear_render_target
;
487 nvfx
->pipe
.clear_depth_stencil
= nvfx_clear_depth_stencil
;