Skip to content

Commit cc785e7

Browse files
committed
quicker code
1 parent 63ba1de commit cc785e7

1 file changed

Lines changed: 21 additions & 26 deletions

File tree

source/VulkanRenderer.cs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class VulkanRenderer : RenderingMachine
5050
private readonly int meshType;
5151
private readonly int worldRendererScissorType;
5252
private readonly int textureBindingType;
53+
private readonly int shaderVertexInputAttributeType;
5354

5455
private Array<ImageView> surfaceImageViews;
5556
private Array<Framebuffer> swapChainFramebuffers;
@@ -110,6 +111,7 @@ public class VulkanRenderer : RenderingMachine
110111
meshType = schema.GetComponentType<IsMesh>();
111112
worldRendererScissorType = schema.GetComponentType<WorldRendererScissor>();
112113
textureBindingType = schema.GetArrayType<TextureBinding>();
114+
shaderVertexInputAttributeType = schema.GetArrayType<ShaderVertexInputAttribute>();
113115
}
114116

115117
/// <summary>
@@ -364,11 +366,11 @@ private CompiledMesh CompileMesh(uint meshEntity, uint vertexShaderEntity)
364366
{
365367
IsMesh mesh = world.GetComponent<IsMesh>(meshEntity, meshType);
366368
int vertexCount = mesh.vertexCount;
367-
Values<ShaderVertexInputAttribute> shaderVertexAttributes = world.GetArray<ShaderVertexInputAttribute>(vertexShaderEntity);
369+
Span<ShaderVertexInputAttribute> shaderVertexAttributes = world.GetArray<ShaderVertexInputAttribute>(vertexShaderEntity, shaderVertexInputAttributeType);
368370
Span<MeshChannel> channels = stackalloc MeshChannel[shaderVertexAttributes.Length];
369371
for (int i = 0; i < shaderVertexAttributes.Length; i++)
370372
{
371-
ref ShaderVertexInputAttribute vertexAttribute = ref shaderVertexAttributes[i];
373+
ShaderVertexInputAttribute vertexAttribute = shaderVertexAttributes[i];
372374
if (vertexAttribute.TryDeduceMeshChannel(out MeshChannel channel))
373375
{
374376
if (!mesh.channels.Contains(channel))
@@ -938,9 +940,14 @@ void TryWait(LogicalDevice logicalDevice)
938940
//build and render everything in one go
939941
Span<IsTexture> textureComponentsSpan = textureComponents.AsSpan();
940942
ref CommandBuffer commandBuffer = ref commandBuffers[currentFrame];
943+
Span<CompiledRenderer> renderersSpan = renderers.AsSpan();
944+
Span<Vector4> scissorsSpan = scissors.AsSpan();
945+
ReadOnlySpan<Slot> slots = world.Slots;
941946
for (int i = 0; i < renderEntities.Length; i++)
942947
{
943948
RenderEntity renderEntity = renderEntities[i];
949+
uint entity = renderEntity.entity;
950+
Slot slot = slots[(int)entity];
944951

945952
//deal with missing or outdated shaders
946953
uint vertexShaderEntity = renderEntity.vertexShaderEntity;
@@ -1023,8 +1030,7 @@ void TryWait(LogicalDevice logicalDevice)
10231030
}
10241031

10251032
//deal with missing or outdated renderers
1026-
uint entity = renderEntity.entity;
1027-
ref CompiledRenderer compiledRenderer = ref renderers[(int)entity];
1033+
ref CompiledRenderer compiledRenderer = ref renderersSpan[(int)entity];
10281034
if (containsPipeline && compiledRenderer != default)
10291035
{
10301036
if (textureBindingsChanged || shaderVersionChanged)
@@ -1046,37 +1052,26 @@ void TryWait(LogicalDevice logicalDevice)
10461052
commandBuffer.BindVertexBuffer(compiledMesh.vertexBuffer);
10471053
commandBuffer.BindIndexBuffer(compiledMesh.indexBuffer);
10481054

1055+
//apply scissor
1056+
commandBuffer.SetScissor(scissorsSpan[(int)entity]);
1057+
1058+
//push constants
10491059
if (knownPushConstants.TryGetValue(materialEntity, out Array<CompiledPushConstant> pushConstants))
10501060
{
1051-
//apply scissor
1052-
ref Vector4 scissor = ref scissors[(int)entity];
1053-
commandBuffer.SetScissor(scissor);
1054-
1055-
//push constants
10561061
int pushOffset = 0;
1057-
for (int p = 0; p < pushConstants.Length; p++)
1062+
Span<CompiledPushConstant> pushConstantsSpan = pushConstants.AsSpan();
1063+
for (int p = 0; p < pushConstantsSpan.Length; p++)
10581064
{
1059-
ref CompiledPushConstant pushConstant = ref pushConstants[p];
1065+
ref CompiledPushConstant pushConstant = ref pushConstantsSpan[p];
10601066
DataType componentType = pushConstant.componentType;
1061-
MemoryAddress component = world.GetComponent(entity, componentType.index, out int componentSize);
1067+
MemoryAddress component = slot.GetComponent(componentType.index);
10621068
commandBuffer.PushConstants(compiledPipeline.pipelineLayout, pushConstant.stageFlags, component, pushConstant.componentType.size, (uint)pushOffset);
1063-
pushOffset += componentSize;
1069+
pushOffset += pushConstant.componentType.size;
10641070
}
1065-
1066-
ref CompiledRenderer renderer = ref renderers[(int)entity];
1067-
commandBuffer.BindDescriptorSet(compiledPipeline.pipelineLayout, renderer.descriptorSet);
1068-
commandBuffer.DrawIndexed(compiledMesh.indexCount, 1, 0, 0, 0);
10691071
}
1070-
else
1071-
{
1072-
//apply scissor
1073-
ref Vector4 scissor = ref scissors[(int)entity];
1074-
commandBuffer.SetScissor(scissor);
10751072

1076-
ref CompiledRenderer renderer = ref renderers[(int)entity];
1077-
commandBuffer.BindDescriptorSet(compiledPipeline.pipelineLayout, renderer.descriptorSet);
1078-
commandBuffer.DrawIndexed(compiledMesh.indexCount, 1, 0, 0, 0);
1079-
}
1073+
commandBuffer.BindDescriptorSet(compiledPipeline.pipelineLayout, compiledRenderer.descriptorSet);
1074+
commandBuffer.DrawIndexed(compiledMesh.indexCount, 1, 0, 0, 0);
10801075

10811076
previouslyRenderedEntities.Add(entity);
10821077
previouslyRenderedGroups.TryAdd(new(materialEntity, meshEntity, vertexShaderEntity, fragmentShaderEntity));

0 commit comments

Comments
 (0)