// For uniforms and shaders setup. Does not handle vertex/index buffers
        private unsafe static void EncodeLit(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ref LitShader litShader, ushort viewId, ref float4x4 tx, ref LitMaterialBGFX mat,
                                             ref LightingBGFX lighting, ref float4x4 viewTx, byte flipCulling, ref LightingViewSpaceBGFX viewSpaceLightCache, uint depth)
        {
            ulong state = mat.state;

            if (flipCulling != 0)
            {
                state = FlipCulling(state);
            }
            bgfx.encoder_set_state(encoder, state, 0);

            fixed(float4x4 *p = &tx)
            bgfx.encoder_set_transform(encoder, p, 1);

            float3x3 minvt = math.transpose(math.inverse(new float3x3(tx.c0.xyz, tx.c1.xyz, tx.c2.xyz)));

            //float3x3 minvt = new float3x3(tx.c0.xyz, tx.c1.xyz, tx.c2.xyz);
            bgfx.encoder_set_uniform(encoder, litShader.m_uniformModelInverseTranspose, &minvt, 1);

            // material uniforms setup
            fixed(float4 *p = &mat.constAlbedo_Opacity)
            bgfx.encoder_set_uniform(encoder, litShader.m_uniformAlbedoOpacity, p, 1);

            fixed(float4 *p = &mat.constMetal_Smoothness_Billboarded)
            bgfx.encoder_set_uniform(encoder, litShader.m_uniformMetalSmoothnessBillboarded, p, 1);

            fixed(float4 *p = &mat.constEmissive_normalMapZScale)
            bgfx.encoder_set_uniform(encoder, litShader.m_uniformEmissiveNormalZScale, p, 1);

            float4 debugVect = sys->m_outputDebugSelect;

            bgfx.encoder_set_uniform(encoder, litShader.m_uniformOutputDebugSelect, &debugVect, 1);

            fixed(float4 *p = &mat.smoothness)
            bgfx.encoder_set_uniform(encoder, litShader.m_uniformSmoothness, p, 1);

            // textures
            bgfx.encoder_set_texture(encoder, 0, litShader.m_samplerAlbedoOpacity, mat.texAlbedoOpacity, UInt32.MaxValue);
            bgfx.encoder_set_texture(encoder, 1, litShader.m_samplerMetal, mat.texMetal, UInt32.MaxValue);
            bgfx.encoder_set_texture(encoder, 2, litShader.m_samplerNormal, mat.texNormal, UInt32.MaxValue);

            bgfx.encoder_set_texture(encoder, 4, litShader.m_samplerEmissive, mat.texEmissive, UInt32.MaxValue);

            fixed(float4 *p = &mat.mainTextureScaleTranslate)
            bgfx.encoder_set_uniform(encoder, litShader.m_uniformTexMad, p, 1);

            // ambient
            fixed(float4 *p = &lighting.ambient)
            bgfx.encoder_set_uniform(encoder, litShader.m_uniformAmbient, p, 1);

            // transform lighting to view space, if needed: this only needs to re-compute if the viewId changed
            // also the lighting view space is per-thread, hence it is passed in
            lighting.TransformToViewSpace(ref viewTx, ref viewSpaceLightCache, viewId);

            // dir or point lights
            fixed(float *p = viewSpaceLightCache.podl_positionOrDirViewSpace)
            bgfx.encoder_set_uniform(encoder, litShader.m_simplelightPosOrDir, p, (ushort)lighting.numPointOrDirLights);

            fixed(float *p = lighting.podl_colorIVR)
            bgfx.encoder_set_uniform(encoder, litShader.m_simplelightColorIVR, p, (ushort)lighting.numPointOrDirLights);

            // mapped lights (always have to set those or there are undefined samplers)
            EncodeMappedLight(encoder, ref lighting.mappedLight0, ref litShader.m_mappedLight0, 0, viewSpaceLightCache.mappedLight0_viewPosOrDir); // sampler 6
            EncodeMappedLight(encoder, ref lighting.mappedLight1, ref litShader.m_mappedLight1, 1, viewSpaceLightCache.mappedLight1_viewPosOrDir); // sampler 7

            fixed(float4 *p = &lighting.mappedLight01sis)
            bgfx.encoder_set_uniform(encoder, litShader.m_texShadow01sis, p, 1);

            // csm
            fixed(float4 *p = &viewSpaceLightCache.csmLight_viewPosOrDir)
            bgfx.encoder_set_uniform(encoder, litShader.m_dirCSM, p, 1);

            fixed(float *p = lighting.csmOffsetScale)
            bgfx.encoder_set_uniform(encoder, litShader.m_offsetScaleCSM, p, 4);

            fixed(float4 *p = &lighting.csmLight.color_invrangesqr)
            bgfx.encoder_set_uniform(encoder, litShader.m_colorCSM, p, 1);

            fixed(float4x4 *p = &lighting.csmLight.projection)
            bgfx.encoder_set_uniform(encoder, litShader.m_matrixCSM, p, 1);

            fixed(float4 *p = &lighting.csmLightsis)
            bgfx.encoder_set_uniform(encoder, litShader.m_sisCSM, p, 1);

            bgfx.encoder_set_texture(encoder, 5, litShader.m_samplerShadowCSM, lighting.csmLight.shadowMap, UInt32.MaxValue);

            float4 numlights = new float4(lighting.numPointOrDirLights, lighting.numMappedLights, lighting.numCsmLights, 0.0f);

            bgfx.encoder_set_uniform(encoder, litShader.m_numLights, &numlights, 1);

            // fog
            fixed(float4 *p = &lighting.fogColor)
            bgfx.encoder_set_uniform(encoder, litShader.m_uniformFogColor, p, 1);

            fixed(float4 *p = &lighting.fogParams)
            bgfx.encoder_set_uniform(encoder, litShader.m_uniformFogParams, p, 1);

            // submit
            bgfx.encoder_submit(encoder, viewId, litShader.m_prog, depth, (byte)bgfx.DiscardFlags.All);
        }
