Path: blob/21.2-virgl/src/gallium/auxiliary/vl/vl_zscan.c
4565 views
/**************************************************************************1*2* Copyright 2011 Christian König3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include <assert.h>2829#include "pipe/p_screen.h"30#include "pipe/p_context.h"3132#include "util/u_draw.h"33#include "util/u_sampler.h"34#include "util/u_inlines.h"35#include "util/u_memory.h"3637#include "tgsi/tgsi_ureg.h"3839#include "vl_defines.h"40#include "vl_types.h"4142#include "vl_zscan.h"43#include "vl_vertex_buffers.h"4445enum VS_OUTPUT46{47VS_O_VPOS = 0,48VS_O_VTEX = 049};5051const int vl_zscan_normal_16[] =52{53/* Zig-Zag scan pattern */540, 1, 4, 8, 5, 2, 3, 6,559,12,13,10, 7,11,14,1556};5758const int vl_zscan_linear[] =59{60/* Linear scan pattern */610, 1, 2, 3, 4, 5, 6, 7,628, 9,10,11,12,13,14,15,6316,17,18,19,20,21,22,23,6424,25,26,27,28,29,30,31,6532,33,34,35,36,37,38,39,6640,41,42,43,44,45,46,47,6748,49,50,51,52,53,54,55,6856,57,58,59,60,61,62,6369};7071const int vl_zscan_normal[] =72{73/* Zig-Zag scan pattern */740, 1, 8,16, 9, 2, 3,10,7517,24,32,25,18,11, 4, 5,7612,19,26,33,40,48,41,34,7727,20,13, 6, 7,14,21,28,7835,42,49,56,57,50,43,36,7929,22,15,23,30,37,44,51,8058,59,52,45,38,31,39,46,8153,60,61,54,47,55,62,6382};8384const int vl_zscan_alternate[] =85{86/* Alternate scan pattern */870, 8,16,24, 1, 9, 2,10,8817,25,32,40,48,56,57,49,8941,33,26,18, 3,11, 4,12,9019,27,34,42,50,58,35,43,9151,59,20,28, 5,13, 6,14,9221,29,36,44,52,60,37,45,9353,61,22,30, 7,15,23,31,9438,46,54,62,39,47,55,6395};9697const int vl_zscan_h265_up_right_diagonal_16[] =98{99/* Up-right diagonal scan order for 4x4 blocks - see H.265 section 6.5.3. */1000, 4, 1, 8, 5, 2, 12, 9,1016, 3, 13, 10, 7, 14, 11, 15,102};103104const int vl_zscan_h265_up_right_diagonal[] =105{106/* Up-right diagonal scan order for 8x8 blocks - see H.265 section 6.5.3. */1070, 8, 1, 16, 9, 2, 24, 17,10810, 3, 32, 25, 18, 11, 4, 40,10933, 26, 19, 12, 5, 48, 41, 34,11027, 20, 13, 6, 56, 49, 42, 35,11128, 21, 14, 7, 57, 50, 43, 36,11229, 22, 15, 58, 51, 44, 37, 30,11323, 59, 52, 45, 38, 31, 60, 53,11446, 39, 61, 54, 47, 62, 55, 63,115};116117118static void *119create_vert_shader(struct vl_zscan *zscan)120{121struct ureg_program *shader;122struct ureg_src scale;123struct ureg_src vrect, vpos, block_num;124struct ureg_dst tmp;125struct ureg_dst o_vpos;126struct ureg_dst *o_vtex;127unsigned i;128129shader = ureg_create(PIPE_SHADER_VERTEX);130if (!shader)131return NULL;132133o_vtex = MALLOC(zscan->num_channels * sizeof(struct ureg_dst));134135scale = ureg_imm2f(shader,136(float)VL_BLOCK_WIDTH / zscan->buffer_width,137(float)VL_BLOCK_HEIGHT / zscan->buffer_height);138139vrect = ureg_DECL_vs_input(shader, VS_I_RECT);140vpos = ureg_DECL_vs_input(shader, VS_I_VPOS);141block_num = ureg_DECL_vs_input(shader, VS_I_BLOCK_NUM);142143tmp = ureg_DECL_temporary(shader);144145o_vpos = ureg_DECL_output(shader, TGSI_SEMANTIC_POSITION, VS_O_VPOS);146147for (i = 0; i < zscan->num_channels; ++i)148o_vtex[i] = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_VTEX + i);149150/*151* o_vpos.xy = (vpos + vrect) * scale152* o_vpos.zw = 1.0f153*154* tmp.xy = InstanceID / blocks_per_line155* tmp.x = frac(tmp.x)156* tmp.y = floor(tmp.y)157*158* o_vtex.x = vrect.x / blocks_per_line + tmp.x159* o_vtex.y = vrect.y160* o_vtex.z = tmp.z * blocks_per_line / blocks_total161*/162ureg_ADD(shader, ureg_writemask(tmp, TGSI_WRITEMASK_XY), vpos, vrect);163ureg_MUL(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_XY), ureg_src(tmp), scale);164ureg_MOV(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_ZW), ureg_imm1f(shader, 1.0f));165166ureg_MUL(shader, ureg_writemask(tmp, TGSI_WRITEMASK_XW), ureg_scalar(block_num, TGSI_SWIZZLE_X),167ureg_imm1f(shader, 1.0f / zscan->blocks_per_line));168169ureg_FRC(shader, ureg_writemask(tmp, TGSI_WRITEMASK_Y), ureg_scalar(ureg_src(tmp), TGSI_SWIZZLE_X));170ureg_FLR(shader, ureg_writemask(tmp, TGSI_WRITEMASK_W), ureg_src(tmp));171172for (i = 0; i < zscan->num_channels; ++i) {173ureg_ADD(shader, ureg_writemask(tmp, TGSI_WRITEMASK_X), ureg_scalar(ureg_src(tmp), TGSI_SWIZZLE_Y),174ureg_imm1f(shader, 1.0f / (zscan->blocks_per_line * VL_BLOCK_WIDTH)175* ((signed)i - (signed)zscan->num_channels / 2)));176177ureg_MAD(shader, ureg_writemask(o_vtex[i], TGSI_WRITEMASK_X), vrect,178ureg_imm1f(shader, 1.0f / zscan->blocks_per_line), ureg_src(tmp));179ureg_MOV(shader, ureg_writemask(o_vtex[i], TGSI_WRITEMASK_Y), vrect);180ureg_MOV(shader, ureg_writemask(o_vtex[i], TGSI_WRITEMASK_Z), vpos);181ureg_MUL(shader, ureg_writemask(o_vtex[i], TGSI_WRITEMASK_W), ureg_src(tmp),182ureg_imm1f(shader, (float)zscan->blocks_per_line / zscan->blocks_total));183}184185ureg_release_temporary(shader, tmp);186ureg_END(shader);187188FREE(o_vtex);189190return ureg_create_shader_and_destroy(shader, zscan->pipe);191}192193static void *194create_frag_shader(struct vl_zscan *zscan)195{196struct ureg_program *shader;197struct ureg_src *vtex;198199struct ureg_src samp_src, samp_scan, samp_quant;200201struct ureg_dst *tmp;202struct ureg_dst quant, fragment;203204unsigned i;205206shader = ureg_create(PIPE_SHADER_FRAGMENT);207if (!shader)208return NULL;209210vtex = MALLOC(zscan->num_channels * sizeof(struct ureg_src));211tmp = MALLOC(zscan->num_channels * sizeof(struct ureg_dst));212213for (i = 0; i < zscan->num_channels; ++i)214vtex[i] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_VTEX + i, TGSI_INTERPOLATE_LINEAR);215216samp_src = ureg_DECL_sampler(shader, 0);217samp_scan = ureg_DECL_sampler(shader, 1);218samp_quant = ureg_DECL_sampler(shader, 2);219220for (i = 0; i < zscan->num_channels; ++i)221tmp[i] = ureg_DECL_temporary(shader);222quant = ureg_DECL_temporary(shader);223224fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0);225226/*227* tmp.x = tex(vtex, 1)228* tmp.y = vtex.z229* fragment = tex(tmp, 0) * quant230*/231for (i = 0; i < zscan->num_channels; ++i)232ureg_TEX(shader, ureg_writemask(tmp[i], TGSI_WRITEMASK_X), TGSI_TEXTURE_2D, vtex[i], samp_scan);233234for (i = 0; i < zscan->num_channels; ++i)235ureg_MOV(shader, ureg_writemask(tmp[i], TGSI_WRITEMASK_Y), ureg_scalar(vtex[i], TGSI_SWIZZLE_W));236237for (i = 0; i < zscan->num_channels; ++i) {238ureg_TEX(shader, ureg_writemask(tmp[0], TGSI_WRITEMASK_X << i), TGSI_TEXTURE_2D, ureg_src(tmp[i]), samp_src);239ureg_TEX(shader, ureg_writemask(quant, TGSI_WRITEMASK_X << i), TGSI_TEXTURE_3D, vtex[i], samp_quant);240}241242ureg_MUL(shader, quant, ureg_src(quant), ureg_imm1f(shader, 16.0f));243ureg_MUL(shader, fragment, ureg_src(tmp[0]), ureg_src(quant));244245for (i = 0; i < zscan->num_channels; ++i)246ureg_release_temporary(shader, tmp[i]);247ureg_END(shader);248249FREE(vtex);250FREE(tmp);251252return ureg_create_shader_and_destroy(shader, zscan->pipe);253}254255static bool256init_shaders(struct vl_zscan *zscan)257{258assert(zscan);259260zscan->vs = create_vert_shader(zscan);261if (!zscan->vs)262goto error_vs;263264zscan->fs = create_frag_shader(zscan);265if (!zscan->fs)266goto error_fs;267268return true;269270error_fs:271zscan->pipe->delete_vs_state(zscan->pipe, zscan->vs);272273error_vs:274return false;275}276277static void278cleanup_shaders(struct vl_zscan *zscan)279{280assert(zscan);281282zscan->pipe->delete_vs_state(zscan->pipe, zscan->vs);283zscan->pipe->delete_fs_state(zscan->pipe, zscan->fs);284}285286static bool287init_state(struct vl_zscan *zscan)288{289struct pipe_blend_state blend;290struct pipe_rasterizer_state rs_state;291struct pipe_sampler_state sampler;292unsigned i;293294assert(zscan);295296memset(&rs_state, 0, sizeof(rs_state));297rs_state.half_pixel_center = true;298rs_state.bottom_edge_rule = true;299rs_state.depth_clip_near = 1;300rs_state.depth_clip_far = 1;301302zscan->rs_state = zscan->pipe->create_rasterizer_state(zscan->pipe, &rs_state);303if (!zscan->rs_state)304goto error_rs_state;305306memset(&blend, 0, sizeof blend);307308blend.independent_blend_enable = 0;309blend.rt[0].blend_enable = 0;310blend.rt[0].rgb_func = PIPE_BLEND_ADD;311blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;312blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ONE;313blend.rt[0].alpha_func = PIPE_BLEND_ADD;314blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;315blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ONE;316blend.logicop_enable = 0;317blend.logicop_func = PIPE_LOGICOP_CLEAR;318/* Needed to allow color writes to FB, even if blending disabled */319blend.rt[0].colormask = PIPE_MASK_RGBA;320blend.dither = 0;321zscan->blend = zscan->pipe->create_blend_state(zscan->pipe, &blend);322if (!zscan->blend)323goto error_blend;324325for (i = 0; i < 3; ++i) {326memset(&sampler, 0, sizeof(sampler));327sampler.wrap_s = PIPE_TEX_WRAP_REPEAT;328sampler.wrap_t = PIPE_TEX_WRAP_REPEAT;329sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;330sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;331sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;332sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;333sampler.compare_mode = PIPE_TEX_COMPARE_NONE;334sampler.compare_func = PIPE_FUNC_ALWAYS;335sampler.normalized_coords = 1;336zscan->samplers[i] = zscan->pipe->create_sampler_state(zscan->pipe, &sampler);337if (!zscan->samplers[i])338goto error_samplers;339}340341return true;342343error_samplers:344for (i = 0; i < 2; ++i)345if (zscan->samplers[i])346zscan->pipe->delete_sampler_state(zscan->pipe, zscan->samplers[i]);347348zscan->pipe->delete_rasterizer_state(zscan->pipe, zscan->rs_state);349350error_blend:351zscan->pipe->delete_blend_state(zscan->pipe, zscan->blend);352353error_rs_state:354return false;355}356357static void358cleanup_state(struct vl_zscan *zscan)359{360unsigned i;361362assert(zscan);363364for (i = 0; i < 3; ++i)365zscan->pipe->delete_sampler_state(zscan->pipe, zscan->samplers[i]);366367zscan->pipe->delete_rasterizer_state(zscan->pipe, zscan->rs_state);368zscan->pipe->delete_blend_state(zscan->pipe, zscan->blend);369}370371struct pipe_sampler_view *372vl_zscan_layout(struct pipe_context *pipe, const int layout[64], unsigned blocks_per_line)373{374const unsigned total_size = blocks_per_line * VL_BLOCK_WIDTH * VL_BLOCK_HEIGHT;375376int patched_layout[64];377378struct pipe_resource res_tmpl, *res;379struct pipe_sampler_view sv_tmpl, *sv;380struct pipe_transfer *buf_transfer;381unsigned x, y, i, pitch;382float *f;383384struct pipe_box rect =385{3860, 0, 0,387VL_BLOCK_WIDTH * blocks_per_line,388VL_BLOCK_HEIGHT,3891390};391392assert(pipe && layout && blocks_per_line);393394for (i = 0; i < 64; ++i)395patched_layout[layout[i]] = i;396397memset(&res_tmpl, 0, sizeof(res_tmpl));398res_tmpl.target = PIPE_TEXTURE_2D;399res_tmpl.format = PIPE_FORMAT_R32_FLOAT;400res_tmpl.width0 = VL_BLOCK_WIDTH * blocks_per_line;401res_tmpl.height0 = VL_BLOCK_HEIGHT;402res_tmpl.depth0 = 1;403res_tmpl.array_size = 1;404res_tmpl.usage = PIPE_USAGE_IMMUTABLE;405res_tmpl.bind = PIPE_BIND_SAMPLER_VIEW;406407res = pipe->screen->resource_create(pipe->screen, &res_tmpl);408if (!res)409goto error_resource;410411f = pipe->texture_map(pipe, res,4120, PIPE_MAP_WRITE | PIPE_MAP_DISCARD_RANGE,413&rect, &buf_transfer);414if (!f)415goto error_map;416417pitch = buf_transfer->stride / sizeof(float);418419for (i = 0; i < blocks_per_line; ++i)420for (y = 0; y < VL_BLOCK_HEIGHT; ++y)421for (x = 0; x < VL_BLOCK_WIDTH; ++x) {422float addr = patched_layout[x + y * VL_BLOCK_WIDTH] +423i * VL_BLOCK_WIDTH * VL_BLOCK_HEIGHT;424425addr /= total_size;426427f[i * VL_BLOCK_WIDTH + y * pitch + x] = addr;428}429430pipe->texture_unmap(pipe, buf_transfer);431432memset(&sv_tmpl, 0, sizeof(sv_tmpl));433u_sampler_view_default_template(&sv_tmpl, res, res->format);434sv = pipe->create_sampler_view(pipe, res, &sv_tmpl);435pipe_resource_reference(&res, NULL);436if (!sv)437goto error_map;438439return sv;440441error_map:442pipe_resource_reference(&res, NULL);443444error_resource:445return NULL;446}447448bool449vl_zscan_init(struct vl_zscan *zscan, struct pipe_context *pipe,450unsigned buffer_width, unsigned buffer_height,451unsigned blocks_per_line, unsigned blocks_total,452unsigned num_channels)453{454assert(zscan && pipe);455456zscan->pipe = pipe;457zscan->buffer_width = buffer_width;458zscan->buffer_height = buffer_height;459zscan->num_channels = num_channels;460zscan->blocks_per_line = blocks_per_line;461zscan->blocks_total = blocks_total;462463if(!init_shaders(zscan))464return false;465466if(!init_state(zscan)) {467cleanup_shaders(zscan);468return false;469}470471return true;472}473474void475vl_zscan_cleanup(struct vl_zscan *zscan)476{477assert(zscan);478479cleanup_shaders(zscan);480cleanup_state(zscan);481}482483bool484vl_zscan_init_buffer(struct vl_zscan *zscan, struct vl_zscan_buffer *buffer,485struct pipe_sampler_view *src, struct pipe_surface *dst)486{487struct pipe_resource res_tmpl, *res;488struct pipe_sampler_view sv_tmpl;489490assert(zscan && buffer);491492memset(buffer, 0, sizeof(struct vl_zscan_buffer));493494pipe_sampler_view_reference(&buffer->src, src);495496buffer->viewport.scale[0] = dst->width;497buffer->viewport.scale[1] = dst->height;498buffer->viewport.scale[2] = 1;499buffer->viewport.translate[0] = 0;500buffer->viewport.translate[1] = 0;501buffer->viewport.translate[2] = 0;502buffer->viewport.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;503buffer->viewport.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;504buffer->viewport.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;505buffer->viewport.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;506507buffer->fb_state.width = dst->width;508buffer->fb_state.height = dst->height;509buffer->fb_state.nr_cbufs = 1;510pipe_surface_reference(&buffer->fb_state.cbufs[0], dst);511512memset(&res_tmpl, 0, sizeof(res_tmpl));513res_tmpl.target = PIPE_TEXTURE_3D;514res_tmpl.format = PIPE_FORMAT_R8_UNORM;515res_tmpl.width0 = VL_BLOCK_WIDTH * zscan->blocks_per_line;516res_tmpl.height0 = VL_BLOCK_HEIGHT;517res_tmpl.depth0 = 2;518res_tmpl.array_size = 1;519res_tmpl.usage = PIPE_USAGE_IMMUTABLE;520res_tmpl.bind = PIPE_BIND_SAMPLER_VIEW;521522res = zscan->pipe->screen->resource_create(zscan->pipe->screen, &res_tmpl);523if (!res)524return false;525526memset(&sv_tmpl, 0, sizeof(sv_tmpl));527u_sampler_view_default_template(&sv_tmpl, res, res->format);528sv_tmpl.swizzle_r = sv_tmpl.swizzle_g = sv_tmpl.swizzle_b = sv_tmpl.swizzle_a = TGSI_SWIZZLE_X;529buffer->quant = zscan->pipe->create_sampler_view(zscan->pipe, res, &sv_tmpl);530pipe_resource_reference(&res, NULL);531if (!buffer->quant)532return false;533534return true;535}536537void538vl_zscan_cleanup_buffer(struct vl_zscan_buffer *buffer)539{540assert(buffer);541542pipe_sampler_view_reference(&buffer->src, NULL);543pipe_sampler_view_reference(&buffer->layout, NULL);544pipe_sampler_view_reference(&buffer->quant, NULL);545pipe_surface_reference(&buffer->fb_state.cbufs[0], NULL);546}547548void549vl_zscan_set_layout(struct vl_zscan_buffer *buffer, struct pipe_sampler_view *layout)550{551assert(buffer);552assert(layout);553554pipe_sampler_view_reference(&buffer->layout, layout);555}556557void558vl_zscan_upload_quant(struct vl_zscan *zscan, struct vl_zscan_buffer *buffer,559const uint8_t matrix[64], bool intra)560{561struct pipe_context *pipe;562struct pipe_transfer *buf_transfer;563unsigned x, y, i, pitch;564uint8_t *data;565566struct pipe_box rect =567{5680, 0, intra ? 1 : 0,569VL_BLOCK_WIDTH,570VL_BLOCK_HEIGHT,5711572};573574assert(buffer);575assert(matrix);576577pipe = zscan->pipe;578579rect.width *= zscan->blocks_per_line;580581data = pipe->texture_map(pipe, buffer->quant->texture,5820, PIPE_MAP_WRITE |583PIPE_MAP_DISCARD_RANGE,584&rect, &buf_transfer);585if (!data)586return;587588pitch = buf_transfer->stride;589590for (i = 0; i < zscan->blocks_per_line; ++i)591for (y = 0; y < VL_BLOCK_HEIGHT; ++y)592for (x = 0; x < VL_BLOCK_WIDTH; ++x)593data[i * VL_BLOCK_WIDTH + y * pitch + x] = matrix[x + y * VL_BLOCK_WIDTH];594595pipe->texture_unmap(pipe, buf_transfer);596}597598void599vl_zscan_render(struct vl_zscan *zscan, struct vl_zscan_buffer *buffer, unsigned num_instances)600{601assert(buffer);602603zscan->pipe->bind_rasterizer_state(zscan->pipe, zscan->rs_state);604zscan->pipe->bind_blend_state(zscan->pipe, zscan->blend);605zscan->pipe->bind_sampler_states(zscan->pipe, PIPE_SHADER_FRAGMENT,6060, 3, zscan->samplers);607zscan->pipe->set_framebuffer_state(zscan->pipe, &buffer->fb_state);608zscan->pipe->set_viewport_states(zscan->pipe, 0, 1, &buffer->viewport);609zscan->pipe->set_sampler_views(zscan->pipe, PIPE_SHADER_FRAGMENT,6100, 3, 0, &buffer->src);611zscan->pipe->bind_vs_state(zscan->pipe, zscan->vs);612zscan->pipe->bind_fs_state(zscan->pipe, zscan->fs);613util_draw_arrays_instanced(zscan->pipe, PIPE_PRIM_QUADS, 0, 4, 0, num_instances);614}615616617