public static unsafe void EncodeLitMesh(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, ref MeshBGFX mesh, ref float4x4 tx,
                                         ref LitMaterialBGFX mat, ref LightingBGFX lighting, ref float4x4 viewTx, int startIndex, int indexCount,
                                         byte flipCulling, ref LightingViewSpaceBGFX viewSpaceLightCache, uint depth)
 {
     mesh.SetForSubmit(encoder, startIndex, indexCount);
     EncodeLit(sys, encoder, ref sys->m_litShader, viewId, ref tx, ref mat, ref lighting, ref viewTx, flipCulling, ref viewSpaceLightCache, depth);
 }
        // ---------------- simple, lit, with mesh ----------------------------------------------------------------------------------------------------------------------
        public unsafe static void SubmitLitMeshDirect(RendererBGFXInstance *sys, ushort viewId, ref MeshBGFX mesh, ref float4x4 tx,
                                                      ref LitMaterialBGFX mat, ref LightingBGFX lighting,
                                                      ref float4x4 viewTx, int startIndex, int indexCount, byte flipCulling, uint depth)
        {
            bgfx.Encoder *        encoder = bgfx.encoder_begin(false);
            LightingViewSpaceBGFX vsLight = default;

            vsLight.cacheTag = -1;
            EncodeLitMesh(sys, encoder, viewId, ref mesh, ref tx, ref mat, ref lighting, ref viewTx, startIndex, indexCount, flipCulling, ref vsLight, depth);
            bgfx.encoder_end(encoder);
        }
        public static unsafe void EncodeLitSkinnedMesh(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, ref MeshBGFX mesh, ref float4x4 tx,
                                                       ref LitMaterialBGFX mat, ref LightingBGFX lighting, ref float4x4 viewTx, int startIndex, int indexCount,
                                                       byte flipCulling, ref LightingViewSpaceBGFX viewSpaceLightCache, uint depth, float4x4[] boneMatrices)
        {
            mesh.SetForSubmit(encoder, startIndex, indexCount);
            fixed(float4x4 *p = boneMatrices)
            {
                bgfx.encoder_set_uniform(encoder, sys->m_litSkinnedMeshShader.m_uniformBoneMatrices, p, (ushort)boneMatrices.Length);
            }

            EncodeLit(sys, encoder, ref sys->m_litSkinnedMeshShader.m_litShader, viewId, ref tx, ref mat, ref lighting, ref viewTx, flipCulling, ref viewSpaceLightCache, depth);
        }
 public static unsafe void EncodeLitTransient(RendererBGFXInstance *sys, bgfx.Encoder *encoder, bgfx.TransientIndexBuffer *tib, bgfx.TransientVertexBuffer *tvb, int nvertices, int nindices, ushort viewId, ref float4x4 tx, ref LitMaterialBGFX mat, ref LightingBGFX lighting, ref float4x4 viewTx, byte flipCulling, ref LightingViewSpaceBGFX viewSpaceLightCache, uint depth)
 {
     EncodeLitTransientBuffers(sys, encoder, tib, tvb, nvertices, nindices);
     EncodeLit(sys, encoder, ref sys->m_litShader, viewId, ref tx, ref mat, ref lighting, ref viewTx, flipCulling, ref viewSpaceLightCache, depth);
 }
        // 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);
        }