Esempio n. 2
0
        public unsafe static void EncodeLit(RendererBGFXSystem sys, bgfx.Encoder *encoder, ushort viewId, ref SimpleMeshBGFX mesh, ref float4x4 tx,
                                            ref LitMaterialBGFX mat, ref LightingBGFX lighting,
                                            ref float4x4 viewTx, int startIndex, int indexCount, byte flipCulling, ref LightingViewSpaceBGFX viewSpaceLightCache)
        {
            ulong state = mat.state;

            if (flipCulling != 0)
            {
                state = FlipCulling(state);
            }
            bgfx.encoder_set_state(encoder, state, 0);

            fixed(float4x4 *p = &tx)
            bgfx.encoder_set_transform(encoder, p, 1);

            float3x3 minvt = math.transpose(math.inverse(new float3x3(tx.c0.xyz, tx.c1.xyz, tx.c2.xyz)));

            //float3x3 minvt = new float3x3(tx.c0.xyz, tx.c1.xyz, tx.c2.xyz);
            bgfx.encoder_set_uniform(encoder, sys.m_litShader.m_uniformModelInverseTranspose, &minvt, 1);

            bgfx.encoder_set_index_buffer(encoder, mesh.indexBufferHandle, (uint)startIndex, (uint)indexCount);
            bgfx.encoder_set_vertex_buffer(encoder, 0, mesh.vertexBufferHandle, (uint)mesh.vertexFirst, (uint)mesh.vertexCount, mesh.vertexDeclHandle);

            // material uniforms setup
            fixed(float4 *p = &mat.constAlbedo_Opacity)
            bgfx.encoder_set_uniform(encoder, sys.m_litShader.m_uniformAlbedoOpacity, p, 1);

            fixed(float4 *p = &mat.constMetal_Smoothness)
            bgfx.encoder_set_uniform(encoder, sys.m_litShader.m_uniformMetalSmoothness, p, 1);

            fixed(float4 *p = &mat.constEmissive_normalMapZScale)
            bgfx.encoder_set_uniform(encoder, sys.m_litShader.m_uniformEmissiveNormalZScale, p, 1);

            float4 debugVect = sys.OutputDebugSelect;

            bgfx.encoder_set_uniform(encoder, sys.m_litShader.m_uniformOutputDebugSelect, &debugVect, 1);
            // textures
            bgfx.encoder_set_texture(encoder, 0, sys.m_litShader.m_samplerAlbedo, mat.texAlbedo, UInt32.MaxValue);
            bgfx.encoder_set_texture(encoder, 1, sys.m_litShader.m_samplerMetal, mat.texMetal, UInt32.MaxValue);
            bgfx.encoder_set_texture(encoder, 2, sys.m_litShader.m_samplerNormal, mat.texNormal, UInt32.MaxValue);
            bgfx.encoder_set_texture(encoder, 3, sys.m_litShader.m_samplerSmoothness, mat.texSmoothness, UInt32.MaxValue);
            bgfx.encoder_set_texture(encoder, 4, sys.m_litShader.m_samplerEmissive, mat.texEmissive, UInt32.MaxValue);
            bgfx.encoder_set_texture(encoder, 5, sys.m_litShader.m_samplerOpacity, mat.texOpacity, UInt32.MaxValue);

            fixed(float4 *p = &mat.mainTextureScaleTranslate)
            bgfx.encoder_set_uniform(encoder, sys.m_litShader.m_uniformTexMad, p, 1);

            // ambient
            fixed(float4 *p = &lighting.ambient)
            bgfx.encoder_set_uniform(encoder, sys.m_litShader.m_uniformAmbient, p, 1);

            // transform lighting to view space, if needed: this only needs to re-compute if the viewId changed
            // also the lighting view space is per-thread, hence it is passed in
            lighting.TransformToViewSpace(ref viewTx, ref viewSpaceLightCache, viewId);

            // dir or point lights
            fixed(float *p = viewSpaceLightCache.podl_positionOrDirViewSpace)
            bgfx.encoder_set_uniform(encoder, sys.m_litShader.m_simplelightPosOrDir, p, (ushort)lighting.numPointOrDirLights);

            fixed(float *p = lighting.podl_colorIVR)
            bgfx.encoder_set_uniform(encoder, sys.m_litShader.m_simplelightColorIVR, p, (ushort)lighting.numPointOrDirLights);

            // mapped lights (always have to set those or there are undefined samplers)
            EncodeMappedLight(encoder, ref lighting.mappedLight0, ref sys.m_litShader.m_mappedLight0, 0, viewSpaceLightCache.mappedLight0_viewPosOrDir);
            EncodeMappedLight(encoder, ref lighting.mappedLight1, ref sys.m_litShader.m_mappedLight1, 1, viewSpaceLightCache.mappedLight1_viewPosOrDir);

            fixed(float4 *p = &lighting.mappedLight01sis)
            bgfx.encoder_set_uniform(encoder, sys.m_litShader.m_texShadow01sis, p, 1);

            float4 numlights = new float4(lighting.numPointOrDirLights, lighting.numMappedLights, 0.0f, 0.0f);

            bgfx.encoder_set_uniform(encoder, sys.m_litShader.m_numLights, &numlights, 1);

            // submit
            bgfx.encoder_submit(encoder, viewId, sys.m_litShader.m_prog, 0, false);
        }