private void AddCascadeMappedLight(ref LightingBGFX r, ref ShadowmappedLight sml, ref Light l, ref float4x4 tx, ref LightMatrices txCache, ref CascadeShadowmappedLight csm, ref CascadeShadowmappedLightCache csmData, RendererBGFXInstance *sys, bool srgbColors) { if (r.numCsmLights >= LightingBGFX.maxCsmLights) { throw new InvalidOperationException("Too many cascade mapped lights"); } bgfx.TextureHandle texShadowMap = sys->m_noShadow; int shadowMapSize = 1; if (sml.shadowMap != Entity.Null && EntityManager.HasComponent <TextureBGFX>(sml.shadowMap)) { var imShadowMap = EntityManager.GetComponentData <Image2D>(sml.shadowMap); Assert.IsTrue(imShadowMap.imagePixelWidth == imShadowMap.imagePixelHeight); shadowMapSize = imShadowMap.imagePixelHeight; texShadowMap = EntityManager.GetComponentData <TextureBGFX>(sml.shadowMap).handle; } float4 worldPosOrDir = new float4(math.normalize(-tx.c2.xyz), 0.0f); float4 mask = new float4(0); float3 c = srgbColors?Color.LinearToSRGB(l.color):l.color; r.SetMappedLight(LightingBGFX.maxMappedLights + r.numCsmLights, txCache.mvp, c * l.intensity, worldPosOrDir, l.clipZFar, mask, texShadowMap, shadowMapSize); unsafe { r.csmOffsetScale[0] = csmData.c0.offset.x; r.csmOffsetScale[1] = csmData.c0.offset.y; r.csmOffsetScale[2] = 0; r.csmOffsetScale[3] = csmData.c0.scale; r.csmOffsetScale[4] = csmData.c1.offset.x; r.csmOffsetScale[5] = csmData.c1.offset.y; r.csmOffsetScale[6] = 0; r.csmOffsetScale[7] = csmData.c1.scale; r.csmOffsetScale[8] = csmData.c2.offset.x; r.csmOffsetScale[9] = csmData.c2.offset.y; r.csmOffsetScale[10] = 0; r.csmOffsetScale[11] = csmData.c2.scale; r.csmOffsetScale[12] = csmData.c3.offset.x; r.csmOffsetScale[13] = csmData.c3.offset.y; r.csmOffsetScale[14] = 0; r.csmOffsetScale[15] = csmData.c3.scale; } // r.csmLightsis set in SetMappedLight r.numCsmLights++; }
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); }
public void Set(float4x4 m, float3 color, float4 _worldPosOrDir, float _range, float4 _mask, bgfx.TextureHandle _shadowMap) { projection = m; color_invrangesqr = new float4(color, LightingBGFX.InverseSquare(_range) * _worldPosOrDir.w); worldPosOrDir = _worldPosOrDir; mask = _mask; shadowMap = _shadowMap; }
// ---------------- 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); }
private void AddMappedLight(ref LightingBGFX r, ref ShadowmappedLight sml, ref Light l, ref float4x4 tx, ref LightMatrices txCache, RendererBGFXInstance *sys, bool isSpot, float4 spotmask, bool srgbColors) { if (r.numMappedLights >= LightingBGFX.maxMappedLights) { throw new InvalidOperationException("Too many mapped lights"); } bgfx.TextureHandle texShadowMap = sys->m_noShadow; int shadowMapSize = 1; if (sml.shadowMap != Entity.Null && EntityManager.HasComponent <TextureBGFX>(sml.shadowMap)) { var imShadowMap = EntityManager.GetComponentData <Image2D>(sml.shadowMap); Assert.IsTrue(imShadowMap.imagePixelWidth == imShadowMap.imagePixelHeight); shadowMapSize = imShadowMap.imagePixelHeight; texShadowMap = EntityManager.GetComponentData <TextureBGFX>(sml.shadowMap).handle; } float4 mask = isSpot ? spotmask : new float4(0.0f, 0.0f, 0.0f, 1.0f); float4 worldPosOrDir = isSpot? new float4(tx.c3.xyz, 1.0f) : new float4(-tx.c2.xyz, 0.0f); float3 c = srgbColors?Color.LinearToSRGB(l.color):l.color; r.SetMappedLight(r.numMappedLights, txCache.mvp, c * l.intensity, worldPosOrDir, l.clipZFar, mask, texShadowMap, shadowMapSize); r.numMappedLights++; }
private void AddMappedLightFromEntity(Entity e, ref LightingBGFX r, bool srgbColors, RendererBGFXInstance *sys) { if (e == Entity.Null) return; }
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); }
private void AddMappedLight(ref LightingBGFX r, ref ShadowmappedLight sml, ref Light l, ref float4x4 tx, ref LightMatrices txCache, RendererBGFXSystem sys, bool isSpot) { if (r.numMappedLights >= LightingBGFX.maxMappedLights) throw new InvalidOperationException("Too many mapped lights"); }
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); }