Path: blob/21.2-virgl/src/gallium/auxiliary/util/u_compute.c
4561 views
/**************************************************************************1*2* Copyright 2019 Sonny Jiang <[email protected]>3* Copyright 2019 Advanced Micro Devices, Inc.4* All Rights Reserved.5*6* Permission is hereby granted, free of charge, to any person obtaining a7* copy of this software and associated documentation files (the8* "Software"), to deal in the Software without restriction, including9* without limitation the rights to use, copy, modify, merge, publish,10* distribute, sub license, and/or sell copies of the Software, and to11* permit persons to whom the Software is furnished to do so, subject to12* the following conditions:13*14* The above copyright notice and this permission notice (including the15* next paragraph) shall be included in all copies or substantial portions16* of the Software.17*18* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS19* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF20* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.21* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR22* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,23* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE24* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.25*26**************************************************************************/2728#include "pipe/p_context.h"29#include "pipe/p_state.h"3031#include "u_bitcast.h"32#include "util/format/u_format.h"33#include "u_sampler.h"34#include "tgsi/tgsi_text.h"35#include "tgsi/tgsi_ureg.h"36#include "u_inlines.h"37#include "u_compute.h"3839static void *blit_compute_shader(struct pipe_context *ctx)40{41static const char text[] =42"COMP\n"43"PROPERTY CS_FIXED_BLOCK_WIDTH 64\n"44"PROPERTY CS_FIXED_BLOCK_HEIGHT 1\n"45"PROPERTY CS_FIXED_BLOCK_DEPTH 1\n"46"DCL SV[0], THREAD_ID\n"47"DCL SV[1], BLOCK_ID\n"48"DCL IMAGE[0], 2D_ARRAY, PIPE_FORMAT_R32G32B32A32_FLOAT, WR\n"49"DCL SAMP[0]\n"50"DCL SVIEW[0], 2D_ARRAY, FLOAT\n"51"DCL CONST[0][0..2]\n" // 0:xyzw 1:xyzw52"DCL TEMP[0..4], LOCAL\n"53"IMM[0] UINT32 {64, 1, 0, 0}\n"5455"UMAD TEMP[0].xyz, SV[1].xyzz, IMM[0].xyyy, SV[0].xyzz\n"56"U2F TEMP[1].xyz, TEMP[0]\n"57"MAD TEMP[2].xyz, TEMP[1], CONST[0][1], CONST[0][0]\n"58"TEX_LZ TEMP[3], TEMP[2], SAMP[0], 2D_ARRAY\n"59"UADD TEMP[4].xyz, TEMP[0], CONST[0][2]\n"60"STORE IMAGE[0], TEMP[4], TEMP[3], 2D_ARRAY, PIPE_FORMAT_R32G32B32A32_FLOAT\n"61"END\n";6263struct tgsi_token tokens[1024];64struct pipe_compute_state state = {0};6566if (!tgsi_text_translate(text, tokens, ARRAY_SIZE(tokens))) {67assert(false);68return NULL;69}7071state.ir_type = PIPE_SHADER_IR_TGSI;72state.prog = tokens;7374return ctx->create_compute_state(ctx, &state);75}7677void util_compute_blit(struct pipe_context *ctx, struct pipe_blit_info *blit_info,78void **compute_state, bool half_texel_offset)79{80if (blit_info->src.box.width == 0 || blit_info->src.box.height == 0 ||81blit_info->dst.box.width == 0 || blit_info->dst.box.height == 0)82return;8384struct pipe_resource *src = blit_info->src.resource;85struct pipe_resource *dst = blit_info->dst.resource;86struct pipe_sampler_view src_templ = {0}, *src_view;87void *sampler_state_p;88unsigned width = blit_info->dst.box.width;89unsigned height = blit_info->dst.box.height;90float x_scale = blit_info->src.box.width / (float)blit_info->dst.box.width;91float y_scale = blit_info->src.box.height / (float)blit_info->dst.box.height;92float z_scale = blit_info->src.box.depth / (float)blit_info->dst.box.depth;93float offset = half_texel_offset ? 0.5 : 0.0;9495unsigned data[] = {u_bitcast_f2u((blit_info->src.box.x + offset) / (float)src->width0),96u_bitcast_f2u((blit_info->src.box.y + offset) / (float)src->height0),97u_bitcast_f2u(blit_info->src.box.z),98u_bitcast_f2u(0),99u_bitcast_f2u(x_scale / src->width0),100u_bitcast_f2u(y_scale / src->height0),101u_bitcast_f2u(z_scale),102u_bitcast_f2u(0),103blit_info->dst.box.x,104blit_info->dst.box.y,105blit_info->dst.box.z,1060};107108struct pipe_constant_buffer cb = {0};109cb.buffer_size = sizeof(data);110cb.user_buffer = data;111ctx->set_constant_buffer(ctx, PIPE_SHADER_COMPUTE, 0, false, &cb);112113struct pipe_image_view image = {0};114image.resource = dst;115image.shader_access = image.access = PIPE_IMAGE_ACCESS_WRITE;116image.format = util_format_linear(blit_info->dst.format);117image.u.tex.level = blit_info->dst.level;118image.u.tex.first_layer = 0;119image.u.tex.last_layer = (unsigned)(dst->array_size - 1);120121ctx->set_shader_images(ctx, PIPE_SHADER_COMPUTE, 0, 1, 0, &image);122123struct pipe_sampler_state sampler_state={0};124sampler_state.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;125sampler_state.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;126sampler_state.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;127sampler_state.normalized_coords = 1;128129if (blit_info->filter == PIPE_TEX_FILTER_LINEAR) {130sampler_state.min_img_filter = PIPE_TEX_FILTER_LINEAR;131sampler_state.mag_img_filter = PIPE_TEX_FILTER_LINEAR;132}133134sampler_state_p = ctx->create_sampler_state(ctx, &sampler_state);135ctx->bind_sampler_states(ctx, PIPE_SHADER_COMPUTE, 0, 1, &sampler_state_p);136137/* Initialize the sampler view. */138u_sampler_view_default_template(&src_templ, src, src->format);139src_templ.format = util_format_linear(blit_info->src.format);140src_view = ctx->create_sampler_view(ctx, src, &src_templ);141ctx->set_sampler_views(ctx, PIPE_SHADER_COMPUTE, 0, 1, 0, &src_view);142143if (!*compute_state)144*compute_state = blit_compute_shader(ctx);145ctx->bind_compute_state(ctx, *compute_state);146147struct pipe_grid_info grid_info = {0};148grid_info.block[0] = 64;149grid_info.last_block[0] = width % 64;150grid_info.block[1] = 1;151grid_info.block[2] = 1;152grid_info.grid[0] = DIV_ROUND_UP(width, 64);153grid_info.grid[1] = height;154grid_info.grid[2] = 1;155156ctx->launch_grid(ctx, &grid_info);157158ctx->memory_barrier(ctx, PIPE_BARRIER_ALL);159160ctx->set_shader_images(ctx, PIPE_SHADER_COMPUTE, 0, 0, 1, NULL);161ctx->set_constant_buffer(ctx, PIPE_SHADER_COMPUTE, 0, false, NULL);162ctx->set_sampler_views(ctx, PIPE_SHADER_COMPUTE, 0, 0, 1, NULL);163pipe_sampler_view_reference(&src_view, NULL);164ctx->delete_sampler_state(ctx, sampler_state_p);165ctx->bind_compute_state(ctx, NULL);166}167168169