00001 // Sh: A GPU metaprogramming language. 00002 // 00003 // Copyright 2003-2005 Serious Hack Inc. 00004 // 00005 // This software is provided 'as-is', without any express or implied 00006 // warranty. In no event will the authors be held liable for any damages 00007 // arising from the use of this software. 00008 // 00009 // Permission is granted to anyone to use this software for any purpose, 00010 // including commercial applications, and to alter it and redistribute it 00011 // freely, subject to the following restrictions: 00012 // 00013 // 1. The origin of this software must not be misrepresented; you must 00014 // not claim that you wrote the original software. If you use this 00015 // software in a product, an acknowledgment in the product documentation 00016 // would be appreciated but is not required. 00017 // 00018 // 2. Altered source versions must be plainly marked as such, and must 00019 // not be misrepresented as being the original software. 00020 // 00021 // 3. This notice may not be removed or altered from any source 00022 // distribution. 00024 #ifndef SHUTIL_KERNELSURFACEIMPL_HPP 00025 #define SHUTIL_KERNELSURFACEIMPL_HPP 00026 00027 #include <sstream> 00028 #include "ShSyntax.hpp" 00029 #include "ShPosition.hpp" 00030 #include "ShManipulator.hpp" 00031 #include "ShAlgebra.hpp" 00032 #include "ShProgram.hpp" 00033 #include "ShNibbles.hpp" 00034 #include "ShKernelSurface.hpp" 00035 #include "ShFunc.hpp" 00036 00041 namespace ShUtil { 00042 00043 using namespace SH; 00044 00045 template<typename T> 00046 ShProgram ShKernelSurface::diffuse() { 00047 ShProgram kernel = SH_BEGIN_FRAGMENT_PROGRAM { 00048 typename T::InputType SH_DECL(kd); 00049 typename T::InputType SH_DECL(irrad); 00050 ShInputNormal3f SH_DECL(normal); 00051 ShInputVector3f SH_DECL(lightVec); 00052 ShInputPosition4f SH_DECL(posh); 00053 00054 irrad *= pos(dot(normalize(normal), normalize(lightVec))); 00055 typename T::OutputType SH_DECL(result); 00056 result = irrad * kd; 00057 } SH_END; 00058 return kernel; 00059 } 00060 00061 template<typename T> 00062 ShProgram ShKernelSurface::specular() { 00063 ShProgram kernel = SH_BEGIN_FRAGMENT_PROGRAM { 00064 typename T::InputType SH_DECL(ks); 00065 ShInputAttrib1f SH_DECL(specExp); 00066 typename T::InputType SH_DECL(irrad); 00067 00068 ShInputNormal3f SH_DECL(normal); 00069 ShInputVector3f SH_DECL(halfVec); 00070 ShInputVector3f SH_DECL(lightVec); 00071 ShInputPosition4f SH_DECL(posh); 00072 00073 normal = normalize(normal); 00074 halfVec = normalize(halfVec); 00075 lightVec = normalize(lightVec); 00076 irrad *= pos(normal | lightVec); 00077 00078 typename T::OutputType SH_DECL(result); 00079 result = irrad * ks * pow(pos(normal | halfVec),specExp); 00080 } SH_END; 00081 return kernel; 00082 } 00083 00084 00085 template<typename T> 00086 ShProgram ShKernelSurface::phong() { 00087 ShProgram kernel = SH_BEGIN_PROGRAM("gpu:fragment") { 00088 typename T::InputType SH_DECL(kd); 00089 typename T::InputType SH_DECL(ks); 00090 ShInputAttrib1f SH_DECL(specExp); 00091 typename T::InputType SH_DECL(irrad); 00092 00093 ShInputNormal3f SH_DECL(normal); 00094 ShInputVector3f SH_DECL(halfVec); 00095 ShInputVector3f SH_DECL(lightVec); 00096 ShInputPosition4f SH_DECL(posh); 00097 00098 typename T::OutputType SH_DECL(result); 00099 00100 normal = normalize(normal); 00101 halfVec = normalize(halfVec); 00102 lightVec = normalize(lightVec); 00103 irrad *= pos(normal | lightVec); 00104 result = irrad * (kd + ks * pow(pos(normal | halfVec), specExp)); 00105 } SH_END; 00106 return kernel; 00107 } 00108 00109 template<typename T> 00110 ShProgram ShKernelSurface::gooch() { 00111 ShProgram kernel = SH_BEGIN_PROGRAM("gpu:fragment") { 00112 typename T::InputType SH_DECL(kd); 00113 typename T::InputType SH_DECL(cool); 00114 typename T::InputType SH_DECL(warm); 00115 typename T::InputType SH_DECL(irrad); 00116 00117 ShInputNormal3f SH_DECL(normal); 00118 ShInputVector3f SH_DECL(lightVec); 00119 ShInputPosition4f SH_DECL(posh); 00120 00121 typename T::OutputType SH_DECL(result); 00122 00123 normal = normalize(normal); 00124 lightVec = normalize(lightVec); 00125 result = lerp(mad((normal | lightVec), ShConstAttrib1f(0.5f), ShConstAttrib1f(0.5f)) * irrad, warm, cool) * kd; 00126 } SH_END; 00127 return kernel; 00128 } 00129 00130 template<typename T> 00131 ShProgram ShKernelSurface::null() { 00132 ShProgram kernel = SH_BEGIN_PROGRAM("gpu:fragment") { 00133 typename T::InputType SH_DECL(irrad); 00134 ShInputPosition4f SH_DECL(posh); 00135 00136 typename T::OutputType SH_DECL(result) = irrad; 00137 } SH_END; 00138 return kernel; 00139 } 00140 00141 } 00142 00143 #endif
1.4.3-20050530