00001 // Sh: A GPU metaprogramming language. 00002 // 00003 // Copyright 2003-2005 Serious Hack Inc. 00004 // 00005 // This library is free software; you can redistribute it and/or 00006 // modify it under the terms of the GNU Lesser General Public 00007 // License as published by the Free Software Foundation; either 00008 // version 2.1 of the License, or (at your option) any later version. 00009 // 00010 // This library is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 // Lesser General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public 00016 // License along with this library; if not, write to the Free Software 00017 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00018 // MA 02110-1301, USA 00020 #ifndef SHUTIL_KERNELSURFACEIMPL_HPP 00021 #define SHUTIL_KERNELSURFACEIMPL_HPP 00022 00023 #include <sstream> 00024 #include "ShSyntax.hpp" 00025 #include "ShPosition.hpp" 00026 #include "ShManipulator.hpp" 00027 #include "ShAlgebra.hpp" 00028 #include "ShProgram.hpp" 00029 #include "ShNibbles.hpp" 00030 #include "ShKernelSurface.hpp" 00031 #include "ShFunc.hpp" 00032 00037 namespace ShUtil { 00038 00039 using namespace SH; 00040 00041 template<typename T> 00042 ShProgram ShKernelSurface::diffuse() { 00043 ShProgram kernel = SH_BEGIN_FRAGMENT_PROGRAM { 00044 typename T::InputType SH_DECL(kd); 00045 typename T::InputType SH_DECL(irrad); 00046 ShInputNormal3f SH_DECL(normal); 00047 ShInputVector3f SH_DECL(lightVec); 00048 ShInputPosition4f SH_DECL(posh); 00049 00050 irrad *= pos(dot(normalize(normal), normalize(lightVec))); 00051 typename T::OutputType SH_DECL(result); 00052 result = irrad * kd; 00053 } SH_END; 00054 return kernel; 00055 } 00056 00057 template<typename T> 00058 ShProgram ShKernelSurface::specular() { 00059 ShProgram kernel = SH_BEGIN_FRAGMENT_PROGRAM { 00060 typename T::InputType SH_DECL(ks); 00061 ShInputAttrib1f SH_DECL(specExp); 00062 typename T::InputType SH_DECL(irrad); 00063 00064 ShInputNormal3f SH_DECL(normal); 00065 ShInputVector3f SH_DECL(halfVec); 00066 ShInputVector3f SH_DECL(lightVec); 00067 ShInputPosition4f SH_DECL(posh); 00068 00069 normal = normalize(normal); 00070 halfVec = normalize(halfVec); 00071 lightVec = normalize(lightVec); 00072 irrad *= pos(normal | lightVec); 00073 00074 typename T::OutputType SH_DECL(result); 00075 result = irrad * ks * pow(pos(normal | halfVec),specExp); 00076 } SH_END; 00077 return kernel; 00078 } 00079 00080 00081 template<typename T> 00082 ShProgram ShKernelSurface::phong() { 00083 ShProgram kernel = SH_BEGIN_PROGRAM("gpu:fragment") { 00084 typename T::InputType SH_DECL(kd); 00085 typename T::InputType SH_DECL(ks); 00086 ShInputAttrib1f SH_DECL(specExp); 00087 typename T::InputType SH_DECL(irrad); 00088 00089 ShInputNormal3f SH_DECL(normal); 00090 ShInputVector3f SH_DECL(halfVec); 00091 ShInputVector3f SH_DECL(lightVec); 00092 ShInputPosition4f SH_DECL(posh); 00093 00094 typename T::OutputType SH_DECL(result); 00095 00096 normal = normalize(normal); 00097 halfVec = normalize(halfVec); 00098 lightVec = normalize(lightVec); 00099 irrad *= pos(normal | lightVec); 00100 result = irrad * (kd + ks * pow(pos(normal | halfVec), specExp)); 00101 } SH_END; 00102 return kernel; 00103 } 00104 00105 template<typename T> 00106 ShProgram ShKernelSurface::gooch() { 00107 ShProgram kernel = SH_BEGIN_PROGRAM("gpu:fragment") { 00108 typename T::InputType SH_DECL(kd); 00109 typename T::InputType SH_DECL(cool); 00110 typename T::InputType SH_DECL(warm); 00111 typename T::InputType SH_DECL(irrad); 00112 00113 ShInputNormal3f SH_DECL(normal); 00114 ShInputVector3f SH_DECL(lightVec); 00115 ShInputPosition4f SH_DECL(posh); 00116 00117 typename T::OutputType SH_DECL(result); 00118 00119 normal = normalize(normal); 00120 lightVec = normalize(lightVec); 00121 result = lerp(mad((normal | lightVec), ShConstAttrib1f(0.5f), ShConstAttrib1f(0.5f)) * irrad, warm, cool) * kd; 00122 } SH_END; 00123 return kernel; 00124 } 00125 00126 template<typename T> 00127 ShProgram ShKernelSurface::null() { 00128 ShProgram kernel = SH_BEGIN_PROGRAM("gpu:fragment") { 00129 typename T::InputType SH_DECL(irrad); 00130 ShInputPosition4f SH_DECL(posh); 00131 00132 typename T::OutputType SH_DECL(result) = irrad; 00133 } SH_END; 00134 return kernel; 00135 } 00136 00137 } 00138 00139 #endif
1.4.5