Path: blob/21.2-virgl/src/gallium/drivers/freedreno/a5xx/fd5_image.c
4574 views
/*1* Copyright (C) 2017 Rob Clark <[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* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* 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 NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors:23* Rob Clark <[email protected]>24*/2526#include "pipe/p_state.h"2728#include "fd5_format.h"29#include "fd5_image.h"30#include "fd5_texture.h"31#include "freedreno_resource.h"3233static enum a4xx_state_block texsb[] = {34[PIPE_SHADER_COMPUTE] = SB4_CS_TEX,35[PIPE_SHADER_FRAGMENT] = SB4_FS_TEX,36};3738static enum a4xx_state_block imgsb[] = {39[PIPE_SHADER_COMPUTE] = SB4_CS_SSBO,40[PIPE_SHADER_FRAGMENT] = SB4_SSBO,41};4243struct fd5_image {44enum pipe_format pfmt;45enum a5xx_tex_fmt fmt;46enum a5xx_tex_type type;47bool srgb;48uint32_t cpp;49uint32_t width;50uint32_t height;51uint32_t depth;52uint32_t pitch;53uint32_t array_pitch;54struct fd_bo *bo;55uint32_t offset;56bool buffer;57};5859static void60translate_image(struct fd5_image *img, struct pipe_image_view *pimg)61{62enum pipe_format format = pimg->format;63struct pipe_resource *prsc = pimg->resource;64struct fd_resource *rsc = fd_resource(prsc);6566if (!pimg->resource) {67memset(img, 0, sizeof(*img));68return;69}7071img->pfmt = format;72img->fmt = fd5_pipe2tex(format);73img->type = fd5_tex_type(prsc->target);74img->srgb = util_format_is_srgb(format);75img->cpp = rsc->layout.cpp;76img->bo = rsc->bo;7778/* Treat cube textures as 2d-array: */79if (img->type == A5XX_TEX_CUBE)80img->type = A5XX_TEX_2D;8182if (prsc->target == PIPE_BUFFER) {83img->buffer = true;84img->offset = pimg->u.buf.offset;85img->pitch = 0;86img->array_pitch = 0;8788/* size is encoded with low 15b in WIDTH and high bits in89* HEIGHT, in units of elements:90*/91unsigned sz = pimg->u.buf.size / util_format_get_blocksize(format);92img->width = sz & MASK(15);93img->height = sz >> 15;94img->depth = 0;95} else {96img->buffer = false;9798unsigned lvl = pimg->u.tex.level;99img->offset = fd_resource_offset(rsc, lvl, pimg->u.tex.first_layer);100img->pitch = fd_resource_pitch(rsc, lvl);101102img->width = u_minify(prsc->width0, lvl);103img->height = u_minify(prsc->height0, lvl);104105unsigned layers = pimg->u.tex.last_layer - pimg->u.tex.first_layer + 1;106107switch (prsc->target) {108case PIPE_TEXTURE_RECT:109case PIPE_TEXTURE_1D:110case PIPE_TEXTURE_2D:111img->array_pitch = rsc->layout.layer_size;112img->depth = 1;113break;114case PIPE_TEXTURE_1D_ARRAY:115case PIPE_TEXTURE_2D_ARRAY:116img->array_pitch = rsc->layout.layer_size;117img->depth = layers;118break;119case PIPE_TEXTURE_CUBE:120case PIPE_TEXTURE_CUBE_ARRAY:121img->array_pitch = rsc->layout.layer_size;122img->depth = layers;123break;124case PIPE_TEXTURE_3D:125img->array_pitch = fd_resource_slice(rsc, lvl)->size0;126img->depth = u_minify(prsc->depth0, lvl);127break;128default:129img->array_pitch = 0;130img->depth = 0;131break;132}133}134}135136static void137emit_image_tex(struct fd_ringbuffer *ring, unsigned slot, struct fd5_image *img,138enum pipe_shader_type shader)139{140OUT_PKT7(ring, CP_LOAD_STATE4, 3 + 12);141OUT_RING(ring, CP_LOAD_STATE4_0_DST_OFF(slot) |142CP_LOAD_STATE4_0_STATE_SRC(SS4_DIRECT) |143CP_LOAD_STATE4_0_STATE_BLOCK(texsb[shader]) |144CP_LOAD_STATE4_0_NUM_UNIT(1));145OUT_RING(ring, CP_LOAD_STATE4_1_STATE_TYPE(ST4_CONSTANTS) |146CP_LOAD_STATE4_1_EXT_SRC_ADDR(0));147OUT_RING(ring, CP_LOAD_STATE4_2_EXT_SRC_ADDR_HI(0));148149OUT_RING(ring, A5XX_TEX_CONST_0_FMT(img->fmt) |150fd5_tex_swiz(img->pfmt, PIPE_SWIZZLE_X, PIPE_SWIZZLE_Y,151PIPE_SWIZZLE_Z, PIPE_SWIZZLE_W) |152COND(img->srgb, A5XX_TEX_CONST_0_SRGB));153OUT_RING(ring, A5XX_TEX_CONST_1_WIDTH(img->width) |154A5XX_TEX_CONST_1_HEIGHT(img->height));155OUT_RING(ring,156COND(img->buffer, A5XX_TEX_CONST_2_UNK4 | A5XX_TEX_CONST_2_UNK31) |157A5XX_TEX_CONST_2_TYPE(img->type) |158A5XX_TEX_CONST_2_PITCH(img->pitch));159OUT_RING(ring, A5XX_TEX_CONST_3_ARRAY_PITCH(img->array_pitch));160if (img->bo) {161OUT_RELOC(ring, img->bo, img->offset,162(uint64_t)A5XX_TEX_CONST_5_DEPTH(img->depth) << 32, 0);163} else {164OUT_RING(ring, 0x00000000);165OUT_RING(ring, A5XX_TEX_CONST_5_DEPTH(img->depth));166}167OUT_RING(ring, 0x00000000);168OUT_RING(ring, 0x00000000);169OUT_RING(ring, 0x00000000);170OUT_RING(ring, 0x00000000);171OUT_RING(ring, 0x00000000);172OUT_RING(ring, 0x00000000);173}174175static void176emit_image_ssbo(struct fd_ringbuffer *ring, unsigned slot,177struct fd5_image *img, enum pipe_shader_type shader)178{179OUT_PKT7(ring, CP_LOAD_STATE4, 3 + 2);180OUT_RING(ring, CP_LOAD_STATE4_0_DST_OFF(slot) |181CP_LOAD_STATE4_0_STATE_SRC(SS4_DIRECT) |182CP_LOAD_STATE4_0_STATE_BLOCK(imgsb[shader]) |183CP_LOAD_STATE4_0_NUM_UNIT(1));184OUT_RING(ring,185CP_LOAD_STATE4_1_STATE_TYPE(1) | CP_LOAD_STATE4_1_EXT_SRC_ADDR(0));186OUT_RING(ring, CP_LOAD_STATE4_2_EXT_SRC_ADDR_HI(0));187OUT_RING(ring,188A5XX_SSBO_1_0_FMT(img->fmt) | A5XX_SSBO_1_0_WIDTH(img->width));189OUT_RING(ring, A5XX_SSBO_1_1_HEIGHT(img->height) |190A5XX_SSBO_1_1_DEPTH(img->depth));191192OUT_PKT7(ring, CP_LOAD_STATE4, 3 + 2);193OUT_RING(ring, CP_LOAD_STATE4_0_DST_OFF(slot) |194CP_LOAD_STATE4_0_STATE_SRC(SS4_DIRECT) |195CP_LOAD_STATE4_0_STATE_BLOCK(imgsb[shader]) |196CP_LOAD_STATE4_0_NUM_UNIT(1));197OUT_RING(ring,198CP_LOAD_STATE4_1_STATE_TYPE(2) | CP_LOAD_STATE4_1_EXT_SRC_ADDR(0));199OUT_RING(ring, CP_LOAD_STATE4_2_EXT_SRC_ADDR_HI(0));200if (img->bo) {201OUT_RELOC(ring, img->bo, img->offset, 0, 0);202} else {203OUT_RING(ring, 0x00000000);204OUT_RING(ring, 0x00000000);205}206}207208/* Emit required "SSBO" and sampler state. The sampler state is used by the209* hw for imageLoad(), and "SSBO" state for imageStore(). Returns max sampler210* used.211*/212void213fd5_emit_images(struct fd_context *ctx, struct fd_ringbuffer *ring,214enum pipe_shader_type shader,215const struct ir3_shader_variant *v)216{217struct fd_shaderimg_stateobj *so = &ctx->shaderimg[shader];218unsigned enabled_mask = so->enabled_mask;219const struct ir3_ibo_mapping *m = &v->image_mapping;220221while (enabled_mask) {222unsigned index = u_bit_scan(&enabled_mask);223struct fd5_image img;224225translate_image(&img, &so->si[index]);226227emit_image_tex(ring, m->image_to_tex[index] + m->tex_base, &img, shader);228emit_image_ssbo(ring, v->shader->nir->info.num_ssbos + index, &img,229shader);230}231}232233234