Path: blob/21.2-virgl/src/gallium/drivers/r300/r300_fs.c
4570 views
/*1* Copyright 2008 Corbin Simpson <[email protected]>2* Joakim Sindholt <[email protected]>3* Copyright 2009 Marek Olšák <[email protected]>4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* on the rights to use, copy, modify, merge, publish, distribute, sub9* license, and/or sell copies of the Software, and to permit persons to whom10* the Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice (including the next13* paragraph) shall be included in all copies or substantial portions of the14* Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL19* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,20* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR21* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE22* USE OR OTHER DEALINGS IN THE SOFTWARE. */2324#include "util/format/u_format.h"25#include "util/u_math.h"26#include "util/u_memory.h"2728#include "tgsi/tgsi_dump.h"29#include "tgsi/tgsi_ureg.h"3031#include "r300_cb.h"32#include "r300_context.h"33#include "r300_emit.h"34#include "r300_screen.h"35#include "r300_fs.h"36#include "r300_reg.h"37#include "r300_texture.h"38#include "r300_tgsi_to_rc.h"3940#include "compiler/radeon_compiler.h"4142/* Convert info about FS input semantics to r300_shader_semantics. */43void r300_shader_read_fs_inputs(struct tgsi_shader_info* info,44struct r300_shader_semantics* fs_inputs)45{46int i;47unsigned index;4849r300_shader_semantics_reset(fs_inputs);5051for (i = 0; i < info->num_inputs; i++) {52index = info->input_semantic_index[i];5354switch (info->input_semantic_name[i]) {55case TGSI_SEMANTIC_COLOR:56assert(index < ATTR_COLOR_COUNT);57fs_inputs->color[index] = i;58break;5960case TGSI_SEMANTIC_GENERIC:61assert(index < ATTR_GENERIC_COUNT);62fs_inputs->generic[index] = i;63break;6465case TGSI_SEMANTIC_FOG:66assert(index == 0);67fs_inputs->fog = i;68break;6970case TGSI_SEMANTIC_POSITION:71assert(index == 0);72fs_inputs->wpos = i;73break;7475case TGSI_SEMANTIC_FACE:76assert(index == 0);77fs_inputs->face = i;78break;7980default:81fprintf(stderr, "r300: FP: Unknown input semantic: %i\n",82info->input_semantic_name[i]);83}84}85}8687static void find_output_registers(struct r300_fragment_program_compiler * compiler,88struct r300_fragment_shader_code *shader)89{90unsigned i, colorbuf_count = 0;9192/* Mark the outputs as not present initially */93compiler->OutputColor[0] = shader->info.num_outputs;94compiler->OutputColor[1] = shader->info.num_outputs;95compiler->OutputColor[2] = shader->info.num_outputs;96compiler->OutputColor[3] = shader->info.num_outputs;97compiler->OutputDepth = shader->info.num_outputs;9899/* Now see where they really are. */100for(i = 0; i < shader->info.num_outputs; ++i) {101switch(shader->info.output_semantic_name[i]) {102case TGSI_SEMANTIC_COLOR:103compiler->OutputColor[colorbuf_count] = i;104colorbuf_count++;105break;106case TGSI_SEMANTIC_POSITION:107compiler->OutputDepth = i;108break;109}110}111}112113static void allocate_hardware_inputs(114struct r300_fragment_program_compiler * c,115void (*allocate)(void * data, unsigned input, unsigned hwreg),116void * mydata)117{118struct r300_shader_semantics* inputs =119(struct r300_shader_semantics*)c->UserData;120int i, reg = 0;121122/* Allocate input registers. */123for (i = 0; i < ATTR_COLOR_COUNT; i++) {124if (inputs->color[i] != ATTR_UNUSED) {125allocate(mydata, inputs->color[i], reg++);126}127}128if (inputs->face != ATTR_UNUSED) {129allocate(mydata, inputs->face, reg++);130}131for (i = 0; i < ATTR_GENERIC_COUNT; i++) {132if (inputs->generic[i] != ATTR_UNUSED) {133allocate(mydata, inputs->generic[i], reg++);134}135}136if (inputs->fog != ATTR_UNUSED) {137allocate(mydata, inputs->fog, reg++);138}139if (inputs->wpos != ATTR_UNUSED) {140allocate(mydata, inputs->wpos, reg++);141}142}143144static void get_external_state(145struct r300_context* r300,146struct r300_fragment_program_external_state* state)147{148struct r300_textures_state *texstate = r300->textures_state.state;149unsigned i;150151state->alpha_to_one = r300->alpha_to_one && r300->msaa_enable;152153for (i = 0; i < texstate->sampler_state_count; i++) {154struct r300_sampler_state *s = texstate->sampler_states[i];155struct r300_sampler_view *v = texstate->sampler_views[i];156struct r300_resource *t;157158if (!s || !v) {159continue;160}161162t = r300_resource(v->base.texture);163164if (s->state.compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) {165state->unit[i].compare_mode_enabled = 1;166167/* Fortunately, no need to translate this. */168state->unit[i].texture_compare_func = s->state.compare_func;169}170171state->unit[i].non_normalized_coords = !s->state.normalized_coords;172173/* Pass texture swizzling to the compiler, some lowering passes need it. */174if (state->unit[i].compare_mode_enabled) {175state->unit[i].texture_swizzle =176RC_MAKE_SWIZZLE(v->swizzle[0], v->swizzle[1],177v->swizzle[2], v->swizzle[3]);178}179180/* XXX this should probably take into account STR, not just S. */181if (t->tex.is_npot) {182switch (s->state.wrap_s) {183case PIPE_TEX_WRAP_REPEAT:184state->unit[i].wrap_mode = RC_WRAP_REPEAT;185break;186187case PIPE_TEX_WRAP_MIRROR_REPEAT:188state->unit[i].wrap_mode = RC_WRAP_MIRRORED_REPEAT;189break;190191case PIPE_TEX_WRAP_MIRROR_CLAMP:192case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:193case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:194state->unit[i].wrap_mode = RC_WRAP_MIRRORED_CLAMP;195break;196197default:198state->unit[i].wrap_mode = RC_WRAP_NONE;199}200201if (t->b.target == PIPE_TEXTURE_3D)202state->unit[i].clamp_and_scale_before_fetch = TRUE;203}204}205}206207static void r300_translate_fragment_shader(208struct r300_context* r300,209struct r300_fragment_shader_code* shader,210const struct tgsi_token *tokens);211212static void r300_dummy_fragment_shader(213struct r300_context* r300,214struct r300_fragment_shader_code* shader)215{216struct pipe_shader_state state;217struct ureg_program *ureg;218struct ureg_dst out;219struct ureg_src imm;220221/* Make a simple fragment shader which outputs (0, 0, 0, 1) */222ureg = ureg_create(PIPE_SHADER_FRAGMENT);223out = ureg_DECL_output(ureg, TGSI_SEMANTIC_COLOR, 0);224imm = ureg_imm4f(ureg, 0, 0, 0, 1);225226ureg_MOV(ureg, out, imm);227ureg_END(ureg);228229state.tokens = ureg_finalize(ureg);230231shader->dummy = TRUE;232r300_translate_fragment_shader(r300, shader, state.tokens);233234ureg_destroy(ureg);235}236237static void r300_emit_fs_code_to_buffer(238struct r300_context *r300,239struct r300_fragment_shader_code *shader)240{241struct rX00_fragment_program_code *generic_code = &shader->code;242unsigned imm_count = shader->immediates_count;243unsigned imm_first = shader->externals_count;244unsigned imm_end = generic_code->constants.Count;245struct rc_constant *constants = generic_code->constants.Constants;246unsigned i;247CB_LOCALS;248249if (r300->screen->caps.is_r500) {250struct r500_fragment_program_code *code = &generic_code->code.r500;251252shader->cb_code_size = 19 +253((code->inst_end + 1) * 6) +254imm_count * 7 +255code->int_constant_count * 2;256257NEW_CB(shader->cb_code, shader->cb_code_size);258OUT_CB_REG(R500_US_CONFIG, R500_ZERO_TIMES_ANYTHING_EQUALS_ZERO);259OUT_CB_REG(R500_US_PIXSIZE, code->max_temp_idx);260OUT_CB_REG(R500_US_FC_CTRL, code->us_fc_ctrl);261for(i = 0; i < code->int_constant_count; i++){262OUT_CB_REG(R500_US_FC_INT_CONST_0 + (i * 4),263code->int_constants[i]);264}265OUT_CB_REG(R500_US_CODE_RANGE,266R500_US_CODE_RANGE_ADDR(0) | R500_US_CODE_RANGE_SIZE(code->inst_end));267OUT_CB_REG(R500_US_CODE_OFFSET, 0);268OUT_CB_REG(R500_US_CODE_ADDR,269R500_US_CODE_START_ADDR(0) | R500_US_CODE_END_ADDR(code->inst_end));270271OUT_CB_REG(R500_GA_US_VECTOR_INDEX, R500_GA_US_VECTOR_INDEX_TYPE_INSTR);272OUT_CB_ONE_REG(R500_GA_US_VECTOR_DATA, (code->inst_end + 1) * 6);273for (i = 0; i <= code->inst_end; i++) {274OUT_CB(code->inst[i].inst0);275OUT_CB(code->inst[i].inst1);276OUT_CB(code->inst[i].inst2);277OUT_CB(code->inst[i].inst3);278OUT_CB(code->inst[i].inst4);279OUT_CB(code->inst[i].inst5);280}281282/* Emit immediates. */283if (imm_count) {284for(i = imm_first; i < imm_end; ++i) {285if (constants[i].Type == RC_CONSTANT_IMMEDIATE) {286const float *data = constants[i].u.Immediate;287288OUT_CB_REG(R500_GA_US_VECTOR_INDEX,289R500_GA_US_VECTOR_INDEX_TYPE_CONST |290(i & R500_GA_US_VECTOR_INDEX_MASK));291OUT_CB_ONE_REG(R500_GA_US_VECTOR_DATA, 4);292OUT_CB_TABLE(data, 4);293}294}295}296} else { /* r300 */297struct r300_fragment_program_code *code = &generic_code->code.r300;298unsigned int alu_length = code->alu.length;299unsigned int alu_iterations = ((alu_length - 1) / 64) + 1;300unsigned int tex_length = code->tex.length;301unsigned int tex_iterations =302tex_length > 0 ? ((tex_length - 1) / 32) + 1 : 0;303unsigned int iterations =304alu_iterations > tex_iterations ? alu_iterations : tex_iterations;305unsigned int bank = 0;306307shader->cb_code_size = 15 +308/* R400_US_CODE_BANK */309(r300->screen->caps.is_r400 ? 2 * (iterations + 1): 0) +310/* R400_US_CODE_EXT */311(r300->screen->caps.is_r400 ? 2 : 0) +312/* R300_US_ALU_{RGB,ALPHA}_{INST,ADDR}_0, R400_US_ALU_EXT_ADDR_0 */313(code->r390_mode ? (5 * alu_iterations) : 4) +314/* R400_US_ALU_EXT_ADDR_[0-63] */315(code->r390_mode ? (code->alu.length) : 0) +316/* R300_US_ALU_{RGB,ALPHA}_{INST,ADDR}_0 */317code->alu.length * 4 +318/* R300_US_TEX_INST_0, R300_US_TEX_INST_[0-31] */319(code->tex.length > 0 ? code->tex.length + tex_iterations : 0) +320imm_count * 5;321322NEW_CB(shader->cb_code, shader->cb_code_size);323324OUT_CB_REG(R300_US_CONFIG, code->config);325OUT_CB_REG(R300_US_PIXSIZE, code->pixsize);326OUT_CB_REG(R300_US_CODE_OFFSET, code->code_offset);327328if (code->r390_mode) {329OUT_CB_REG(R400_US_CODE_EXT, code->r400_code_offset_ext);330} else if (r300->screen->caps.is_r400) {331/* This register appears to affect shaders even if r390_mode is332* disabled, so it needs to be set to 0 for shaders that333* don't use r390_mode. */334OUT_CB_REG(R400_US_CODE_EXT, 0);335}336337OUT_CB_REG_SEQ(R300_US_CODE_ADDR_0, 4);338OUT_CB_TABLE(code->code_addr, 4);339340do {341unsigned int bank_alu_length = (alu_length < 64 ? alu_length : 64);342unsigned int bank_alu_offset = bank * 64;343unsigned int bank_tex_length = (tex_length < 32 ? tex_length : 32);344unsigned int bank_tex_offset = bank * 32;345346if (r300->screen->caps.is_r400) {347OUT_CB_REG(R400_US_CODE_BANK, code->r390_mode ?348(bank << R400_BANK_SHIFT) | R400_R390_MODE_ENABLE : 0);//2349}350351if (bank_alu_length > 0) {352OUT_CB_REG_SEQ(R300_US_ALU_RGB_INST_0, bank_alu_length);353for (i = 0; i < bank_alu_length; i++)354OUT_CB(code->alu.inst[i + bank_alu_offset].rgb_inst);355356OUT_CB_REG_SEQ(R300_US_ALU_RGB_ADDR_0, bank_alu_length);357for (i = 0; i < bank_alu_length; i++)358OUT_CB(code->alu.inst[i + bank_alu_offset].rgb_addr);359360OUT_CB_REG_SEQ(R300_US_ALU_ALPHA_INST_0, bank_alu_length);361for (i = 0; i < bank_alu_length; i++)362OUT_CB(code->alu.inst[i + bank_alu_offset].alpha_inst);363364OUT_CB_REG_SEQ(R300_US_ALU_ALPHA_ADDR_0, bank_alu_length);365for (i = 0; i < bank_alu_length; i++)366OUT_CB(code->alu.inst[i + bank_alu_offset].alpha_addr);367368if (code->r390_mode) {369OUT_CB_REG_SEQ(R400_US_ALU_EXT_ADDR_0, bank_alu_length);370for (i = 0; i < bank_alu_length; i++)371OUT_CB(code->alu.inst[i + bank_alu_offset].r400_ext_addr);372}373}374375if (bank_tex_length > 0) {376OUT_CB_REG_SEQ(R300_US_TEX_INST_0, bank_tex_length);377OUT_CB_TABLE(code->tex.inst + bank_tex_offset, bank_tex_length);378}379380alu_length -= bank_alu_length;381tex_length -= bank_tex_length;382bank++;383} while(code->r390_mode && (alu_length > 0 || tex_length > 0));384385/* R400_US_CODE_BANK needs to be reset to 0, otherwise some shaders386* will be rendered incorrectly. */387if (r300->screen->caps.is_r400) {388OUT_CB_REG(R400_US_CODE_BANK,389code->r390_mode ? R400_R390_MODE_ENABLE : 0);390}391392/* Emit immediates. */393if (imm_count) {394for(i = imm_first; i < imm_end; ++i) {395if (constants[i].Type == RC_CONSTANT_IMMEDIATE) {396const float *data = constants[i].u.Immediate;397398OUT_CB_REG_SEQ(R300_PFS_PARAM_0_X + i * 16, 4);399OUT_CB(pack_float24(data[0]));400OUT_CB(pack_float24(data[1]));401OUT_CB(pack_float24(data[2]));402OUT_CB(pack_float24(data[3]));403}404}405}406}407408OUT_CB_REG(R300_FG_DEPTH_SRC, shader->fg_depth_src);409OUT_CB_REG(R300_US_W_FMT, shader->us_out_w);410END_CB;411}412413static void r300_translate_fragment_shader(414struct r300_context* r300,415struct r300_fragment_shader_code* shader,416const struct tgsi_token *tokens)417{418struct r300_fragment_program_compiler compiler;419struct tgsi_to_rc ttr;420int wpos, face;421unsigned i;422423tgsi_scan_shader(tokens, &shader->info);424r300_shader_read_fs_inputs(&shader->info, &shader->inputs);425426wpos = shader->inputs.wpos;427face = shader->inputs.face;428429/* Setup the compiler. */430memset(&compiler, 0, sizeof(compiler));431rc_init(&compiler.Base, &r300->fs_regalloc_state);432DBG_ON(r300, DBG_FP) ? compiler.Base.Debug |= RC_DBG_LOG : 0;433DBG_ON(r300, DBG_P_STAT) ? compiler.Base.Debug |= RC_DBG_STATS : 0;434435compiler.code = &shader->code;436compiler.state = shader->compare_state;437compiler.Base.is_r500 = r300->screen->caps.is_r500;438compiler.Base.is_r400 = r300->screen->caps.is_r400;439compiler.Base.disable_optimizations = DBG_ON(r300, DBG_NO_OPT);440compiler.Base.has_half_swizzles = TRUE;441compiler.Base.has_presub = TRUE;442compiler.Base.has_omod = TRUE;443compiler.Base.max_temp_regs =444compiler.Base.is_r500 ? 128 : (compiler.Base.is_r400 ? 64 : 32);445compiler.Base.max_constants = compiler.Base.is_r500 ? 256 : 32;446compiler.Base.max_alu_insts =447(compiler.Base.is_r500 || compiler.Base.is_r400) ? 512 : 64;448compiler.Base.max_tex_insts =449(compiler.Base.is_r500 || compiler.Base.is_r400) ? 512 : 32;450compiler.AllocateHwInputs = &allocate_hardware_inputs;451compiler.UserData = &shader->inputs;452453find_output_registers(&compiler, shader);454455shader->write_all =456shader->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS];457458if (compiler.Base.Debug & RC_DBG_LOG) {459DBG(r300, DBG_FP, "r300: Initial fragment program\n");460tgsi_dump(tokens, 0);461}462463/* Translate TGSI to our internal representation */464ttr.compiler = &compiler.Base;465ttr.info = &shader->info;466ttr.use_half_swizzles = TRUE;467468r300_tgsi_to_rc(&ttr, tokens);469470if (ttr.error) {471fprintf(stderr, "r300 FP: Cannot translate a shader. "472"Using a dummy shader instead.\n");473r300_dummy_fragment_shader(r300, shader);474return;475}476477if (!r300->screen->caps.is_r500 ||478compiler.Base.Program.Constants.Count > 200) {479compiler.Base.remove_unused_constants = TRUE;480}481482/**483* Transform the program to support WPOS.484*485* Introduce a small fragment at the start of the program that will be486* the only code that directly reads the WPOS input.487* All other code pieces that reference that input will be rewritten488* to read from a newly allocated temporary. */489if (wpos != ATTR_UNUSED) {490/* Moving the input to some other reg is not really necessary. */491rc_transform_fragment_wpos(&compiler.Base, wpos, wpos, TRUE);492}493494if (face != ATTR_UNUSED) {495rc_transform_fragment_face(&compiler.Base, face);496}497498/* Invoke the compiler */499r3xx_compile_fragment_program(&compiler);500501if (compiler.Base.Error) {502fprintf(stderr, "r300 FP: Compiler Error:\n%sUsing a dummy shader"503" instead.\n", compiler.Base.ErrorMsg);504505if (shader->dummy) {506fprintf(stderr, "r300 FP: Cannot compile the dummy shader! "507"Giving up...\n");508abort();509}510511rc_destroy(&compiler.Base);512r300_dummy_fragment_shader(r300, shader);513return;514}515516/* Shaders with zero instructions are invalid,517* use the dummy shader instead. */518if (shader->code.code.r500.inst_end == -1) {519rc_destroy(&compiler.Base);520r300_dummy_fragment_shader(r300, shader);521return;522}523524/* Initialize numbers of constants for each type. */525shader->externals_count = 0;526for (i = 0;527i < shader->code.constants.Count &&528shader->code.constants.Constants[i].Type == RC_CONSTANT_EXTERNAL; i++) {529shader->externals_count = i+1;530}531shader->immediates_count = 0;532shader->rc_state_count = 0;533534for (i = shader->externals_count; i < shader->code.constants.Count; i++) {535switch (shader->code.constants.Constants[i].Type) {536case RC_CONSTANT_IMMEDIATE:537++shader->immediates_count;538break;539case RC_CONSTANT_STATE:540++shader->rc_state_count;541break;542default:543assert(0);544}545}546547/* Setup shader depth output. */548if (shader->code.writes_depth) {549shader->fg_depth_src = R300_FG_DEPTH_SRC_SHADER;550shader->us_out_w = R300_W_FMT_W24 | R300_W_SRC_US;551} else {552shader->fg_depth_src = R300_FG_DEPTH_SRC_SCAN;553shader->us_out_w = R300_W_FMT_W0 | R300_W_SRC_US;554}555556/* And, finally... */557rc_destroy(&compiler.Base);558559/* Build the command buffer. */560r300_emit_fs_code_to_buffer(r300, shader);561}562563boolean r300_pick_fragment_shader(struct r300_context* r300)564{565struct r300_fragment_shader* fs = r300_fs(r300);566struct r300_fragment_program_external_state state;567struct r300_fragment_shader_code* ptr;568569memset(&state, 0, sizeof(state));570get_external_state(r300, &state);571572if (!fs->first) {573/* Build the fragment shader for the first time. */574fs->first = fs->shader = CALLOC_STRUCT(r300_fragment_shader_code);575576memcpy(&fs->shader->compare_state, &state,577sizeof(struct r300_fragment_program_external_state));578r300_translate_fragment_shader(r300, fs->shader, fs->state.tokens);579return TRUE;580581} else {582/* Check if the currently-bound shader has been compiled583* with the texture-compare state we need. */584if (memcmp(&fs->shader->compare_state, &state, sizeof(state)) != 0) {585/* Search for the right shader. */586ptr = fs->first;587while (ptr) {588if (memcmp(&ptr->compare_state, &state, sizeof(state)) == 0) {589if (fs->shader != ptr) {590fs->shader = ptr;591return TRUE;592}593/* The currently-bound one is OK. */594return FALSE;595}596ptr = ptr->next;597}598599/* Not found, gotta compile a new one. */600ptr = CALLOC_STRUCT(r300_fragment_shader_code);601ptr->next = fs->first;602fs->first = fs->shader = ptr;603604ptr->compare_state = state;605r300_translate_fragment_shader(r300, ptr, fs->state.tokens);606return TRUE;607}608}609610return FALSE;611}612613614