1bf215546Sopenharmony_ci/************************************************************************** 2bf215546Sopenharmony_ci * 3bf215546Sopenharmony_ci * Copyright 2003 VMware, Inc. 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 /* 29bf215546Sopenharmony_ci * Authors: 30bf215546Sopenharmony_ci * Keith Whitwell <keithw@vmware.com> 31bf215546Sopenharmony_ci */ 32bf215546Sopenharmony_ci 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_ci#ifndef ST_PROGRAM_H 35bf215546Sopenharmony_ci#define ST_PROGRAM_H 36bf215546Sopenharmony_ci 37bf215546Sopenharmony_ci#include "main/atifragshader.h" 38bf215546Sopenharmony_ci#include "program/program.h" 39bf215546Sopenharmony_ci#include "pipe/p_state.h" 40bf215546Sopenharmony_ci#include "tgsi/tgsi_from_mesa.h" 41bf215546Sopenharmony_ci#include "st_context.h" 42bf215546Sopenharmony_ci#include "st_texture.h" 43bf215546Sopenharmony_ci 44bf215546Sopenharmony_ci#ifdef __cplusplus 45bf215546Sopenharmony_ciextern "C" { 46bf215546Sopenharmony_ci#endif 47bf215546Sopenharmony_ci 48bf215546Sopenharmony_cistruct st_external_sampler_key 49bf215546Sopenharmony_ci{ 50bf215546Sopenharmony_ci GLuint lower_nv12; /**< bitmask of 2 plane YUV samplers */ 51bf215546Sopenharmony_ci GLuint lower_iyuv; /**< bitmask of 3 plane YUV samplers */ 52bf215546Sopenharmony_ci GLuint lower_xy_uxvx; /**< bitmask of 2 plane YUV samplers */ 53bf215546Sopenharmony_ci GLuint lower_yx_xuxv; /**< bitmask of 2 plane YUV samplers */ 54bf215546Sopenharmony_ci GLuint lower_ayuv; 55bf215546Sopenharmony_ci GLuint lower_xyuv; 56bf215546Sopenharmony_ci GLuint lower_yuv; 57bf215546Sopenharmony_ci GLuint lower_yu_yv; 58bf215546Sopenharmony_ci GLuint lower_y41x; 59bf215546Sopenharmony_ci GLuint bt709; 60bf215546Sopenharmony_ci GLuint bt2020; 61bf215546Sopenharmony_ci GLuint yuv_full_range; 62bf215546Sopenharmony_ci}; 63bf215546Sopenharmony_ci 64bf215546Sopenharmony_cistatic inline struct st_external_sampler_key 65bf215546Sopenharmony_cist_get_external_sampler_key(struct st_context *st, struct gl_program *prog) 66bf215546Sopenharmony_ci{ 67bf215546Sopenharmony_ci unsigned mask = prog->ExternalSamplersUsed; 68bf215546Sopenharmony_ci struct st_external_sampler_key key; 69bf215546Sopenharmony_ci 70bf215546Sopenharmony_ci memset(&key, 0, sizeof(key)); 71bf215546Sopenharmony_ci 72bf215546Sopenharmony_ci while (unlikely(mask)) { 73bf215546Sopenharmony_ci unsigned unit = u_bit_scan(&mask); 74bf215546Sopenharmony_ci struct gl_texture_object *stObj = 75bf215546Sopenharmony_ci st_get_texture_object(st->ctx, prog, unit); 76bf215546Sopenharmony_ci enum pipe_format format = st_get_view_format(stObj); 77bf215546Sopenharmony_ci 78bf215546Sopenharmony_ci /* if resource format matches then YUV wasn't lowered */ 79bf215546Sopenharmony_ci if (format == stObj->pt->format) 80bf215546Sopenharmony_ci continue; 81bf215546Sopenharmony_ci 82bf215546Sopenharmony_ci switch (format) { 83bf215546Sopenharmony_ci case PIPE_FORMAT_NV12: 84bf215546Sopenharmony_ci if (stObj->pt->format == PIPE_FORMAT_R8_G8B8_420_UNORM) { 85bf215546Sopenharmony_ci key.lower_yuv |= (1 << unit); 86bf215546Sopenharmony_ci break; 87bf215546Sopenharmony_ci } 88bf215546Sopenharmony_ci FALLTHROUGH; 89bf215546Sopenharmony_ci case PIPE_FORMAT_P010: 90bf215546Sopenharmony_ci case PIPE_FORMAT_P012: 91bf215546Sopenharmony_ci case PIPE_FORMAT_P016: 92bf215546Sopenharmony_ci key.lower_nv12 |= (1 << unit); 93bf215546Sopenharmony_ci break; 94bf215546Sopenharmony_ci case PIPE_FORMAT_IYUV: 95bf215546Sopenharmony_ci key.lower_iyuv |= (1 << unit); 96bf215546Sopenharmony_ci break; 97bf215546Sopenharmony_ci case PIPE_FORMAT_YUYV: 98bf215546Sopenharmony_ci if (stObj->pt->format == PIPE_FORMAT_R8G8_R8B8_UNORM) { 99bf215546Sopenharmony_ci key.lower_yu_yv |= (1 << unit); 100bf215546Sopenharmony_ci break; 101bf215546Sopenharmony_ci } 102bf215546Sopenharmony_ci FALLTHROUGH; 103bf215546Sopenharmony_ci case PIPE_FORMAT_Y210: 104bf215546Sopenharmony_ci case PIPE_FORMAT_Y212: 105bf215546Sopenharmony_ci case PIPE_FORMAT_Y216: 106bf215546Sopenharmony_ci key.lower_yx_xuxv |= (1 << unit); 107bf215546Sopenharmony_ci break; 108bf215546Sopenharmony_ci case PIPE_FORMAT_UYVY: 109bf215546Sopenharmony_ci if (stObj->pt->format == PIPE_FORMAT_G8R8_B8R8_UNORM) { 110bf215546Sopenharmony_ci key.lower_yu_yv |= (1 << unit); 111bf215546Sopenharmony_ci break; 112bf215546Sopenharmony_ci } 113bf215546Sopenharmony_ci key.lower_xy_uxvx |= (1 << unit); 114bf215546Sopenharmony_ci break; 115bf215546Sopenharmony_ci case PIPE_FORMAT_AYUV: 116bf215546Sopenharmony_ci key.lower_ayuv |= (1 << unit); 117bf215546Sopenharmony_ci break; 118bf215546Sopenharmony_ci case PIPE_FORMAT_XYUV: 119bf215546Sopenharmony_ci key.lower_xyuv |= (1 << unit); 120bf215546Sopenharmony_ci break; 121bf215546Sopenharmony_ci case PIPE_FORMAT_Y410: 122bf215546Sopenharmony_ci case PIPE_FORMAT_Y412: 123bf215546Sopenharmony_ci case PIPE_FORMAT_Y416: 124bf215546Sopenharmony_ci key.lower_y41x |= (1 << unit); 125bf215546Sopenharmony_ci break; 126bf215546Sopenharmony_ci default: 127bf215546Sopenharmony_ci printf("mesa: st_get_external_sampler_key: unhandled pipe format %u\n", 128bf215546Sopenharmony_ci format); 129bf215546Sopenharmony_ci break; 130bf215546Sopenharmony_ci } 131bf215546Sopenharmony_ci 132bf215546Sopenharmony_ci switch (stObj->yuv_color_space) { 133bf215546Sopenharmony_ci case GL_TEXTURE_YUV_COLOR_SPACE_REC601: 134bf215546Sopenharmony_ci break; 135bf215546Sopenharmony_ci case GL_TEXTURE_YUV_COLOR_SPACE_REC709: 136bf215546Sopenharmony_ci key.bt709 |= (1 << unit); 137bf215546Sopenharmony_ci break; 138bf215546Sopenharmony_ci case GL_TEXTURE_YUV_COLOR_SPACE_REC2020: 139bf215546Sopenharmony_ci key.bt2020 |= (1 << unit); 140bf215546Sopenharmony_ci break; 141bf215546Sopenharmony_ci } 142bf215546Sopenharmony_ci 143bf215546Sopenharmony_ci if (stObj->yuv_full_range) 144bf215546Sopenharmony_ci key.yuv_full_range |= (1 << unit); 145bf215546Sopenharmony_ci } 146bf215546Sopenharmony_ci 147bf215546Sopenharmony_ci return key; 148bf215546Sopenharmony_ci} 149bf215546Sopenharmony_ci 150bf215546Sopenharmony_ci/** Fragment program variant key 151bf215546Sopenharmony_ci * 152bf215546Sopenharmony_ci * Please update st_get_fp_variant() perf_debug() when adding fields. 153bf215546Sopenharmony_ci */ 154bf215546Sopenharmony_cistruct st_fp_variant_key 155bf215546Sopenharmony_ci{ 156bf215546Sopenharmony_ci struct st_context *st; /**< variants are per-context */ 157bf215546Sopenharmony_ci 158bf215546Sopenharmony_ci /** for glBitmap */ 159bf215546Sopenharmony_ci GLuint bitmap:1; /**< glBitmap variant? */ 160bf215546Sopenharmony_ci 161bf215546Sopenharmony_ci /** for glDrawPixels */ 162bf215546Sopenharmony_ci GLuint drawpixels:1; /**< glDrawPixels variant */ 163bf215546Sopenharmony_ci GLuint scaleAndBias:1; /**< glDrawPixels w/ scale and/or bias? */ 164bf215546Sopenharmony_ci GLuint pixelMaps:1; /**< glDrawPixels w/ pixel lookup map? */ 165bf215546Sopenharmony_ci 166bf215546Sopenharmony_ci /** for ARB_color_buffer_float */ 167bf215546Sopenharmony_ci GLuint clamp_color:1; 168bf215546Sopenharmony_ci 169bf215546Sopenharmony_ci /** for ARB_sample_shading */ 170bf215546Sopenharmony_ci GLuint persample_shading:1; 171bf215546Sopenharmony_ci 172bf215546Sopenharmony_ci /** needed for ATI_fragment_shader */ 173bf215546Sopenharmony_ci GLuint fog:2; 174bf215546Sopenharmony_ci 175bf215546Sopenharmony_ci /** for OpenGL 1.0 on modern hardware */ 176bf215546Sopenharmony_ci GLuint lower_two_sided_color:1; 177bf215546Sopenharmony_ci 178bf215546Sopenharmony_ci GLuint lower_flatshade:1; 179bf215546Sopenharmony_ci GLuint lower_texcoord_replace:MAX_TEXTURE_COORD_UNITS; 180bf215546Sopenharmony_ci unsigned lower_alpha_func:3; 181bf215546Sopenharmony_ci 182bf215546Sopenharmony_ci /** needed for ATI_fragment_shader */ 183bf215546Sopenharmony_ci uint8_t texture_index[MAX_NUM_FRAGMENT_REGISTERS_ATI]; 184bf215546Sopenharmony_ci 185bf215546Sopenharmony_ci struct st_external_sampler_key external; 186bf215546Sopenharmony_ci 187bf215546Sopenharmony_ci /* bitmask of sampler units; PIPE_CAP_GL_CLAMP */ 188bf215546Sopenharmony_ci uint32_t gl_clamp[3]; 189bf215546Sopenharmony_ci}; 190bf215546Sopenharmony_ci 191bf215546Sopenharmony_ci/** 192bf215546Sopenharmony_ci * Base class for shader variants. 193bf215546Sopenharmony_ci */ 194bf215546Sopenharmony_cistruct st_variant 195bf215546Sopenharmony_ci{ 196bf215546Sopenharmony_ci /** next in linked list */ 197bf215546Sopenharmony_ci struct st_variant *next; 198bf215546Sopenharmony_ci 199bf215546Sopenharmony_ci /** st_context from the shader key */ 200bf215546Sopenharmony_ci struct st_context *st; 201bf215546Sopenharmony_ci 202bf215546Sopenharmony_ci void *driver_shader; 203bf215546Sopenharmony_ci}; 204bf215546Sopenharmony_ci 205bf215546Sopenharmony_ci/** 206bf215546Sopenharmony_ci * Variant of a fragment program. 207bf215546Sopenharmony_ci */ 208bf215546Sopenharmony_cistruct st_fp_variant 209bf215546Sopenharmony_ci{ 210bf215546Sopenharmony_ci struct st_variant base; 211bf215546Sopenharmony_ci 212bf215546Sopenharmony_ci /** Parameters which generated this version of fragment program */ 213bf215546Sopenharmony_ci struct st_fp_variant_key key; 214bf215546Sopenharmony_ci 215bf215546Sopenharmony_ci /** For glBitmap variants */ 216bf215546Sopenharmony_ci uint bitmap_sampler; 217bf215546Sopenharmony_ci 218bf215546Sopenharmony_ci /** For glDrawPixels variants */ 219bf215546Sopenharmony_ci unsigned drawpix_sampler; 220bf215546Sopenharmony_ci unsigned pixelmap_sampler; 221bf215546Sopenharmony_ci}; 222bf215546Sopenharmony_ci 223bf215546Sopenharmony_ci 224bf215546Sopenharmony_ci/** Shader key shared by other shaders. 225bf215546Sopenharmony_ci * 226bf215546Sopenharmony_ci * Please update st_get_common_variant() perf_debug() when adding fields. 227bf215546Sopenharmony_ci */ 228bf215546Sopenharmony_cistruct st_common_variant_key 229bf215546Sopenharmony_ci{ 230bf215546Sopenharmony_ci struct st_context *st; /**< variants are per-context */ 231bf215546Sopenharmony_ci bool passthrough_edgeflags; 232bf215546Sopenharmony_ci 233bf215546Sopenharmony_ci /** for ARB_color_buffer_float */ 234bf215546Sopenharmony_ci bool clamp_color; 235bf215546Sopenharmony_ci 236bf215546Sopenharmony_ci /** lower glPointSize to gl_PointSize */ 237bf215546Sopenharmony_ci boolean export_point_size; 238bf215546Sopenharmony_ci 239bf215546Sopenharmony_ci /* for user-defined clip-planes */ 240bf215546Sopenharmony_ci uint8_t lower_ucp; 241bf215546Sopenharmony_ci 242bf215546Sopenharmony_ci /* Whether st_variant::driver_shader is for the draw module, 243bf215546Sopenharmony_ci * not for the driver. 244bf215546Sopenharmony_ci */ 245bf215546Sopenharmony_ci bool is_draw_shader; 246bf215546Sopenharmony_ci 247bf215546Sopenharmony_ci /* bitmask of sampler units; PIPE_CAP_GL_CLAMP */ 248bf215546Sopenharmony_ci uint32_t gl_clamp[3]; 249bf215546Sopenharmony_ci}; 250bf215546Sopenharmony_ci 251bf215546Sopenharmony_ci 252bf215546Sopenharmony_ci/** 253bf215546Sopenharmony_ci * Common shader variant. 254bf215546Sopenharmony_ci */ 255bf215546Sopenharmony_cistruct st_common_variant 256bf215546Sopenharmony_ci{ 257bf215546Sopenharmony_ci struct st_variant base; 258bf215546Sopenharmony_ci 259bf215546Sopenharmony_ci /* Parameters which generated this variant. */ 260bf215546Sopenharmony_ci struct st_common_variant_key key; 261bf215546Sopenharmony_ci 262bf215546Sopenharmony_ci /* Bitfield of VERT_BIT_* bits matching vertex shader inputs. */ 263bf215546Sopenharmony_ci GLbitfield vert_attrib_mask; 264bf215546Sopenharmony_ci}; 265bf215546Sopenharmony_ci 266bf215546Sopenharmony_cistatic inline struct st_common_variant * 267bf215546Sopenharmony_cist_common_variant(struct st_variant *v) 268bf215546Sopenharmony_ci{ 269bf215546Sopenharmony_ci return (struct st_common_variant*)v; 270bf215546Sopenharmony_ci} 271bf215546Sopenharmony_ci 272bf215546Sopenharmony_cistatic inline struct st_fp_variant * 273bf215546Sopenharmony_cist_fp_variant(struct st_variant *v) 274bf215546Sopenharmony_ci{ 275bf215546Sopenharmony_ci return (struct st_fp_variant*)v; 276bf215546Sopenharmony_ci} 277bf215546Sopenharmony_ci 278bf215546Sopenharmony_ci/** 279bf215546Sopenharmony_ci * This defines mapping from Mesa VARYING_SLOTs to TGSI GENERIC slots. 280bf215546Sopenharmony_ci */ 281bf215546Sopenharmony_cistatic inline unsigned 282bf215546Sopenharmony_cist_get_generic_varying_index(struct st_context *st, GLuint attr) 283bf215546Sopenharmony_ci{ 284bf215546Sopenharmony_ci return tgsi_get_generic_gl_varying_index((gl_varying_slot)attr, 285bf215546Sopenharmony_ci st->needs_texcoord_semantic); 286bf215546Sopenharmony_ci} 287bf215546Sopenharmony_ci 288bf215546Sopenharmony_ciextern void 289bf215546Sopenharmony_cist_set_prog_affected_state_flags(struct gl_program *prog); 290bf215546Sopenharmony_ci 291bf215546Sopenharmony_ci 292bf215546Sopenharmony_ciextern struct st_fp_variant * 293bf215546Sopenharmony_cist_get_fp_variant(struct st_context *st, 294bf215546Sopenharmony_ci struct gl_program *stfp, 295bf215546Sopenharmony_ci const struct st_fp_variant_key *key); 296bf215546Sopenharmony_ci 297bf215546Sopenharmony_ciextern struct st_common_variant * 298bf215546Sopenharmony_cist_get_common_variant(struct st_context *st, 299bf215546Sopenharmony_ci struct gl_program *p, 300bf215546Sopenharmony_ci const struct st_common_variant_key *key); 301bf215546Sopenharmony_ci 302bf215546Sopenharmony_ciextern void 303bf215546Sopenharmony_cist_release_variants(struct st_context *st, struct gl_program *p); 304bf215546Sopenharmony_ci 305bf215546Sopenharmony_ciextern void 306bf215546Sopenharmony_cist_release_program(struct st_context *st, struct gl_program **p); 307bf215546Sopenharmony_ci 308bf215546Sopenharmony_ciextern void 309bf215546Sopenharmony_cist_destroy_program_variants(struct st_context *st); 310bf215546Sopenharmony_ci 311bf215546Sopenharmony_ciextern void 312bf215546Sopenharmony_cist_finalize_nir_before_variants(struct nir_shader *nir); 313bf215546Sopenharmony_ci 314bf215546Sopenharmony_ciextern void 315bf215546Sopenharmony_cist_prepare_vertex_program(struct gl_program *stvp); 316bf215546Sopenharmony_ci 317bf215546Sopenharmony_ciextern void 318bf215546Sopenharmony_cist_translate_stream_output_info(struct gl_program *prog); 319bf215546Sopenharmony_ci 320bf215546Sopenharmony_ciextern void 321bf215546Sopenharmony_cist_serialize_nir(struct gl_program *stp); 322bf215546Sopenharmony_ci 323bf215546Sopenharmony_ciextern void 324bf215546Sopenharmony_cist_finalize_program(struct st_context *st, struct gl_program *prog); 325bf215546Sopenharmony_ci 326bf215546Sopenharmony_cistruct pipe_shader_state * 327bf215546Sopenharmony_cist_create_nir_shader(struct st_context *st, struct pipe_shader_state *state); 328bf215546Sopenharmony_ci 329bf215546Sopenharmony_ciGLboolean st_program_string_notify(struct gl_context *ctx, 330bf215546Sopenharmony_ci GLenum target, 331bf215546Sopenharmony_ci struct gl_program *prog); 332bf215546Sopenharmony_ci 333bf215546Sopenharmony_cibool 334bf215546Sopenharmony_cist_can_add_pointsize_to_program(struct st_context *st, struct gl_program *prog); 335bf215546Sopenharmony_ci 336bf215546Sopenharmony_ci#ifdef __cplusplus 337bf215546Sopenharmony_ci} 338bf215546Sopenharmony_ci#endif 339bf215546Sopenharmony_ci 340bf215546Sopenharmony_ci#endif 341