advancedLightBufferConditioner.cpp
Engine/source/lighting/advanced/advancedLightBufferConditioner.cpp
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platform.h" 25#include "lighting/advanced/advancedLightBufferConditioner.h" 26 27#include "shaderGen/shaderOp.h" 28#include "gfx/gfxDevice.h" 29#include "core/util/safeDelete.h" 30 31 32AdvancedLightBufferConditioner::~AdvancedLightBufferConditioner() 33{ 34} 35 36Var *AdvancedLightBufferConditioner::_conditionOutput( Var *unconditionedOutput, MultiLine *meta ) 37{ 38 Var *conditionedOutput = new Var; 39 40 if(GFX->getAdapterType() == OpenGL) 41 conditionedOutput->setType("vec4"); 42 else 43 conditionedOutput->setType("float4"); 44 45 DecOp *outputDecl = new DecOp(conditionedOutput); 46 if(mColorFormat == RGB) 47 { 48 conditionedOutput->setName("rgbLightInfoOut"); 49 50 // If this is a 16 bit integer format, scale up/down the values. All other 51 // formats just write out the full 0..1 52 if(getBufferFormat() == GFXFormatR16G16B16A16) 53 meta->addStatement( new GenOp( " @ = max(4.0, (float4(lightColor, specular) * NL_att + float4(bufferSample.rgb, 0.0)) / 4.0);\r\n", outputDecl ) ); 54 else 55 meta->addStatement( new GenOp( " @ = float4(lightColor, 0) * NL_att + float4(bufferSample.rgb, specular);\r\n", outputDecl ) ); 56 } 57 else 58 { 59 // Input u'v' assumed to be scaled 60 conditionedOutput->setName("luvLightInfoOut"); 61 meta->addStatement( new GenOp( " @ = float4( lerp(bufferSample.xy, lightColor.xy, saturate(NL_att / bufferSample.z) * 0.5),\r\n", outputDecl ) ); 62 meta->addStatement( new GenOp( " bufferSample.z + NL_att, bufferSample.w + saturate(specular * NL_att) );\r\n" ) ); 63 } 64 65 return conditionedOutput; 66} 67 68Var *AdvancedLightBufferConditioner::_unconditionInput( Var *conditionedInput, MultiLine *meta ) 69{ 70 if(mColorFormat == RGB) 71 { 72 if(getBufferFormat() == GFXFormatR16G16B16A16) 73 meta->addStatement( new GenOp( " lightColor = @.rgb * 4.0;\r\n", conditionedInput ) ); 74 else 75 meta->addStatement( new GenOp( " lightColor = @.rgb;\r\n", conditionedInput ) ); 76 meta->addStatement( new GenOp( " NL_att = dot(@.rgb, float3(0.3576, 0.7152, 0.1192));\r\n", conditionedInput ) ); 77 } 78 else 79 { 80 meta->addStatement( new GenOp( " // TODO: This clamps HDR values.\r\n" ) ); 81 meta->addStatement( new GenOp( " NL_att = @.b;\r\n", conditionedInput ) ); 82 meta->addStatement( new GenOp( " lightColor = DecodeLuv(float3(saturate(NL_att), @.rg * 0.62));\r\n", conditionedInput ) ); 83 } 84 meta->addStatement( new GenOp( " specular = @.a;\r\n", conditionedInput ) ); 85 86 return NULL; 87} 88 89Var *AdvancedLightBufferConditioner::printMethodHeader( MethodType methodType, const String &methodName, Stream &stream, MultiLine *meta ) 90{ 91 Var *methodVar = new Var; 92 methodVar->setName(methodName); 93 DecOp *methodDecl = new DecOp(methodVar); 94 95 Var *lightColor = new Var; 96 lightColor->setName("lightColor"); 97 DecOp *lightColorDecl = new DecOp(lightColor); 98 99 Var *NLAtt = new Var; 100 NLAtt->setName("NL_att"); 101 DecOp *NLAttDecl = new DecOp(NLAtt); 102 103 Var *specular = new Var; 104 specular->setName("specular"); 105 DecOp *specularDecl = new DecOp(specular); 106 107 Var *bufferSample = new Var; 108 bufferSample->setName("bufferSample"); 109 DecOp *bufferSampleDecl = new DecOp(bufferSample); 110 111 const bool isCondition = ( methodType == ConditionerFeature::ConditionMethod ); 112 113 if(GFX->getAdapterType() == OpenGL) 114 { 115 methodVar->setType(avar("%s", isCondition ? "vec4" : "void")); 116 lightColor->setType(avar("%s vec3", isCondition ? "in" : "out")); 117 NLAtt->setType(avar("%s float", isCondition ? "in" : "out")); 118 specular->setType(avar("%s float", isCondition ? "in" : "out")); 119 bufferSample->setType("in vec4"); 120 } 121 else 122 { 123 methodVar->setType(avar("inline %s", isCondition ? "float4" : "void")); 124 lightColor->setType(avar("%s float3", isCondition ? "in" : "out")); 125 NLAtt->setType(avar("%s float", isCondition ? "in" : "out")); 126 specular->setType(avar("%s float", isCondition ? "in" : "out")); 127 bufferSample->setType("in float4"); 128 } 129 130 // If this is LUV, print methods to convert RGB<->LUV as needed 131 if(mColorFormat == LUV) 132 { 133 if(!isCondition) 134 { 135 meta->addStatement( new GenOp( "float3 DecodeLuv(float3 Luv)\r\n{\r\n" ) ); 136 meta->addStatement( new GenOp( " float2 xy = float2(9.0f, 4.0f) * Luv.yz / (dot(Luv.yz, float2(6.0f, -16.0f)) + 12.0f);\r\n" ) ); 137 meta->addStatement( new GenOp( " float Ld = Luv.x;\r\n" ) ); 138 meta->addStatement( new GenOp( " float3 XYZ = float3(xy.x, Ld, 1.0f - xy.x - xy.y);\r\n" ) ); 139 meta->addStatement( new GenOp( " XYZ.xz = XYZ.xz * Ld / xy.y;\r\n" ) ); 140 meta->addStatement( new GenOp( " const float3x3 XYZ2RGB =\r\n" ) ); 141 meta->addStatement( new GenOp( " {\r\n" ) ); 142 meta->addStatement( new GenOp( " 2.5651f, -1.1665f, -0.3986f,\r\n" ) ); 143 meta->addStatement( new GenOp( " -1.0217f, 1.9777f, 0.0439f,\r\n" ) ); 144 meta->addStatement( new GenOp( " 0.0753f, -0.2543f, 1.1892f\r\n" ) ); 145 meta->addStatement( new GenOp( " };\r\n" ) ); 146 meta->addStatement( new GenOp( " return tMul(XYZ2RGB, XYZ);\r\n" ) ); 147 meta->addStatement( new GenOp( "}\r\n\r\n" ) ); 148 } 149 else 150 { 151 // Shouldn't need this 152 } 153 } 154 155 // Method header and opening bracket 156 if(isCondition) 157 { 158 // All parameters are input parameters, and the return value is float4. 159 // If this is an LUV buffer format, than the previous pixel value is needed 160 // for interpolation. 161 meta->addStatement( new GenOp( "@(@, @, @, @)\r\n", methodDecl, lightColorDecl, NLAttDecl, specularDecl, bufferSampleDecl ) ); 162 } 163 else 164 { 165 // Sample as input, parameters as output. Void return. 166 meta->addStatement( new GenOp( "@(@, @, @, @)\r\n", methodDecl, bufferSampleDecl, lightColorDecl, NLAttDecl, specularDecl ) ); 167 } 168 169 meta->addStatement( new GenOp( "{\r\n" ) ); 170 171 // We don't use this way of passing var's around, so this should cause a crash 172 // if something uses this improperly 173 return ( isCondition ? NULL : bufferSample ); 174} 175 176void AdvancedLightBufferConditioner::printMethodFooter( ConditionerFeature::MethodType methodType, Var *retVar, Stream &stream, MultiLine *meta ) 177{ 178 // Return and closing bracket 179 if(methodType == ConditionerFeature::ConditionMethod) 180 meta->addStatement( new GenOp( "\r\n return @;\r\n", retVar ) ); 181 182 // Uncondition will assign output parameters 183 meta->addStatement( new GenOp( "}\r\n" ) ); 184} 185