Path: blob/21.2-virgl/src/gallium/drivers/r600/r600_blit.c
4570 views
/*1* Copyright 2010 Jerome Glisse <[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.21*/22#include "r600_pipe.h"23#include "compute_memory_pool.h"24#include "evergreen_compute.h"25#include "util/u_surface.h"26#include "util/format/u_format.h"27#include "evergreend.h"2829enum r600_blitter_op /* bitmask */30{31R600_SAVE_FRAGMENT_STATE = 1,32R600_SAVE_TEXTURES = 2,33R600_SAVE_FRAMEBUFFER = 4,34R600_DISABLE_RENDER_COND = 8,3536R600_CLEAR = R600_SAVE_FRAGMENT_STATE,3738R600_CLEAR_SURFACE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER,3940R600_COPY_BUFFER = R600_DISABLE_RENDER_COND,4142R600_COPY_TEXTURE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES |43R600_DISABLE_RENDER_COND,4445R600_BLIT = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES,4647R600_DECOMPRESS = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_DISABLE_RENDER_COND,4849R600_COLOR_RESOLVE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER50};5152static void r600_blitter_begin(struct pipe_context *ctx, enum r600_blitter_op op)53{54struct r600_context *rctx = (struct r600_context *)ctx;5556if (rctx->cmd_buf_is_compute) {57rctx->b.gfx.flush(rctx, PIPE_FLUSH_ASYNC, NULL);58rctx->cmd_buf_is_compute = false;59}6061util_blitter_save_vertex_buffer_slot(rctx->blitter, rctx->vertex_buffer_state.vb);62util_blitter_save_vertex_elements(rctx->blitter, rctx->vertex_fetch_shader.cso);63util_blitter_save_vertex_shader(rctx->blitter, rctx->vs_shader);64util_blitter_save_geometry_shader(rctx->blitter, rctx->gs_shader);65util_blitter_save_tessctrl_shader(rctx->blitter, rctx->tcs_shader);66util_blitter_save_tesseval_shader(rctx->blitter, rctx->tes_shader);67util_blitter_save_so_targets(rctx->blitter, rctx->b.streamout.num_targets,68(struct pipe_stream_output_target**)rctx->b.streamout.targets);69util_blitter_save_rasterizer(rctx->blitter, rctx->rasterizer_state.cso);7071if (op & R600_SAVE_FRAGMENT_STATE) {72util_blitter_save_viewport(rctx->blitter, &rctx->b.viewports.states[0]);73util_blitter_save_scissor(rctx->blitter, &rctx->b.scissors.states[0]);74util_blitter_save_fragment_shader(rctx->blitter, rctx->ps_shader);75util_blitter_save_blend(rctx->blitter, rctx->blend_state.cso);76util_blitter_save_depth_stencil_alpha(rctx->blitter, rctx->dsa_state.cso);77util_blitter_save_stencil_ref(rctx->blitter, &rctx->stencil_ref.pipe_state);78util_blitter_save_sample_mask(rctx->blitter, rctx->sample_mask.sample_mask);79}8081if (op & R600_SAVE_FRAMEBUFFER)82util_blitter_save_framebuffer(rctx->blitter, &rctx->framebuffer.state);8384if (op & R600_SAVE_TEXTURES) {85util_blitter_save_fragment_sampler_states(86rctx->blitter, util_last_bit(rctx->samplers[PIPE_SHADER_FRAGMENT].states.enabled_mask),87(void**)rctx->samplers[PIPE_SHADER_FRAGMENT].states.states);8889util_blitter_save_fragment_sampler_views(90rctx->blitter, util_last_bit(rctx->samplers[PIPE_SHADER_FRAGMENT].views.enabled_mask),91(struct pipe_sampler_view**)rctx->samplers[PIPE_SHADER_FRAGMENT].views.views);92}9394if (op & R600_DISABLE_RENDER_COND)95rctx->b.render_cond_force_off = true;96}9798static void r600_blitter_end(struct pipe_context *ctx)99{100struct r600_context *rctx = (struct r600_context *)ctx;101102rctx->b.render_cond_force_off = false;103}104105static unsigned u_max_sample(struct pipe_resource *r)106{107return r->nr_samples ? r->nr_samples - 1 : 0;108}109110static void r600_blit_decompress_depth(struct pipe_context *ctx,111struct r600_texture *texture,112struct r600_texture *staging,113unsigned first_level, unsigned last_level,114unsigned first_layer, unsigned last_layer,115unsigned first_sample, unsigned last_sample)116{117struct r600_context *rctx = (struct r600_context *)ctx;118unsigned layer, level, sample, checked_last_layer, max_layer, max_sample;119struct r600_texture *flushed_depth_texture = staging ?120staging : texture->flushed_depth_texture;121const struct util_format_description *desc =122util_format_description(texture->resource.b.b.format);123float depth;124125if (!staging && !texture->dirty_level_mask)126return;127128max_sample = u_max_sample(&texture->resource.b.b);129130/* XXX Decompressing MSAA depth textures is broken on R6xx.131* There is also a hardlock if CMASK and FMASK are not present.132* Just skip this until we find out how to fix it. */133if (rctx->b.chip_class == R600 && max_sample > 0) {134texture->dirty_level_mask = 0;135return;136}137138if (rctx->b.family == CHIP_RV610 || rctx->b.family == CHIP_RV630 ||139rctx->b.family == CHIP_RV620 || rctx->b.family == CHIP_RV635)140depth = 0.0f;141else142depth = 1.0f;143144/* Enable decompression in DB_RENDER_CONTROL */145rctx->db_misc_state.flush_depthstencil_through_cb = true;146rctx->db_misc_state.copy_depth = util_format_has_depth(desc);147rctx->db_misc_state.copy_stencil = util_format_has_stencil(desc);148rctx->db_misc_state.copy_sample = first_sample;149r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);150151for (level = first_level; level <= last_level; level++) {152if (!staging && !(texture->dirty_level_mask & (1 << level)))153continue;154155/* The smaller the mipmap level, the less layers there are156* as far as 3D textures are concerned. */157max_layer = util_max_layer(&texture->resource.b.b, level);158checked_last_layer = last_layer < max_layer ? last_layer : max_layer;159160for (layer = first_layer; layer <= checked_last_layer; layer++) {161for (sample = first_sample; sample <= last_sample; sample++) {162struct pipe_surface *zsurf, *cbsurf, surf_tmpl;163164if (sample != rctx->db_misc_state.copy_sample) {165rctx->db_misc_state.copy_sample = sample;166r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);167}168169surf_tmpl.format = texture->resource.b.b.format;170surf_tmpl.u.tex.level = level;171surf_tmpl.u.tex.first_layer = layer;172surf_tmpl.u.tex.last_layer = layer;173174zsurf = ctx->create_surface(ctx, &texture->resource.b.b, &surf_tmpl);175176surf_tmpl.format = flushed_depth_texture->resource.b.b.format;177cbsurf = ctx->create_surface(ctx,178&flushed_depth_texture->resource.b.b, &surf_tmpl);179180r600_blitter_begin(ctx, R600_DECOMPRESS);181util_blitter_custom_depth_stencil(rctx->blitter, zsurf, cbsurf, 1 << sample,182rctx->custom_dsa_flush, depth);183r600_blitter_end(ctx);184185pipe_surface_reference(&zsurf, NULL);186pipe_surface_reference(&cbsurf, NULL);187}188}189190/* The texture will always be dirty if some layers or samples aren't flushed.191* I don't think this case occurs often though. */192if (!staging &&193first_layer == 0 && last_layer == max_layer &&194first_sample == 0 && last_sample == max_sample) {195texture->dirty_level_mask &= ~(1 << level);196}197}198199/* reenable compression in DB_RENDER_CONTROL */200rctx->db_misc_state.flush_depthstencil_through_cb = false;201r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);202}203204static void r600_blit_decompress_depth_in_place(struct r600_context *rctx,205struct r600_texture *texture,206bool is_stencil_sampler,207unsigned first_level, unsigned last_level,208unsigned first_layer, unsigned last_layer)209{210struct pipe_surface *zsurf, surf_tmpl = {{0}};211unsigned layer, max_layer, checked_last_layer, level;212unsigned *dirty_level_mask;213214/* Enable decompression in DB_RENDER_CONTROL */215if (is_stencil_sampler) {216rctx->db_misc_state.flush_stencil_inplace = true;217dirty_level_mask = &texture->stencil_dirty_level_mask;218} else {219rctx->db_misc_state.flush_depth_inplace = true;220dirty_level_mask = &texture->dirty_level_mask;221}222r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);223224surf_tmpl.format = texture->resource.b.b.format;225226for (level = first_level; level <= last_level; level++) {227if (!(*dirty_level_mask & (1 << level)))228continue;229230surf_tmpl.u.tex.level = level;231232/* The smaller the mipmap level, the less layers there are233* as far as 3D textures are concerned. */234max_layer = util_max_layer(&texture->resource.b.b, level);235checked_last_layer = last_layer < max_layer ? last_layer : max_layer;236237for (layer = first_layer; layer <= checked_last_layer; layer++) {238surf_tmpl.u.tex.first_layer = layer;239surf_tmpl.u.tex.last_layer = layer;240241zsurf = rctx->b.b.create_surface(&rctx->b.b, &texture->resource.b.b, &surf_tmpl);242243r600_blitter_begin(&rctx->b.b, R600_DECOMPRESS);244util_blitter_custom_depth_stencil(rctx->blitter, zsurf, NULL, ~0,245rctx->custom_dsa_flush, 1.0f);246r600_blitter_end(&rctx->b.b);247248pipe_surface_reference(&zsurf, NULL);249}250251/* The texture will always be dirty if some layers or samples aren't flushed.252* I don't think this case occurs often though. */253if (first_layer == 0 && last_layer == max_layer) {254*dirty_level_mask &= ~(1 << level);255}256}257258/* Disable decompression in DB_RENDER_CONTROL */259rctx->db_misc_state.flush_depth_inplace = false;260rctx->db_misc_state.flush_stencil_inplace = false;261r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);262}263264void r600_decompress_depth_textures(struct r600_context *rctx,265struct r600_samplerview_state *textures)266{267unsigned i;268unsigned depth_texture_mask = textures->compressed_depthtex_mask;269270while (depth_texture_mask) {271struct pipe_sampler_view *view;272struct r600_pipe_sampler_view *rview;273struct r600_texture *tex;274275i = u_bit_scan(&depth_texture_mask);276277view = &textures->views[i]->base;278assert(view);279rview = (struct r600_pipe_sampler_view*)view;280281tex = (struct r600_texture *)view->texture;282assert(tex->db_compatible);283284if (r600_can_sample_zs(tex, rview->is_stencil_sampler)) {285r600_blit_decompress_depth_in_place(rctx, tex,286rview->is_stencil_sampler,287view->u.tex.first_level, view->u.tex.last_level,2880, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));289} else {290r600_blit_decompress_depth(&rctx->b.b, tex, NULL,291view->u.tex.first_level, view->u.tex.last_level,2920, util_max_layer(&tex->resource.b.b, view->u.tex.first_level),2930, u_max_sample(&tex->resource.b.b));294}295}296}297298void r600_decompress_depth_images(struct r600_context *rctx,299struct r600_image_state *images)300{301unsigned i;302unsigned depth_texture_mask = images->compressed_depthtex_mask;303304while (depth_texture_mask) {305struct r600_image_view *view;306struct r600_texture *tex;307308i = u_bit_scan(&depth_texture_mask);309310view = &images->views[i];311assert(view);312313tex = (struct r600_texture *)view->base.resource;314assert(tex->db_compatible);315316if (r600_can_sample_zs(tex, false)) {317r600_blit_decompress_depth_in_place(rctx, tex,318false,319view->base.u.tex.level,320view->base.u.tex.level,3210, util_max_layer(&tex->resource.b.b, view->base.u.tex.level));322} else {323r600_blit_decompress_depth(&rctx->b.b, tex, NULL,324view->base.u.tex.level,325view->base.u.tex.level,3260, util_max_layer(&tex->resource.b.b, view->base.u.tex.level),3270, u_max_sample(&tex->resource.b.b));328}329}330}331332static void r600_blit_decompress_color(struct pipe_context *ctx,333struct r600_texture *rtex,334unsigned first_level, unsigned last_level,335unsigned first_layer, unsigned last_layer)336{337struct r600_context *rctx = (struct r600_context *)ctx;338unsigned layer, level, checked_last_layer, max_layer;339340if (!rtex->dirty_level_mask)341return;342343for (level = first_level; level <= last_level; level++) {344if (!(rtex->dirty_level_mask & (1 << level)))345continue;346347/* The smaller the mipmap level, the less layers there are348* as far as 3D textures are concerned. */349max_layer = util_max_layer(&rtex->resource.b.b, level);350checked_last_layer = last_layer < max_layer ? last_layer : max_layer;351352for (layer = first_layer; layer <= checked_last_layer; layer++) {353struct pipe_surface *cbsurf, surf_tmpl;354355surf_tmpl.format = rtex->resource.b.b.format;356surf_tmpl.u.tex.level = level;357surf_tmpl.u.tex.first_layer = layer;358surf_tmpl.u.tex.last_layer = layer;359cbsurf = ctx->create_surface(ctx, &rtex->resource.b.b, &surf_tmpl);360361r600_blitter_begin(ctx, R600_DECOMPRESS);362util_blitter_custom_color(rctx->blitter, cbsurf,363rtex->fmask.size ? rctx->custom_blend_decompress : rctx->custom_blend_fastclear);364r600_blitter_end(ctx);365366pipe_surface_reference(&cbsurf, NULL);367}368369/* The texture will always be dirty if some layers aren't flushed.370* I don't think this case occurs often though. */371if (first_layer == 0 && last_layer == max_layer) {372rtex->dirty_level_mask &= ~(1 << level);373}374}375}376377void r600_decompress_color_textures(struct r600_context *rctx,378struct r600_samplerview_state *textures)379{380unsigned i;381unsigned mask = textures->compressed_colortex_mask;382383while (mask) {384struct pipe_sampler_view *view;385struct r600_texture *tex;386387i = u_bit_scan(&mask);388389view = &textures->views[i]->base;390assert(view);391392tex = (struct r600_texture *)view->texture;393assert(tex->cmask.size);394395r600_blit_decompress_color(&rctx->b.b, tex,396view->u.tex.first_level, view->u.tex.last_level,3970, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));398}399}400401void r600_decompress_color_images(struct r600_context *rctx,402struct r600_image_state *images)403{404unsigned i;405unsigned mask = images->compressed_colortex_mask;406407while (mask) {408struct r600_image_view *view;409struct r600_texture *tex;410411i = u_bit_scan(&mask);412413view = &images->views[i];414assert(view);415416tex = (struct r600_texture *)view->base.resource;417assert(tex->cmask.size);418419r600_blit_decompress_color(&rctx->b.b, tex,420view->base.u.tex.level, view->base.u.tex.level,421view->base.u.tex.first_layer,422view->base.u.tex.last_layer);423}424}425426/* Helper for decompressing a portion of a color or depth resource before427* blitting if any decompression is needed.428* The driver doesn't decompress resources automatically while u_blitter is429* rendering. */430static bool r600_decompress_subresource(struct pipe_context *ctx,431struct pipe_resource *tex,432unsigned level,433unsigned first_layer, unsigned last_layer)434{435struct r600_context *rctx = (struct r600_context *)ctx;436struct r600_texture *rtex = (struct r600_texture*)tex;437438if (rtex->db_compatible) {439if (r600_can_sample_zs(rtex, false)) {440r600_blit_decompress_depth_in_place(rctx, rtex, false,441level, level,442first_layer, last_layer);443if (rtex->surface.has_stencil) {444r600_blit_decompress_depth_in_place(rctx, rtex, true,445level, level,446first_layer, last_layer);447}448} else {449if (!r600_init_flushed_depth_texture(ctx, tex, NULL))450return false; /* error */451452r600_blit_decompress_depth(ctx, rtex, NULL,453level, level,454first_layer, last_layer,4550, u_max_sample(tex));456}457} else if (rtex->cmask.size) {458r600_blit_decompress_color(ctx, rtex, level, level,459first_layer, last_layer);460}461return true;462}463464static void r600_clear(struct pipe_context *ctx, unsigned buffers,465const struct pipe_scissor_state *scissor_state,466const union pipe_color_union *color,467double depth, unsigned stencil)468{469struct r600_context *rctx = (struct r600_context *)ctx;470struct pipe_framebuffer_state *fb = &rctx->framebuffer.state;471472if (buffers & PIPE_CLEAR_COLOR && rctx->b.chip_class >= EVERGREEN) {473evergreen_do_fast_color_clear(&rctx->b, fb, &rctx->framebuffer.atom,474&buffers, NULL, color);475if (!buffers)476return; /* all buffers have been fast cleared */477}478479if (buffers & PIPE_CLEAR_COLOR) {480int i;481482/* These buffers cannot use fast clear, make sure to disable expansion. */483for (i = 0; i < fb->nr_cbufs; i++) {484struct r600_texture *tex;485486/* If not clearing this buffer, skip. */487if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))488continue;489490if (!fb->cbufs[i])491continue;492493tex = (struct r600_texture *)fb->cbufs[i]->texture;494if (tex->fmask.size == 0)495tex->dirty_level_mask &= ~(1 << fb->cbufs[i]->u.tex.level);496}497}498499/* if hyperz enabled just clear hyperz */500if (fb->zsbuf && (buffers & PIPE_CLEAR_DEPTH)) {501struct r600_texture *rtex;502unsigned level = fb->zsbuf->u.tex.level;503504rtex = (struct r600_texture*)fb->zsbuf->texture;505506/* We can't use hyperz fast clear if each slice of a texture507* array are clear to different value. To simplify code just508* disable fast clear for texture array.509*/510if (r600_htile_enabled(rtex, level) &&511fb->zsbuf->u.tex.first_layer == 0 &&512fb->zsbuf->u.tex.last_layer == util_max_layer(&rtex->resource.b.b, level)) {513if (rtex->depth_clear_value != depth) {514rtex->depth_clear_value = depth;515r600_mark_atom_dirty(rctx, &rctx->db_state.atom);516}517rctx->db_misc_state.htile_clear = true;518r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);519}520}521522r600_blitter_begin(ctx, R600_CLEAR);523util_blitter_clear(rctx->blitter, fb->width, fb->height,524util_framebuffer_get_num_layers(fb),525buffers, color, depth, stencil,526util_framebuffer_get_num_samples(fb) > 1);527r600_blitter_end(ctx);528529/* disable fast clear */530if (rctx->db_misc_state.htile_clear) {531rctx->db_misc_state.htile_clear = false;532r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);533}534}535536static void r600_clear_render_target(struct pipe_context *ctx,537struct pipe_surface *dst,538const union pipe_color_union *color,539unsigned dstx, unsigned dsty,540unsigned width, unsigned height,541bool render_condition_enabled)542{543struct r600_context *rctx = (struct r600_context *)ctx;544545r600_blitter_begin(ctx, R600_CLEAR_SURFACE |546(render_condition_enabled ? 0 : R600_DISABLE_RENDER_COND));547util_blitter_clear_render_target(rctx->blitter, dst, color,548dstx, dsty, width, height);549r600_blitter_end(ctx);550}551552static void r600_clear_depth_stencil(struct pipe_context *ctx,553struct pipe_surface *dst,554unsigned clear_flags,555double depth,556unsigned stencil,557unsigned dstx, unsigned dsty,558unsigned width, unsigned height,559bool render_condition_enabled)560{561struct r600_context *rctx = (struct r600_context *)ctx;562563r600_blitter_begin(ctx, R600_CLEAR_SURFACE |564(render_condition_enabled ? 0 : R600_DISABLE_RENDER_COND));565util_blitter_clear_depth_stencil(rctx->blitter, dst, clear_flags, depth, stencil,566dstx, dsty, width, height);567r600_blitter_end(ctx);568}569570static void r600_copy_buffer(struct pipe_context *ctx, struct pipe_resource *dst, unsigned dstx,571struct pipe_resource *src, const struct pipe_box *src_box)572{573struct r600_context *rctx = (struct r600_context*)ctx;574575if (rctx->screen->b.has_cp_dma) {576r600_cp_dma_copy_buffer(rctx, dst, dstx, src, src_box->x, src_box->width);577}578else if (rctx->screen->b.has_streamout &&579/* Require 4-byte alignment. */580dstx % 4 == 0 && src_box->x % 4 == 0 && src_box->width % 4 == 0) {581582r600_blitter_begin(ctx, R600_COPY_BUFFER);583util_blitter_copy_buffer(rctx->blitter, dst, dstx, src, src_box->x, src_box->width);584r600_blitter_end(ctx);585} else {586util_resource_copy_region(ctx, dst, 0, dstx, 0, 0, src, 0, src_box);587}588}589590/**591* Global buffers are not really resources, they are are actually offsets592* into a single global resource (r600_screen::global_pool). The means593* they don't have their own buf handle, so they cannot be passed594* to r600_copy_buffer() and must be handled separately.595*/596static void r600_copy_global_buffer(struct pipe_context *ctx,597struct pipe_resource *dst, unsigned598dstx, struct pipe_resource *src,599const struct pipe_box *src_box)600{601struct r600_context *rctx = (struct r600_context*)ctx;602struct compute_memory_pool *pool = rctx->screen->global_pool;603struct pipe_box new_src_box = *src_box;604605if (src->bind & PIPE_BIND_GLOBAL) {606struct r600_resource_global *rsrc =607(struct r600_resource_global *)src;608struct compute_memory_item *item = rsrc->chunk;609610if (is_item_in_pool(item)) {611new_src_box.x += 4 * item->start_in_dw;612src = (struct pipe_resource *)pool->bo;613} else {614if (item->real_buffer == NULL) {615item->real_buffer =616r600_compute_buffer_alloc_vram(pool->screen,617item->size_in_dw * 4);618}619src = (struct pipe_resource*)item->real_buffer;620}621}622if (dst->bind & PIPE_BIND_GLOBAL) {623struct r600_resource_global *rdst =624(struct r600_resource_global *)dst;625struct compute_memory_item *item = rdst->chunk;626627if (is_item_in_pool(item)) {628dstx += 4 * item->start_in_dw;629dst = (struct pipe_resource *)pool->bo;630} else {631if (item->real_buffer == NULL) {632item->real_buffer =633r600_compute_buffer_alloc_vram(pool->screen,634item->size_in_dw * 4);635}636dst = (struct pipe_resource*)item->real_buffer;637}638}639640r600_copy_buffer(ctx, dst, dstx, src, &new_src_box);641}642643static void r600_clear_buffer(struct pipe_context *ctx, struct pipe_resource *dst,644uint64_t offset, uint64_t size, unsigned value,645enum r600_coherency coher)646{647struct r600_context *rctx = (struct r600_context*)ctx;648649if (rctx->screen->b.has_cp_dma &&650rctx->b.chip_class >= EVERGREEN &&651offset % 4 == 0 && size % 4 == 0) {652evergreen_cp_dma_clear_buffer(rctx, dst, offset, size, value, coher);653} else if (rctx->screen->b.has_streamout && offset % 4 == 0 && size % 4 == 0) {654union pipe_color_union clear_value;655clear_value.ui[0] = value;656657r600_blitter_begin(ctx, R600_DISABLE_RENDER_COND);658util_blitter_clear_buffer(rctx->blitter, dst, offset, size,6591, &clear_value);660r600_blitter_end(ctx);661} else {662uint32_t *map = r600_buffer_map_sync_with_rings(&rctx->b, r600_resource(dst),663PIPE_MAP_WRITE);664map += offset / 4;665size /= 4;666for (unsigned i = 0; i < size; i++)667*map++ = value;668}669}670671void r600_resource_copy_region(struct pipe_context *ctx,672struct pipe_resource *dst,673unsigned dst_level,674unsigned dstx, unsigned dsty, unsigned dstz,675struct pipe_resource *src,676unsigned src_level,677const struct pipe_box *src_box)678{679struct r600_context *rctx = (struct r600_context *)ctx;680struct pipe_surface *dst_view, dst_templ;681struct pipe_sampler_view src_templ, *src_view;682unsigned dst_width, dst_height, src_width0, src_height0, src_widthFL, src_heightFL;683unsigned src_force_level = 0;684struct pipe_box sbox, dstbox;685686/* Handle buffers first. */687if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {688if ((src->bind & PIPE_BIND_GLOBAL) ||689(dst->bind & PIPE_BIND_GLOBAL)) {690r600_copy_global_buffer(ctx, dst, dstx, src, src_box);691} else {692r600_copy_buffer(ctx, dst, dstx, src, src_box);693}694return;695}696697assert(u_max_sample(dst) == u_max_sample(src));698699/* The driver doesn't decompress resources automatically while700* u_blitter is rendering. */701if (!r600_decompress_subresource(ctx, src, src_level,702src_box->z, src_box->z + src_box->depth - 1)) {703return; /* error */704}705706dst_width = u_minify(dst->width0, dst_level);707dst_height = u_minify(dst->height0, dst_level);708src_width0 = src->width0;709src_height0 = src->height0;710src_widthFL = u_minify(src->width0, src_level);711src_heightFL = u_minify(src->height0, src_level);712713util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);714util_blitter_default_src_texture(rctx->blitter, &src_templ, src, src_level);715716if (util_format_is_compressed(src->format) ||717util_format_is_compressed(dst->format)) {718unsigned blocksize = util_format_get_blocksize(src->format);719720if (blocksize == 8)721src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */722else723src_templ.format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */724dst_templ.format = src_templ.format;725726dst_width = util_format_get_nblocksx(dst->format, dst_width);727dst_height = util_format_get_nblocksy(dst->format, dst_height);728src_width0 = util_format_get_nblocksx(src->format, src_width0);729src_height0 = util_format_get_nblocksy(src->format, src_height0);730src_widthFL = util_format_get_nblocksx(src->format, src_widthFL);731src_heightFL = util_format_get_nblocksy(src->format, src_heightFL);732733dstx = util_format_get_nblocksx(dst->format, dstx);734dsty = util_format_get_nblocksy(dst->format, dsty);735736sbox.x = util_format_get_nblocksx(src->format, src_box->x);737sbox.y = util_format_get_nblocksy(src->format, src_box->y);738sbox.z = src_box->z;739sbox.width = util_format_get_nblocksx(src->format, src_box->width);740sbox.height = util_format_get_nblocksy(src->format, src_box->height);741sbox.depth = src_box->depth;742src_box = &sbox;743744src_force_level = src_level;745} else if (!util_blitter_is_copy_supported(rctx->blitter, dst, src)) {746if (util_format_is_subsampled_422(src->format)) {747748src_templ.format = PIPE_FORMAT_R8G8B8A8_UINT;749dst_templ.format = PIPE_FORMAT_R8G8B8A8_UINT;750751dst_width = util_format_get_nblocksx(dst->format, dst_width);752src_width0 = util_format_get_nblocksx(src->format, src_width0);753src_widthFL = util_format_get_nblocksx(src->format, src_widthFL);754755dstx = util_format_get_nblocksx(dst->format, dstx);756757sbox = *src_box;758sbox.x = util_format_get_nblocksx(src->format, src_box->x);759sbox.width = util_format_get_nblocksx(src->format, src_box->width);760src_box = &sbox;761} else {762unsigned blocksize = util_format_get_blocksize(src->format);763764switch (blocksize) {765case 1:766dst_templ.format = PIPE_FORMAT_R8_UNORM;767src_templ.format = PIPE_FORMAT_R8_UNORM;768break;769case 2:770dst_templ.format = PIPE_FORMAT_R8G8_UNORM;771src_templ.format = PIPE_FORMAT_R8G8_UNORM;772break;773case 4:774dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;775src_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;776break;777case 8:778dst_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;779src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;780break;781case 16:782dst_templ.format = PIPE_FORMAT_R32G32B32A32_UINT;783src_templ.format = PIPE_FORMAT_R32G32B32A32_UINT;784break;785default:786fprintf(stderr, "Unhandled format %s with blocksize %u\n",787util_format_short_name(src->format), blocksize);788assert(0);789}790}791}792793dst_view = r600_create_surface_custom(ctx, dst, &dst_templ,794/* we don't care about these two for r600g */795dst->width0, dst->height0,796dst_width, dst_height);797798if (rctx->b.chip_class >= EVERGREEN) {799src_view = evergreen_create_sampler_view_custom(ctx, src, &src_templ,800src_width0, src_height0,801src_force_level);802} else {803src_view = r600_create_sampler_view_custom(ctx, src, &src_templ,804src_widthFL, src_heightFL);805}806807u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),808abs(src_box->depth), &dstbox);809810/* Copy. */811r600_blitter_begin(ctx, R600_COPY_TEXTURE);812util_blitter_blit_generic(rctx->blitter, dst_view, &dstbox,813src_view, src_box, src_width0, src_height0,814PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,815FALSE);816r600_blitter_end(ctx);817818pipe_surface_reference(&dst_view, NULL);819pipe_sampler_view_reference(&src_view, NULL);820}821822static bool do_hardware_msaa_resolve(struct pipe_context *ctx,823const struct pipe_blit_info *info)824{825struct r600_context *rctx = (struct r600_context*)ctx;826struct r600_texture *dst = (struct r600_texture*)info->dst.resource;827unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);828unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);829enum pipe_format format = info->src.format;830unsigned sample_mask =831rctx->b.chip_class == CAYMAN ? ~0 :832((1ull << MAX2(1, info->src.resource->nr_samples)) - 1);833struct pipe_resource *tmp, templ;834struct pipe_blit_info blit;835836/* Check basic requirements for hw resolve. */837if (!(info->src.resource->nr_samples > 1 &&838info->dst.resource->nr_samples <= 1 &&839!util_format_is_pure_integer(format) &&840!util_format_is_depth_or_stencil(format) &&841util_max_layer(info->src.resource, 0) == 0))842return false;843844/* Check the remaining requirements for hw resolve. */845if (util_max_layer(info->dst.resource, info->dst.level) == 0 &&846util_is_format_compatible(util_format_description(info->src.format),847util_format_description(info->dst.format)) &&848!info->scissor_enable &&849(info->mask & PIPE_MASK_RGBA) == PIPE_MASK_RGBA &&850dst_width == info->src.resource->width0 &&851dst_height == info->src.resource->height0 &&852info->dst.box.x == 0 &&853info->dst.box.y == 0 &&854info->dst.box.width == dst_width &&855info->dst.box.height == dst_height &&856info->dst.box.depth == 1 &&857info->src.box.x == 0 &&858info->src.box.y == 0 &&859info->src.box.width == dst_width &&860info->src.box.height == dst_height &&861info->src.box.depth == 1 &&862dst->surface.u.legacy.level[info->dst.level].mode >= RADEON_SURF_MODE_1D &&863(!dst->cmask.size || !dst->dirty_level_mask) /* dst cannot be fast-cleared */) {864r600_blitter_begin(ctx, R600_COLOR_RESOLVE |865(info->render_condition_enable ? 0 : R600_DISABLE_RENDER_COND));866util_blitter_custom_resolve_color(rctx->blitter,867info->dst.resource, info->dst.level,868info->dst.box.z,869info->src.resource, info->src.box.z,870sample_mask, rctx->custom_blend_resolve,871format);872r600_blitter_end(ctx);873return true;874}875876/* Shader-based resolve is VERY SLOW. Instead, resolve into877* a temporary texture and blit.878*/879memset(&templ, 0, sizeof(templ));880templ.target = PIPE_TEXTURE_2D;881templ.format = info->src.resource->format;882templ.width0 = info->src.resource->width0;883templ.height0 = info->src.resource->height0;884templ.depth0 = 1;885templ.array_size = 1;886templ.usage = PIPE_USAGE_DEFAULT;887templ.flags = R600_RESOURCE_FLAG_FORCE_TILING;888889tmp = ctx->screen->resource_create(ctx->screen, &templ);890if (!tmp)891return false;892893/* resolve */894r600_blitter_begin(ctx, R600_COLOR_RESOLVE |895(info->render_condition_enable ? 0 : R600_DISABLE_RENDER_COND));896util_blitter_custom_resolve_color(rctx->blitter, tmp, 0, 0,897info->src.resource, info->src.box.z,898sample_mask, rctx->custom_blend_resolve,899format);900r600_blitter_end(ctx);901902/* blit */903blit = *info;904blit.src.resource = tmp;905blit.src.box.z = 0;906907r600_blitter_begin(ctx, R600_BLIT |908(info->render_condition_enable ? 0 : R600_DISABLE_RENDER_COND));909util_blitter_blit(rctx->blitter, &blit);910r600_blitter_end(ctx);911912pipe_resource_reference(&tmp, NULL);913return true;914}915916static void r600_blit(struct pipe_context *ctx,917const struct pipe_blit_info *info)918{919struct r600_context *rctx = (struct r600_context*)ctx;920struct r600_texture *rdst = (struct r600_texture *)info->dst.resource;921922if (do_hardware_msaa_resolve(ctx, info)) {923return;924}925926/* Using SDMA for copying to a linear texture in GTT is much faster.927* This improves DRI PRIME performance.928*929* resource_copy_region can't do this yet, because dma_copy calls it930* on failure (recursion).931*/932if (rdst->surface.u.legacy.level[info->dst.level].mode ==933RADEON_SURF_MODE_LINEAR_ALIGNED &&934rctx->b.dma_copy &&935util_can_blit_via_copy_region(info, false)) {936rctx->b.dma_copy(ctx, info->dst.resource, info->dst.level,937info->dst.box.x, info->dst.box.y,938info->dst.box.z,939info->src.resource, info->src.level,940&info->src.box);941return;942}943944assert(util_blitter_is_blit_supported(rctx->blitter, info));945946/* The driver doesn't decompress resources automatically while947* u_blitter is rendering. */948if (!r600_decompress_subresource(ctx, info->src.resource, info->src.level,949info->src.box.z,950info->src.box.z + info->src.box.depth - 1)) {951return; /* error */952}953954if (rctx->screen->b.debug_flags & DBG_FORCE_DMA &&955util_try_blit_via_copy_region(ctx, info))956return;957958r600_blitter_begin(ctx, R600_BLIT |959(info->render_condition_enable ? 0 : R600_DISABLE_RENDER_COND));960util_blitter_blit(rctx->blitter, info);961r600_blitter_end(ctx);962}963964static void r600_flush_resource(struct pipe_context *ctx,965struct pipe_resource *res)966{967struct r600_texture *rtex = (struct r600_texture*)res;968969assert(res->target != PIPE_BUFFER);970971if (!rtex->is_depth && rtex->cmask.size) {972r600_blit_decompress_color(ctx, rtex, 0, res->last_level,9730, util_max_layer(res, 0));974}975}976977void r600_init_blit_functions(struct r600_context *rctx)978{979rctx->b.b.clear = r600_clear;980rctx->b.b.clear_render_target = r600_clear_render_target;981rctx->b.b.clear_depth_stencil = r600_clear_depth_stencil;982rctx->b.b.resource_copy_region = r600_resource_copy_region;983rctx->b.b.blit = r600_blit;984rctx->b.b.flush_resource = r600_flush_resource;985rctx->b.clear_buffer = r600_clear_buffer;986rctx->b.blit_decompress_depth = r600_blit_decompress_depth;987}988989990