summaryrefslogtreecommitdiffstats
path: root/RenderScript/src/BlendStateParser.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'RenderScript/src/BlendStateParser.cpp')
-rw-r--r--RenderScript/src/BlendStateParser.cpp196
1 files changed, 196 insertions, 0 deletions
diff --git a/RenderScript/src/BlendStateParser.cpp b/RenderScript/src/BlendStateParser.cpp
new file mode 100644
index 0000000..adf26ae
--- /dev/null
+++ b/RenderScript/src/BlendStateParser.cpp
@@ -0,0 +1,196 @@
+/* Copyright 2015 Egor Yusov
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 OF ANY PROPRIETARY RIGHTS.
+ *
+ * In no event and under no legal theory, whether in tort (including negligence),
+ * contract, or otherwise, unless required by applicable law (such as deliberate
+ * and grossly negligent acts) or agreed to in writing, shall any Contributor be
+ * liable for any damages, including any direct, indirect, special, incidental,
+ * or consequential damages of any character arising as a result of this License or
+ * out of the use or inability to use the software (including but not limited to damages
+ * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
+ * all other commercial damages or losses), even if such Contributor has been advised
+ * of the possibility of such damages.
+ */
+
+#include "pch.h"
+#include "BlendStateParser.h"
+
+namespace std
+{
+ DEFINE_ENUM_HASH( Diligent::BLEND_FACTOR )
+ DEFINE_ENUM_HASH( Diligent::BLEND_OPERATION )
+ DEFINE_ENUM_HASH( Diligent::COLOR_MASK )
+}
+
+namespace Diligent
+{
+ class RenderTargetBlendStateParser;
+
+ template<>
+ class MemberBinder<RenderTargetBlendStateParser> : public MemberBinderBase
+ {
+ public:
+ MemberBinder( size_t MemberOffset, size_t Dummy ) :
+ MemberBinderBase( MemberOffset )
+ {
+ DEFINE_BINDER( m_Bindings, RenderTargetBlendDesc, BlendEnable, Bool, Validator<Bool>() )
+
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendFactorEnumMapping, BLEND_FACTOR_ZERO);
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendFactorEnumMapping, BLEND_FACTOR_ONE);
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendFactorEnumMapping, BLEND_FACTOR_SRC_COLOR);
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendFactorEnumMapping, BLEND_FACTOR_INV_SRC_COLOR);
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendFactorEnumMapping, BLEND_FACTOR_SRC_ALPHA);
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendFactorEnumMapping, BLEND_FACTOR_INV_SRC_ALPHA);
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendFactorEnumMapping, BLEND_FACTOR_DEST_ALPHA);
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendFactorEnumMapping, BLEND_FACTOR_INV_DEST_ALPHA);
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendFactorEnumMapping, BLEND_FACTOR_DEST_COLOR);
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendFactorEnumMapping, BLEND_FACTOR_INV_DEST_COLOR);
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendFactorEnumMapping, BLEND_FACTOR_SRC_ALPHA_SAT);
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendFactorEnumMapping, BLEND_FACTOR_BLEND_FACTOR);
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendFactorEnumMapping, BLEND_FACTOR_INV_BLEND_FACTOR);
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendFactorEnumMapping, BLEND_FACTOR_SRC1_COLOR);
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendFactorEnumMapping, BLEND_FACTOR_INV_SRC1_COLOR);
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendFactorEnumMapping, BLEND_FACTOR_SRC1_ALPHA);
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendFactorEnumMapping, BLEND_FACTOR_INV_SRC1_ALPHA);
+ VERIFY( m_BlendFactorEnumMapping.m_Str2ValMap.size() == BLEND_FACTOR_NUM_FACTORS - 1,
+ "Unexpected map size. Did you update BLEND_FACTOR enum?" );
+ VERIFY( m_BlendFactorEnumMapping.m_Val2StrMap.size() == BLEND_FACTOR_NUM_FACTORS - 1,
+ "Unexpected map size. Did you update BLEND_FACTOR enum?" );
+
+ DEFINE_ENUM_BINDER( m_Bindings, RenderTargetBlendDesc, SrcBlend, BLEND_FACTOR, m_BlendFactorEnumMapping );
+ DEFINE_ENUM_BINDER( m_Bindings, RenderTargetBlendDesc, DestBlend, BLEND_FACTOR, m_BlendFactorEnumMapping );
+ DEFINE_ENUM_BINDER( m_Bindings, RenderTargetBlendDesc, SrcBlendAlpha, BLEND_FACTOR, m_BlendFactorEnumMapping );
+ DEFINE_ENUM_BINDER( m_Bindings, RenderTargetBlendDesc, DestBlendAlpha, BLEND_FACTOR, m_BlendFactorEnumMapping );
+
+
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendOpEnumMapping, BLEND_OPERATION_ADD );
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendOpEnumMapping, BLEND_OPERATION_SUBTRACT );
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendOpEnumMapping, BLEND_OPERATION_REV_SUBTRACT );
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendOpEnumMapping, BLEND_OPERATION_MIN );
+ DEFINE_ENUM_ELEMENT_MAPPING( m_BlendOpEnumMapping, BLEND_OPERATION_MAX );
+ VERIFY( m_BlendOpEnumMapping.m_Str2ValMap.size() == BLEND_OPERATION_NUM_OPERATIONS - 1,
+ "Unexpected map size. Did you update BLEND_OPERATION enum?" );
+ VERIFY( m_BlendOpEnumMapping.m_Val2StrMap.size() == BLEND_OPERATION_NUM_OPERATIONS - 1,
+ "Unexpected map size. Did you update BLEND_OPERATION enum?" );
+
+ DEFINE_ENUM_BINDER( m_Bindings, RenderTargetBlendDesc, BlendOp, BLEND_OPERATION, m_BlendOpEnumMapping );
+ DEFINE_ENUM_BINDER( m_Bindings, RenderTargetBlendDesc, BlendOpAlpha, BLEND_OPERATION, m_BlendOpEnumMapping );
+
+
+ DEFINE_ENUM_ELEMENT_MAPPING( m_ColorMaskEnumMapping, COLOR_MASK_RED );
+ DEFINE_ENUM_ELEMENT_MAPPING( m_ColorMaskEnumMapping, COLOR_MASK_GREEN );
+ DEFINE_ENUM_ELEMENT_MAPPING( m_ColorMaskEnumMapping, COLOR_MASK_BLUE );
+ DEFINE_ENUM_ELEMENT_MAPPING( m_ColorMaskEnumMapping, COLOR_MASK_ALPHA );
+ DEFINE_ENUM_ELEMENT_MAPPING( m_ColorMaskEnumMapping, COLOR_MASK_ALL );
+ DEFINE_FLAGS_BINDER( m_Bindings, RenderTargetBlendDesc, RenderTargetWriteMask, COLOR_MASK, m_ColorMaskEnumMapping );
+ }
+
+ virtual void GetValue( lua_State *L, const void* pBasePointer )
+ {
+ const auto &RTs = GetMemberByOffest< const RenderTargetBlendDesc >(pBasePointer, m_MemberOffset);
+ PushLuaArray( L, &RTs, &RTs+BlendStateDesc::MaxRenderTargets, [&](const RenderTargetBlendDesc &Elem)
+ {
+ PushLuaTable( L, &Elem, m_Bindings );
+ }
+ );
+ }
+
+ virtual void SetValue( lua_State *L, int Index, void* pBasePointer )
+ {
+ ParseLuaArray( L, Index, pBasePointer, [&]( void* _pBasePointer, int StackIndex, int NewArrayIndex )
+ {
+ VERIFY( pBasePointer == _pBasePointer, "Sanity check" );
+ auto &RenderTargets = GetMemberByOffest<RenderTargetBlendDesc>( pBasePointer, m_MemberOffset );
+
+ auto MaxRTs = BlendStateDesc::MaxRenderTargets;
+ if( !(NewArrayIndex >= 1 && NewArrayIndex <= MaxRTs) )
+ SCRIPT_PARSING_ERROR( L, "Incorrect render target index ", NewArrayIndex, ". Only 1..", MaxRTs, " are allowed" );
+
+ ParseLuaTable( L, StackIndex, &RenderTargets + (NewArrayIndex-1), m_Bindings );
+ }
+ );
+ }
+ private:
+ BindingsMapType m_Bindings;
+
+ EnumMapping < BLEND_FACTOR > m_BlendFactorEnumMapping;
+ EnumMapping < BLEND_OPERATION > m_BlendOpEnumMapping;
+ EnumMapping < COLOR_MASK > m_ColorMaskEnumMapping;
+ };
+
+
+ const Char* BlendStateParser::BlendStateLibName = "BlendState";
+
+ BlendStateParser::BlendStateParser( IRenderDevice *pRenderDevice, lua_State *L ) :
+ EngineObjectParserCommon<IBlendState>( pRenderDevice, L, BlendStateLibName ),
+ m_SetBlendBinding( this, L, "Context", "SetBlendState", &BlendStateParser::SetBlendState )
+ {
+ DEFINE_BUFFERED_STRING_BINDER( m_Bindings, SBSDescWrapper, Name, NameBuffer )
+
+ DEFINE_BINDER( m_Bindings, SBSDescWrapper, AlphaToCoverageEnable, Bool, Validator<Bool>() )
+ DEFINE_BINDER( m_Bindings, SBSDescWrapper, IndependentBlendEnable, Bool, Validator<Bool>() )
+
+ DEFINE_BINDER( m_Bindings, SBSDescWrapper, RenderTargets, RenderTargetBlendStateParser, 0 )
+ };
+
+ void BlendStateParser::CreateObj( lua_State *L )
+ {
+ INIT_LUA_STACK_TRACKING( L );
+
+ SBSDescWrapper BlendDesc;
+ ParseLuaTable( L, 1, &BlendDesc, m_Bindings );
+
+ CHECK_LUA_STACK_HEIGHT();
+
+ auto ppBlendState = reinterpret_cast<IBlendState**>(lua_newuserdata( L, sizeof( IBlendState* ) ));
+ *ppBlendState = nullptr;
+ m_pRenderDevice->CreateBlendState( BlendDesc, ppBlendState );
+ if( *ppBlendState == nullptr )
+ SCRIPT_PARSING_ERROR( L, "Failed to create blend state object" )
+
+ CHECK_LUA_STACK_HEIGHT( +1 );
+ }
+
+ int BlendStateParser::SetBlendState( lua_State *L )
+ {
+ auto *pBS = *GetUserData<IBlendState**>( L, 1, m_MetatableRegistryName.c_str() );
+
+ auto NumArgs = lua_gettop( L );
+ float BlendFactors[4] = { 1, 1, 1, 1 };
+ if( NumArgs >= 2 )
+ {
+ INIT_LUA_STACK_TRACKING( L );
+
+ ParseLuaArray( L, 2, BlendFactors, [&]( void* _pBasePointer, int StackIndex, int NewArrayIndex )
+ {
+ VERIFY( BlendFactors == _pBasePointer, "Sanity check" );
+ if( !(NewArrayIndex >= 1 && NewArrayIndex <= 4) )
+ SCRIPT_PARSING_ERROR( L, "Incorrect blend factor index ", NewArrayIndex, ". Only 1..4 are allowed." );
+
+ BlendFactors[NewArrayIndex - 1] = ReadValueFromLua<Float32>( L, StackIndex );
+ }
+ );
+
+ CHECK_LUA_STACK_HEIGHT();
+ }
+
+ Uint32 SampleMask = 0xFFFFFFFF;
+ if( NumArgs >= 3 )
+ {
+ SampleMask = ReadValueFromLua<Uint32>( L, 3 );
+ }
+
+ auto *pContext = LoadDeviceContextFromRegistry( L );
+ pContext->SetBlendState( pBS, BlendFactors, SampleMask );
+ return 0;
+ }
+}