Пример #6
0
        public unsafe bool UpdateLitMaterialBGFX(RendererBGFXInstance *sys, ref LitMaterial mat, ref LitMaterialBGFX matBGFX, bool srgbColors)
        {
            bool stillLoading = false;

            if (InitTexture(ref matBGFX.texAlbedoOpacity, mat.texAlbedoOpacity, sys->m_whiteTexture))
            {
                stillLoading = true;
            }
            if (InitTexture(ref matBGFX.texNormal, mat.texNormal, sys->m_upTexture))
            {
                stillLoading = true;
            }
            if (InitTexture(ref matBGFX.texMetal, mat.texMetal, sys->m_whiteTexture))
            {
                stillLoading = true;
            }
            if (InitTexture(ref matBGFX.texEmissive, mat.texEmissive, sys->m_whiteTexture))
            {
                stillLoading = true;
            }

            matBGFX.constAlbedo_Opacity = srgbColors ?
                                          new float4(Color.LinearToSRGB(mat.constAlbedo), mat.constOpacity) :
                                          new float4(mat.constAlbedo, mat.constOpacity);
            matBGFX.constMetal_Smoothness_Billboarded = new float4(mat.constMetal, mat.constSmoothness, mat.billboarded ? 1 : 0, 0);
            matBGFX.constEmissive_normalMapZScale     = srgbColors ?
                                                        new float4(Color.LinearToSRGB(mat.constEmissive), mat.normalMapZScale) :
                                                        new float4(mat.constEmissive, mat.normalMapZScale);
            matBGFX.mainTextureScaleTranslate = new float4(mat.scale, mat.offset);
            matBGFX.smoothness   = new float4(0.0f);
            matBGFX.smoothness.x = (!mat.transparent && mat.smoothnessAlbedoAlpha) ? 1 : 0;
            matBGFX.smoothness.y = (!mat.transparent && !mat.smoothnessAlbedoAlpha) ? 1 : 0;
            matBGFX.smoothness.z = !mat.transparent ? 1 : 0;

            // if twoSided, need to update state
            matBGFX.state = (ulong)(bgfx.StateFlags.WriteRgb | bgfx.StateFlags.WriteA | bgfx.StateFlags.DepthTestLess);
            if (!mat.twoSided && !mat.billboarded)
            {
                matBGFX.state |= (ulong)bgfx.StateFlags.CullCcw;
            }
            if (mat.transparent)
            {
                matBGFX.state |= RendererBGFXStatic.MakeBGFXBlend(bgfx.StateFlags.BlendOne, bgfx.StateFlags.BlendInvSrcAlpha);
            }
            else
            {
                matBGFX.state |= (ulong)bgfx.StateFlags.WriteZ;
            }
            return(!stillLoading);
        }
Пример #7
0
        public bool UpdateLitMaterialBGFX(RendererBGFXSystem sys, ref LitMaterial mat, ref LitMaterialBGFX matBGFX)
        {
            bool stillLoading = false;

            if (InitTexture(ref matBGFX.texAlbedo, mat.texAlbedo, sys.WhiteTexture))
            {
                stillLoading = true;
            }
            if (InitTexture(ref matBGFX.texOpacity, mat.texOpacity, sys.WhiteTexture))
            {
                stillLoading = true;
            }
            if (InitTexture(ref matBGFX.texNormal, mat.texNormal, sys.UpTexture))
            {
                stillLoading = true;
            }
            if (InitTexture(ref matBGFX.texMetal, mat.texMetal, sys.BlackTexture))
            {
                stillLoading = true;
            }
            if (InitTexture(ref matBGFX.texEmissive, mat.texEmissive, sys.BlackTexture))
            {
                stillLoading = true;
            }
            if (InitTexture(ref matBGFX.texSmoothness, mat.texSmoothness, sys.GreyTexture))
            {
                stillLoading = true;
            }

            matBGFX.constAlbedo_Opacity           = new float4(mat.constAlbedo, mat.constOpacity);
            matBGFX.constMetal_Smoothness         = new float4(mat.constMetal, mat.constSmoothness, 0, 0);
            matBGFX.constEmissive_normalMapZScale = new float4(mat.constEmissive, mat.normalMapZScale);
            matBGFX.mainTextureScaleTranslate     = new float4(mat.scale, mat.offset);

            // if twoSided, need to update state
            matBGFX.state = (ulong)(bgfx.StateFlags.WriteRgb | bgfx.StateFlags.WriteA | bgfx.StateFlags.DepthTestLess);
            if (!mat.twoSided)
            {
                matBGFX.state |= (ulong)bgfx.StateFlags.CullCcw;
            }
            if (mat.transparent)
            {
                matBGFX.state |= RendererBGFXSystem.MakeBGFXBlend(bgfx.StateFlags.BlendOne, bgfx.StateFlags.BlendInvSrcAlpha);
            }
            else
            {
                matBGFX.state |= (ulong)bgfx.StateFlags.WriteZ;
            }
            return(!stillLoading);
        }
Пример #8
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);
        }