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"
36 #include "util/u_surface.h"
38 #include "nouveau/nouveau_winsys.h"
39 #include "nouveau/nouveau_screen.h"
40 #include "nvfx_context.h"
41 #include "nvfx_screen.h"
42 #include "nvfx_resource.h"
45 #include <nouveau/nouveau_bo.h>
48 nvfx_region_set_format(struct nv04_region
* rgn
, enum pipe_format format
)
50 unsigned bits
= util_format_get_blocksizebits(format
);
61 if(format
== PIPE_FORMAT_B5G5R5X1_UNORM
)
66 if(format
== PIPE_FORMAT_R8G8B8X8_UNORM
|| format
== PIPE_FORMAT_B8G8R8X8_UNORM
)
80 rgn
->x
= util_format_get_nblocksx(format
, rgn
->x
) << shift
;
81 rgn
->y
= util_format_get_nblocksy(format
, rgn
->y
);
87 nvfx_region_init_for_surface(struct nv04_region
* rgn
, struct nvfx_surface
* surf
, unsigned x
, unsigned y
, boolean for_write
)
93 rgn
->bo
= ((struct nvfx_resource
*)surf
->base
.texture
)->bo
;
94 rgn
->offset
= surf
->offset
;
96 if(surf
->base
.texture
->flags
& NOUVEAU_RESOURCE_FLAG_LINEAR
)
97 rgn
->pitch
= surf
->pitch
;
100 rgn
->z
= surf
->base
.u
.tex
.first_layer
;
101 rgn
->w
= surf
->base
.width
;
102 rgn
->h
= surf
->base
.height
;
103 rgn
->d
= u_minify(surf
->base
.texture
->depth0
, surf
->base
.u
.tex
.level
);
106 nvfx_region_set_format(rgn
, surf
->base
.format
);
108 nv04_region_try_to_linearize(rgn
);
112 nvfx_region_init_for_subresource(struct nv04_region
* rgn
, struct pipe_resource
* pt
, unsigned level
, unsigned x
, unsigned y
, unsigned z
, boolean for_write
)
114 if(pt
->target
!= PIPE_BUFFER
)
116 struct nvfx_surface
* ns
= (struct nvfx_surface
*)util_surfaces_peek(&((struct nvfx_miptree
*)pt
)->surfaces
, pt
, level
, z
);
119 nvfx_region_init_for_surface(rgn
, ns
, x
, y
, for_write
);
124 rgn
->bo
= ((struct nvfx_resource
*)pt
)->bo
;
125 rgn
->offset
= nvfx_subresource_offset(pt
, z
, level
, z
);
129 if(pt
->flags
& NOUVEAU_RESOURCE_FLAG_LINEAR
)
131 rgn
->pitch
= nvfx_subresource_pitch(pt
, level
);
138 rgn
->w
= u_minify(pt
->width0
, level
);
139 rgn
->h
= u_minify(pt
->height0
, level
);
140 rgn
->d
= u_minify(pt
->depth0
, level
);
143 nvfx_region_set_format(rgn
, pt
->format
);
145 nv04_region_try_to_linearize(rgn
);
148 // don't save index buffer because blitter doesn't setit
149 static struct blitter_context
*
150 nvfx_get_blitter(struct pipe_context
* pipe
, int copy
)
152 struct nvfx_context
* nvfx
= nvfx_context(pipe
);
153 struct blitter_context
** pblitter
;
154 struct blitter_context
* blitter
;
156 assert(nvfx
->blitters_in_use
< Elements(nvfx
->blitter
));
158 if(nvfx
->query
&& !nvfx
->blitters_in_use
)
160 struct nouveau_channel
* chan
= nvfx
->screen
->base
.channel
;
161 struct nouveau_grobj
*eng3d
= nvfx
->screen
->eng3d
;
162 BEGIN_RING(chan
, eng3d
, NV30_3D_QUERY_ENABLE
, 1);
166 pblitter
= &nvfx
->blitter
[nvfx
->blitters_in_use
++];
168 *pblitter
= util_blitter_create(pipe
);
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_vertex_elements(blitter
, nvfx
->vtxelt
);
180 util_blitter_save_vertex_buffers(blitter
, nvfx
->vtxbuf_nr
, nvfx
->vtxbuf
);
184 util_blitter_save_fragment_sampler_states(blitter
, nvfx
->nr_samplers
, (void**)nvfx
->tex_sampler
);
185 util_blitter_save_fragment_sampler_views(blitter
, nvfx
->nr_textures
, nvfx
->fragment_sampler_views
);
192 nvfx_put_blitter(struct pipe_context
* pipe
, struct blitter_context
* blitter
)
194 struct nvfx_context
* nvfx
= nvfx_context(pipe
);
195 --nvfx
->blitters_in_use
;
196 assert(nvfx
->blitters_in_use
>= 0);
198 if(nvfx
->query
&& !nvfx
->blitters_in_use
)
200 struct nouveau_channel
* chan
= nvfx
->screen
->base
.channel
;
201 struct nouveau_grobj
*eng3d
= nvfx
->screen
->eng3d
;
202 BEGIN_RING(chan
, eng3d
, NV30_3D_QUERY_ENABLE
, 1);
208 nvfx_region_clone(struct nv04_2d_context
* ctx
, struct nv04_region
* rgn
, unsigned w
, unsigned h
, boolean for_read
)
210 unsigned begin
= nv04_region_begin(rgn
, w
, h
);
211 unsigned end
= nv04_region_end(rgn
, w
, h
);
212 unsigned size
= end
- begin
;
213 struct nouveau_bo
* bo
= 0;
214 nouveau_bo_new(rgn
->bo
->device
, NOUVEAU_BO_MAP
| NOUVEAU_BO_GART
, 256, size
, &bo
);
216 if(for_read
|| (size
> ((w
* h
) << rgn
->bpps
)))
217 nv04_memcpy(ctx
, bo
, 0, rgn
->bo
, rgn
->offset
+ begin
, size
);
220 rgn
->offset
= -begin
;
225 nvfx_resource_copy_region(struct pipe_context
*pipe
,
226 struct pipe_resource
*dstr
, unsigned dst_level
,
227 unsigned dstx
, unsigned dsty
, unsigned dstz
,
228 struct pipe_resource
*srcr
, unsigned src_level
,
229 const struct pipe_box
*src_box
)
231 static int copy_threshold
= -1;
232 struct nv04_2d_context
*ctx
= nvfx_screen(pipe
->screen
)->eng2d
;
233 struct nv04_region dst
, src
;
238 unsigned w
= src_box
->width
;
239 unsigned h
= src_box
->height
;
244 /* Fallback for buffers. */
245 if (dstr
->target
== PIPE_BUFFER
&& srcr
->target
== PIPE_BUFFER
) {
246 util_resource_copy_region(pipe
, dstr
, dst_level
, dstx
, dsty
, dstz
,
247 srcr
, src_level
, src_box
);
251 if(copy_threshold
< 0)
252 copy_threshold
= debug_get_num_option("NOUVEAU_COPY_THRESHOLD", 4);
254 dst_to_gpu
= dstr
->usage
!= PIPE_USAGE_DYNAMIC
&& dstr
->usage
!= PIPE_USAGE_STAGING
;
255 src_on_gpu
= nvfx_resource_on_gpu(srcr
);
257 nvfx_region_init_for_subresource(&dst
, dstr
, dst_level
, dstx
, dsty
, dstz
, TRUE
);
258 nvfx_region_init_for_subresource(&src
, srcr
, src_level
, src_box
->x
, src_box
->y
, src_box
->z
, FALSE
);
259 w
= util_format_get_stride(dstr
->format
, w
) >> dst
.bpps
;
260 h
= util_format_get_nblocksy(dstr
->format
, h
);
262 small
= (w
* h
<= copy_threshold
);
263 if((!dst_to_gpu
|| !src_on_gpu
) && small
)
264 ret
= -1; /* use the CPU */
266 ret
= nv04_region_copy_2d(ctx
, &dst
, &src
, w
, h
, dst_to_gpu
, src_on_gpu
);
270 && dstr
->bind
& (PIPE_BIND_RENDER_TARGET
| PIPE_BIND_DEPTH_STENCIL
)
271 && srcr
->bind
& PIPE_BIND_SAMPLER_VIEW
)
273 /* this currently works because we hack the bind flags on resource creation to be
274 * the maximum set that the resource type actually supports
276 * TODO: perhaps support reinterpreting the formats
278 struct blitter_context
* blitter
= nvfx_get_blitter(pipe
, 1);
279 util_blitter_copy_texture(blitter
, dstr
, dst_level
, dstx
, dsty
, dstz
, srcr
, src_level
, src_box
, TRUE
);
280 nvfx_put_blitter(pipe
, blitter
);
284 struct nv04_region dstt
= dst
;
285 struct nv04_region srct
= src
;
286 unsigned dstbegin
= 0;
291 nvfx_region_clone(ctx
, &srct
, w
, h
, TRUE
);
294 dstbegin
= nvfx_region_clone(ctx
, &dstt
, w
, h
, FALSE
);
297 nv04_region_copy_cpu(&dstt
, &srct
, w
, h
);
299 if(srct
.bo
!= src
.bo
)
300 nouveau_screen_bo_release(pipe
->screen
, srct
.bo
);
302 if(dstt
.bo
!= dst
.bo
)
304 nv04_memcpy(ctx
, dst
.bo
, dst
.offset
+ dstbegin
, dstt
.bo
, 0, dstt
.bo
->size
);
305 nouveau_screen_bo_release(pipe
->screen
, dstt
.bo
);
311 nvfx_surface_fill(struct pipe_context
* pipe
, struct pipe_surface
*dsts
,
312 unsigned dx
, unsigned dy
, unsigned w
, unsigned h
, unsigned value
)
314 struct nv04_2d_context
*ctx
= nvfx_screen(pipe
->screen
)->eng2d
;
315 struct nv04_region dst
;
317 /* Always try to use the GPU right now, if possible
318 * If the user wanted the surface data on the CPU, he would have cleared with memset (hopefully) */
320 // we don't care about interior pixel order since we set all them to the same value
321 nvfx_region_init_for_surface(&dst
, (struct nvfx_surface
*)dsts
, dx
, dy
, TRUE
);
323 w
= util_format_get_stride(dsts
->format
, w
) >> dst
.bpps
;
324 h
= util_format_get_nblocksy(dsts
->format
, h
);
326 ret
= nv04_region_fill_2d(ctx
, &dst
, w
, h
, value
);
327 if(ret
> 0 && dsts
->texture
->bind
& PIPE_BIND_RENDER_TARGET
)
331 struct nv04_region dstt
= dst
;
332 unsigned dstbegin
= 0;
334 if(nvfx_resource_on_gpu(dsts
->texture
))
335 dstbegin
= nvfx_region_clone(ctx
, &dstt
, w
, h
, FALSE
);
337 nv04_region_fill_cpu(&dstt
, w
, h
, value
);
339 if(dstt
.bo
!= dst
.bo
)
341 nv04_memcpy(ctx
, dst
.bo
, dst
.offset
+ dstbegin
, dstt
.bo
, 0, dstt
.bo
->size
);
342 nouveau_screen_bo_release(pipe
->screen
, dstt
.bo
);
351 nvfx_screen_surface_takedown(struct pipe_screen
*pscreen
)
353 nv04_2d_context_takedown(nvfx_screen(pscreen
)->eng2d
);
354 nvfx_screen(pscreen
)->eng2d
= 0;
358 nvfx_screen_surface_init(struct pipe_screen
*pscreen
)
360 struct nv04_2d_context
* ctx
= nv04_2d_context_init(nouveau_screen(pscreen
)->channel
);
363 nvfx_screen(pscreen
)->eng2d
= ctx
;
368 nvfx_clear_render_target(struct pipe_context
*pipe
,
369 struct pipe_surface
*dst
,
370 const union pipe_color_union
*color
,
371 unsigned dstx
, unsigned dsty
,
372 unsigned width
, unsigned height
)
375 util_pack_color(color
->f
, dst
->format
, &uc
);
377 if(util_format_get_blocksizebits(dst
->format
) > 32
378 || nvfx_surface_fill(pipe
, dst
, dstx
, dsty
, width
, height
, uc
.ui
))
380 // TODO: probably should use hardware clear here instead if possible
381 struct blitter_context
* blitter
= nvfx_get_blitter(pipe
, 0);
382 util_blitter_clear_render_target(blitter
, dst
, color
, dstx
, dsty
, width
, height
);
383 nvfx_put_blitter(pipe
, blitter
);
388 nvfx_clear_depth_stencil(struct pipe_context
*pipe
,
389 struct pipe_surface
*dst
,
390 unsigned clear_flags
,
393 unsigned dstx
, unsigned dsty
,
394 unsigned width
, unsigned height
)
396 if(util_format_get_blocksizebits(dst
->format
) > 32
397 || nvfx_surface_fill(pipe
, dst
, dstx
, dsty
, width
, height
, util_pack_z_stencil(dst
->format
, depth
, stencil
)))
399 // TODO: probably should use hardware clear here instead if possible
400 struct blitter_context
* blitter
= nvfx_get_blitter(pipe
, 0);
401 util_blitter_clear_depth_stencil(blitter
, dst
, clear_flags
, depth
, stencil
, dstx
, dsty
, width
, height
);
402 nvfx_put_blitter(pipe
, blitter
);
408 nvfx_init_surface_functions(struct nvfx_context
*nvfx
)
410 nvfx
->pipe
.resource_copy_region
= nvfx_resource_copy_region
;
411 nvfx
->pipe
.clear_render_target
= nvfx_clear_render_target
;
412 nvfx
->pipe
.clear_depth_stencil
= nvfx_clear_depth_stencil
;