public static unsafe void EncodeLinePreTransformed(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, SimpleVertex *vertices, int n) { bgfx.TransientIndexBuffer tib; bgfx.TransientVertexBuffer tvb; int ni = (n / 4) * 6; if (!bgfx.alloc_transient_buffers(&tvb, &sys->m_simpleVertexBufferDecl, (uint)n, &tib, (uint)ni)) { throw new InvalidOperationException("Out of transient bgfx memory!"); } UnsafeUtility.MemCpy((SimpleVertex *)tvb.data, vertices, sizeof(SimpleVertex) * n); ushort *indices = (ushort *)tib.data; for (int i = 0; i < n; i += 4) { indices[0] = (ushort)i; indices[1] = (ushort)(i + 1); indices[2] = (ushort)(i + 2); indices[3] = (ushort)(i + 2); indices[4] = (ushort)(i + 3); indices[5] = (ushort)i; indices += 6; } bgfx.encoder_set_transient_index_buffer(encoder, &tib, 0, (uint)ni); bgfx.encoder_set_transient_vertex_buffer(encoder, 0, &tvb, 0, (uint)n, sys->m_simpleVertexBufferDeclHandle); // material uniforms setup ulong state = (ulong)(bgfx.StateFlags.DepthTestLess | bgfx.StateFlags.WriteRgb) | RendererBGFXStatic.MakeBGFXBlend(bgfx.StateFlags.BlendOne, bgfx.StateFlags.BlendInvSrcAlpha); bgfx.encoder_set_state(encoder, state, 0); bgfx.encoder_submit(encoder, viewId, sys->m_lineShader.m_prog, 0, (byte)bgfx.DiscardFlags.All); }
public static unsafe void EncodeLine(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, float3 p0, float3 p1, float4 color, float2 width, ref float4x4 objTx, ref float4x4 viewTx, ref float4x4 projTx) { float4 p0t = math.mul(projTx, math.mul(viewTx, math.mul(objTx, new float4(p0, 1)))); float4 p1t = math.mul(projTx, math.mul(viewTx, math.mul(objTx, new float4(p1, 1)))); for (int i = 0; i < 3; i++) // really only need to clip z near, but clip all to make sure clipping works { if (!ClipLinePositive(ref p0t, ref p1t, i)) { return; } if (!ClipLineNegative(ref p0t, ref p1t, i)) { return; } } SimpleVertex *buf = stackalloc SimpleVertex[4]; p0t.xyz *= 1.0f / p0t.w; p1t.xyz *= 1.0f / p1t.w; float2 dp = math.normalizesafe(p1t.xy - p0t.xy); float2 dprefl = new float2(-dp.y, dp.x); float3 dv = new float3(dprefl * width, 0); float3 du = new float3(dp * width * .5f, 0); buf[0].Position = p0t.xyz + dv - du; buf[0].Color = color; buf[0].TexCoord0 = new float2(0, 1); buf[1].Position = p0t.xyz - dv - du; buf[1].Color = color; buf[1].TexCoord0 = new float2(0, -1); buf[2].Position = p1t.xyz - dv + du; buf[2].Color = color; buf[2].TexCoord0 = new float2(1, -1); buf[3].Position = p1t.xyz + dv + du; buf[3].Color = color; buf[3].TexCoord0 = new float2(1, 1); EncodeLinePreTransformed(sys, encoder, viewId, buf, 4); }
// For uniforms and shaders setup. Does not handle vertex/index buffers private static unsafe void EncodeSimple(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ref SimpleShader simpleShader, ushort viewId, ref float4x4 tx, ref SimpleMaterialBGFX mat, byte flipCulling, uint depth) { ulong state = mat.state; if (flipCulling != 0) { state = FlipCulling(state); } bgfx.set_state(state, 0); fixed(float4x4 *p = &tx) bgfx.encoder_set_transform(encoder, p, 1); // material uniforms setup fixed(float4 *p = &mat.constAlbedo_Opacity) bgfx.encoder_set_uniform(encoder, simpleShader.m_uniformColor0, p, 1); fixed(float4 *p = &mat.mainTextureScaleTranslate) bgfx.encoder_set_uniform(encoder, simpleShader.m_uniformTexMad, p, 1); fixed(float4 *p = &mat.billboarded) bgfx.encoder_set_uniform(encoder, simpleShader.m_uniformBillboarded, p, 1); bgfx.encoder_set_texture(encoder, 0, simpleShader.m_samplerTexColor0, mat.texAlbedoOpacity, UInt32.MaxValue); bgfx.encoder_submit(encoder, viewId, simpleShader.m_prog, depth, (byte)bgfx.DiscardFlags.All); }
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 unsafe bool UpdateSimpleMaterialBGFX(RendererBGFXInstance *sys, ref SimpleMaterial mat, ref SimpleMaterialBGFX matBGFX, bool srgbColors) { // if constants changed, need to update packed value matBGFX.constAlbedo_Opacity = srgbColors ? new float4(Color.LinearToSRGB(mat.constAlbedo), mat.constOpacity) : new float4(mat.constAlbedo, mat.constOpacity); // if texture entity OR load state changed need to update texture handles // content of texture change should transparently update texture referenced by handle bool stillLoading = false; if (InitTexture(ref matBGFX.texAlbedoOpacity, mat.texAlbedoOpacity, sys->m_whiteTexture)) { stillLoading = true; } // if twoSided or hasalpha changed, 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.CullCw; } if (mat.transparent) { matBGFX.state |= RendererBGFXStatic.MakeBGFXBlend(bgfx.StateFlags.BlendOne, bgfx.StateFlags.BlendInvSrcAlpha); } else { matBGFX.state |= (ulong)bgfx.StateFlags.WriteZ; } matBGFX.mainTextureScaleTranslate = new float4(mat.scale, mat.offset); matBGFX.billboarded = new float4(mat.billboarded ? 1 : 0, 0, 0, 0); return(!stillLoading); }
public unsafe void UpdateLitMaterialBGFX(RendererBGFXInstance *sys, Entity e, bool srgbColors) { var mat = EntityManager.GetComponentData <LitMaterial>(e); var matBGFX = EntityManager.GetComponentData <LitMaterialBGFX>(e); UpdateLitMaterialBGFX(sys, ref mat, ref matBGFX, srgbColors); EntityManager.SetComponentData(e, matBGFX); }
public static unsafe MeshBGFX CreateStaticMeshFromBlobAsset(RendererBGFXInstance *inst, LitMeshRenderData mesh) { ushort * indices = (ushort *)mesh.Mesh.Value.Indices.GetUnsafePtr(); int nindices = mesh.Mesh.Value.Indices.Length; LitVertex *vertices = (LitVertex *)mesh.Mesh.Value.Vertices.GetUnsafePtr(); int nvertices = mesh.Mesh.Value.Vertices.Length; return(CreateStaticMesh(inst, indices, nindices, vertices, nvertices)); }
public static unsafe void EncodeShadowMapTransient(RendererBGFXInstance *sys, bgfx.Encoder *encoder, bgfx.TransientIndexBuffer *tib, bgfx.TransientVertexBuffer *tvb, int nvertices, int nindices, ushort viewId, ref float4x4 tx, byte flipCulling, float4 bias) { EncodeLitTransientBuffers(sys, encoder, tib, tvb, nvertices, nindices); EncodeShadowMap(sys, encoder, ref sys->m_shadowMapShader, viewId, ref tx, flipCulling, bias #if DEBUG , new float4(1) #endif ); }
public static unsafe MeshBGFX CreateStaticMesh(RendererBGFXInstance *inst, ushort *indices, int nindices, SimpleVertex *vertices, int nvertices, SkinnedMeshVertex *skinningdata = null) { bool hasSkinningData = skinningdata != null; #if ENABLE_DOTSRUNTIME_PROFILER ProfilerStats.AccumStats.memMeshCount.Accumulate(1); long bytes = nvertices * sizeof(SimpleVertex) + nindices * sizeof(ushort); if (hasSkinningData) { bytes += nvertices * sizeof(SkinnedMeshVertex); } ProfilerStats.AccumStats.memMesh.Accumulate(bytes); ProfilerStats.AccumStats.memReservedGFX.Accumulate(bytes); ProfilerStats.AccumStats.memUsedGFX.Accumulate(bytes); #endif if (hasSkinningData) { int simpleVertexSize = sizeof(SimpleVertex); int skinningVertexSize = sizeof(SkinnedMeshVertex); int totalVertexSize = simpleVertexSize + skinningVertexSize; byte *tmpBlock = (byte *)UnsafeUtility.Malloc(totalVertexSize * nvertices, 4, Allocator.Temp); UnsafeUtility.MemCpyStride(tmpBlock, totalVertexSize, vertices, simpleVertexSize, simpleVertexSize, nvertices); UnsafeUtility.MemCpyStride(tmpBlock + simpleVertexSize, totalVertexSize, skinningdata, skinningVertexSize, skinningVertexSize, nvertices); bgfx.Memory *bgfxMemory = RendererBGFXStatic.CreateMemoryBlock((byte *)tmpBlock, nvertices * totalVertexSize); UnsafeUtility.Free(tmpBlock, Allocator.Temp); return(new MeshBGFX { indexBufferHandle = bgfx.create_index_buffer(RendererBGFXStatic.CreateMemoryBlock((byte *)indices, nindices * 2), (ushort)bgfx.BufferFlags.None).idx, vertexBufferHandle = bgfx.create_vertex_buffer(bgfxMemory, &inst->m_simpleSkinnedVertexBufferDecl, (ushort)bgfx.BufferFlags.None).idx, indexCount = nindices, vertexCount = nvertices, maxIndexCount = nindices, maxVertexCount = nvertices, vertexLayoutHandle = inst->m_simpleSkinnedVertexBufferDeclHandle, isDynamic = false, vertexSize = totalVertexSize, }); } else { return(new MeshBGFX { indexBufferHandle = bgfx.create_index_buffer(RendererBGFXStatic.CreateMemoryBlock((byte *)indices, nindices * 2), (ushort)bgfx.BufferFlags.None).idx, vertexBufferHandle = bgfx.create_vertex_buffer(RendererBGFXStatic.CreateMemoryBlock((byte *)vertices, nvertices * sizeof(SimpleVertex)), &inst->m_simpleVertexBufferDecl, (ushort)bgfx.BufferFlags.None).idx, indexCount = nindices, vertexCount = nvertices, maxIndexCount = nindices, maxVertexCount = nvertices, vertexLayoutHandle = inst->m_simpleVertexBufferDeclHandle, isDynamic = false, vertexSize = sizeof(SimpleVertex), }); } }
public static unsafe void EncodeDebugTangents(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, float2 width, float length, ref LitMeshRenderData mesh, ref float4x4 objTx, ref float4x4 viewTx, ref float4x4 projTx) { int nv = (int)mesh.Mesh.Value.Vertices.Length; LitVertex *vertices = (LitVertex *)mesh.Mesh.Value.Vertices.GetUnsafePtr(); for (int i = 0; i < nv; i++) { EncodeLine(sys, encoder, viewId, vertices[i].Position, vertices[i].Position + vertices[i].Normal * length, new float4(0, 0, 1, 1), width, ref objTx, ref viewTx, ref projTx); EncodeLine(sys, encoder, viewId, vertices[i].Position, vertices[i].Position + vertices[i].Tangent * length, new float4(1, 0, 0, 1), width, ref objTx, ref viewTx, ref projTx); } }
// ---------------- 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 MeshBGFX CreateStaticSkinnedMeshFromBlobAsset(RendererBGFXInstance *inst, LitMeshRenderData meshData, SkinnedMeshRenderData skinnedData) { ushort * indices = (ushort *)meshData.Mesh.Value.Indices.GetUnsafePtr(); int nindices = meshData.Mesh.Value.Indices.Length; LitVertex * vertices = (LitVertex *)meshData.Mesh.Value.Vertices.GetUnsafePtr(); int nvertices = meshData.Mesh.Value.Vertices.Length; SkinnedMeshVertex *skinningdata = (SkinnedMeshVertex *)skinnedData.SkinnedMeshDataRef.Value.Vertices.GetUnsafePtr(); return(CreateStaticMesh(inst, indices, nindices, vertices, nvertices, skinningdata)); }
public static unsafe void EncodeSimpleSkinnedmesh(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, ref MeshBGFX mesh, ref float4x4 tx, ref SimpleMaterialBGFX mat, int startIndex, int indexCount, byte flipCulling, uint depth, float4x4[] boneMatrices) { mesh.SetForSubmit(encoder, startIndex, indexCount); fixed(float4x4 *p = boneMatrices) { bgfx.encoder_set_uniform(encoder, sys->m_simpleSkinnedMeshShader.m_uniformBoneMatrices, p, (ushort)boneMatrices.Length); } EncodeSimple(sys, encoder, ref sys->m_simpleSkinnedMeshShader.m_simpleShader, viewId, ref tx, ref mat, flipCulling, depth); }
public static unsafe void EncodeShadowMapSkinnedMesh(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, ref MeshBGFX mesh, ref float4x4 tx, int startIndex, int indexCount, byte flipCulling, float4 bias, float4x4[] boneMatrices) { mesh.SetForSubmit(encoder, startIndex, indexCount); fixed(float4x4 *p = boneMatrices) { bgfx.encoder_set_uniform(encoder, sys->m_skinnedMeshShadowMapShader.m_uniformBoneMatrices, p, (ushort)boneMatrices.Length); } EncodeShadowMap(sys, encoder, ref sys->m_skinnedMeshShadowMapShader.m_shadowMapShader, viewId, ref tx, flipCulling, bias); }
protected override void OnStopRunning() { m_TextShader.Destroy(); m_TextSDFShader.Destroy(); m_BGFXSystem = null; m_BGFXInstance = null; #if UNITY_ANDROID PlatformEvents.OnSuspendResume -= OnSuspendResume; #endif base.OnStopRunning(); }
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 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; } InitShader(ref matBGFX.shaderProgram, mat.shader, sys->m_litShader.m_prog); 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); }
// ---------------- shadow map ---------------------------------------------------------------------------------------------------------------------- public static unsafe void EncodeShadowMapMesh(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, ref MeshBGFX mesh, ref float4x4 tx, int startIndex, int indexCount, byte flipCulling, float4 bias) { #if DEBUG float4 cd = GetShadowDebugColor(mesh.DebugIndex() + startIndex); #endif mesh.SetForSubmit(encoder, startIndex, indexCount); EncodeShadowMap(sys, encoder, ref sys->m_shadowMapShader, viewId, ref tx, flipCulling, bias #if DEBUG , cd #endif ); }
private void EncodeBox(RendererBGFXInstance *sys, bgfx.Encoder *encoder, Entity ePass, ref float4x4 tx, float3 cMin, float3 cMax, float width, float4 color) { var pass = EntityManager.GetComponentData <RenderPass>(ePass); float4x4 adjustedProjection = sys->GetAdjustedProjection(ref pass); float2 normWidth = new float2(width / pass.viewport.w, width / pass.viewport.h); for (int j = 0; j < Culling.EdgeTable.Length; j++) { float3 p0 = Culling.SelectCoordsMinMax(cMin, cMax, Culling.EdgeTable[j] & 7); float3 p1 = Culling.SelectCoordsMinMax(cMin, cMax, Culling.EdgeTable[j] >> 3); SubmitHelper.EncodeLine(sys, encoder, pass.viewId, p0, p1, color, normWidth, ref tx, ref pass.viewTransform, ref adjustedProjection); } }
public static unsafe bool SubmitTransientAlloc(RendererBGFXInstance *sys, bgfx.TransientIndexBuffer *tib, bgfx.TransientVertexBuffer *tvb, int nvertices, int nindices, bgfx.VertexLayout *layout) { if (!bgfx.alloc_transient_buffers(tvb, layout, (uint)nvertices, tib, (uint)nindices)) { #if DEBUG // TODO: throw or ignore draw? throw new InvalidOperationException("Out of transient bgfx memory!"); #else RenderDebug.LogFormat("Warning: Out of transient bgfx memory! Skipping draw call."); return(false); #endif } return(true); }
// For uniforms and shaders setup. Does not handle vertex/index buffers private unsafe static void EncodeZOnly(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, ref float4x4 tx, byte flipCulling) { ulong state = (ulong)(bgfx.StateFlags.WriteZ | bgfx.StateFlags.DepthTestLess | bgfx.StateFlags.CullCcw); if (flipCulling != 0) { state = FlipCulling(state); } bgfx.encoder_set_state(encoder, state, 0); unsafe { fixed(float4x4 *p = &tx) bgfx.encoder_set_transform(encoder, p, 1); } float4 color = new float4(1, 0, 0, 1); bgfx.encoder_set_uniform(encoder, sys->m_zOnlyShader.m_uniformDebugColor, &color, 1); bgfx.encoder_submit(encoder, viewId, sys->m_zOnlyShader.m_prog, 0, (byte)bgfx.DiscardFlags.All); }
public ulong state; // includes blending and culling! internal unsafe bool Update(EntityManager em, RendererBGFXInstance *sys, ref BitmapFontMaterial mat) { constClipRect = mat.ConstClipRect; constMaskSoftness = mat.ConstMaskSoftness; // if texture entity OR load state changed need to update texture handles // content of texture change should transparently update texture referenced by handle bool stillLoading = UpdateTextMaterialsSystem.InitTexture(em, ref texAtlas, mat.AtlasTexture, sys->m_whiteTexture); // text is always two-sided and transparent state = (ulong)(bgfx.StateFlags.WriteRgb | bgfx.StateFlags.WriteA | bgfx.StateFlags.DepthTestLess); state |= RendererBGFXStatic.MakeBGFXBlend(bgfx.StateFlags.BlendSrcAlpha, bgfx.StateFlags.BlendInvSrcAlpha); return(!stillLoading); }
static private void Circle(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, float3 org, float3 du, float3 dv, float r, int n, float4 color, float2 normWidth, ref float4x4 tx, ref float4x4 txView, ref float4x4 txProj) { float3 pprev = org + dv * r; for (int i = 1; i <= n; i++) { float a = ((float)i / (float)n) * math.PI * 2.0f; float u = math.sin(a) * r; float v = math.cos(a) * r; float3 p = org + du * u + dv * v; SubmitHelper.EncodeLine(sys, encoder, viewId, pprev, p, color, normWidth, ref tx, ref txView, ref txProj); pprev = p; } }
// For uniforms and shaders setup. Does not handle vertex/index buffers private static unsafe void EncodeShadowMap(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ref ShadowMapShader shadowMapShader, ushort viewId, ref float4x4 tx, byte flipCulling, float4 bias) { ulong state = (ulong)(bgfx.StateFlags.WriteZ | bgfx.StateFlags.DepthTestLess | bgfx.StateFlags.CullCcw); if (flipCulling != 0) { state = FlipCulling(state); } #if DEBUG state |= (ulong)bgfx.StateFlags.WriteRgb | (ulong)bgfx.StateFlags.WriteA; float4 c = new float4(1); bgfx.encoder_set_uniform(encoder, shadowMapShader.m_uniformDebugColor, &c, 1); #endif bgfx.encoder_set_state(encoder, state, 0); unsafe { fixed(float4x4 *p = &tx) bgfx.encoder_set_transform(encoder, p, 1); } bgfx.encoder_set_uniform(encoder, shadowMapShader.m_uniformBias, &bias, 1); bgfx.encoder_submit(encoder, viewId, shadowMapShader.m_prog, 0, (byte)bgfx.DiscardFlags.All); }
public static unsafe MeshBGFX CreateDynamicMeshSimple(RendererBGFXInstance *inst, int maxVertices, int maxIndices, bool hasSkinningData) { Assert.IsTrue(maxVertices <= 0x10000 && maxVertices > 0 && maxIndices > 0 && maxIndices <= 0xf0000); #if ENABLE_DOTSRUNTIME_PROFILER ProfilerStats.AccumStats.memMeshCount.Accumulate(1); long bytes = maxVertices * sizeof(SimpleVertex) + maxIndices * sizeof(ushort); if (hasSkinningData) { bytes += maxVertices * sizeof(SkinnedMeshVertex); } ProfilerStats.AccumStats.memMesh.Accumulate(bytes); ProfilerStats.AccumStats.memReservedGFX.Accumulate(bytes); #endif if (hasSkinningData) { return(new MeshBGFX { maxVertexCount = maxVertices, maxIndexCount = maxIndices, vertexCount = 0, indexCount = 0, vertexBufferHandle = bgfx.create_dynamic_vertex_buffer((uint)maxVertices, &inst->m_simpleSkinnedVertexBufferDecl, (ushort)bgfx.BufferFlags.None).idx, indexBufferHandle = bgfx.create_dynamic_index_buffer((uint)maxIndices, (ushort)bgfx.BufferFlags.None).idx, vertexLayoutHandle = inst->m_simpleSkinnedVertexBufferDeclHandle, isDynamic = true, vertexSize = sizeof(SimpleVertex) + sizeof(SkinnedMeshVertex), }); } else { return(new MeshBGFX { maxVertexCount = maxVertices, maxIndexCount = maxIndices, vertexCount = 0, indexCount = 0, vertexBufferHandle = bgfx.create_dynamic_vertex_buffer((uint)maxVertices, &inst->m_simpleVertexBufferDecl, (ushort)bgfx.BufferFlags.None).idx, indexBufferHandle = bgfx.create_dynamic_index_buffer((uint)maxIndices, (ushort)bgfx.BufferFlags.None).idx, vertexLayoutHandle = inst->m_simpleVertexBufferDeclHandle, isDynamic = true, vertexSize = sizeof(SimpleVertex), }); } }
bool EnsureInitialized() { // early-out assumption that if we initialized the shader, we're good to go if (m_TextShader.Initialized) { return(true); } if (!m_BGFXSystem.IsInitialized()) { return(false); } m_BGFXInstance = m_BGFXSystem.InstancePointer(); // TODO -- need a better way to find a shader given a guid int foundShaders = 0; Entities.ForEach((ref BuiltInShader builtInShader, ref ShaderBinData shaders) => { foundShaders++; if (builtInShader.Guid == BitmapFontMaterial.ShaderGuid) { m_TextShader.Init(BGFXShaderHelper.GetPrecompiledShaderData(m_BGFXInstance->m_rendererType, shaders, ref builtInShader.Name)); } else if (builtInShader.Guid == SDFFontMaterial.ShaderGuid) { m_TextSDFShader.Init(BGFXShaderHelper.GetPrecompiledShaderData(m_BGFXInstance->m_rendererType, shaders, ref builtInShader.Name)); } else { foundShaders--; } }); // must have the shader if (foundShaders != 2) { throw new Exception("Couldn't find all needed Text precompiled shaders"); } return(true); }
public static unsafe MeshBGFX CreateStaticMesh(RendererBGFXInstance *inst, ushort *indices, int nindices, SimpleVertex *vertices, int nvertices) { #if ENABLE_DOTSRUNTIME_PROFILER ProfilerStats.AccumStats.memMeshCount.Accumulate(1); long bytes = nvertices * sizeof(SimpleVertex) + nindices * sizeof(ushort); ProfilerStats.AccumStats.memMesh.Accumulate(bytes); ProfilerStats.AccumStats.memReservedGFX.Accumulate(bytes); ProfilerStats.AccumStats.memUsedGFX.Accumulate(bytes); #endif return(new MeshBGFX { indexBufferHandle = bgfx.create_index_buffer(RendererBGFXStatic.CreateMemoryBlock((byte *)indices, nindices * 2), (ushort)bgfx.BufferFlags.None).idx, vertexBufferHandle = bgfx.create_vertex_buffer(RendererBGFXStatic.CreateMemoryBlock((byte *)vertices, nvertices * sizeof(SimpleVertex)), &inst->m_simpleVertexBufferDecl, (ushort)bgfx.BufferFlags.None).idx, indexCount = nindices, vertexCount = nvertices, maxIndexCount = nindices, maxVertexCount = nvertices, vertexLayoutHandle = inst->m_simpleVertexBufferDeclHandle, isDynamic = false, vertexSize = sizeof(SimpleVertex), }); }
public static unsafe void SubmitBlitDirectExtended(RendererBGFXInstance *sys, ushort viewId, ref float4x4 tx, bgfx.TextureHandle tetxure, bool fromSRGB, bool toSRGB, float reinhard, float4 mulColor, float4 addColor, bool premultiply) { unsafe { bgfx.Encoder *encoder = bgfx.encoder_begin(false); bgfx.encoder_set_state(encoder, (uint)(bgfx.StateFlags.WriteRgb | bgfx.StateFlags.WriteA), 0); fixed(float4x4 *p = &tx) bgfx.encoder_set_transform(encoder, p, 1); sys->m_quadMesh.SetForSubmit(encoder, 0, 6); // material uniforms setup bgfx.encoder_set_uniform(encoder, sys->m_blitShader.m_colormul, &mulColor, 1); bgfx.encoder_set_uniform(encoder, sys->m_blitShader.m_coloradd, &addColor, 1); float4 noTexMad = new float4(1, 1, 0, 0); bgfx.encoder_set_uniform(encoder, sys->m_blitShader.m_uniformTexMad, &noTexMad, 1); bgfx.encoder_set_texture(encoder, 0, sys->m_blitShader.m_samplerTexColor0, tetxure, UInt32.MaxValue); float4 s = new float4(fromSRGB ? 1.0f : 0.0f, toSRGB ? 1.0f : 0.0f, reinhard, premultiply ? 1.0f : 0.0f); bgfx.encoder_set_uniform(encoder, sys->m_blitShader.m_decodeSRGB_encodeSRGB_reinhard_premultiply, &s, 1); // submit bgfx.encoder_submit(encoder, viewId, sys->m_blitShader.m_prog, 0, (byte)bgfx.DiscardFlags.All); bgfx.encoder_end(encoder); } }
// ---------------- blit ---------------------------------------------------------------------------------------------------------------------- public static unsafe void SubmitBlitDirectFast(RendererBGFXInstance *sys, ushort viewId, ref float4x4 tx, float4 color, bgfx.TextureHandle tetxure) { unsafe { bgfx.Encoder *encoder = bgfx.encoder_begin(false); bgfx.set_state((uint)(bgfx.StateFlags.WriteRgb | bgfx.StateFlags.WriteA), 0); fixed(float4x4 *p = &tx) bgfx.encoder_set_transform(encoder, p, 1); sys->m_quadMesh.SetForSubmit(encoder, 0, 6); // material uniforms setup bgfx.encoder_set_uniform(encoder, sys->m_simpleShader.m_uniformColor0, &color, 1); float4 noTexMad = new float4(1, 1, 0, 0); bgfx.encoder_set_uniform(encoder, sys->m_simpleShader.m_uniformTexMad, &noTexMad, 1); bgfx.encoder_set_texture(encoder, 0, sys->m_simpleShader.m_samplerTexColor0, tetxure, UInt32.MaxValue); fixed(float4 *p = &float4.zero) bgfx.encoder_set_uniform(encoder, sys->m_simpleShader.m_uniformBillboarded, p, 1); // submit bgfx.encoder_submit(encoder, viewId, sys->m_simpleShader.m_prog, 0, (byte)bgfx.DiscardFlags.All); bgfx.encoder_end(encoder); } }
private void AddMappedLightFromEntity(Entity e, ref LightingBGFX r, bool srgbColors, RendererBGFXInstance *sys) { if (e == Entity.Null) return; }