Path: blob/21.2-virgl/src/gallium/drivers/asahi/agx_blit.c
4570 views
/*1* Copyright (C) 2021 Alyssa Rosenzweig2* Copyright (C) 2020-2021 Collabora, Ltd.3* Copyright (C) 2014 Broadcom4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* the rights to use, copy, modify, merge, publish, distribute, sublicense,9* and/or sell copies of the Software, and to permit persons to whom the10* Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice (including the next13* paragraph) shall be included in all copies or substantial portions of the14* Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL19* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE22* SOFTWARE.23*/2425#include "agx_state.h"26#include "compiler/nir/nir_builder.h"27#include "asahi/compiler/agx_compile.h"28#include "gallium/auxiliary/util/u_blitter.h"2930static void31agx_build_reload_shader(struct agx_device *dev)32{33nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_FRAGMENT,34&agx_nir_options, "agx_reload");35b.shader->info.internal = true;3637nir_variable *out = nir_variable_create(b.shader, nir_var_shader_out,38glsl_vector_type(GLSL_TYPE_FLOAT, 4), "output");39out->data.location = FRAG_RESULT_DATA0;4041nir_ssa_def *fragcoord = nir_load_frag_coord(&b);42nir_ssa_def *coord = nir_channels(&b, fragcoord, 0x3);4344nir_tex_instr *tex = nir_tex_instr_create(b.shader, 1);45tex->dest_type = nir_type_float32;46tex->sampler_dim = GLSL_SAMPLER_DIM_RECT;47tex->op = nir_texop_tex;48tex->src[0].src_type = nir_tex_src_coord;49tex->src[0].src = nir_src_for_ssa(coord);50tex->coord_components = 2;51nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);52nir_builder_instr_insert(&b, &tex->instr);53nir_store_var(&b, out, &tex->dest.ssa, 0xFF);5455unsigned offset = 0;56unsigned bo_size = 4096;5758struct agx_bo *bo = agx_bo_create(dev, bo_size, AGX_MEMORY_TYPE_SHADER);59dev->reload.bo = bo;6061for (unsigned i = 0; i < AGX_NUM_FORMATS; ++i) {62struct util_dynarray binary;63util_dynarray_init(&binary, NULL);6465nir_shader *s = nir_shader_clone(NULL, b.shader);66struct agx_shader_info info;6768struct agx_shader_key key = {69.fs.tib_formats[0] = i70};7172agx_compile_shader_nir(s, &key, &binary, &info);7374assert(offset + binary.size < bo_size);75memcpy(((uint8_t *) bo->ptr.cpu) + offset, binary.data, binary.size);7677dev->reload.format[i] = bo->ptr.gpu + offset;78offset += ALIGN_POT(binary.size, 128);7980util_dynarray_fini(&binary);81}82}8384static void85agx_blitter_save(struct agx_context *ctx, struct blitter_context *blitter,86bool render_cond)87{88util_blitter_save_vertex_buffer_slot(blitter, ctx->vertex_buffers);89util_blitter_save_vertex_elements(blitter, ctx->attributes);90util_blitter_save_vertex_shader(blitter, ctx->stage[PIPE_SHADER_VERTEX].shader);91util_blitter_save_rasterizer(blitter, ctx->rast);92util_blitter_save_viewport(blitter, &ctx->viewport);93util_blitter_save_scissor(blitter, &ctx->scissor);94util_blitter_save_fragment_shader(blitter, ctx->stage[PIPE_SHADER_FRAGMENT].shader);95util_blitter_save_blend(blitter, ctx->blend);96util_blitter_save_depth_stencil_alpha(blitter, &ctx->zs);97util_blitter_save_stencil_ref(blitter, &ctx->stencil_ref);98util_blitter_save_so_targets(blitter, 0, NULL);99util_blitter_save_sample_mask(blitter, ctx->sample_mask);100101util_blitter_save_framebuffer(blitter, &ctx->framebuffer);102util_blitter_save_fragment_sampler_states(blitter,103ctx->stage[PIPE_SHADER_FRAGMENT].sampler_count,104(void **)(ctx->stage[PIPE_SHADER_FRAGMENT].samplers));105util_blitter_save_fragment_sampler_views(blitter,106ctx->stage[PIPE_SHADER_FRAGMENT].texture_count,107(struct pipe_sampler_view **)ctx->stage[PIPE_SHADER_FRAGMENT].textures);108util_blitter_save_fragment_constant_buffer_slot(blitter,109ctx->stage[PIPE_SHADER_FRAGMENT].cb);110111if (!render_cond) {112util_blitter_save_render_condition(blitter,113(struct pipe_query *) ctx->cond_query,114ctx->cond_cond, ctx->cond_mode);115}116}117118void119agx_blit(struct pipe_context *pipe,120const struct pipe_blit_info *info)121{122//if (info->render_condition_enable &&123// !agx_render_condition_check(pan_context(pipe)))124// return;125126struct agx_context *ctx = agx_context(pipe);127128if (!util_blitter_is_blit_supported(ctx->blitter, info))129unreachable("Unsupported blit\n");130131agx_blitter_save(ctx, ctx->blitter, info->render_condition_enable);132util_blitter_blit(ctx->blitter, info);133}134135/* We need some fixed shaders for common rendering tasks. When colour buffer136* reload is not in use, a shader is used to clear a particular colour. At the137* end of rendering a tile, a shader is used to write it out. These shaders are138* too trivial to go through the compiler at this stage. */139#define AGX_STOP \1400x88, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, \1410x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00 \142143#define AGX_BLEND \1440x09, 0x00, 0x00, 0x04, 0xf0, 0xfc, 0x80, 0x03145146/* Clears the tilebuffer, where u6-u7 are preloaded with the FP16 clear colour1471480: 7e018c098040 bitop_mov r0, u61496: 7e058e098000 bitop_mov r1, u7150c: 09000004f0fc8003 TODO.blend151*/152153static uint8_t shader_clear[] = {1540x7e, 0x01, 0x8c, 0x09, 0x80, 0x40,1550x7e, 0x05, 0x8e, 0x09, 0x80, 0x00,156AGX_BLEND,157AGX_STOP158};159160static uint8_t shader_store[] = {1610x7e, 0x00, 0x04, 0x09, 0x80, 0x00,1620xb1, 0x80, 0x00, 0x80, 0x00, 0x4a, 0x00, 0x00, 0x0a, 0x00,163AGX_STOP164};165166void167agx_internal_shaders(struct agx_device *dev)168{169unsigned clear_offset = 0;170unsigned store_offset = 1024;171172struct agx_bo *bo = agx_bo_create(dev, 4096, AGX_MEMORY_TYPE_SHADER);173memcpy(((uint8_t *) bo->ptr.cpu) + clear_offset, shader_clear, sizeof(shader_clear));174memcpy(((uint8_t *) bo->ptr.cpu) + store_offset, shader_store, sizeof(shader_store));175176dev->internal.bo = bo;177dev->internal.clear = bo->ptr.gpu + clear_offset;178dev->internal.store = bo->ptr.gpu + store_offset;179180agx_build_reload_shader(dev);181}182183184