📄 material.cpp
字号:
/* Copyright (c) 2007 Massachusetts Institute of Technology * * 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, sublicense, 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 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 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS 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. *//** * material.cpp - Implementation for routines of the material class * @author Russel Ryan */extern "C" {#include <stdio.h> //#include <libvector.h>#include <libmisc.h>#include <math.h>//#include <simdmath.h>#include <simdmath/expf4.h>#include <simdmath/fminf4.h>#include <simdmath/floorf4.h>#include <simdmath/fmodf4.h>#include <simdmath/sqrtf4.h>#include <simdmath/sinf4.h>#include <simdmath/divf4.h>#include <simdmath/fabsf4.h>}#define expf4 _expf4#define fminf4 _fminf4#define floorf4 _floorf4#define fmodf4 _fmodf4#define sqrtf4 _sqrtf4#define sinf4 _sinf4#define divf4 _divf4#define fabsf4 _fabsf4#include <set_spec_exponent9.h>#define set_spec_exponent9 _set_spec_exponent9#include <spec9.h>#define spec9 _spec9#include <spec9_v.h>#define spec9_v _spec9_v#include <splat_matrix4x4.h>#define splat_matrix4x4 _splat_matrix4x4#include <xform_vec3_v.h>#define xform_vec3_v _xform_vec3_v#include <lerp_vec3_v.h>#define lerp_vec3_v _lerp_vec3_v#include <normalize3_v.h>#define normalize3_v _normalize3_v#include <dot_product3_v.h>#define dot_product3_v _dot_product3_v#include "material.h"//#define USE_IFSstruct fp_struct { union { struct { MATERIAL_SHADER_FP(shader); MATERIAL_SHADER_SPECULAR_FP(specular); MATERIAL_SHADER_BLENDFACTOR_FP(blendfactor); MATERIAL_SHADER_DIFFUSE_FP(diffuse); MATERIAL_SHADER_REFLECTIVE_FP(reflective); MATERIAL_SHADER_TRANSPARENT_FP(transparent); MATERIAL_SHADER_REFRACTIVE_FP(refractive); }; vector unsigned int fp_v; };};const struct fp_struct fp_shader[NUM_MATERIAL_TYPES] = { //Phong { material_BRDFStyleShade, material_getPhongSpecularCoefficient, material_getBlendFactorDummy, material_getBRDFDiffuseColor, material_getBRDFReflectiveColor, material_getBRDFTransparentColor, material_getBRDFRefractiveIndex }, //Cook Torrance { material_BRDFStyleShade, material_getCookTorranceSpecularCoefficient, material_getBlendFactorDummy, material_getBRDFDiffuseColor, material_getBRDFReflectiveColor, material_getBRDFTransparentColor, material_getBRDFRefractiveIndex }, //Checkerboard { material_CheckerboardShade, material_getSpecularCoefficientDummy, material_getBlendFactorDummy, material_getCheckerboardDiffuseColor, material_getCheckerboardReflectiveColor, material_getCheckerboardTransparentColor, material_getCheckerboardRefractiveIndex }, //Marble { material_BlenderShade, material_getSpecularCoefficientDummy, material_getMarbleBlendFactor, material_getBlenderDiffuseColor, material_getBlenderReflectiveColor, material_getBlenderTransparentColor, material_getBlenderRefractiveIndex }, //Wood { material_BlenderShade, material_getSpecularCoefficientDummy, material_getWoodBlendFactor, material_getBlenderDiffuseColor, material_getBlenderReflectiveColor, material_getBlenderTransparentColor, material_getBlenderRefractiveIndex }, //Bump { material_BumpShade, material_getSpecularCoefficientDummy, material_getBlendFactorDummy, material_getBumpDiffuseColor, material_getBumpReflectiveColor, material_getBumpTransparentColor, material_getBumpRefractiveIndex }};MATERIAL_SHADER(materialShadeDummy) { dprintf("materialShadeDummy\r\n");}MATERIAL_SHADER(materialShade) { dprintf("materialShade\r\n");#ifndef USE_IFS union { vector unsigned int functions; struct { material_shader f1; material_shader f2; material_shader f3; material_shader f4; }; }; union { vector unsigned int matTypes; struct { unsigned int mat1_type, mat2_type, mat3_type, mat4_type; }; }; matTypes = (vector unsigned int) {(unsigned int)materials[hp.materialID1].materialType, (unsigned int)materials[hp.materialID2].materialType, (unsigned int)materials[hp.materialID3].materialType, (unsigned int)materials[hp.materialID4].materialType}; vector unsigned int dummy = spu_splats((unsigned int)materialShadeDummy); functions = (vector unsigned int) { (unsigned int)fp_shader[mat1_type].shader, (unsigned int)fp_shader[mat2_type].shader, (unsigned int)fp_shader[mat3_type].shader, (unsigned int)fp_shader[mat4_type].shader}; vector unsigned int thisID; thisID = spu_cmpeq(matTypes, spu_splats(mat1_type)); (*f1)(materials, rgbp, rp, hp, dirToLight_x, dirToLight_y, dirToLight_z, lightColor_r, lightColor_g, lightColor_b, spu_and(shadeBits, thisID)); functions = spu_sel(functions, dummy, thisID); thisID = spu_cmpeq(matTypes, spu_splats(mat2_type)); (*f2)(materials, rgbp, rp, hp, dirToLight_x, dirToLight_y, dirToLight_z, lightColor_r, lightColor_g, lightColor_b, spu_and(shadeBits, thisID)); functions = spu_sel(functions, dummy, thisID); thisID = spu_cmpeq(matTypes, spu_splats(mat3_type)); (*f3)(materials, rgbp, rp, hp, dirToLight_x, dirToLight_y, dirToLight_z, lightColor_r, lightColor_g, lightColor_b, spu_and(shadeBits, thisID)); functions = spu_sel(functions, dummy, thisID); thisID = spu_cmpeq(matTypes, spu_splats(mat4_type)); (*f4)(materials, rgbp, rp, hp, dirToLight_x, dirToLight_y, dirToLight_z, lightColor_r, lightColor_g, lightColor_b, spu_and(shadeBits, thisID)); functions = spu_sel(functions, dummy, thisID); #else unsigned int i=0; union { vector unsigned int res_v; struct { unsigned int res_i[4]; }; }; unsigned int prevID[3] = { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF }; //call material 1's shader, set bits so that it shades anything else with material1's id unsigned int id = spu_extract(hp.materialID, 0); res_v = spu_cmpeq(hp.materialID, spu_splats(id)); res_v = spu_and(shadeBits, res_v); ((*fp_shader[materials[id].materialType].shader) (materials, rgbp, rp, hp, dirToLight_x, dirToLight_y, dirToLight_z, lightColor_r, lightColor_g, lightColor_b, res_v)); prevID[0] = id; //call material 2's shader, don't call if it's already been called, set bits so that it shades anything else with material1's id id = spu_extract(hp.materialID, 1); if(id != prevID[0]) { res_v = spu_cmpeq(hp.materialID, spu_splats(id)); res_v = spu_and(shadeBits, res_v); ((*fp_shader[materials[id].materialType].shader) (materials, rgbp, rp, hp, dirToLight_x, dirToLight_y, dirToLight_z, lightColor_r, lightColor_g, lightColor_b, res_v)); prevID[1] = id; } //call material 3's shader, don't call if it's already been called, set bits so that it shades anything else with material1's id id = spu_extract(hp.materialID, 2); if(id != prevID[0] && id != prevID[1]) { res_v = spu_cmpeq(hp.materialID, spu_splats(id)); res_v = spu_and(shadeBits, res_v); ((*fp_shader[materials[id].materialType].shader) (materials, rgbp, rp, hp, dirToLight_x, dirToLight_y, dirToLight_z, lightColor_r, lightColor_g, lightColor_b, res_v)); prevID[2] = id; } //call material 4's shader, don't call if it's already been called, set bits so that it shades anything else with material1's id id = spu_extract(hp.materialID, 3); if(id != prevID[0] && id != prevID[1] && id != prevID[2]) { res_v = spu_cmpeq(hp.materialID, spu_splats(id)); res_v = spu_and(shadeBits, res_v); ((*fp_shader[materials[id].materialType].shader) (materials, rgbp, rp, hp, dirToLight_x, dirToLight_y, dirToLight_z, lightColor_r, lightColor_g, lightColor_b, res_v)); } #endif return;}MATERIAL_SHADER_SPECULAR(material_getSpecularCoefficientDummy) { dprintf("material_getSpecularCoefficientDummy\r\n");}MATERIAL_SHADER_BLENDFACTOR(material_getBlendFactorDummy) { dprintf("material_getBlendFactorDummy\r\n"); }MATERIAL_SHADER_DIFFUSE(material_getDiffuseColorDummy) { dprintf("material_getDiffuseColorDummy\r\n");}MATERIAL_SHADER_DIFFUSE(material_getDiffuseColor) { dprintf("material_getDiffuseColor\r\n"); //The New Way (tm) union { vector unsigned int functions; struct { material_shader_diffuse f1; material_shader_diffuse f2; material_shader_diffuse f3; material_shader_diffuse f4; }; }; union { vector unsigned int matTypes; struct { unsigned int mat1_type, mat2_type, mat3_type, mat4_type; }; }; matTypes = (vector unsigned int) {materials[hp.materialID1].materialType, materials[hp.materialID2].materialType, materials[hp.materialID3].materialType, materials[hp.materialID4].materialType}; vector unsigned int dummy = spu_splats((unsigned int)material_getDiffuseColorDummy); functions = (vector unsigned int){ (unsigned int)fp_shader[mat1_type].diffuse, (unsigned int)fp_shader[mat2_type].diffuse, (unsigned int)fp_shader[mat3_type].diffuse, (unsigned int)fp_shader[mat4_type].diffuse}; vector unsigned int thisID; thisID = spu_cmpeq(matTypes, spu_splats(mat1_type)); (*f1)(materials, rgbp, hp, p_x, p_y, p_z, spu_and(shadeBits, thisID)); functions = spu_sel(functions, dummy, thisID); thisID = spu_cmpeq(matTypes, spu_splats(mat2_type)); (*f2)(materials, rgbp, hp, p_x, p_y, p_z, spu_and(shadeBits, thisID)); functions = spu_sel(functions, dummy, thisID); thisID = spu_cmpeq(matTypes, spu_splats(mat3_type)); (*f3)(materials, rgbp, hp, p_x, p_y, p_z, spu_and(shadeBits, thisID)); functions = spu_sel(functions, dummy, thisID); thisID = spu_cmpeq(matTypes, spu_splats(mat4_type)); (*f4)(materials, rgbp, hp, p_x, p_y, p_z, spu_and(shadeBits, thisID)); functions = spu_sel(functions, dummy, thisID); /* //MAJOR TODO : MAKE IT SO IT DOESN'T CALL F'ING FUNCTIONS WITH SHADEBITS == 0 //STUPID TRICK TO GET AROUND LOOPING AND SUCH //IDEA: make a dummy function that does nothing, remove branching. Do spu_ bitmagic to spu_sel the function pointer to call // between the real one and the dummy one. That way we call unconditionally. unsigned int i=0; union { vector unsigned int res_v; struct { unsigned int res_i[4]; }; }; unsigned int prevID[3] = { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF }; //call material 1's diffuse, set bits so that it shades anything else with material1's id unsigned int id = spu_extract(hp.materialID, 0); res_v = spu_cmpeq(hp.materialID, spu_splats(id)); res_v = spu_and(shadeBits, res_v); ((*fp_shader[materials[id].materialType].diffuse) (materials, rgbp, hp, p_x, p_y, p_z, res_v)); prevID[0] = id; dprintf("getDiffuse: calling first material's function.\r\n"); //this is the style fix we need //id = spu_extract(hp.materialID, 1); //res_v = spu_cmpeq(hp.materialID, spu_slqwbyte(hp.materialID, 8)); //res_v = spu_shuffle(res_v, res_v, (vector unsigned char)(0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03)); //material_shader_diffuse fp = (material_shader_diffuse)spu_extract(spu_sel(spu_splats((unsigned int)fp_shader[materials[id].materialType].diffuse), spu_splats((unsigned int)material_getDiffuseColorDummy), res_v), 0); //((*fp) (materials, rgbp, hp, p_x, p_y, p_z, res_v)); //call material 2's shader, don't call if it's already been called, set bits so that it shades anything else with material1's id id = spu_extract(hp.materialID, 1); if(id != prevID[0]) { res_v = spu_cmpeq(hp.materialID, spu_splats(id)); res_v = spu_and(shadeBits, res_v); ((*fp_shader[materials[id].materialType].diffuse) (materials, rgbp, hp, p_x, p_y, p_z, res_v)); prevID[1] = id; dprintf("getDiffuse: calling second material's function.\r\n"); } //call material 3's shader, don't call if it's already been called, set bits so that it shades anything else with material1's id id = spu_extract(hp.materialID, 2); if(id != prevID[0] && id != prevID[1]) { res_v = spu_cmpeq(hp.materialID, spu_splats(id)); res_v = spu_and(shadeBits, res_v); ((*fp_shader[materials[id].materialType].diffuse) (materials, rgbp, hp, p_x, p_y, p_z, res_v)); prevID[2] = id; dprintf("getDiffuse: calling third material's function.\r\n"); } //call material 4's shader, don't call if it's already been called, set bits so that it shades anything else with material1's id id = spu_extract(hp.materialID, 3); if(id != prevID[0] && id != prevID[1] && id != prevID[2]) { res_v = spu_cmpeq(hp.materialID, spu_splats(id)); res_v = spu_and(shadeBits, res_v);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -