1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Mesa 3-D graphics library 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Copyright (C) 1999-2007 Brian Paul 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 "Software"), 8bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 9bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 11bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 12bf215546Sopenharmony_ci * 13bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included 14bf215546Sopenharmony_ci * in all copies or substantial portions of the Software. 15bf215546Sopenharmony_ci * 16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20bf215546Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22bf215546Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE. 23bf215546Sopenharmony_ci */ 24bf215546Sopenharmony_ci 25bf215546Sopenharmony_ci/** 26bf215546Sopenharmony_ci * \file programopt.c 27bf215546Sopenharmony_ci * Vertex/Fragment program optimizations and transformations for program 28bf215546Sopenharmony_ci * options, etc. 29bf215546Sopenharmony_ci * 30bf215546Sopenharmony_ci * \author Brian Paul 31bf215546Sopenharmony_ci */ 32bf215546Sopenharmony_ci 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_ci#include "main/glheader.h" 35bf215546Sopenharmony_ci#include "main/context.h" 36bf215546Sopenharmony_ci#include "prog_parameter.h" 37bf215546Sopenharmony_ci#include "prog_statevars.h" 38bf215546Sopenharmony_ci#include "program.h" 39bf215546Sopenharmony_ci#include "programopt.h" 40bf215546Sopenharmony_ci#include "prog_instruction.h" 41bf215546Sopenharmony_ci 42bf215546Sopenharmony_ci 43bf215546Sopenharmony_ci/** 44bf215546Sopenharmony_ci * This function inserts instructions for coordinate modelview * projection 45bf215546Sopenharmony_ci * into a vertex program. 46bf215546Sopenharmony_ci * May be used to implement the position_invariant option. 47bf215546Sopenharmony_ci */ 48bf215546Sopenharmony_cistatic void 49bf215546Sopenharmony_ciinsert_mvp_dp4_code(struct gl_context *ctx, struct gl_program *vprog) 50bf215546Sopenharmony_ci{ 51bf215546Sopenharmony_ci struct prog_instruction *newInst; 52bf215546Sopenharmony_ci const GLuint origLen = vprog->arb.NumInstructions; 53bf215546Sopenharmony_ci const GLuint newLen = origLen + 4; 54bf215546Sopenharmony_ci GLuint i; 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_ci /* 57bf215546Sopenharmony_ci * Setup state references for the modelview/projection matrix. 58bf215546Sopenharmony_ci * XXX we should check if these state vars are already declared. 59bf215546Sopenharmony_ci */ 60bf215546Sopenharmony_ci static const gl_state_index16 mvpState[4][STATE_LENGTH] = { 61bf215546Sopenharmony_ci { STATE_MVP_MATRIX, 0, 0, 0 }, /* state.matrix.mvp.row[0] */ 62bf215546Sopenharmony_ci { STATE_MVP_MATRIX, 0, 1, 1 }, /* state.matrix.mvp.row[1] */ 63bf215546Sopenharmony_ci { STATE_MVP_MATRIX, 0, 2, 2 }, /* state.matrix.mvp.row[2] */ 64bf215546Sopenharmony_ci { STATE_MVP_MATRIX, 0, 3, 3 }, /* state.matrix.mvp.row[3] */ 65bf215546Sopenharmony_ci }; 66bf215546Sopenharmony_ci GLint mvpRef[4]; 67bf215546Sopenharmony_ci 68bf215546Sopenharmony_ci for (i = 0; i < 4; i++) { 69bf215546Sopenharmony_ci mvpRef[i] = _mesa_add_state_reference(vprog->Parameters, mvpState[i]); 70bf215546Sopenharmony_ci } 71bf215546Sopenharmony_ci 72bf215546Sopenharmony_ci /* Alloc storage for new instructions */ 73bf215546Sopenharmony_ci newInst = rzalloc_array(vprog, struct prog_instruction, newLen); 74bf215546Sopenharmony_ci if (!newInst) { 75bf215546Sopenharmony_ci _mesa_error(ctx, GL_OUT_OF_MEMORY, 76bf215546Sopenharmony_ci "glProgramString(inserting position_invariant code)"); 77bf215546Sopenharmony_ci return; 78bf215546Sopenharmony_ci } 79bf215546Sopenharmony_ci 80bf215546Sopenharmony_ci /* 81bf215546Sopenharmony_ci * Generated instructions: 82bf215546Sopenharmony_ci * newInst[0] = DP4 result.position.x, mvp.row[0], vertex.position; 83bf215546Sopenharmony_ci * newInst[1] = DP4 result.position.y, mvp.row[1], vertex.position; 84bf215546Sopenharmony_ci * newInst[2] = DP4 result.position.z, mvp.row[2], vertex.position; 85bf215546Sopenharmony_ci * newInst[3] = DP4 result.position.w, mvp.row[3], vertex.position; 86bf215546Sopenharmony_ci */ 87bf215546Sopenharmony_ci _mesa_init_instructions(newInst, 4); 88bf215546Sopenharmony_ci for (i = 0; i < 4; i++) { 89bf215546Sopenharmony_ci newInst[i].Opcode = OPCODE_DP4; 90bf215546Sopenharmony_ci newInst[i].DstReg.File = PROGRAM_OUTPUT; 91bf215546Sopenharmony_ci newInst[i].DstReg.Index = VARYING_SLOT_POS; 92bf215546Sopenharmony_ci newInst[i].DstReg.WriteMask = (WRITEMASK_X << i); 93bf215546Sopenharmony_ci newInst[i].SrcReg[0].File = PROGRAM_STATE_VAR; 94bf215546Sopenharmony_ci newInst[i].SrcReg[0].Index = mvpRef[i]; 95bf215546Sopenharmony_ci newInst[i].SrcReg[0].Swizzle = SWIZZLE_NOOP; 96bf215546Sopenharmony_ci newInst[i].SrcReg[1].File = PROGRAM_INPUT; 97bf215546Sopenharmony_ci newInst[i].SrcReg[1].Index = VERT_ATTRIB_POS; 98bf215546Sopenharmony_ci newInst[i].SrcReg[1].Swizzle = SWIZZLE_NOOP; 99bf215546Sopenharmony_ci } 100bf215546Sopenharmony_ci 101bf215546Sopenharmony_ci /* Append original instructions after new instructions */ 102bf215546Sopenharmony_ci _mesa_copy_instructions (newInst + 4, vprog->arb.Instructions, origLen); 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ci /* free old instructions */ 105bf215546Sopenharmony_ci ralloc_free(vprog->arb.Instructions); 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_ci /* install new instructions */ 108bf215546Sopenharmony_ci vprog->arb.Instructions = newInst; 109bf215546Sopenharmony_ci vprog->arb.NumInstructions = newLen; 110bf215546Sopenharmony_ci vprog->info.inputs_read |= VERT_BIT_POS; 111bf215546Sopenharmony_ci vprog->info.outputs_written |= BITFIELD64_BIT(VARYING_SLOT_POS); 112bf215546Sopenharmony_ci} 113bf215546Sopenharmony_ci 114bf215546Sopenharmony_ci 115bf215546Sopenharmony_cistatic void 116bf215546Sopenharmony_ciinsert_mvp_mad_code(struct gl_context *ctx, struct gl_program *vprog) 117bf215546Sopenharmony_ci{ 118bf215546Sopenharmony_ci struct prog_instruction *newInst; 119bf215546Sopenharmony_ci const GLuint origLen = vprog->arb.NumInstructions; 120bf215546Sopenharmony_ci const GLuint newLen = origLen + 4; 121bf215546Sopenharmony_ci GLuint hposTemp; 122bf215546Sopenharmony_ci GLuint i; 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_ci /* 125bf215546Sopenharmony_ci * Setup state references for the modelview/projection matrix. 126bf215546Sopenharmony_ci * XXX we should check if these state vars are already declared. 127bf215546Sopenharmony_ci */ 128bf215546Sopenharmony_ci static const gl_state_index16 mvpState[4][STATE_LENGTH] = { 129bf215546Sopenharmony_ci { STATE_MVP_MATRIX_TRANSPOSE, 0, 0, 0 }, 130bf215546Sopenharmony_ci { STATE_MVP_MATRIX_TRANSPOSE, 0, 1, 1 }, 131bf215546Sopenharmony_ci { STATE_MVP_MATRIX_TRANSPOSE, 0, 2, 2 }, 132bf215546Sopenharmony_ci { STATE_MVP_MATRIX_TRANSPOSE, 0, 3, 3 }, 133bf215546Sopenharmony_ci }; 134bf215546Sopenharmony_ci GLint mvpRef[4]; 135bf215546Sopenharmony_ci 136bf215546Sopenharmony_ci for (i = 0; i < 4; i++) { 137bf215546Sopenharmony_ci mvpRef[i] = _mesa_add_state_reference(vprog->Parameters, mvpState[i]); 138bf215546Sopenharmony_ci } 139bf215546Sopenharmony_ci 140bf215546Sopenharmony_ci /* Alloc storage for new instructions */ 141bf215546Sopenharmony_ci newInst = rzalloc_array(vprog, struct prog_instruction, newLen); 142bf215546Sopenharmony_ci if (!newInst) { 143bf215546Sopenharmony_ci _mesa_error(ctx, GL_OUT_OF_MEMORY, 144bf215546Sopenharmony_ci "glProgramString(inserting position_invariant code)"); 145bf215546Sopenharmony_ci return; 146bf215546Sopenharmony_ci } 147bf215546Sopenharmony_ci 148bf215546Sopenharmony_ci /* TEMP hposTemp; */ 149bf215546Sopenharmony_ci hposTemp = vprog->arb.NumTemporaries++; 150bf215546Sopenharmony_ci 151bf215546Sopenharmony_ci /* 152bf215546Sopenharmony_ci * Generated instructions: 153bf215546Sopenharmony_ci * emit_op2(p, OPCODE_MUL, tmp, 0, swizzle1(src,X), mat[0]); 154bf215546Sopenharmony_ci * emit_op3(p, OPCODE_MAD, tmp, 0, swizzle1(src,Y), mat[1], tmp); 155bf215546Sopenharmony_ci * emit_op3(p, OPCODE_MAD, tmp, 0, swizzle1(src,Z), mat[2], tmp); 156bf215546Sopenharmony_ci * emit_op3(p, OPCODE_MAD, dest, 0, swizzle1(src,W), mat[3], tmp); 157bf215546Sopenharmony_ci */ 158bf215546Sopenharmony_ci _mesa_init_instructions(newInst, 4); 159bf215546Sopenharmony_ci 160bf215546Sopenharmony_ci newInst[0].Opcode = OPCODE_MUL; 161bf215546Sopenharmony_ci newInst[0].DstReg.File = PROGRAM_TEMPORARY; 162bf215546Sopenharmony_ci newInst[0].DstReg.Index = hposTemp; 163bf215546Sopenharmony_ci newInst[0].DstReg.WriteMask = WRITEMASK_XYZW; 164bf215546Sopenharmony_ci newInst[0].SrcReg[0].File = PROGRAM_INPUT; 165bf215546Sopenharmony_ci newInst[0].SrcReg[0].Index = VERT_ATTRIB_POS; 166bf215546Sopenharmony_ci newInst[0].SrcReg[0].Swizzle = SWIZZLE_XXXX; 167bf215546Sopenharmony_ci newInst[0].SrcReg[1].File = PROGRAM_STATE_VAR; 168bf215546Sopenharmony_ci newInst[0].SrcReg[1].Index = mvpRef[0]; 169bf215546Sopenharmony_ci newInst[0].SrcReg[1].Swizzle = SWIZZLE_NOOP; 170bf215546Sopenharmony_ci 171bf215546Sopenharmony_ci for (i = 1; i <= 2; i++) { 172bf215546Sopenharmony_ci newInst[i].Opcode = OPCODE_MAD; 173bf215546Sopenharmony_ci newInst[i].DstReg.File = PROGRAM_TEMPORARY; 174bf215546Sopenharmony_ci newInst[i].DstReg.Index = hposTemp; 175bf215546Sopenharmony_ci newInst[i].DstReg.WriteMask = WRITEMASK_XYZW; 176bf215546Sopenharmony_ci newInst[i].SrcReg[0].File = PROGRAM_INPUT; 177bf215546Sopenharmony_ci newInst[i].SrcReg[0].Index = VERT_ATTRIB_POS; 178bf215546Sopenharmony_ci newInst[i].SrcReg[0].Swizzle = MAKE_SWIZZLE4(i,i,i,i); 179bf215546Sopenharmony_ci newInst[i].SrcReg[1].File = PROGRAM_STATE_VAR; 180bf215546Sopenharmony_ci newInst[i].SrcReg[1].Index = mvpRef[i]; 181bf215546Sopenharmony_ci newInst[i].SrcReg[1].Swizzle = SWIZZLE_NOOP; 182bf215546Sopenharmony_ci newInst[i].SrcReg[2].File = PROGRAM_TEMPORARY; 183bf215546Sopenharmony_ci newInst[i].SrcReg[2].Index = hposTemp; 184bf215546Sopenharmony_ci newInst[1].SrcReg[2].Swizzle = SWIZZLE_NOOP; 185bf215546Sopenharmony_ci } 186bf215546Sopenharmony_ci 187bf215546Sopenharmony_ci newInst[3].Opcode = OPCODE_MAD; 188bf215546Sopenharmony_ci newInst[3].DstReg.File = PROGRAM_OUTPUT; 189bf215546Sopenharmony_ci newInst[3].DstReg.Index = VARYING_SLOT_POS; 190bf215546Sopenharmony_ci newInst[3].DstReg.WriteMask = WRITEMASK_XYZW; 191bf215546Sopenharmony_ci newInst[3].SrcReg[0].File = PROGRAM_INPUT; 192bf215546Sopenharmony_ci newInst[3].SrcReg[0].Index = VERT_ATTRIB_POS; 193bf215546Sopenharmony_ci newInst[3].SrcReg[0].Swizzle = SWIZZLE_WWWW; 194bf215546Sopenharmony_ci newInst[3].SrcReg[1].File = PROGRAM_STATE_VAR; 195bf215546Sopenharmony_ci newInst[3].SrcReg[1].Index = mvpRef[3]; 196bf215546Sopenharmony_ci newInst[3].SrcReg[1].Swizzle = SWIZZLE_NOOP; 197bf215546Sopenharmony_ci newInst[3].SrcReg[2].File = PROGRAM_TEMPORARY; 198bf215546Sopenharmony_ci newInst[3].SrcReg[2].Index = hposTemp; 199bf215546Sopenharmony_ci newInst[3].SrcReg[2].Swizzle = SWIZZLE_NOOP; 200bf215546Sopenharmony_ci 201bf215546Sopenharmony_ci 202bf215546Sopenharmony_ci /* Append original instructions after new instructions */ 203bf215546Sopenharmony_ci _mesa_copy_instructions (newInst + 4, vprog->arb.Instructions, origLen); 204bf215546Sopenharmony_ci 205bf215546Sopenharmony_ci /* free old instructions */ 206bf215546Sopenharmony_ci ralloc_free(vprog->arb.Instructions); 207bf215546Sopenharmony_ci 208bf215546Sopenharmony_ci /* install new instructions */ 209bf215546Sopenharmony_ci vprog->arb.Instructions = newInst; 210bf215546Sopenharmony_ci vprog->arb.NumInstructions = newLen; 211bf215546Sopenharmony_ci vprog->info.inputs_read |= VERT_BIT_POS; 212bf215546Sopenharmony_ci vprog->info.outputs_written |= BITFIELD64_BIT(VARYING_SLOT_POS); 213bf215546Sopenharmony_ci} 214bf215546Sopenharmony_ci 215bf215546Sopenharmony_ci 216bf215546Sopenharmony_civoid 217bf215546Sopenharmony_ci_mesa_insert_mvp_code(struct gl_context *ctx, struct gl_program *vprog) 218bf215546Sopenharmony_ci{ 219bf215546Sopenharmony_ci if (ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].OptimizeForAOS) 220bf215546Sopenharmony_ci insert_mvp_dp4_code( ctx, vprog ); 221bf215546Sopenharmony_ci else 222bf215546Sopenharmony_ci insert_mvp_mad_code( ctx, vprog ); 223bf215546Sopenharmony_ci} 224bf215546Sopenharmony_ci 225bf215546Sopenharmony_ci 226bf215546Sopenharmony_ci 227bf215546Sopenharmony_ci 228bf215546Sopenharmony_ci 229bf215546Sopenharmony_ci 230bf215546Sopenharmony_ci/** 231bf215546Sopenharmony_ci * Append instructions to implement fog 232bf215546Sopenharmony_ci * 233bf215546Sopenharmony_ci * The \c fragment.fogcoord input is used to compute the fog blend factor. 234bf215546Sopenharmony_ci * 235bf215546Sopenharmony_ci * \param ctx The GL context 236bf215546Sopenharmony_ci * \param fprog Fragment program that fog instructions will be appended to. 237bf215546Sopenharmony_ci * \param fog_mode Fog mode. One of \c GL_EXP, \c GL_EXP2, or \c GL_LINEAR. 238bf215546Sopenharmony_ci * \param saturate True if writes to color outputs should be clamped to [0, 1] 239bf215546Sopenharmony_ci * 240bf215546Sopenharmony_ci * \note 241bf215546Sopenharmony_ci * This function sets \c VARYING_BIT_FOGC in \c fprog->info.inputs_read. 242bf215546Sopenharmony_ci * 243bf215546Sopenharmony_ci * \todo With a little work, this function could be adapted to add fog code 244bf215546Sopenharmony_ci * to vertex programs too. 245bf215546Sopenharmony_ci */ 246bf215546Sopenharmony_civoid 247bf215546Sopenharmony_ci_mesa_append_fog_code(struct gl_context *ctx, struct gl_program *fprog, 248bf215546Sopenharmony_ci GLenum fog_mode, GLboolean saturate) 249bf215546Sopenharmony_ci{ 250bf215546Sopenharmony_ci static const gl_state_index16 fogPStateOpt[STATE_LENGTH] 251bf215546Sopenharmony_ci = { STATE_FOG_PARAMS_OPTIMIZED, 0, 0 }; 252bf215546Sopenharmony_ci static const gl_state_index16 fogColorState[STATE_LENGTH] 253bf215546Sopenharmony_ci = { STATE_FOG_COLOR, 0, 0, 0 }; 254bf215546Sopenharmony_ci struct prog_instruction *newInst, *inst; 255bf215546Sopenharmony_ci const GLuint origLen = fprog->arb.NumInstructions; 256bf215546Sopenharmony_ci const GLuint newLen = origLen + 5; 257bf215546Sopenharmony_ci GLuint i; 258bf215546Sopenharmony_ci GLint fogPRefOpt, fogColorRef; /* state references */ 259bf215546Sopenharmony_ci GLuint colorTemp, fogFactorTemp; /* temporary registerss */ 260bf215546Sopenharmony_ci 261bf215546Sopenharmony_ci if (fog_mode == GL_NONE) { 262bf215546Sopenharmony_ci _mesa_problem(ctx, "_mesa_append_fog_code() called for fragment program" 263bf215546Sopenharmony_ci " with fog_mode == GL_NONE"); 264bf215546Sopenharmony_ci return; 265bf215546Sopenharmony_ci } 266bf215546Sopenharmony_ci 267bf215546Sopenharmony_ci if (!(fprog->info.outputs_written & (1 << FRAG_RESULT_COLOR))) { 268bf215546Sopenharmony_ci /* program doesn't output color, so nothing to do */ 269bf215546Sopenharmony_ci return; 270bf215546Sopenharmony_ci } 271bf215546Sopenharmony_ci 272bf215546Sopenharmony_ci /* Alloc storage for new instructions */ 273bf215546Sopenharmony_ci newInst = rzalloc_array(fprog, struct prog_instruction, newLen); 274bf215546Sopenharmony_ci if (!newInst) { 275bf215546Sopenharmony_ci _mesa_error(ctx, GL_OUT_OF_MEMORY, 276bf215546Sopenharmony_ci "glProgramString(inserting fog_option code)"); 277bf215546Sopenharmony_ci return; 278bf215546Sopenharmony_ci } 279bf215546Sopenharmony_ci 280bf215546Sopenharmony_ci /* Copy orig instructions into new instruction buffer */ 281bf215546Sopenharmony_ci _mesa_copy_instructions(newInst, fprog->arb.Instructions, origLen); 282bf215546Sopenharmony_ci 283bf215546Sopenharmony_ci /* PARAM fogParamsRefOpt = internal optimized fog params; */ 284bf215546Sopenharmony_ci fogPRefOpt 285bf215546Sopenharmony_ci = _mesa_add_state_reference(fprog->Parameters, fogPStateOpt); 286bf215546Sopenharmony_ci /* PARAM fogColorRef = state.fog.color; */ 287bf215546Sopenharmony_ci fogColorRef 288bf215546Sopenharmony_ci = _mesa_add_state_reference(fprog->Parameters, fogColorState); 289bf215546Sopenharmony_ci 290bf215546Sopenharmony_ci /* TEMP colorTemp; */ 291bf215546Sopenharmony_ci colorTemp = fprog->arb.NumTemporaries++; 292bf215546Sopenharmony_ci /* TEMP fogFactorTemp; */ 293bf215546Sopenharmony_ci fogFactorTemp = fprog->arb.NumTemporaries++; 294bf215546Sopenharmony_ci 295bf215546Sopenharmony_ci /* Scan program to find where result.color is written */ 296bf215546Sopenharmony_ci inst = newInst; 297bf215546Sopenharmony_ci for (i = 0; i < fprog->arb.NumInstructions; i++) { 298bf215546Sopenharmony_ci if (inst->Opcode == OPCODE_END) 299bf215546Sopenharmony_ci break; 300bf215546Sopenharmony_ci if (inst->DstReg.File == PROGRAM_OUTPUT && 301bf215546Sopenharmony_ci inst->DstReg.Index == FRAG_RESULT_COLOR) { 302bf215546Sopenharmony_ci /* change the instruction to write to colorTemp w/ clamping */ 303bf215546Sopenharmony_ci inst->DstReg.File = PROGRAM_TEMPORARY; 304bf215546Sopenharmony_ci inst->DstReg.Index = colorTemp; 305bf215546Sopenharmony_ci inst->Saturate = saturate; 306bf215546Sopenharmony_ci /* don't break (may be several writes to result.color) */ 307bf215546Sopenharmony_ci } 308bf215546Sopenharmony_ci inst++; 309bf215546Sopenharmony_ci } 310bf215546Sopenharmony_ci assert(inst->Opcode == OPCODE_END); /* we'll overwrite this inst */ 311bf215546Sopenharmony_ci 312bf215546Sopenharmony_ci _mesa_init_instructions(inst, 5); 313bf215546Sopenharmony_ci 314bf215546Sopenharmony_ci /* emit instructions to compute fog blending factor */ 315bf215546Sopenharmony_ci /* this is always clamped to [0, 1] regardless of fragment clamping */ 316bf215546Sopenharmony_ci if (fog_mode == GL_LINEAR) { 317bf215546Sopenharmony_ci /* MAD fogFactorTemp.x, fragment.fogcoord.x, fogPRefOpt.x, fogPRefOpt.y; */ 318bf215546Sopenharmony_ci inst->Opcode = OPCODE_MAD; 319bf215546Sopenharmony_ci inst->DstReg.File = PROGRAM_TEMPORARY; 320bf215546Sopenharmony_ci inst->DstReg.Index = fogFactorTemp; 321bf215546Sopenharmony_ci inst->DstReg.WriteMask = WRITEMASK_X; 322bf215546Sopenharmony_ci inst->SrcReg[0].File = PROGRAM_INPUT; 323bf215546Sopenharmony_ci inst->SrcReg[0].Index = VARYING_SLOT_FOGC; 324bf215546Sopenharmony_ci inst->SrcReg[0].Swizzle = SWIZZLE_XXXX; 325bf215546Sopenharmony_ci inst->SrcReg[1].File = PROGRAM_STATE_VAR; 326bf215546Sopenharmony_ci inst->SrcReg[1].Index = fogPRefOpt; 327bf215546Sopenharmony_ci inst->SrcReg[1].Swizzle = SWIZZLE_XXXX; 328bf215546Sopenharmony_ci inst->SrcReg[2].File = PROGRAM_STATE_VAR; 329bf215546Sopenharmony_ci inst->SrcReg[2].Index = fogPRefOpt; 330bf215546Sopenharmony_ci inst->SrcReg[2].Swizzle = SWIZZLE_YYYY; 331bf215546Sopenharmony_ci inst->Saturate = GL_TRUE; 332bf215546Sopenharmony_ci inst++; 333bf215546Sopenharmony_ci } 334bf215546Sopenharmony_ci else { 335bf215546Sopenharmony_ci assert(fog_mode == GL_EXP || fog_mode == GL_EXP2); 336bf215546Sopenharmony_ci /* fogPRefOpt.z = d/ln(2), fogPRefOpt.w = d/sqrt(ln(2) */ 337bf215546Sopenharmony_ci /* EXP: MUL fogFactorTemp.x, fogPRefOpt.z, fragment.fogcoord.x; */ 338bf215546Sopenharmony_ci /* EXP2: MUL fogFactorTemp.x, fogPRefOpt.w, fragment.fogcoord.x; */ 339bf215546Sopenharmony_ci inst->Opcode = OPCODE_MUL; 340bf215546Sopenharmony_ci inst->DstReg.File = PROGRAM_TEMPORARY; 341bf215546Sopenharmony_ci inst->DstReg.Index = fogFactorTemp; 342bf215546Sopenharmony_ci inst->DstReg.WriteMask = WRITEMASK_X; 343bf215546Sopenharmony_ci inst->SrcReg[0].File = PROGRAM_STATE_VAR; 344bf215546Sopenharmony_ci inst->SrcReg[0].Index = fogPRefOpt; 345bf215546Sopenharmony_ci inst->SrcReg[0].Swizzle 346bf215546Sopenharmony_ci = (fog_mode == GL_EXP) ? SWIZZLE_ZZZZ : SWIZZLE_WWWW; 347bf215546Sopenharmony_ci inst->SrcReg[1].File = PROGRAM_INPUT; 348bf215546Sopenharmony_ci inst->SrcReg[1].Index = VARYING_SLOT_FOGC; 349bf215546Sopenharmony_ci inst->SrcReg[1].Swizzle = SWIZZLE_XXXX; 350bf215546Sopenharmony_ci inst++; 351bf215546Sopenharmony_ci if (fog_mode == GL_EXP2) { 352bf215546Sopenharmony_ci /* MUL fogFactorTemp.x, fogFactorTemp.x, fogFactorTemp.x; */ 353bf215546Sopenharmony_ci inst->Opcode = OPCODE_MUL; 354bf215546Sopenharmony_ci inst->DstReg.File = PROGRAM_TEMPORARY; 355bf215546Sopenharmony_ci inst->DstReg.Index = fogFactorTemp; 356bf215546Sopenharmony_ci inst->DstReg.WriteMask = WRITEMASK_X; 357bf215546Sopenharmony_ci inst->SrcReg[0].File = PROGRAM_TEMPORARY; 358bf215546Sopenharmony_ci inst->SrcReg[0].Index = fogFactorTemp; 359bf215546Sopenharmony_ci inst->SrcReg[0].Swizzle = SWIZZLE_XXXX; 360bf215546Sopenharmony_ci inst->SrcReg[1].File = PROGRAM_TEMPORARY; 361bf215546Sopenharmony_ci inst->SrcReg[1].Index = fogFactorTemp; 362bf215546Sopenharmony_ci inst->SrcReg[1].Swizzle = SWIZZLE_XXXX; 363bf215546Sopenharmony_ci inst++; 364bf215546Sopenharmony_ci } 365bf215546Sopenharmony_ci /* EX2_SAT fogFactorTemp.x, -fogFactorTemp.x; */ 366bf215546Sopenharmony_ci inst->Opcode = OPCODE_EX2; 367bf215546Sopenharmony_ci inst->DstReg.File = PROGRAM_TEMPORARY; 368bf215546Sopenharmony_ci inst->DstReg.Index = fogFactorTemp; 369bf215546Sopenharmony_ci inst->DstReg.WriteMask = WRITEMASK_X; 370bf215546Sopenharmony_ci inst->SrcReg[0].File = PROGRAM_TEMPORARY; 371bf215546Sopenharmony_ci inst->SrcReg[0].Index = fogFactorTemp; 372bf215546Sopenharmony_ci inst->SrcReg[0].Negate = NEGATE_XYZW; 373bf215546Sopenharmony_ci inst->SrcReg[0].Swizzle = SWIZZLE_XXXX; 374bf215546Sopenharmony_ci inst->Saturate = GL_TRUE; 375bf215546Sopenharmony_ci inst++; 376bf215546Sopenharmony_ci } 377bf215546Sopenharmony_ci /* LRP result.color.xyz, fogFactorTemp.xxxx, colorTemp, fogColorRef; */ 378bf215546Sopenharmony_ci inst->Opcode = OPCODE_LRP; 379bf215546Sopenharmony_ci inst->DstReg.File = PROGRAM_OUTPUT; 380bf215546Sopenharmony_ci inst->DstReg.Index = FRAG_RESULT_COLOR; 381bf215546Sopenharmony_ci inst->DstReg.WriteMask = WRITEMASK_XYZ; 382bf215546Sopenharmony_ci inst->SrcReg[0].File = PROGRAM_TEMPORARY; 383bf215546Sopenharmony_ci inst->SrcReg[0].Index = fogFactorTemp; 384bf215546Sopenharmony_ci inst->SrcReg[0].Swizzle = SWIZZLE_XXXX; 385bf215546Sopenharmony_ci inst->SrcReg[1].File = PROGRAM_TEMPORARY; 386bf215546Sopenharmony_ci inst->SrcReg[1].Index = colorTemp; 387bf215546Sopenharmony_ci inst->SrcReg[1].Swizzle = SWIZZLE_NOOP; 388bf215546Sopenharmony_ci inst->SrcReg[2].File = PROGRAM_STATE_VAR; 389bf215546Sopenharmony_ci inst->SrcReg[2].Index = fogColorRef; 390bf215546Sopenharmony_ci inst->SrcReg[2].Swizzle = SWIZZLE_NOOP; 391bf215546Sopenharmony_ci inst++; 392bf215546Sopenharmony_ci /* MOV result.color.w, colorTemp.x; # copy alpha */ 393bf215546Sopenharmony_ci inst->Opcode = OPCODE_MOV; 394bf215546Sopenharmony_ci inst->DstReg.File = PROGRAM_OUTPUT; 395bf215546Sopenharmony_ci inst->DstReg.Index = FRAG_RESULT_COLOR; 396bf215546Sopenharmony_ci inst->DstReg.WriteMask = WRITEMASK_W; 397bf215546Sopenharmony_ci inst->SrcReg[0].File = PROGRAM_TEMPORARY; 398bf215546Sopenharmony_ci inst->SrcReg[0].Index = colorTemp; 399bf215546Sopenharmony_ci inst->SrcReg[0].Swizzle = SWIZZLE_NOOP; 400bf215546Sopenharmony_ci inst++; 401bf215546Sopenharmony_ci /* END; */ 402bf215546Sopenharmony_ci inst->Opcode = OPCODE_END; 403bf215546Sopenharmony_ci inst++; 404bf215546Sopenharmony_ci 405bf215546Sopenharmony_ci /* free old instructions */ 406bf215546Sopenharmony_ci ralloc_free(fprog->arb.Instructions); 407bf215546Sopenharmony_ci 408bf215546Sopenharmony_ci /* install new instructions */ 409bf215546Sopenharmony_ci fprog->arb.Instructions = newInst; 410bf215546Sopenharmony_ci fprog->arb.NumInstructions = inst - newInst; 411bf215546Sopenharmony_ci fprog->info.inputs_read |= VARYING_BIT_FOGC; 412bf215546Sopenharmony_ci assert(fprog->info.outputs_written & (1 << FRAG_RESULT_COLOR)); 413bf215546Sopenharmony_ci} 414