📄 i915_fragprog.c
字号:
/************************************************************************** * * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas. * All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sub license, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice (including the * next paragraph) shall be included in all copies or substantial portions * of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * **************************************************************************/#include "glheader.h"#include "macros.h"#include "enums.h"#include "shader/prog_instruction.h"#include "shader/prog_parameter.h"#include "shader/program.h"#include "shader/programopt.h"#include "tnl/tnl.h"#include "tnl/t_context.h"#include "intel_batchbuffer.h"#include "i915_reg.h"#include "i915_context.h"#include "i915_program.h"static const GLfloat sin_quad_constants[2][4] = { { 2.0, -1.0, .5, .75 }, { 4.0, -4.0, 1.0 / (2.0 * M_PI), .2225 }};static const GLfloat sin_constants[4] = { 1.0, -1.0 / (3 * 2 * 1), 1.0 / (5 * 4 * 3 * 2 * 1), -1.0 / (7 * 6 * 5 * 4 * 3 * 2 * 1)};/* 1, -1/2!, 1/4!, -1/6! */static const GLfloat cos_constants[4] = { 1.0, -1.0 / (2 * 1), 1.0 / (4 * 3 * 2 * 1), -1.0 / (6 * 5 * 4 * 3 * 2 * 1)};/** * Retrieve a ureg for the given source register. Will emit * constants, apply swizzling and negation as needed. */static GLuintsrc_vector(struct i915_fragment_program *p, const struct prog_src_register *source, const struct gl_fragment_program *program){ GLuint src; switch (source->File) { /* Registers: */ case PROGRAM_TEMPORARY: if (source->Index >= I915_MAX_TEMPORARY) { i915_program_error(p, "Exceeded max temporary reg"); return 0; } src = UREG(REG_TYPE_R, source->Index); break; case PROGRAM_INPUT: switch (source->Index) { case FRAG_ATTRIB_WPOS: src = i915_emit_decl(p, REG_TYPE_T, p->wpos_tex, D0_CHANNEL_ALL); break; case FRAG_ATTRIB_COL0: src = i915_emit_decl(p, REG_TYPE_T, T_DIFFUSE, D0_CHANNEL_ALL); break; case FRAG_ATTRIB_COL1: src = i915_emit_decl(p, REG_TYPE_T, T_SPECULAR, D0_CHANNEL_XYZ); src = swizzle(src, X, Y, Z, ONE); break; case FRAG_ATTRIB_FOGC: src = i915_emit_decl(p, REG_TYPE_T, T_FOG_W, D0_CHANNEL_W); src = swizzle(src, W, ZERO, ZERO, ONE); break; case FRAG_ATTRIB_TEX0: case FRAG_ATTRIB_TEX1: case FRAG_ATTRIB_TEX2: case FRAG_ATTRIB_TEX3: case FRAG_ATTRIB_TEX4: case FRAG_ATTRIB_TEX5: case FRAG_ATTRIB_TEX6: case FRAG_ATTRIB_TEX7: src = i915_emit_decl(p, REG_TYPE_T, T_TEX0 + (source->Index - FRAG_ATTRIB_TEX0), D0_CHANNEL_ALL); break; default: i915_program_error(p, "Bad source->Index"); return 0; } break; /* Various paramters and env values. All emitted to * hardware as program constants. */ case PROGRAM_LOCAL_PARAM: src = i915_emit_param4fv(p, program->Base.LocalParams[source->Index]); break; case PROGRAM_ENV_PARAM: src = i915_emit_param4fv(p, p->ctx->FragmentProgram.Parameters[source-> Index]); break; case PROGRAM_CONSTANT: case PROGRAM_STATE_VAR: case PROGRAM_NAMED_PARAM: src = i915_emit_param4fv(p, program->Base.Parameters->ParameterValues[source-> Index]); break; default: i915_program_error(p, "Bad source->File"); return 0; } src = swizzle(src, GET_SWZ(source->Swizzle, 0), GET_SWZ(source->Swizzle, 1), GET_SWZ(source->Swizzle, 2), GET_SWZ(source->Swizzle, 3)); if (source->NegateBase) src = negate(src, GET_BIT(source->NegateBase, 0), GET_BIT(source->NegateBase, 1), GET_BIT(source->NegateBase, 2), GET_BIT(source->NegateBase, 3)); return src;}static GLuintget_result_vector(struct i915_fragment_program *p, const struct prog_instruction *inst){ switch (inst->DstReg.File) { case PROGRAM_OUTPUT: switch (inst->DstReg.Index) { case FRAG_RESULT_COLR: return UREG(REG_TYPE_OC, 0); case FRAG_RESULT_DEPR: p->depth_written = 1; return UREG(REG_TYPE_OD, 0); default: i915_program_error(p, "Bad inst->DstReg.Index"); return 0; } case PROGRAM_TEMPORARY: return UREG(REG_TYPE_R, inst->DstReg.Index); default: i915_program_error(p, "Bad inst->DstReg.File"); return 0; }}static GLuintget_result_flags(const struct prog_instruction *inst){ GLuint flags = 0; if (inst->SaturateMode == SATURATE_ZERO_ONE) flags |= A0_DEST_SATURATE; if (inst->DstReg.WriteMask & WRITEMASK_X) flags |= A0_DEST_CHANNEL_X; if (inst->DstReg.WriteMask & WRITEMASK_Y) flags |= A0_DEST_CHANNEL_Y; if (inst->DstReg.WriteMask & WRITEMASK_Z) flags |= A0_DEST_CHANNEL_Z; if (inst->DstReg.WriteMask & WRITEMASK_W) flags |= A0_DEST_CHANNEL_W; return flags;}static GLuinttranslate_tex_src_target(struct i915_fragment_program *p, GLubyte bit){ switch (bit) { case TEXTURE_1D_INDEX: return D0_SAMPLE_TYPE_2D; case TEXTURE_2D_INDEX: return D0_SAMPLE_TYPE_2D; case TEXTURE_RECT_INDEX: return D0_SAMPLE_TYPE_2D; case TEXTURE_3D_INDEX: return D0_SAMPLE_TYPE_VOLUME; case TEXTURE_CUBE_INDEX: return D0_SAMPLE_TYPE_CUBE; default: i915_program_error(p, "TexSrcBit"); return 0; }}#define EMIT_TEX( OP ) \do { \ GLuint dim = translate_tex_src_target( p, inst->TexSrcTarget ); \ GLuint sampler = i915_emit_decl(p, REG_TYPE_S, \ inst->TexSrcUnit, dim); \ GLuint coord = src_vector( p, &inst->SrcReg[0], program); \ /* Texel lookup */ \ \ i915_emit_texld( p, get_live_regs(p, inst), \ get_result_vector( p, inst ), \ get_result_flags( inst ), \ sampler, \ coord, \ OP); \} while (0)#define EMIT_ARITH( OP, N ) \do { \ i915_emit_arith( p, \ OP, \ get_result_vector( p, inst ), \ get_result_flags( inst ), 0, \ (N<1)?0:src_vector( p, &inst->SrcReg[0], program), \ (N<2)?0:src_vector( p, &inst->SrcReg[1], program), \ (N<3)?0:src_vector( p, &inst->SrcReg[2], program)); \} while (0)#define EMIT_1ARG_ARITH( OP ) EMIT_ARITH( OP, 1 )#define EMIT_2ARG_ARITH( OP ) EMIT_ARITH( OP, 2 )#define EMIT_3ARG_ARITH( OP ) EMIT_ARITH( OP, 3 )/* * TODO: consider moving this into core */static void calc_live_regs( struct i915_fragment_program *p ){ const struct gl_fragment_program *program = p->ctx->FragmentProgram._Current; GLuint regsUsed = 0xffff0000; GLint i; for (i = program->Base.NumInstructions - 1; i >= 0; i--) { struct prog_instruction *inst = &program->Base.Instructions[i]; int opArgs = _mesa_num_inst_src_regs(inst->Opcode); int a; /* Register is written to: unmark as live for this and preceeding ops */ if (inst->DstReg.File == PROGRAM_TEMPORARY) regsUsed &= ~(1 << inst->DstReg.Index); for (a = 0; a < opArgs; a++) { /* Register is read from: mark as live for this and preceeding ops */ if (inst->SrcReg[a].File == PROGRAM_TEMPORARY) regsUsed |= 1 << inst->SrcReg[a].Index; } p->usedRegs[i] = regsUsed; }}static GLuint get_live_regs( struct i915_fragment_program *p, const struct prog_instruction *inst ){ const struct gl_fragment_program *program = p->ctx->FragmentProgram._Current; GLuint nr = inst - program->Base.Instructions; return p->usedRegs[nr];} /* Possible concerns: * * SIN, COS -- could use another taylor step? * LIT -- results seem a little different to sw mesa * LOG -- different to mesa on negative numbers, but this is conformant. * * Parse failures -- Mesa doesn't currently give a good indication * internally whether a particular program string parsed or not. This * can lead to confusion -- hopefully we cope with it ok now. * */static voidupload_program(struct i915_fragment_program *p){ const struct gl_fragment_program *program = p->ctx->FragmentProgram._Current; const struct prog_instruction *inst = program->Base.Instructions;/* _mesa_debug_fp_inst(program->Base.NumInstructions, inst); */ /* Is this a parse-failed program? Ensure a valid program is * loaded, as the flagging of an error isn't sufficient to stop * this being uploaded to hardware. */ if (inst[0].Opcode == OPCODE_END) { GLuint tmp = i915_get_utemp(p); i915_emit_arith(p, A0_MOV, UREG(REG_TYPE_OC, 0), A0_DEST_CHANNEL_ALL, 0, swizzle(tmp, ONE, ZERO, ONE, ONE), 0, 0); return; } if (program->Base.NumInstructions > I915_MAX_INSN) { i915_program_error( p, "Exceeded max instructions" ); return; } /* Not always needed: */ calc_live_regs(p); while (1) { GLuint src0, src1, src2, flags; GLuint tmp = 0, consts0 = 0, consts1 = 0; switch (inst->Opcode) { case OPCODE_ABS: src0 = src_vector(p, &inst->SrcReg[0], program); i915_emit_arith(p, A0_MAX, get_result_vector(p, inst), get_result_flags(inst), 0, src0, negate(src0, 1, 1, 1, 1), 0); break; case OPCODE_ADD: EMIT_2ARG_ARITH(A0_ADD); break; case OPCODE_CMP: src0 = src_vector(p, &inst->SrcReg[0], program); src1 = src_vector(p, &inst->SrcReg[1], program); src2 = src_vector(p, &inst->SrcReg[2], program); i915_emit_arith(p, A0_CMP, get_result_vector(p, inst), get_result_flags(inst), 0, src0, src2, src1); /* NOTE: order of src2, src1 */ break; case OPCODE_COS: src0 = src_vector(p, &inst->SrcReg[0], program); tmp = i915_get_utemp(p); consts0 = i915_emit_const4fv(p, sin_quad_constants[0]); consts1 = i915_emit_const4fv(p, sin_quad_constants[1]); /* Reduce range from repeating about [-pi,pi] to [-1,1] */ i915_emit_arith(p, A0_MAD, tmp, A0_DEST_CHANNEL_X, 0, src0, swizzle(consts1, Z, ZERO, ZERO, ZERO), /* 1/(2pi) */ swizzle(consts0, W, ZERO, ZERO, ZERO)); /* .75 */ i915_emit_arith(p, A0_FRC, tmp, A0_DEST_CHANNEL_X, 0, tmp, 0, 0); i915_emit_arith(p, A0_MAD, tmp, A0_DEST_CHANNEL_X, 0, tmp, swizzle(consts0, X, ZERO, ZERO, ZERO), /* 2 */ swizzle(consts0, Y, ZERO, ZERO, ZERO)); /* -1 */ /* Compute COS with the same calculation used for SIN, but a * different source range has been mapped to [-1,1] this time.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -