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
);
73 rgn
->x
= util_format_get_nblocksx(format
, rgn
->x
) << shift
;
74 rgn
->y
= util_format_get_nblocksy(format
, rgn
->y
);
80 nvfx_region_init_for_surface(struct nv04_region
* rgn
, struct nvfx_surface
* surf
, unsigned x
, unsigned y
, bool for_write
)
88 rgn
->bo
= surf
->temp
->base
.bo
;
90 rgn
->pitch
= surf
->temp
->linear_pitch
;
93 util_dirty_surface_set_dirty(nvfx_surface_get_dirty_surfaces(&surf
->base
.base
), &surf
->base
);
95 rgn
->bo
= ((struct nvfx_resource
*)surf
->base
.base
.texture
)->bo
;
96 rgn
->offset
= surf
->base
.base
.offset
;
98 if(surf
->base
.base
.texture
->flags
& NVFX_RESOURCE_FLAG_LINEAR
)
99 rgn
->pitch
= surf
->pitch
;
103 rgn
->z
= surf
->base
.base
.zslice
;
104 rgn
->w
= surf
->base
.base
.width
;
105 rgn
->h
= surf
->base
.base
.height
;
106 rgn
->d
= u_minify(surf
->base
.base
.texture
->depth0
, surf
->base
.base
.level
);
110 nvfx_region_set_format(rgn
, surf
->base
.base
.format
);
112 nv04_region_try_to_linearize(rgn
);
116 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
)
118 if(pt
->target
!= PIPE_BUFFER
)
120 struct nvfx_surface
* ns
= (struct nvfx_surface
*)util_surfaces_peek(&((struct nvfx_miptree
*)pt
)->surfaces
, pt
, sub
.face
, sub
.level
, z
);
121 if(ns
&& util_dirty_surface_is_dirty(&ns
->base
))
123 nvfx_region_init_for_surface(rgn
, ns
, x
, y
, for_write
);
128 rgn
->bo
= ((struct nvfx_resource
*)pt
)->bo
;
129 rgn
->offset
= nvfx_subresource_offset(pt
, sub
.face
, sub
.level
, z
);
133 if(pt
->flags
& NVFX_RESOURCE_FLAG_LINEAR
)
135 rgn
->pitch
= nvfx_subresource_pitch(pt
, sub
.level
);
142 rgn
->w
= u_minify(pt
->width0
, sub
.level
);
143 rgn
->h
= u_minify(pt
->height0
, sub
.level
);
144 rgn
->d
= u_minify(pt
->depth0
, sub
.level
);
147 nvfx_region_set_format(rgn
, pt
->format
);
149 nv04_region_try_to_linearize(rgn
);
152 // TODO: actually test this for all formats, it's probably wrong for some...
155 nvfx_surface_format(enum pipe_format format
)
157 switch(util_format_get_blocksize(format
)) {
159 return NV04_CONTEXT_SURFACES_2D_FORMAT_Y8
;
161 //return NV04_CONTEXT_SURFACES_2D_FORMAT_Y16;
162 return NV04_CONTEXT_SURFACES_2D_FORMAT_R5G6B5
;
164 //if(format == PIPE_FORMAT_B8G8R8X8_UNORM || format == PIPE_FORMAT_B8G8R8A8_UNORM)
165 return NV04_CONTEXT_SURFACES_2D_FORMAT_A8R8G8B8
;
167 // return NV04_CONTEXT_SURFACES_2D_FORMAT_Y32;
174 nv04_scaled_image_format(enum pipe_format format
)
176 switch(util_format_get_blocksize(format
)) {
178 return NV03_SCALED_IMAGE_FROM_MEMORY_COLOR_FORMAT_Y8
;
180 //if(format == PIPE_FORMAT_B5G5R5A1_UNORM)
181 // return NV03_SCALED_IMAGE_FROM_MEMORY_COLOR_FORMAT_A1R5G5B5;
183 return NV03_SCALED_IMAGE_FROM_MEMORY_COLOR_FORMAT_R5G6B5
;
185 if(format
== PIPE_FORMAT_B8G8R8X8_UNORM
)
186 return NV03_SCALED_IMAGE_FROM_MEMORY_COLOR_FORMAT_X8R8G8B8
;
188 return NV03_SCALED_IMAGE_FROM_MEMORY_COLOR_FORMAT_A8R8G8B8
;
194 // don't save index buffer because blitter doesn't setit
195 static struct blitter_context
*
196 nvfx_get_blitter(struct pipe_context
* pipe
, int copy
)
198 struct nvfx_context
* nvfx
= nvfx_context(pipe
);
200 assert(nvfx
->blitters_in_use
< Elements(nvfx
->blitter
));
202 struct blitter_context
** pblitter
= &nvfx
->blitter
[nvfx
->blitters_in_use
++];
204 *pblitter
= util_blitter_create(pipe
);
205 struct blitter_context
* blitter
= *pblitter
;
207 util_blitter_save_blend(blitter
, nvfx
->blend
);
208 util_blitter_save_depth_stencil_alpha(blitter
, nvfx
->zsa
);
209 util_blitter_save_stencil_ref(blitter
, &nvfx
->stencil_ref
);
210 util_blitter_save_rasterizer(blitter
, nvfx
->rasterizer
);
211 util_blitter_save_fragment_shader(blitter
, nvfx
->fragprog
);
212 util_blitter_save_vertex_shader(blitter
, nvfx
->vertprog
);
213 util_blitter_save_viewport(blitter
, &nvfx
->viewport
);
214 util_blitter_save_framebuffer(blitter
, &nvfx
->framebuffer
);
215 util_blitter_save_clip(blitter
, &nvfx
->clip
);
216 util_blitter_save_vertex_elements(blitter
, nvfx
->vtxelt
);
217 util_blitter_save_vertex_buffers(blitter
, nvfx
->vtxbuf_nr
, nvfx
->vtxbuf
);
221 util_blitter_save_fragment_sampler_states(blitter
, nvfx
->nr_samplers
, (void**)nvfx
->tex_sampler
);
222 util_blitter_save_fragment_sampler_views(blitter
, nvfx
->nr_textures
, nvfx
->fragment_sampler_views
);
229 nvfx_put_blitter(struct pipe_context
* pipe
, struct blitter_context
* blitter
)
231 struct nvfx_context
* nvfx
= nvfx_context(pipe
);
232 --nvfx
->blitters_in_use
;
233 assert(nvfx
->blitters_in_use
>= 0);
237 nvfx_region_clone(struct nv04_2d_context
* ctx
, struct nv04_region
* rgn
, unsigned w
, unsigned h
, boolean for_read
)
239 unsigned begin
= nv04_region_begin(rgn
, w
, h
);
240 unsigned end
= nv04_region_end(rgn
, w
, h
);
241 unsigned size
= end
- begin
;
242 struct nouveau_bo
* bo
= 0;
243 nouveau_bo_new(rgn
->bo
->device
, NOUVEAU_BO_MAP
| NOUVEAU_BO_GART
, 256, size
, &bo
);
245 if(for_read
|| (size
> ((w
* h
) << rgn
->bpps
)))
246 nv04_memcpy(ctx
, bo
, 0, rgn
->bo
, rgn
->offset
+ begin
, size
);
249 rgn
->offset
= -begin
;
254 nvfx_resource_copy_region(struct pipe_context
*pipe
,
255 struct pipe_resource
*dstr
, struct pipe_subresource subdst
,
256 unsigned dstx
, unsigned dsty
, unsigned dstz
,
257 struct pipe_resource
*srcr
, struct pipe_subresource subsrc
,
258 unsigned srcx
, unsigned srcy
, unsigned srcz
,
259 unsigned w
, unsigned h
)
261 static int copy_threshold
= -1;
262 struct nv04_2d_context
*ctx
= nvfx_screen(pipe
->screen
)->eng2d
;
263 struct nv04_region dst
, src
;
272 if(copy_threshold
< 0)
273 copy_threshold
= debug_get_num_option("NOUVEAU_COPY_THRESHOLD", 4);
275 dst_to_gpu
= dstr
->usage
!= PIPE_USAGE_DYNAMIC
&& dstr
->usage
!= PIPE_USAGE_STAGING
;
276 src_on_gpu
= nvfx_resource_on_gpu(srcr
);
278 nvfx_region_init_for_subresource(&dst
, dstr
, subdst
, dstx
, dsty
, dstz
, TRUE
);
279 nvfx_region_init_for_subresource(&src
, srcr
, subsrc
, srcx
, srcy
, srcz
, FALSE
);
280 w
= util_format_get_stride(dstr
->format
, w
) >> dst
.bpps
;
281 h
= util_format_get_nblocksy(dstr
->format
, h
);
283 small
= (w
* h
<= copy_threshold
);
284 if((!dst_to_gpu
|| !src_on_gpu
) && small
)
285 ret
= -1; /* use the CPU */
287 ret
= nv04_region_copy_2d(ctx
, &dst
, &src
, w
, h
,
288 dstr
->target
== PIPE_BUFFER
? -1 : nvfx_surface_format(dstr
->format
),
289 dstr
->target
== PIPE_BUFFER
? -1 : nv04_scaled_image_format(dstr
->format
),
290 dst_to_gpu
, src_on_gpu
);
293 else if(ret
> 0 && dstr
->bind
& PIPE_BIND_RENDER_TARGET
&& srcr
->bind
& PIPE_BIND_SAMPLER_VIEW
)
295 struct blitter_context
* blitter
= nvfx_get_blitter(pipe
, 1);
296 util_blitter_copy_region(blitter
, dstr
, subdst
, dstx
, dsty
, dstz
, srcr
, subsrc
, srcx
, srcy
, srcz
, w
, h
, TRUE
);
297 nvfx_put_blitter(pipe
, blitter
);
301 struct nv04_region dstt
= dst
;
302 struct nv04_region srct
= src
;
303 unsigned dstbegin
= 0;
308 nvfx_region_clone(ctx
, &srct
, w
, h
, TRUE
);
311 dstbegin
= nvfx_region_clone(ctx
, &dstt
, w
, h
, FALSE
);
314 nv04_region_copy_cpu(&dstt
, &srct
, w
, h
);
316 if(srct
.bo
!= src
.bo
)
317 nouveau_screen_bo_release(pipe
->screen
, srct
.bo
);
319 if(dstt
.bo
!= dst
.bo
)
321 nv04_memcpy(ctx
, dst
.bo
, dst
.offset
+ dstbegin
, dstt
.bo
, 0, dstt
.bo
->size
);
322 nouveau_screen_bo_release(pipe
->screen
, dstt
.bo
);
328 nvfx_surface_fill(struct pipe_context
* pipe
, struct pipe_surface
*dsts
,
329 unsigned dx
, unsigned dy
, unsigned w
, unsigned h
, unsigned value
)
331 struct nv04_2d_context
*ctx
= nvfx_screen(pipe
->screen
)->eng2d
;
332 struct nv04_region dst
;
334 /* Always try to use the GPU right now, if possible
335 * If the user wanted the surface data on the CPU, he would have cleared with memset (hopefully) */
337 // we don't care about interior pixel order since we set all them to the same value
338 nvfx_region_init_for_surface(&dst
, (struct nvfx_surface
*)dsts
, dx
, dy
, TRUE
);
340 w
= util_format_get_stride(dsts
->format
, w
) >> dst
.bpps
;
341 h
= util_format_get_nblocksy(dsts
->format
, h
);
343 ret
= nv04_region_fill_2d(ctx
, &dst
, w
, h
, value
);
344 if(ret
> 0 && dsts
->texture
->bind
& PIPE_BIND_RENDER_TARGET
)
348 struct nv04_region dstt
= dst
;
349 unsigned dstbegin
= 0;
351 if(nvfx_resource_on_gpu(dsts
->texture
))
352 dstbegin
= nvfx_region_clone(ctx
, &dstt
, w
, h
, FALSE
);
354 nv04_region_fill_cpu(&dstt
, w
, h
, value
);
356 if(dstt
.bo
!= dst
.bo
)
358 nv04_memcpy(ctx
, dst
.bo
, dst
.offset
+ dstbegin
, dstt
.bo
, 0, dstt
.bo
->size
);
359 nouveau_screen_bo_release(pipe
->screen
, dstt
.bo
);
368 nvfx_screen_surface_takedown(struct pipe_screen
*pscreen
)
370 nv04_2d_context_takedown(nvfx_screen(pscreen
)->eng2d
);
371 nvfx_screen(pscreen
)->eng2d
= 0;
375 nvfx_screen_surface_init(struct pipe_screen
*pscreen
)
377 struct nv04_2d_context
* ctx
= nv04_2d_context_init(nouveau_screen(pscreen
)->channel
);
380 nvfx_screen(pscreen
)->eng2d
= ctx
;
385 nvfx_surface_copy_temp(struct pipe_context
* pipe
, struct pipe_surface
* surf
, int to_temp
)
387 struct nvfx_surface
* ns
= (struct nvfx_surface
*)surf
;
388 struct pipe_subresource tempsr
, surfsr
;
389 struct nvfx_context
* nvfx
= nvfx_context(pipe
);
391 /* temporarily detach the temp, so it isn't used in place of the actual resource */
392 struct nvfx_miptree
* temp
= ns
->temp
;
395 // TODO: we really should do this validation before setting these variable in draw calls
396 unsigned use_vertex_buffers
= nvfx
->use_vertex_buffers
;
397 boolean use_index_buffer
= nvfx
->use_index_buffer
;
398 unsigned base_vertex
= nvfx
->base_vertex
;
402 surfsr
.face
= surf
->face
;
403 surfsr
.level
= surf
->level
;
406 nvfx_resource_copy_region(pipe
, &temp
->base
.base
, tempsr
, 0, 0, 0, surf
->texture
, surfsr
, 0, 0, surf
->zslice
, surf
->width
, surf
->height
);
408 nvfx_resource_copy_region(pipe
, surf
->texture
, surfsr
, 0, 0, surf
->zslice
, &temp
->base
.base
, tempsr
, 0, 0, 0, surf
->width
, surf
->height
);
410 /* If this triggers, it probably means we attempted to use the blitter
411 * but failed due to non-renderability of the target.
412 * Obviously, this would lead to infinite recursion if supported. */
417 nvfx
->use_vertex_buffers
= use_vertex_buffers
;
418 nvfx
->use_index_buffer
= use_index_buffer
;
419 nvfx
->base_vertex
= base_vertex
;
421 nvfx
->dirty
|= NVFX_NEW_ARRAYS
;
422 nvfx
->draw_dirty
|= NVFX_NEW_ARRAYS
;
426 nvfx_surface_create_temp(struct pipe_context
* pipe
, struct pipe_surface
* surf
)
428 struct nvfx_surface
* ns
= (struct nvfx_surface
*)surf
;
429 struct pipe_resource
template;
430 memset(&template, 0, sizeof(struct pipe_resource
));
431 template.target
= PIPE_TEXTURE_2D
;
432 template.format
= surf
->format
;
433 template.width0
= surf
->width
;
434 template.height0
= surf
->height
;
436 template.nr_samples
= surf
->texture
->nr_samples
;
437 template.flags
= NVFX_RESOURCE_FLAG_LINEAR
;
439 assert(!ns
->temp
&& !util_dirty_surface_is_dirty(&ns
->base
));
441 ns
->temp
= (struct nvfx_miptree
*)nvfx_miptree_create(pipe
->screen
, &template);
442 nvfx_surface_copy_temp(pipe
, surf
, 1);
446 nvfx_surface_flush(struct pipe_context
* pipe
, struct pipe_surface
* surf
)
448 struct nvfx_context
* nvfx
= (struct nvfx_context
*)pipe
;
449 struct nvfx_surface
* ns
= (struct nvfx_surface
*)surf
;
450 boolean bound
= FALSE
;
452 nvfx_surface_copy_temp(pipe
, surf
, 0);
454 util_dirty_surface_set_clean(nvfx_surface_get_dirty_surfaces(surf
), &ns
->base
);
456 if(nvfx
->framebuffer
.zsbuf
== surf
)
460 for(unsigned i
= 0; i
< nvfx
->framebuffer
.nr_cbufs
; ++i
)
462 if(nvfx
->framebuffer
.cbufs
[i
] == surf
)
471 pipe_resource_reference((struct pipe_resource
**)&ns
->temp
, 0);
475 nvfx_clear_render_target(struct pipe_context
*pipe
,
476 struct pipe_surface
*dst
,
478 unsigned dstx
, unsigned dsty
,
479 unsigned width
, unsigned height
)
482 util_pack_color(rgba
, dst
->format
, &uc
);
484 if(util_format_get_blocksizebits(dst
->format
) > 32
485 || nvfx_surface_fill(pipe
, dst
, dstx
, dsty
, width
, height
, uc
.ui
))
487 // TODO: probably should use hardware clear here instead if possible
488 struct blitter_context
* blitter
= nvfx_get_blitter(pipe
, 0);
489 util_blitter_clear_render_target(blitter
, dst
, rgba
, dstx
, dsty
, width
, height
);
490 nvfx_put_blitter(pipe
, blitter
);
495 nvfx_clear_depth_stencil(struct pipe_context
*pipe
,
496 struct pipe_surface
*dst
,
497 unsigned clear_flags
,
500 unsigned dstx
, unsigned dsty
,
501 unsigned width
, unsigned height
)
503 if(util_format_get_blocksizebits(dst
->format
) > 32
504 || nvfx_surface_fill(pipe
, dst
, dstx
, dsty
, width
, height
, util_pack_z_stencil(dst
->format
, depth
, stencil
)))
506 // TODO: probably should use hardware clear here instead if possible
507 struct blitter_context
* blitter
= nvfx_get_blitter(pipe
, 0);
508 util_blitter_clear_depth_stencil(blitter
, dst
, clear_flags
, depth
, stencil
, dstx
, dsty
, width
, height
);
509 nvfx_put_blitter(pipe
, blitter
);
515 nvfx_init_surface_functions(struct nvfx_context
*nvfx
)
517 nvfx
->pipe
.resource_copy_region
= nvfx_resource_copy_region
;
518 nvfx
->pipe
.clear_render_target
= nvfx_clear_render_target
;
519 nvfx
->pipe
.clear_depth_stencil
= nvfx_clear_depth_stencil
;