1bf215546Sopenharmony_ci/************************************************************************** 2bf215546Sopenharmony_ci * 3bf215546Sopenharmony_ci * Copyright 2011 Christian König 4bf215546Sopenharmony_ci * All Rights Reserved. 5bf215546Sopenharmony_ci * 6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the 8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to 11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 12bf215546Sopenharmony_ci * the following conditions: 13bf215546Sopenharmony_ci * 14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the 15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions 16bf215546Sopenharmony_ci * of the Software. 17bf215546Sopenharmony_ci * 18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25bf215546Sopenharmony_ci * 26bf215546Sopenharmony_ci **************************************************************************/ 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci#include <assert.h> 29bf215546Sopenharmony_ci 30bf215546Sopenharmony_ci#include "pipe/p_screen.h" 31bf215546Sopenharmony_ci#include "pipe/p_context.h" 32bf215546Sopenharmony_ci 33bf215546Sopenharmony_ci#include "util/u_draw.h" 34bf215546Sopenharmony_ci#include "util/u_sampler.h" 35bf215546Sopenharmony_ci#include "util/u_inlines.h" 36bf215546Sopenharmony_ci#include "util/u_memory.h" 37bf215546Sopenharmony_ci 38bf215546Sopenharmony_ci#include "tgsi/tgsi_ureg.h" 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_ci#include "vl_defines.h" 41bf215546Sopenharmony_ci#include "vl_types.h" 42bf215546Sopenharmony_ci 43bf215546Sopenharmony_ci#include "vl_zscan.h" 44bf215546Sopenharmony_ci#include "vl_vertex_buffers.h" 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_cienum VS_OUTPUT 47bf215546Sopenharmony_ci{ 48bf215546Sopenharmony_ci VS_O_VPOS = 0, 49bf215546Sopenharmony_ci VS_O_VTEX = 0 50bf215546Sopenharmony_ci}; 51bf215546Sopenharmony_ci 52bf215546Sopenharmony_ciconst int vl_zscan_normal_16[] = 53bf215546Sopenharmony_ci{ 54bf215546Sopenharmony_ci /* Zig-Zag scan pattern */ 55bf215546Sopenharmony_ci 0, 1, 4, 8, 5, 2, 3, 6, 56bf215546Sopenharmony_ci 9,12,13,10, 7,11,14,15 57bf215546Sopenharmony_ci}; 58bf215546Sopenharmony_ci 59bf215546Sopenharmony_ciconst int vl_zscan_linear[] = 60bf215546Sopenharmony_ci{ 61bf215546Sopenharmony_ci /* Linear scan pattern */ 62bf215546Sopenharmony_ci 0, 1, 2, 3, 4, 5, 6, 7, 63bf215546Sopenharmony_ci 8, 9,10,11,12,13,14,15, 64bf215546Sopenharmony_ci 16,17,18,19,20,21,22,23, 65bf215546Sopenharmony_ci 24,25,26,27,28,29,30,31, 66bf215546Sopenharmony_ci 32,33,34,35,36,37,38,39, 67bf215546Sopenharmony_ci 40,41,42,43,44,45,46,47, 68bf215546Sopenharmony_ci 48,49,50,51,52,53,54,55, 69bf215546Sopenharmony_ci 56,57,58,59,60,61,62,63 70bf215546Sopenharmony_ci}; 71bf215546Sopenharmony_ci 72bf215546Sopenharmony_ciconst int vl_zscan_normal[] = 73bf215546Sopenharmony_ci{ 74bf215546Sopenharmony_ci /* Zig-Zag scan pattern */ 75bf215546Sopenharmony_ci 0, 1, 8,16, 9, 2, 3,10, 76bf215546Sopenharmony_ci 17,24,32,25,18,11, 4, 5, 77bf215546Sopenharmony_ci 12,19,26,33,40,48,41,34, 78bf215546Sopenharmony_ci 27,20,13, 6, 7,14,21,28, 79bf215546Sopenharmony_ci 35,42,49,56,57,50,43,36, 80bf215546Sopenharmony_ci 29,22,15,23,30,37,44,51, 81bf215546Sopenharmony_ci 58,59,52,45,38,31,39,46, 82bf215546Sopenharmony_ci 53,60,61,54,47,55,62,63 83bf215546Sopenharmony_ci}; 84bf215546Sopenharmony_ci 85bf215546Sopenharmony_ciconst int vl_zscan_alternate[] = 86bf215546Sopenharmony_ci{ 87bf215546Sopenharmony_ci /* Alternate scan pattern */ 88bf215546Sopenharmony_ci 0, 8,16,24, 1, 9, 2,10, 89bf215546Sopenharmony_ci 17,25,32,40,48,56,57,49, 90bf215546Sopenharmony_ci 41,33,26,18, 3,11, 4,12, 91bf215546Sopenharmony_ci 19,27,34,42,50,58,35,43, 92bf215546Sopenharmony_ci 51,59,20,28, 5,13, 6,14, 93bf215546Sopenharmony_ci 21,29,36,44,52,60,37,45, 94bf215546Sopenharmony_ci 53,61,22,30, 7,15,23,31, 95bf215546Sopenharmony_ci 38,46,54,62,39,47,55,63 96bf215546Sopenharmony_ci}; 97bf215546Sopenharmony_ci 98bf215546Sopenharmony_ciconst int vl_zscan_h265_up_right_diagonal_16[] = 99bf215546Sopenharmony_ci{ 100bf215546Sopenharmony_ci /* Up-right diagonal scan order for 4x4 blocks - see H.265 section 6.5.3. */ 101bf215546Sopenharmony_ci 0, 4, 1, 8, 5, 2, 12, 9, 102bf215546Sopenharmony_ci 6, 3, 13, 10, 7, 14, 11, 15, 103bf215546Sopenharmony_ci}; 104bf215546Sopenharmony_ci 105bf215546Sopenharmony_ciconst int vl_zscan_h265_up_right_diagonal[] = 106bf215546Sopenharmony_ci{ 107bf215546Sopenharmony_ci /* Up-right diagonal scan order for 8x8 blocks - see H.265 section 6.5.3. */ 108bf215546Sopenharmony_ci 0, 8, 1, 16, 9, 2, 24, 17, 109bf215546Sopenharmony_ci 10, 3, 32, 25, 18, 11, 4, 40, 110bf215546Sopenharmony_ci 33, 26, 19, 12, 5, 48, 41, 34, 111bf215546Sopenharmony_ci 27, 20, 13, 6, 56, 49, 42, 35, 112bf215546Sopenharmony_ci 28, 21, 14, 7, 57, 50, 43, 36, 113bf215546Sopenharmony_ci 29, 22, 15, 58, 51, 44, 37, 30, 114bf215546Sopenharmony_ci 23, 59, 52, 45, 38, 31, 60, 53, 115bf215546Sopenharmony_ci 46, 39, 61, 54, 47, 62, 55, 63, 116bf215546Sopenharmony_ci}; 117bf215546Sopenharmony_ci 118bf215546Sopenharmony_ci 119bf215546Sopenharmony_cistatic void * 120bf215546Sopenharmony_cicreate_vert_shader(struct vl_zscan *zscan) 121bf215546Sopenharmony_ci{ 122bf215546Sopenharmony_ci struct ureg_program *shader; 123bf215546Sopenharmony_ci struct ureg_src scale; 124bf215546Sopenharmony_ci struct ureg_src vrect, vpos, block_num; 125bf215546Sopenharmony_ci struct ureg_dst tmp; 126bf215546Sopenharmony_ci struct ureg_dst o_vpos; 127bf215546Sopenharmony_ci struct ureg_dst *o_vtex; 128bf215546Sopenharmony_ci unsigned i; 129bf215546Sopenharmony_ci 130bf215546Sopenharmony_ci shader = ureg_create(PIPE_SHADER_VERTEX); 131bf215546Sopenharmony_ci if (!shader) 132bf215546Sopenharmony_ci return NULL; 133bf215546Sopenharmony_ci 134bf215546Sopenharmony_ci o_vtex = MALLOC(zscan->num_channels * sizeof(struct ureg_dst)); 135bf215546Sopenharmony_ci 136bf215546Sopenharmony_ci scale = ureg_imm2f(shader, 137bf215546Sopenharmony_ci (float)VL_BLOCK_WIDTH / zscan->buffer_width, 138bf215546Sopenharmony_ci (float)VL_BLOCK_HEIGHT / zscan->buffer_height); 139bf215546Sopenharmony_ci 140bf215546Sopenharmony_ci vrect = ureg_DECL_vs_input(shader, VS_I_RECT); 141bf215546Sopenharmony_ci vpos = ureg_DECL_vs_input(shader, VS_I_VPOS); 142bf215546Sopenharmony_ci block_num = ureg_DECL_vs_input(shader, VS_I_BLOCK_NUM); 143bf215546Sopenharmony_ci 144bf215546Sopenharmony_ci tmp = ureg_DECL_temporary(shader); 145bf215546Sopenharmony_ci 146bf215546Sopenharmony_ci o_vpos = ureg_DECL_output(shader, TGSI_SEMANTIC_POSITION, VS_O_VPOS); 147bf215546Sopenharmony_ci 148bf215546Sopenharmony_ci for (i = 0; i < zscan->num_channels; ++i) 149bf215546Sopenharmony_ci o_vtex[i] = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_VTEX + i); 150bf215546Sopenharmony_ci 151bf215546Sopenharmony_ci /* 152bf215546Sopenharmony_ci * o_vpos.xy = (vpos + vrect) * scale 153bf215546Sopenharmony_ci * o_vpos.zw = 1.0f 154bf215546Sopenharmony_ci * 155bf215546Sopenharmony_ci * tmp.xy = InstanceID / blocks_per_line 156bf215546Sopenharmony_ci * tmp.x = frac(tmp.x) 157bf215546Sopenharmony_ci * tmp.y = floor(tmp.y) 158bf215546Sopenharmony_ci * 159bf215546Sopenharmony_ci * o_vtex.x = vrect.x / blocks_per_line + tmp.x 160bf215546Sopenharmony_ci * o_vtex.y = vrect.y 161bf215546Sopenharmony_ci * o_vtex.z = tmp.z * blocks_per_line / blocks_total 162bf215546Sopenharmony_ci */ 163bf215546Sopenharmony_ci ureg_ADD(shader, ureg_writemask(tmp, TGSI_WRITEMASK_XY), vpos, vrect); 164bf215546Sopenharmony_ci ureg_MUL(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_XY), ureg_src(tmp), scale); 165bf215546Sopenharmony_ci ureg_MOV(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_ZW), ureg_imm1f(shader, 1.0f)); 166bf215546Sopenharmony_ci 167bf215546Sopenharmony_ci ureg_MUL(shader, ureg_writemask(tmp, TGSI_WRITEMASK_XW), ureg_scalar(block_num, TGSI_SWIZZLE_X), 168bf215546Sopenharmony_ci ureg_imm1f(shader, 1.0f / zscan->blocks_per_line)); 169bf215546Sopenharmony_ci 170bf215546Sopenharmony_ci ureg_FRC(shader, ureg_writemask(tmp, TGSI_WRITEMASK_Y), ureg_scalar(ureg_src(tmp), TGSI_SWIZZLE_X)); 171bf215546Sopenharmony_ci ureg_FLR(shader, ureg_writemask(tmp, TGSI_WRITEMASK_W), ureg_src(tmp)); 172bf215546Sopenharmony_ci 173bf215546Sopenharmony_ci for (i = 0; i < zscan->num_channels; ++i) { 174bf215546Sopenharmony_ci ureg_ADD(shader, ureg_writemask(tmp, TGSI_WRITEMASK_X), ureg_scalar(ureg_src(tmp), TGSI_SWIZZLE_Y), 175bf215546Sopenharmony_ci ureg_imm1f(shader, 1.0f / (zscan->blocks_per_line * VL_BLOCK_WIDTH) 176bf215546Sopenharmony_ci * ((signed)i - (signed)zscan->num_channels / 2))); 177bf215546Sopenharmony_ci 178bf215546Sopenharmony_ci ureg_MAD(shader, ureg_writemask(o_vtex[i], TGSI_WRITEMASK_X), vrect, 179bf215546Sopenharmony_ci ureg_imm1f(shader, 1.0f / zscan->blocks_per_line), ureg_src(tmp)); 180bf215546Sopenharmony_ci ureg_MOV(shader, ureg_writemask(o_vtex[i], TGSI_WRITEMASK_Y), vrect); 181bf215546Sopenharmony_ci ureg_MOV(shader, ureg_writemask(o_vtex[i], TGSI_WRITEMASK_Z), vpos); 182bf215546Sopenharmony_ci ureg_MUL(shader, ureg_writemask(o_vtex[i], TGSI_WRITEMASK_W), ureg_src(tmp), 183bf215546Sopenharmony_ci ureg_imm1f(shader, (float)zscan->blocks_per_line / zscan->blocks_total)); 184bf215546Sopenharmony_ci } 185bf215546Sopenharmony_ci 186bf215546Sopenharmony_ci ureg_release_temporary(shader, tmp); 187bf215546Sopenharmony_ci ureg_END(shader); 188bf215546Sopenharmony_ci 189bf215546Sopenharmony_ci FREE(o_vtex); 190bf215546Sopenharmony_ci 191bf215546Sopenharmony_ci return ureg_create_shader_and_destroy(shader, zscan->pipe); 192bf215546Sopenharmony_ci} 193bf215546Sopenharmony_ci 194bf215546Sopenharmony_cistatic void * 195bf215546Sopenharmony_cicreate_frag_shader(struct vl_zscan *zscan) 196bf215546Sopenharmony_ci{ 197bf215546Sopenharmony_ci struct ureg_program *shader; 198bf215546Sopenharmony_ci struct ureg_src *vtex; 199bf215546Sopenharmony_ci 200bf215546Sopenharmony_ci struct ureg_src samp_src, samp_scan, samp_quant; 201bf215546Sopenharmony_ci 202bf215546Sopenharmony_ci struct ureg_dst *tmp; 203bf215546Sopenharmony_ci struct ureg_dst quant, fragment; 204bf215546Sopenharmony_ci 205bf215546Sopenharmony_ci unsigned i; 206bf215546Sopenharmony_ci 207bf215546Sopenharmony_ci shader = ureg_create(PIPE_SHADER_FRAGMENT); 208bf215546Sopenharmony_ci if (!shader) 209bf215546Sopenharmony_ci return NULL; 210bf215546Sopenharmony_ci 211bf215546Sopenharmony_ci vtex = MALLOC(zscan->num_channels * sizeof(struct ureg_src)); 212bf215546Sopenharmony_ci tmp = MALLOC(zscan->num_channels * sizeof(struct ureg_dst)); 213bf215546Sopenharmony_ci 214bf215546Sopenharmony_ci for (i = 0; i < zscan->num_channels; ++i) 215bf215546Sopenharmony_ci vtex[i] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_VTEX + i, TGSI_INTERPOLATE_LINEAR); 216bf215546Sopenharmony_ci 217bf215546Sopenharmony_ci samp_src = ureg_DECL_sampler(shader, 0); 218bf215546Sopenharmony_ci samp_scan = ureg_DECL_sampler(shader, 1); 219bf215546Sopenharmony_ci samp_quant = ureg_DECL_sampler(shader, 2); 220bf215546Sopenharmony_ci 221bf215546Sopenharmony_ci for (i = 0; i < zscan->num_channels; ++i) 222bf215546Sopenharmony_ci tmp[i] = ureg_DECL_temporary(shader); 223bf215546Sopenharmony_ci quant = ureg_DECL_temporary(shader); 224bf215546Sopenharmony_ci 225bf215546Sopenharmony_ci fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0); 226bf215546Sopenharmony_ci 227bf215546Sopenharmony_ci /* 228bf215546Sopenharmony_ci * tmp.x = tex(vtex, 1) 229bf215546Sopenharmony_ci * tmp.y = vtex.z 230bf215546Sopenharmony_ci * fragment = tex(tmp, 0) * quant 231bf215546Sopenharmony_ci */ 232bf215546Sopenharmony_ci for (i = 0; i < zscan->num_channels; ++i) 233bf215546Sopenharmony_ci ureg_TEX(shader, ureg_writemask(tmp[i], TGSI_WRITEMASK_X), TGSI_TEXTURE_2D, vtex[i], samp_scan); 234bf215546Sopenharmony_ci 235bf215546Sopenharmony_ci for (i = 0; i < zscan->num_channels; ++i) 236bf215546Sopenharmony_ci ureg_MOV(shader, ureg_writemask(tmp[i], TGSI_WRITEMASK_Y), ureg_scalar(vtex[i], TGSI_SWIZZLE_W)); 237bf215546Sopenharmony_ci 238bf215546Sopenharmony_ci for (i = 0; i < zscan->num_channels; ++i) { 239bf215546Sopenharmony_ci ureg_TEX(shader, ureg_writemask(tmp[0], TGSI_WRITEMASK_X << i), TGSI_TEXTURE_2D, ureg_src(tmp[i]), samp_src); 240bf215546Sopenharmony_ci ureg_TEX(shader, ureg_writemask(quant, TGSI_WRITEMASK_X << i), TGSI_TEXTURE_3D, vtex[i], samp_quant); 241bf215546Sopenharmony_ci } 242bf215546Sopenharmony_ci 243bf215546Sopenharmony_ci ureg_MUL(shader, quant, ureg_src(quant), ureg_imm1f(shader, 16.0f)); 244bf215546Sopenharmony_ci ureg_MUL(shader, fragment, ureg_src(tmp[0]), ureg_src(quant)); 245bf215546Sopenharmony_ci 246bf215546Sopenharmony_ci for (i = 0; i < zscan->num_channels; ++i) 247bf215546Sopenharmony_ci ureg_release_temporary(shader, tmp[i]); 248bf215546Sopenharmony_ci ureg_END(shader); 249bf215546Sopenharmony_ci 250bf215546Sopenharmony_ci FREE(vtex); 251bf215546Sopenharmony_ci FREE(tmp); 252bf215546Sopenharmony_ci 253bf215546Sopenharmony_ci return ureg_create_shader_and_destroy(shader, zscan->pipe); 254bf215546Sopenharmony_ci} 255bf215546Sopenharmony_ci 256bf215546Sopenharmony_cistatic bool 257bf215546Sopenharmony_ciinit_shaders(struct vl_zscan *zscan) 258bf215546Sopenharmony_ci{ 259bf215546Sopenharmony_ci assert(zscan); 260bf215546Sopenharmony_ci 261bf215546Sopenharmony_ci zscan->vs = create_vert_shader(zscan); 262bf215546Sopenharmony_ci if (!zscan->vs) 263bf215546Sopenharmony_ci goto error_vs; 264bf215546Sopenharmony_ci 265bf215546Sopenharmony_ci zscan->fs = create_frag_shader(zscan); 266bf215546Sopenharmony_ci if (!zscan->fs) 267bf215546Sopenharmony_ci goto error_fs; 268bf215546Sopenharmony_ci 269bf215546Sopenharmony_ci return true; 270bf215546Sopenharmony_ci 271bf215546Sopenharmony_cierror_fs: 272bf215546Sopenharmony_ci zscan->pipe->delete_vs_state(zscan->pipe, zscan->vs); 273bf215546Sopenharmony_ci 274bf215546Sopenharmony_cierror_vs: 275bf215546Sopenharmony_ci return false; 276bf215546Sopenharmony_ci} 277bf215546Sopenharmony_ci 278bf215546Sopenharmony_cistatic void 279bf215546Sopenharmony_cicleanup_shaders(struct vl_zscan *zscan) 280bf215546Sopenharmony_ci{ 281bf215546Sopenharmony_ci assert(zscan); 282bf215546Sopenharmony_ci 283bf215546Sopenharmony_ci zscan->pipe->delete_vs_state(zscan->pipe, zscan->vs); 284bf215546Sopenharmony_ci zscan->pipe->delete_fs_state(zscan->pipe, zscan->fs); 285bf215546Sopenharmony_ci} 286bf215546Sopenharmony_ci 287bf215546Sopenharmony_cistatic bool 288bf215546Sopenharmony_ciinit_state(struct vl_zscan *zscan) 289bf215546Sopenharmony_ci{ 290bf215546Sopenharmony_ci struct pipe_blend_state blend; 291bf215546Sopenharmony_ci struct pipe_rasterizer_state rs_state; 292bf215546Sopenharmony_ci struct pipe_sampler_state sampler; 293bf215546Sopenharmony_ci unsigned i; 294bf215546Sopenharmony_ci 295bf215546Sopenharmony_ci assert(zscan); 296bf215546Sopenharmony_ci 297bf215546Sopenharmony_ci memset(&rs_state, 0, sizeof(rs_state)); 298bf215546Sopenharmony_ci rs_state.half_pixel_center = true; 299bf215546Sopenharmony_ci rs_state.bottom_edge_rule = true; 300bf215546Sopenharmony_ci rs_state.depth_clip_near = 1; 301bf215546Sopenharmony_ci rs_state.depth_clip_far = 1; 302bf215546Sopenharmony_ci 303bf215546Sopenharmony_ci zscan->rs_state = zscan->pipe->create_rasterizer_state(zscan->pipe, &rs_state); 304bf215546Sopenharmony_ci if (!zscan->rs_state) 305bf215546Sopenharmony_ci goto error_rs_state; 306bf215546Sopenharmony_ci 307bf215546Sopenharmony_ci memset(&blend, 0, sizeof blend); 308bf215546Sopenharmony_ci 309bf215546Sopenharmony_ci blend.independent_blend_enable = 0; 310bf215546Sopenharmony_ci blend.rt[0].blend_enable = 0; 311bf215546Sopenharmony_ci blend.rt[0].rgb_func = PIPE_BLEND_ADD; 312bf215546Sopenharmony_ci blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE; 313bf215546Sopenharmony_ci blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ONE; 314bf215546Sopenharmony_ci blend.rt[0].alpha_func = PIPE_BLEND_ADD; 315bf215546Sopenharmony_ci blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE; 316bf215546Sopenharmony_ci blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ONE; 317bf215546Sopenharmony_ci blend.logicop_enable = 0; 318bf215546Sopenharmony_ci blend.logicop_func = PIPE_LOGICOP_CLEAR; 319bf215546Sopenharmony_ci /* Needed to allow color writes to FB, even if blending disabled */ 320bf215546Sopenharmony_ci blend.rt[0].colormask = PIPE_MASK_RGBA; 321bf215546Sopenharmony_ci blend.dither = 0; 322bf215546Sopenharmony_ci zscan->blend = zscan->pipe->create_blend_state(zscan->pipe, &blend); 323bf215546Sopenharmony_ci if (!zscan->blend) 324bf215546Sopenharmony_ci goto error_blend; 325bf215546Sopenharmony_ci 326bf215546Sopenharmony_ci for (i = 0; i < 3; ++i) { 327bf215546Sopenharmony_ci memset(&sampler, 0, sizeof(sampler)); 328bf215546Sopenharmony_ci sampler.wrap_s = PIPE_TEX_WRAP_REPEAT; 329bf215546Sopenharmony_ci sampler.wrap_t = PIPE_TEX_WRAP_REPEAT; 330bf215546Sopenharmony_ci sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 331bf215546Sopenharmony_ci sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST; 332bf215546Sopenharmony_ci sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 333bf215546Sopenharmony_ci sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST; 334bf215546Sopenharmony_ci sampler.compare_mode = PIPE_TEX_COMPARE_NONE; 335bf215546Sopenharmony_ci sampler.compare_func = PIPE_FUNC_ALWAYS; 336bf215546Sopenharmony_ci sampler.normalized_coords = 1; 337bf215546Sopenharmony_ci zscan->samplers[i] = zscan->pipe->create_sampler_state(zscan->pipe, &sampler); 338bf215546Sopenharmony_ci if (!zscan->samplers[i]) 339bf215546Sopenharmony_ci goto error_samplers; 340bf215546Sopenharmony_ci } 341bf215546Sopenharmony_ci 342bf215546Sopenharmony_ci return true; 343bf215546Sopenharmony_ci 344bf215546Sopenharmony_cierror_samplers: 345bf215546Sopenharmony_ci for (i = 0; i < 2; ++i) 346bf215546Sopenharmony_ci if (zscan->samplers[i]) 347bf215546Sopenharmony_ci zscan->pipe->delete_sampler_state(zscan->pipe, zscan->samplers[i]); 348bf215546Sopenharmony_ci 349bf215546Sopenharmony_ci zscan->pipe->delete_rasterizer_state(zscan->pipe, zscan->rs_state); 350bf215546Sopenharmony_ci 351bf215546Sopenharmony_cierror_blend: 352bf215546Sopenharmony_ci zscan->pipe->delete_blend_state(zscan->pipe, zscan->blend); 353bf215546Sopenharmony_ci 354bf215546Sopenharmony_cierror_rs_state: 355bf215546Sopenharmony_ci return false; 356bf215546Sopenharmony_ci} 357bf215546Sopenharmony_ci 358bf215546Sopenharmony_cistatic void 359bf215546Sopenharmony_cicleanup_state(struct vl_zscan *zscan) 360bf215546Sopenharmony_ci{ 361bf215546Sopenharmony_ci unsigned i; 362bf215546Sopenharmony_ci 363bf215546Sopenharmony_ci assert(zscan); 364bf215546Sopenharmony_ci 365bf215546Sopenharmony_ci for (i = 0; i < 3; ++i) 366bf215546Sopenharmony_ci zscan->pipe->delete_sampler_state(zscan->pipe, zscan->samplers[i]); 367bf215546Sopenharmony_ci 368bf215546Sopenharmony_ci zscan->pipe->delete_rasterizer_state(zscan->pipe, zscan->rs_state); 369bf215546Sopenharmony_ci zscan->pipe->delete_blend_state(zscan->pipe, zscan->blend); 370bf215546Sopenharmony_ci} 371bf215546Sopenharmony_ci 372bf215546Sopenharmony_cistruct pipe_sampler_view * 373bf215546Sopenharmony_civl_zscan_layout(struct pipe_context *pipe, const int layout[64], unsigned blocks_per_line) 374bf215546Sopenharmony_ci{ 375bf215546Sopenharmony_ci const unsigned total_size = blocks_per_line * VL_BLOCK_WIDTH * VL_BLOCK_HEIGHT; 376bf215546Sopenharmony_ci 377bf215546Sopenharmony_ci int patched_layout[64]; 378bf215546Sopenharmony_ci 379bf215546Sopenharmony_ci struct pipe_resource res_tmpl, *res; 380bf215546Sopenharmony_ci struct pipe_sampler_view sv_tmpl, *sv; 381bf215546Sopenharmony_ci struct pipe_transfer *buf_transfer; 382bf215546Sopenharmony_ci unsigned x, y, i, pitch; 383bf215546Sopenharmony_ci float *f; 384bf215546Sopenharmony_ci 385bf215546Sopenharmony_ci struct pipe_box rect = 386bf215546Sopenharmony_ci { 387bf215546Sopenharmony_ci 0, 0, 0, 388bf215546Sopenharmony_ci VL_BLOCK_WIDTH * blocks_per_line, 389bf215546Sopenharmony_ci VL_BLOCK_HEIGHT, 390bf215546Sopenharmony_ci 1 391bf215546Sopenharmony_ci }; 392bf215546Sopenharmony_ci 393bf215546Sopenharmony_ci assert(pipe && layout && blocks_per_line); 394bf215546Sopenharmony_ci 395bf215546Sopenharmony_ci for (i = 0; i < 64; ++i) 396bf215546Sopenharmony_ci patched_layout[layout[i]] = i; 397bf215546Sopenharmony_ci 398bf215546Sopenharmony_ci memset(&res_tmpl, 0, sizeof(res_tmpl)); 399bf215546Sopenharmony_ci res_tmpl.target = PIPE_TEXTURE_2D; 400bf215546Sopenharmony_ci res_tmpl.format = PIPE_FORMAT_R32_FLOAT; 401bf215546Sopenharmony_ci res_tmpl.width0 = VL_BLOCK_WIDTH * blocks_per_line; 402bf215546Sopenharmony_ci res_tmpl.height0 = VL_BLOCK_HEIGHT; 403bf215546Sopenharmony_ci res_tmpl.depth0 = 1; 404bf215546Sopenharmony_ci res_tmpl.array_size = 1; 405bf215546Sopenharmony_ci res_tmpl.usage = PIPE_USAGE_IMMUTABLE; 406bf215546Sopenharmony_ci res_tmpl.bind = PIPE_BIND_SAMPLER_VIEW; 407bf215546Sopenharmony_ci 408bf215546Sopenharmony_ci res = pipe->screen->resource_create(pipe->screen, &res_tmpl); 409bf215546Sopenharmony_ci if (!res) 410bf215546Sopenharmony_ci goto error_resource; 411bf215546Sopenharmony_ci 412bf215546Sopenharmony_ci f = pipe->texture_map(pipe, res, 413bf215546Sopenharmony_ci 0, PIPE_MAP_WRITE | PIPE_MAP_DISCARD_RANGE, 414bf215546Sopenharmony_ci &rect, &buf_transfer); 415bf215546Sopenharmony_ci if (!f) 416bf215546Sopenharmony_ci goto error_map; 417bf215546Sopenharmony_ci 418bf215546Sopenharmony_ci pitch = buf_transfer->stride / sizeof(float); 419bf215546Sopenharmony_ci 420bf215546Sopenharmony_ci for (i = 0; i < blocks_per_line; ++i) 421bf215546Sopenharmony_ci for (y = 0; y < VL_BLOCK_HEIGHT; ++y) 422bf215546Sopenharmony_ci for (x = 0; x < VL_BLOCK_WIDTH; ++x) { 423bf215546Sopenharmony_ci float addr = patched_layout[x + y * VL_BLOCK_WIDTH] + 424bf215546Sopenharmony_ci i * VL_BLOCK_WIDTH * VL_BLOCK_HEIGHT; 425bf215546Sopenharmony_ci 426bf215546Sopenharmony_ci addr /= total_size; 427bf215546Sopenharmony_ci 428bf215546Sopenharmony_ci f[i * VL_BLOCK_WIDTH + y * pitch + x] = addr; 429bf215546Sopenharmony_ci } 430bf215546Sopenharmony_ci 431bf215546Sopenharmony_ci pipe->texture_unmap(pipe, buf_transfer); 432bf215546Sopenharmony_ci 433bf215546Sopenharmony_ci memset(&sv_tmpl, 0, sizeof(sv_tmpl)); 434bf215546Sopenharmony_ci u_sampler_view_default_template(&sv_tmpl, res, res->format); 435bf215546Sopenharmony_ci sv = pipe->create_sampler_view(pipe, res, &sv_tmpl); 436bf215546Sopenharmony_ci pipe_resource_reference(&res, NULL); 437bf215546Sopenharmony_ci if (!sv) 438bf215546Sopenharmony_ci goto error_map; 439bf215546Sopenharmony_ci 440bf215546Sopenharmony_ci return sv; 441bf215546Sopenharmony_ci 442bf215546Sopenharmony_cierror_map: 443bf215546Sopenharmony_ci pipe_resource_reference(&res, NULL); 444bf215546Sopenharmony_ci 445bf215546Sopenharmony_cierror_resource: 446bf215546Sopenharmony_ci return NULL; 447bf215546Sopenharmony_ci} 448bf215546Sopenharmony_ci 449bf215546Sopenharmony_cibool 450bf215546Sopenharmony_civl_zscan_init(struct vl_zscan *zscan, struct pipe_context *pipe, 451bf215546Sopenharmony_ci unsigned buffer_width, unsigned buffer_height, 452bf215546Sopenharmony_ci unsigned blocks_per_line, unsigned blocks_total, 453bf215546Sopenharmony_ci unsigned num_channels) 454bf215546Sopenharmony_ci{ 455bf215546Sopenharmony_ci assert(zscan && pipe); 456bf215546Sopenharmony_ci 457bf215546Sopenharmony_ci zscan->pipe = pipe; 458bf215546Sopenharmony_ci zscan->buffer_width = buffer_width; 459bf215546Sopenharmony_ci zscan->buffer_height = buffer_height; 460bf215546Sopenharmony_ci zscan->num_channels = num_channels; 461bf215546Sopenharmony_ci zscan->blocks_per_line = blocks_per_line; 462bf215546Sopenharmony_ci zscan->blocks_total = blocks_total; 463bf215546Sopenharmony_ci 464bf215546Sopenharmony_ci if(!init_shaders(zscan)) 465bf215546Sopenharmony_ci return false; 466bf215546Sopenharmony_ci 467bf215546Sopenharmony_ci if(!init_state(zscan)) { 468bf215546Sopenharmony_ci cleanup_shaders(zscan); 469bf215546Sopenharmony_ci return false; 470bf215546Sopenharmony_ci } 471bf215546Sopenharmony_ci 472bf215546Sopenharmony_ci return true; 473bf215546Sopenharmony_ci} 474bf215546Sopenharmony_ci 475bf215546Sopenharmony_civoid 476bf215546Sopenharmony_civl_zscan_cleanup(struct vl_zscan *zscan) 477bf215546Sopenharmony_ci{ 478bf215546Sopenharmony_ci assert(zscan); 479bf215546Sopenharmony_ci 480bf215546Sopenharmony_ci cleanup_shaders(zscan); 481bf215546Sopenharmony_ci cleanup_state(zscan); 482bf215546Sopenharmony_ci} 483bf215546Sopenharmony_ci 484bf215546Sopenharmony_cibool 485bf215546Sopenharmony_civl_zscan_init_buffer(struct vl_zscan *zscan, struct vl_zscan_buffer *buffer, 486bf215546Sopenharmony_ci struct pipe_sampler_view *src, struct pipe_surface *dst) 487bf215546Sopenharmony_ci{ 488bf215546Sopenharmony_ci struct pipe_resource res_tmpl, *res; 489bf215546Sopenharmony_ci struct pipe_sampler_view sv_tmpl; 490bf215546Sopenharmony_ci 491bf215546Sopenharmony_ci assert(zscan && buffer); 492bf215546Sopenharmony_ci 493bf215546Sopenharmony_ci memset(buffer, 0, sizeof(struct vl_zscan_buffer)); 494bf215546Sopenharmony_ci 495bf215546Sopenharmony_ci pipe_sampler_view_reference(&buffer->src, src); 496bf215546Sopenharmony_ci 497bf215546Sopenharmony_ci buffer->viewport.scale[0] = dst->width; 498bf215546Sopenharmony_ci buffer->viewport.scale[1] = dst->height; 499bf215546Sopenharmony_ci buffer->viewport.scale[2] = 1; 500bf215546Sopenharmony_ci buffer->viewport.translate[0] = 0; 501bf215546Sopenharmony_ci buffer->viewport.translate[1] = 0; 502bf215546Sopenharmony_ci buffer->viewport.translate[2] = 0; 503bf215546Sopenharmony_ci buffer->viewport.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X; 504bf215546Sopenharmony_ci buffer->viewport.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y; 505bf215546Sopenharmony_ci buffer->viewport.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z; 506bf215546Sopenharmony_ci buffer->viewport.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W; 507bf215546Sopenharmony_ci 508bf215546Sopenharmony_ci buffer->fb_state.width = dst->width; 509bf215546Sopenharmony_ci buffer->fb_state.height = dst->height; 510bf215546Sopenharmony_ci buffer->fb_state.nr_cbufs = 1; 511bf215546Sopenharmony_ci pipe_surface_reference(&buffer->fb_state.cbufs[0], dst); 512bf215546Sopenharmony_ci 513bf215546Sopenharmony_ci memset(&res_tmpl, 0, sizeof(res_tmpl)); 514bf215546Sopenharmony_ci res_tmpl.target = PIPE_TEXTURE_3D; 515bf215546Sopenharmony_ci res_tmpl.format = PIPE_FORMAT_R8_UNORM; 516bf215546Sopenharmony_ci res_tmpl.width0 = VL_BLOCK_WIDTH * zscan->blocks_per_line; 517bf215546Sopenharmony_ci res_tmpl.height0 = VL_BLOCK_HEIGHT; 518bf215546Sopenharmony_ci res_tmpl.depth0 = 2; 519bf215546Sopenharmony_ci res_tmpl.array_size = 1; 520bf215546Sopenharmony_ci res_tmpl.usage = PIPE_USAGE_IMMUTABLE; 521bf215546Sopenharmony_ci res_tmpl.bind = PIPE_BIND_SAMPLER_VIEW; 522bf215546Sopenharmony_ci 523bf215546Sopenharmony_ci res = zscan->pipe->screen->resource_create(zscan->pipe->screen, &res_tmpl); 524bf215546Sopenharmony_ci if (!res) 525bf215546Sopenharmony_ci return false; 526bf215546Sopenharmony_ci 527bf215546Sopenharmony_ci memset(&sv_tmpl, 0, sizeof(sv_tmpl)); 528bf215546Sopenharmony_ci u_sampler_view_default_template(&sv_tmpl, res, res->format); 529bf215546Sopenharmony_ci sv_tmpl.swizzle_r = sv_tmpl.swizzle_g = sv_tmpl.swizzle_b = sv_tmpl.swizzle_a = TGSI_SWIZZLE_X; 530bf215546Sopenharmony_ci buffer->quant = zscan->pipe->create_sampler_view(zscan->pipe, res, &sv_tmpl); 531bf215546Sopenharmony_ci pipe_resource_reference(&res, NULL); 532bf215546Sopenharmony_ci if (!buffer->quant) 533bf215546Sopenharmony_ci return false; 534bf215546Sopenharmony_ci 535bf215546Sopenharmony_ci return true; 536bf215546Sopenharmony_ci} 537bf215546Sopenharmony_ci 538bf215546Sopenharmony_civoid 539bf215546Sopenharmony_civl_zscan_cleanup_buffer(struct vl_zscan_buffer *buffer) 540bf215546Sopenharmony_ci{ 541bf215546Sopenharmony_ci assert(buffer); 542bf215546Sopenharmony_ci 543bf215546Sopenharmony_ci pipe_sampler_view_reference(&buffer->src, NULL); 544bf215546Sopenharmony_ci pipe_sampler_view_reference(&buffer->layout, NULL); 545bf215546Sopenharmony_ci pipe_sampler_view_reference(&buffer->quant, NULL); 546bf215546Sopenharmony_ci pipe_surface_reference(&buffer->fb_state.cbufs[0], NULL); 547bf215546Sopenharmony_ci} 548bf215546Sopenharmony_ci 549bf215546Sopenharmony_civoid 550bf215546Sopenharmony_civl_zscan_set_layout(struct vl_zscan_buffer *buffer, struct pipe_sampler_view *layout) 551bf215546Sopenharmony_ci{ 552bf215546Sopenharmony_ci assert(buffer); 553bf215546Sopenharmony_ci assert(layout); 554bf215546Sopenharmony_ci 555bf215546Sopenharmony_ci pipe_sampler_view_reference(&buffer->layout, layout); 556bf215546Sopenharmony_ci} 557bf215546Sopenharmony_ci 558bf215546Sopenharmony_civoid 559bf215546Sopenharmony_civl_zscan_upload_quant(struct vl_zscan *zscan, struct vl_zscan_buffer *buffer, 560bf215546Sopenharmony_ci const uint8_t matrix[64], bool intra) 561bf215546Sopenharmony_ci{ 562bf215546Sopenharmony_ci struct pipe_context *pipe; 563bf215546Sopenharmony_ci struct pipe_transfer *buf_transfer; 564bf215546Sopenharmony_ci unsigned x, y, i, pitch; 565bf215546Sopenharmony_ci uint8_t *data; 566bf215546Sopenharmony_ci 567bf215546Sopenharmony_ci struct pipe_box rect = 568bf215546Sopenharmony_ci { 569bf215546Sopenharmony_ci 0, 0, intra ? 1 : 0, 570bf215546Sopenharmony_ci VL_BLOCK_WIDTH, 571bf215546Sopenharmony_ci VL_BLOCK_HEIGHT, 572bf215546Sopenharmony_ci 1 573bf215546Sopenharmony_ci }; 574bf215546Sopenharmony_ci 575bf215546Sopenharmony_ci assert(buffer); 576bf215546Sopenharmony_ci assert(matrix); 577bf215546Sopenharmony_ci 578bf215546Sopenharmony_ci pipe = zscan->pipe; 579bf215546Sopenharmony_ci 580bf215546Sopenharmony_ci rect.width *= zscan->blocks_per_line; 581bf215546Sopenharmony_ci 582bf215546Sopenharmony_ci data = pipe->texture_map(pipe, buffer->quant->texture, 583bf215546Sopenharmony_ci 0, PIPE_MAP_WRITE | 584bf215546Sopenharmony_ci PIPE_MAP_DISCARD_RANGE, 585bf215546Sopenharmony_ci &rect, &buf_transfer); 586bf215546Sopenharmony_ci if (!data) 587bf215546Sopenharmony_ci return; 588bf215546Sopenharmony_ci 589bf215546Sopenharmony_ci pitch = buf_transfer->stride; 590bf215546Sopenharmony_ci 591bf215546Sopenharmony_ci for (i = 0; i < zscan->blocks_per_line; ++i) 592bf215546Sopenharmony_ci for (y = 0; y < VL_BLOCK_HEIGHT; ++y) 593bf215546Sopenharmony_ci for (x = 0; x < VL_BLOCK_WIDTH; ++x) 594bf215546Sopenharmony_ci data[i * VL_BLOCK_WIDTH + y * pitch + x] = matrix[x + y * VL_BLOCK_WIDTH]; 595bf215546Sopenharmony_ci 596bf215546Sopenharmony_ci pipe->texture_unmap(pipe, buf_transfer); 597bf215546Sopenharmony_ci} 598bf215546Sopenharmony_ci 599bf215546Sopenharmony_civoid 600bf215546Sopenharmony_civl_zscan_render(struct vl_zscan *zscan, struct vl_zscan_buffer *buffer, unsigned num_instances) 601bf215546Sopenharmony_ci{ 602bf215546Sopenharmony_ci assert(buffer); 603bf215546Sopenharmony_ci 604bf215546Sopenharmony_ci zscan->pipe->bind_rasterizer_state(zscan->pipe, zscan->rs_state); 605bf215546Sopenharmony_ci zscan->pipe->bind_blend_state(zscan->pipe, zscan->blend); 606bf215546Sopenharmony_ci zscan->pipe->bind_sampler_states(zscan->pipe, PIPE_SHADER_FRAGMENT, 607bf215546Sopenharmony_ci 0, 3, zscan->samplers); 608bf215546Sopenharmony_ci zscan->pipe->set_framebuffer_state(zscan->pipe, &buffer->fb_state); 609bf215546Sopenharmony_ci zscan->pipe->set_viewport_states(zscan->pipe, 0, 1, &buffer->viewport); 610bf215546Sopenharmony_ci zscan->pipe->set_sampler_views(zscan->pipe, PIPE_SHADER_FRAGMENT, 611bf215546Sopenharmony_ci 0, 3, 0, false, &buffer->src); 612bf215546Sopenharmony_ci zscan->pipe->bind_vs_state(zscan->pipe, zscan->vs); 613bf215546Sopenharmony_ci zscan->pipe->bind_fs_state(zscan->pipe, zscan->fs); 614bf215546Sopenharmony_ci util_draw_arrays_instanced(zscan->pipe, PIPE_PRIM_QUADS, 0, 4, 0, num_instances); 615bf215546Sopenharmony_ci} 616