Path: blob/21.2-virgl/src/gallium/drivers/r300/r300_blit.c
4570 views
/*1* Copyright 2009 Marek Olšák <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* on the rights to use, copy, modify, merge, publish, distribute, sub7* license, and/or sell copies of the Software, and to permit persons to whom8* the Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL17* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,18* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR19* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE20* USE OR OTHER DEALINGS IN THE SOFTWARE. */2122#include "r300_context.h"23#include "r300_emit.h"24#include "r300_texture.h"25#include "r300_reg.h"2627#include "util/format/u_format.h"28#include "util/half_float.h"29#include "util/u_pack_color.h"30#include "util/u_surface.h"3132enum r300_blitter_op /* bitmask */33{34R300_STOP_QUERY = 1,35R300_SAVE_TEXTURES = 2,36R300_SAVE_FRAMEBUFFER = 4,37R300_IGNORE_RENDER_COND = 8,3839R300_CLEAR = R300_STOP_QUERY,4041R300_CLEAR_SURFACE = R300_STOP_QUERY | R300_SAVE_FRAMEBUFFER,4243R300_COPY = R300_STOP_QUERY | R300_SAVE_FRAMEBUFFER |44R300_SAVE_TEXTURES | R300_IGNORE_RENDER_COND,4546R300_BLIT = R300_STOP_QUERY | R300_SAVE_FRAMEBUFFER |47R300_SAVE_TEXTURES,4849R300_DECOMPRESS = R300_STOP_QUERY | R300_IGNORE_RENDER_COND,50};5152static void r300_blitter_begin(struct r300_context* r300, enum r300_blitter_op op)53{54if ((op & R300_STOP_QUERY) && r300->query_current) {55r300->blitter_saved_query = r300->query_current;56r300_stop_query(r300);57}5859/* Yeah we have to save all those states to ensure the blitter operation60* is really transparent. The states will be restored by the blitter once61* copying is done. */62util_blitter_save_blend(r300->blitter, r300->blend_state.state);63util_blitter_save_depth_stencil_alpha(r300->blitter, r300->dsa_state.state);64util_blitter_save_stencil_ref(r300->blitter, &(r300->stencil_ref));65util_blitter_save_rasterizer(r300->blitter, r300->rs_state.state);66util_blitter_save_fragment_shader(r300->blitter, r300->fs.state);67util_blitter_save_vertex_shader(r300->blitter, r300->vs_state.state);68util_blitter_save_viewport(r300->blitter, &r300->viewport);69util_blitter_save_scissor(r300->blitter, r300->scissor_state.state);70util_blitter_save_sample_mask(r300->blitter, *(unsigned*)r300->sample_mask.state);71util_blitter_save_vertex_buffer_slot(r300->blitter, r300->vertex_buffer);72util_blitter_save_vertex_elements(r300->blitter, r300->velems);7374if (op & R300_SAVE_FRAMEBUFFER) {75util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);76}7778if (op & R300_SAVE_TEXTURES) {79struct r300_textures_state* state =80(struct r300_textures_state*)r300->textures_state.state;8182util_blitter_save_fragment_sampler_states(83r300->blitter, state->sampler_state_count,84(void**)state->sampler_states);8586util_blitter_save_fragment_sampler_views(87r300->blitter, state->sampler_view_count,88(struct pipe_sampler_view**)state->sampler_views);89}9091if (op & R300_IGNORE_RENDER_COND) {92/* Save the flag. */93r300->blitter_saved_skip_rendering = r300->skip_rendering+1;94r300->skip_rendering = FALSE;95} else {96r300->blitter_saved_skip_rendering = 0;97}98}99100static void r300_blitter_end(struct r300_context *r300)101{102if (r300->blitter_saved_query) {103r300_resume_query(r300, r300->blitter_saved_query);104r300->blitter_saved_query = NULL;105}106107if (r300->blitter_saved_skip_rendering) {108/* Restore the flag. */109r300->skip_rendering = r300->blitter_saved_skip_rendering-1;110}111}112113static uint32_t r300_depth_clear_cb_value(enum pipe_format format,114const float* rgba)115{116union util_color uc;117util_pack_color(rgba, format, &uc);118119if (util_format_get_blocksizebits(format) == 32)120return uc.ui[0];121else122return uc.us | (uc.us << 16);123}124125static boolean r300_cbzb_clear_allowed(struct r300_context *r300,126unsigned clear_buffers)127{128struct pipe_framebuffer_state *fb =129(struct pipe_framebuffer_state*)r300->fb_state.state;130131/* Only color clear allowed, and only one colorbuffer. */132if ((clear_buffers & ~PIPE_CLEAR_COLOR) != 0 || fb->nr_cbufs != 1 || !fb->cbufs[0])133return FALSE;134135return r300_surface(fb->cbufs[0])->cbzb_allowed;136}137138static boolean r300_fast_zclear_allowed(struct r300_context *r300,139unsigned clear_buffers)140{141struct pipe_framebuffer_state *fb =142(struct pipe_framebuffer_state*)r300->fb_state.state;143144return r300_resource(fb->zsbuf->texture)->tex.zmask_dwords[fb->zsbuf->u.tex.level] != 0;145}146147static boolean r300_hiz_clear_allowed(struct r300_context *r300)148{149struct pipe_framebuffer_state *fb =150(struct pipe_framebuffer_state*)r300->fb_state.state;151152return r300_resource(fb->zsbuf->texture)->tex.hiz_dwords[fb->zsbuf->u.tex.level] != 0;153}154155static uint32_t r300_depth_clear_value(enum pipe_format format,156double depth, unsigned stencil)157{158switch (format) {159case PIPE_FORMAT_Z16_UNORM:160case PIPE_FORMAT_X8Z24_UNORM:161return util_pack_z(format, depth);162163case PIPE_FORMAT_S8_UINT_Z24_UNORM:164return util_pack_z_stencil(format, depth, stencil);165166default:167assert(0);168return 0;169}170}171172static uint32_t r300_hiz_clear_value(double depth)173{174uint32_t r = (uint32_t)(CLAMP(depth, 0, 1) * 255.5);175assert(r <= 255);176return r | (r << 8) | (r << 16) | (r << 24);177}178179static void r300_set_clear_color(struct r300_context *r300,180const union pipe_color_union *color)181{182struct pipe_framebuffer_state *fb =183(struct pipe_framebuffer_state*)r300->fb_state.state;184union util_color uc;185186memset(&uc, 0, sizeof(uc));187util_pack_color(color->f, fb->cbufs[0]->format, &uc);188189if (fb->cbufs[0]->format == PIPE_FORMAT_R16G16B16A16_FLOAT ||190fb->cbufs[0]->format == PIPE_FORMAT_R16G16B16X16_FLOAT) {191/* (0,1,2,3) maps to (B,G,R,A) */192r300->color_clear_value_gb = uc.h[0] | ((uint32_t)uc.h[1] << 16);193r300->color_clear_value_ar = uc.h[2] | ((uint32_t)uc.h[3] << 16);194} else {195r300->color_clear_value = uc.ui[0];196}197}198199DEBUG_GET_ONCE_BOOL_OPTION(hyperz, "RADEON_HYPERZ", FALSE)200201/* Clear currently bound buffers. */202static void r300_clear(struct pipe_context* pipe,203unsigned buffers,204const struct pipe_scissor_state *scissor_state,205const union pipe_color_union *color,206double depth,207unsigned stencil)208{209/* My notes about Zbuffer compression:210*211* 1) The zbuffer must be micro-tiled and whole microtiles must be212* written if compression is enabled. If microtiling is disabled,213* it locks up.214*215* 2) There is ZMASK RAM which contains a compressed zbuffer.216* Each dword of the Z Mask contains compression information217* for 16 4x4 pixel tiles, that is 2 bits for each tile.218* On chips with 2 Z pipes, every other dword maps to a different219* pipe. On newer chipsets, there is a new compression mode220* with 8x8 pixel tiles per 2 bits.221*222* 3) The FASTFILL bit has nothing to do with filling. It only tells hw223* it should look in the ZMASK RAM first before fetching from a real224* zbuffer.225*226* 4) If a pixel is in a cleared state, ZB_DEPTHCLEARVALUE is returned227* during zbuffer reads instead of the value that is actually stored228* in the zbuffer memory. A pixel is in a cleared state when its ZMASK229* is equal to 0. Therefore, if you clear ZMASK with zeros, you may230* leave the zbuffer memory uninitialized, but then you must enable231* compression, so that the ZMASK RAM is actually used.232*233* 5) Each 4x4 (or 8x8) tile is automatically decompressed and recompressed234* during zbuffer updates. A special decompressing operation should be235* used to fully decompress a zbuffer, which basically just stores all236* compressed tiles in ZMASK to the zbuffer memory.237*238* 6) For a 16-bit zbuffer, compression causes a hung with one or239* two samples and should not be used.240*241* 7) FORCE_COMPRESSED_STENCIL_VALUE should be enabled for stencil clears242* to avoid needless decompression.243*244* 8) Fastfill must not be used if reading of compressed Z data is disabled245* and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),246* i.e. it cannot be used to compress the zbuffer.247*248* 9) ZB_CB_CLEAR does not interact with zbuffer compression in any way.249*250* - Marek251*/252253struct r300_context* r300 = r300_context(pipe);254struct pipe_framebuffer_state *fb =255(struct pipe_framebuffer_state*)r300->fb_state.state;256struct r300_hyperz_state *hyperz =257(struct r300_hyperz_state*)r300->hyperz_state.state;258uint32_t width = fb->width;259uint32_t height = fb->height;260uint32_t hyperz_dcv = hyperz->zb_depthclearvalue;261262/* Use fast Z clear.263* The zbuffer must be in micro-tiled mode, otherwise it locks up. */264if (buffers & PIPE_CLEAR_DEPTHSTENCIL) {265boolean zmask_clear, hiz_clear;266267/* If both depth and stencil are present, they must be cleared together. */268if (fb->zsbuf->texture->format == PIPE_FORMAT_S8_UINT_Z24_UNORM &&269(buffers & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) {270zmask_clear = FALSE;271hiz_clear = FALSE;272} else {273zmask_clear = r300_fast_zclear_allowed(r300, buffers);274hiz_clear = r300_hiz_clear_allowed(r300);275}276277/* If we need Hyper-Z. */278if (zmask_clear || hiz_clear) {279/* Try to obtain the access to Hyper-Z buffers if we don't have one. */280if (!r300->hyperz_enabled &&281(r300->screen->caps.is_r500 || debug_get_option_hyperz())) {282r300->hyperz_enabled =283r300->rws->cs_request_feature(&r300->cs,284RADEON_FID_R300_HYPERZ_ACCESS,285TRUE);286if (r300->hyperz_enabled) {287/* Need to emit HyperZ buffer regs for the first time. */288r300_mark_fb_state_dirty(r300, R300_CHANGED_HYPERZ_FLAG);289}290}291292/* Setup Hyper-Z clears. */293if (r300->hyperz_enabled) {294if (zmask_clear) {295hyperz_dcv = hyperz->zb_depthclearvalue =296r300_depth_clear_value(fb->zsbuf->format, depth, stencil);297298r300_mark_atom_dirty(r300, &r300->zmask_clear);299r300_mark_atom_dirty(r300, &r300->gpu_flush);300buffers &= ~PIPE_CLEAR_DEPTHSTENCIL;301}302303if (hiz_clear) {304r300->hiz_clear_value = r300_hiz_clear_value(depth);305r300_mark_atom_dirty(r300, &r300->hiz_clear);306r300_mark_atom_dirty(r300, &r300->gpu_flush);307}308r300->num_z_clears++;309}310}311}312313/* Use fast color clear for an AA colorbuffer.314* The CMASK is shared between all colorbuffers, so we use it315* if there is only one colorbuffer bound. */316if ((buffers & PIPE_CLEAR_COLOR) && fb->nr_cbufs == 1 && fb->cbufs[0] &&317r300_resource(fb->cbufs[0]->texture)->tex.cmask_dwords) {318/* Try to obtain the access to the CMASK if we don't have one. */319if (!r300->cmask_access) {320r300->cmask_access =321r300->rws->cs_request_feature(&r300->cs,322RADEON_FID_R300_CMASK_ACCESS,323TRUE);324}325326/* Setup the clear. */327if (r300->cmask_access) {328/* Pair the resource with the CMASK to avoid other resources329* accessing it. */330if (!r300->screen->cmask_resource) {331mtx_lock(&r300->screen->cmask_mutex);332/* Double checking (first unlocked, then locked). */333if (!r300->screen->cmask_resource) {334/* Don't reference this, so that the texture can be335* destroyed while set in cmask_resource.336* Then in texture_destroy, we set cmask_resource to NULL. */337r300->screen->cmask_resource = fb->cbufs[0]->texture;338}339mtx_unlock(&r300->screen->cmask_mutex);340}341342if (r300->screen->cmask_resource == fb->cbufs[0]->texture) {343r300_set_clear_color(r300, color);344r300_mark_atom_dirty(r300, &r300->cmask_clear);345r300_mark_atom_dirty(r300, &r300->gpu_flush);346buffers &= ~PIPE_CLEAR_COLOR;347}348}349}350/* Enable CBZB clear. */351else if (r300_cbzb_clear_allowed(r300, buffers)) {352struct r300_surface *surf = r300_surface(fb->cbufs[0]);353354hyperz->zb_depthclearvalue =355r300_depth_clear_cb_value(surf->base.format, color->f);356357width = surf->cbzb_width;358height = surf->cbzb_height;359360r300->cbzb_clear = TRUE;361r300_mark_fb_state_dirty(r300, R300_CHANGED_HYPERZ_FLAG);362}363364/* Clear. */365if (buffers) {366/* Clear using the blitter. */367r300_blitter_begin(r300, R300_CLEAR);368util_blitter_clear(r300->blitter, width, height, 1,369buffers, color, depth, stencil,370util_framebuffer_get_num_samples(fb) > 1);371r300_blitter_end(r300);372} else if (r300->zmask_clear.dirty ||373r300->hiz_clear.dirty ||374r300->cmask_clear.dirty) {375/* Just clear zmask and hiz now, this does not use the standard draw376* procedure. */377/* Calculate zmask_clear and hiz_clear atom sizes. */378unsigned dwords =379r300->gpu_flush.size +380(r300->zmask_clear.dirty ? r300->zmask_clear.size : 0) +381(r300->hiz_clear.dirty ? r300->hiz_clear.size : 0) +382(r300->cmask_clear.dirty ? r300->cmask_clear.size : 0) +383r300_get_num_cs_end_dwords(r300);384385/* Reserve CS space. */386if (!r300->rws->cs_check_space(&r300->cs, dwords, false)) {387r300_flush(&r300->context, PIPE_FLUSH_ASYNC, NULL);388}389390/* Emit clear packets. */391r300_emit_gpu_flush(r300, r300->gpu_flush.size, r300->gpu_flush.state);392r300->gpu_flush.dirty = FALSE;393394if (r300->zmask_clear.dirty) {395r300_emit_zmask_clear(r300, r300->zmask_clear.size,396r300->zmask_clear.state);397r300->zmask_clear.dirty = FALSE;398}399if (r300->hiz_clear.dirty) {400r300_emit_hiz_clear(r300, r300->hiz_clear.size,401r300->hiz_clear.state);402r300->hiz_clear.dirty = FALSE;403}404if (r300->cmask_clear.dirty) {405r300_emit_cmask_clear(r300, r300->cmask_clear.size,406r300->cmask_clear.state);407r300->cmask_clear.dirty = FALSE;408}409} else {410assert(0);411}412413/* Disable CBZB clear. */414if (r300->cbzb_clear) {415r300->cbzb_clear = FALSE;416hyperz->zb_depthclearvalue = hyperz_dcv;417r300_mark_fb_state_dirty(r300, R300_CHANGED_HYPERZ_FLAG);418}419420/* Enable fastfill and/or hiz.421*422* If we cleared zmask/hiz, it's in use now. The Hyper-Z state update423* looks if zmask/hiz is in use and programs hardware accordingly. */424if (r300->zmask_in_use || r300->hiz_in_use) {425r300_mark_atom_dirty(r300, &r300->hyperz_state);426}427}428429/* Clear a region of a color surface to a constant value. */430static void r300_clear_render_target(struct pipe_context *pipe,431struct pipe_surface *dst,432const union pipe_color_union *color,433unsigned dstx, unsigned dsty,434unsigned width, unsigned height,435bool render_condition_enabled)436{437struct r300_context *r300 = r300_context(pipe);438439r300_blitter_begin(r300, R300_CLEAR_SURFACE |440(render_condition_enabled ? 0 : R300_IGNORE_RENDER_COND));441util_blitter_clear_render_target(r300->blitter, dst, color,442dstx, dsty, width, height);443r300_blitter_end(r300);444}445446/* Clear a region of a depth stencil surface. */447static void r300_clear_depth_stencil(struct pipe_context *pipe,448struct pipe_surface *dst,449unsigned clear_flags,450double depth,451unsigned stencil,452unsigned dstx, unsigned dsty,453unsigned width, unsigned height,454bool render_condition_enabled)455{456struct r300_context *r300 = r300_context(pipe);457struct pipe_framebuffer_state *fb =458(struct pipe_framebuffer_state*)r300->fb_state.state;459460if (r300->zmask_in_use && !r300->locked_zbuffer) {461if (fb->zsbuf->texture == dst->texture) {462r300_decompress_zmask(r300);463}464}465466/* XXX Do not decompress ZMask of the currently-set zbuffer. */467r300_blitter_begin(r300, R300_CLEAR_SURFACE |468(render_condition_enabled ? 0 : R300_IGNORE_RENDER_COND));469util_blitter_clear_depth_stencil(r300->blitter, dst, clear_flags, depth, stencil,470dstx, dsty, width, height);471r300_blitter_end(r300);472}473474void r300_decompress_zmask(struct r300_context *r300)475{476struct pipe_framebuffer_state *fb =477(struct pipe_framebuffer_state*)r300->fb_state.state;478479if (!r300->zmask_in_use || r300->locked_zbuffer)480return;481482r300->zmask_decompress = TRUE;483r300_mark_atom_dirty(r300, &r300->hyperz_state);484485r300_blitter_begin(r300, R300_DECOMPRESS);486util_blitter_custom_clear_depth(r300->blitter, fb->width, fb->height, 0,487r300->dsa_decompress_zmask);488r300_blitter_end(r300);489490r300->zmask_decompress = FALSE;491r300->zmask_in_use = FALSE;492r300_mark_atom_dirty(r300, &r300->hyperz_state);493}494495void r300_decompress_zmask_locked_unsafe(struct r300_context *r300)496{497struct pipe_framebuffer_state fb;498499memset(&fb, 0, sizeof(fb));500fb.width = r300->locked_zbuffer->width;501fb.height = r300->locked_zbuffer->height;502fb.zsbuf = r300->locked_zbuffer;503504r300->context.set_framebuffer_state(&r300->context, &fb);505r300_decompress_zmask(r300);506}507508void r300_decompress_zmask_locked(struct r300_context *r300)509{510struct pipe_framebuffer_state saved_fb;511512memset(&saved_fb, 0, sizeof(saved_fb));513util_copy_framebuffer_state(&saved_fb, r300->fb_state.state);514r300_decompress_zmask_locked_unsafe(r300);515r300->context.set_framebuffer_state(&r300->context, &saved_fb);516util_unreference_framebuffer_state(&saved_fb);517518pipe_surface_reference(&r300->locked_zbuffer, NULL);519}520521bool r300_is_blit_supported(enum pipe_format format)522{523const struct util_format_description *desc =524util_format_description(format);525526return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN ||527desc->layout == UTIL_FORMAT_LAYOUT_S3TC ||528desc->layout == UTIL_FORMAT_LAYOUT_RGTC;529}530531/* Copy a block of pixels from one surface to another. */532static void r300_resource_copy_region(struct pipe_context *pipe,533struct pipe_resource *dst,534unsigned dst_level,535unsigned dstx, unsigned dsty, unsigned dstz,536struct pipe_resource *src,537unsigned src_level,538const struct pipe_box *src_box)539{540struct pipe_screen *screen = pipe->screen;541struct r300_context *r300 = r300_context(pipe);542struct pipe_framebuffer_state *fb =543(struct pipe_framebuffer_state*)r300->fb_state.state;544unsigned src_width0 = r300_resource(src)->tex.width0;545unsigned src_height0 = r300_resource(src)->tex.height0;546unsigned dst_width0 = r300_resource(dst)->tex.width0;547unsigned dst_height0 = r300_resource(dst)->tex.height0;548unsigned layout;549struct pipe_box box, dstbox;550struct pipe_sampler_view src_templ, *src_view;551struct pipe_surface dst_templ, *dst_view;552553/* Fallback for buffers. */554if ((dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) ||555!r300_is_blit_supported(dst->format)) {556util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,557src, src_level, src_box);558return;559}560561/* Can't read MSAA textures. */562if (src->nr_samples > 1 || dst->nr_samples > 1) {563return;564}565566/* The code below changes the texture format so that the copy can be done567* on hardware. E.g. depth-stencil surfaces are copied as RGBA568* colorbuffers. */569570util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);571util_blitter_default_src_texture(r300->blitter, &src_templ, src, src_level);572573layout = util_format_description(dst_templ.format)->layout;574575/* Handle non-renderable plain formats. */576if (layout == UTIL_FORMAT_LAYOUT_PLAIN &&577(!screen->is_format_supported(screen, src_templ.format, src->target,578src->nr_samples, src->nr_storage_samples,579PIPE_BIND_SAMPLER_VIEW) ||580!screen->is_format_supported(screen, dst_templ.format, dst->target,581dst->nr_samples, dst->nr_storage_samples,582PIPE_BIND_RENDER_TARGET))) {583switch (util_format_get_blocksize(dst_templ.format)) {584case 1:585dst_templ.format = PIPE_FORMAT_I8_UNORM;586break;587case 2:588dst_templ.format = PIPE_FORMAT_B4G4R4A4_UNORM;589break;590case 4:591dst_templ.format = PIPE_FORMAT_B8G8R8A8_UNORM;592break;593case 8:594dst_templ.format = PIPE_FORMAT_R16G16B16A16_UNORM;595break;596default:597debug_printf("r300: copy_region: Unhandled format: %s. Falling back to software.\n"598"r300: copy_region: Software fallback doesn't work for tiled textures.\n",599util_format_short_name(dst_templ.format));600}601src_templ.format = dst_templ.format;602}603604/* Handle compressed formats. */605if (layout == UTIL_FORMAT_LAYOUT_S3TC ||606layout == UTIL_FORMAT_LAYOUT_RGTC) {607assert(src_templ.format == dst_templ.format);608609box = *src_box;610src_box = &box;611612dst_width0 = align(dst_width0, 4);613dst_height0 = align(dst_height0, 4);614src_width0 = align(src_width0, 4);615src_height0 = align(src_height0, 4);616box.width = align(box.width, 4);617box.height = align(box.height, 4);618619switch (util_format_get_blocksize(dst_templ.format)) {620case 8:621/* one 4x4 pixel block has 8 bytes.622* we set 1 pixel = 4 bytes ===> 1 block corrensponds to 2 pixels. */623dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;624dst_width0 = dst_width0 / 2;625src_width0 = src_width0 / 2;626dstx /= 2;627box.x /= 2;628box.width /= 2;629break;630case 16:631/* one 4x4 pixel block has 16 bytes.632* we set 1 pixel = 4 bytes ===> 1 block corresponds to 4 pixels. */633dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;634break;635}636src_templ.format = dst_templ.format;637638dst_height0 = dst_height0 / 4;639src_height0 = src_height0 / 4;640dsty /= 4;641box.y /= 4;642box.height /= 4;643}644645/* Fallback for textures. */646if (!screen->is_format_supported(screen, dst_templ.format,647dst->target, dst->nr_samples,648dst->nr_storage_samples,649PIPE_BIND_RENDER_TARGET) ||650!screen->is_format_supported(screen, src_templ.format,651src->target, src->nr_samples,652src->nr_storage_samples,653PIPE_BIND_SAMPLER_VIEW)) {654assert(0 && "this shouldn't happen, update r300_is_blit_supported");655util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,656src, src_level, src_box);657return;658}659660/* Decompress ZMASK. */661if (r300->zmask_in_use && !r300->locked_zbuffer) {662if (fb->zsbuf->texture == src ||663fb->zsbuf->texture == dst) {664r300_decompress_zmask(r300);665}666}667668dst_view = r300_create_surface_custom(pipe, dst, &dst_templ, dst_width0, dst_height0);669src_view = r300_create_sampler_view_custom(pipe, src, &src_templ, src_width0, src_height0);670671u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),672abs(src_box->depth), &dstbox);673674r300_blitter_begin(r300, R300_COPY);675util_blitter_blit_generic(r300->blitter, dst_view, &dstbox,676src_view, src_box, src_width0, src_height0,677PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,678FALSE);679r300_blitter_end(r300);680681pipe_surface_reference(&dst_view, NULL);682pipe_sampler_view_reference(&src_view, NULL);683}684685static boolean r300_is_simple_msaa_resolve(const struct pipe_blit_info *info)686{687unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);688unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);689690return info->src.resource->nr_samples > 1 &&691info->dst.resource->nr_samples <= 1 &&692info->dst.resource->format == info->src.resource->format &&693info->dst.resource->format == info->dst.format &&694info->src.resource->format == info->src.format &&695!info->scissor_enable &&696info->mask == PIPE_MASK_RGBA &&697dst_width == info->src.resource->width0 &&698dst_height == info->src.resource->height0 &&699info->dst.box.x == 0 &&700info->dst.box.y == 0 &&701info->dst.box.width == dst_width &&702info->dst.box.height == dst_height &&703info->src.box.x == 0 &&704info->src.box.y == 0 &&705info->src.box.width == dst_width &&706info->src.box.height == dst_height &&707(r300_resource(info->dst.resource)->tex.microtile != RADEON_LAYOUT_LINEAR ||708r300_resource(info->dst.resource)->tex.macrotile[info->dst.level] != RADEON_LAYOUT_LINEAR);709}710711static void r300_simple_msaa_resolve(struct pipe_context *pipe,712struct pipe_resource *dst,713unsigned dst_level,714unsigned dst_layer,715struct pipe_resource *src,716enum pipe_format format)717{718struct r300_context *r300 = r300_context(pipe);719struct r300_surface *srcsurf, *dstsurf;720struct pipe_surface surf_tmpl;721struct r300_aa_state *aa = (struct r300_aa_state*)r300->aa_state.state;722723memset(&surf_tmpl, 0, sizeof(surf_tmpl));724surf_tmpl.format = format;725srcsurf = r300_surface(pipe->create_surface(pipe, src, &surf_tmpl));726727surf_tmpl.format = format;728surf_tmpl.u.tex.level = dst_level;729surf_tmpl.u.tex.first_layer =730surf_tmpl.u.tex.last_layer = dst_layer;731dstsurf = r300_surface(pipe->create_surface(pipe, dst, &surf_tmpl));732733/* COLORPITCH should contain the tiling info of the resolve buffer.734* The tiling of the AA buffer isn't programmable anyway. */735srcsurf->pitch &= ~(R300_COLOR_TILE(1) | R300_COLOR_MICROTILE(3));736srcsurf->pitch |= dstsurf->pitch & (R300_COLOR_TILE(1) | R300_COLOR_MICROTILE(3));737738/* Enable AA resolve. */739aa->dest = dstsurf;740r300->aa_state.size = 8;741r300_mark_atom_dirty(r300, &r300->aa_state);742743/* Resolve the surface. */744r300_blitter_begin(r300, R300_CLEAR_SURFACE);745util_blitter_custom_color(r300->blitter, &srcsurf->base, NULL);746r300_blitter_end(r300);747748/* Disable AA resolve. */749aa->dest = NULL;750r300->aa_state.size = 4;751r300_mark_atom_dirty(r300, &r300->aa_state);752753pipe_surface_reference((struct pipe_surface**)&srcsurf, NULL);754pipe_surface_reference((struct pipe_surface**)&dstsurf, NULL);755}756757static void r300_msaa_resolve(struct pipe_context *pipe,758const struct pipe_blit_info *info)759{760struct r300_context *r300 = r300_context(pipe);761struct pipe_screen *screen = pipe->screen;762struct pipe_resource *tmp, templ;763struct pipe_blit_info blit;764765assert(info->src.level == 0);766assert(info->src.box.z == 0);767assert(info->src.box.depth == 1);768assert(info->dst.box.depth == 1);769770if (r300_is_simple_msaa_resolve(info)) {771r300_simple_msaa_resolve(pipe, info->dst.resource, info->dst.level,772info->dst.box.z, info->src.resource,773info->src.format);774return;775}776777/* resolve into a temporary texture, then blit */778memset(&templ, 0, sizeof(templ));779templ.target = PIPE_TEXTURE_2D;780templ.format = info->src.resource->format;781templ.width0 = info->src.resource->width0;782templ.height0 = info->src.resource->height0;783templ.depth0 = 1;784templ.array_size = 1;785templ.usage = PIPE_USAGE_DEFAULT;786templ.flags = R300_RESOURCE_FORCE_MICROTILING;787788tmp = screen->resource_create(screen, &templ);789790/* resolve */791r300_simple_msaa_resolve(pipe, tmp, 0, 0, info->src.resource,792info->src.format);793794/* blit */795blit = *info;796blit.src.resource = tmp;797blit.src.box.z = 0;798799r300_blitter_begin(r300, R300_BLIT | R300_IGNORE_RENDER_COND);800util_blitter_blit(r300->blitter, &blit);801r300_blitter_end(r300);802803pipe_resource_reference(&tmp, NULL);804}805806static void r300_blit(struct pipe_context *pipe,807const struct pipe_blit_info *blit)808{809struct r300_context *r300 = r300_context(pipe);810struct pipe_framebuffer_state *fb =811(struct pipe_framebuffer_state*)r300->fb_state.state;812struct pipe_blit_info info = *blit;813814/* The driver supports sRGB textures but not framebuffers. Blitting815* from sRGB to sRGB should be the same as blitting from linear816* to linear, so use that, This avoids incorrect linearization.817*/818if (util_format_is_srgb(info.src.format)) {819info.src.format = util_format_linear(info.src.format);820info.dst.format = util_format_linear(info.dst.format);821}822823/* MSAA resolve. */824if (info.src.resource->nr_samples > 1 &&825!util_format_is_depth_or_stencil(info.src.resource->format)) {826r300_msaa_resolve(pipe, &info);827return;828}829830/* Can't read MSAA textures. */831if (info.src.resource->nr_samples > 1) {832return;833}834835/* Blit a combined depth-stencil resource as color.836* S8Z24 is the only supported stencil format. */837if ((info.mask & PIPE_MASK_S) &&838info.src.format == PIPE_FORMAT_S8_UINT_Z24_UNORM &&839info.dst.format == PIPE_FORMAT_S8_UINT_Z24_UNORM) {840if (info.dst.resource->nr_samples > 1) {841/* Cannot do that with MSAA buffers. */842info.mask &= ~PIPE_MASK_S;843if (!(info.mask & PIPE_MASK_Z)) {844return;845}846} else {847/* Single-sample buffer. */848info.src.format = PIPE_FORMAT_B8G8R8A8_UNORM;849info.dst.format = PIPE_FORMAT_B8G8R8A8_UNORM;850if (info.mask & PIPE_MASK_Z) {851info.mask = PIPE_MASK_RGBA; /* depth+stencil */852} else {853info.mask = PIPE_MASK_B; /* stencil only */854}855}856}857858/* Decompress ZMASK. */859if (r300->zmask_in_use && !r300->locked_zbuffer) {860if (fb->zsbuf->texture == info.src.resource ||861fb->zsbuf->texture == info.dst.resource) {862r300_decompress_zmask(r300);863}864}865866r300_blitter_begin(r300, R300_BLIT |867(info.render_condition_enable ? 0 : R300_IGNORE_RENDER_COND));868util_blitter_blit(r300->blitter, &info);869r300_blitter_end(r300);870}871872static void r300_flush_resource(struct pipe_context *ctx,873struct pipe_resource *resource)874{875}876877void r300_init_blit_functions(struct r300_context *r300)878{879r300->context.clear = r300_clear;880r300->context.clear_render_target = r300_clear_render_target;881r300->context.clear_depth_stencil = r300_clear_depth_stencil;882r300->context.resource_copy_region = r300_resource_copy_region;883r300->context.blit = r300_blit;884r300->context.flush_resource = r300_flush_resource;885}886887888