Implement original gadget panel and playable shrink ray
This commit is contained in:
1 parent
0272f7bef7
commit
7b5230ceb8
57 files changed
+3545
-48
No files matched your search
@@ -29,7 +29,7 @@ public sealed unsafe class VulkanRenderer : IDisposable
|
||||
private ImageView colorView,depthView;
|
||||
private RenderPass renderPass;
|
||||
private Framebuffer framebuffer;
|
||||
private Pipeline pipeline,overlayPipeline;
|
||||
private Pipeline pipeline,overlayPipeline,alphaPipeline,additivePipeline,overlayAlphaPipeline,overlayAdditivePipeline;
|
||||
private PipelineLayout pipelineLayout;
|
||||
private DescriptorSetLayout descriptorLayout;
|
||||
private DescriptorPool descriptorPool;
|
||||
@@ -130,6 +130,7 @@ public sealed unsafe class VulkanRenderer : IDisposable
|
||||
throw new ArgumentException("Scene requires nonempty, bounded triangle geometry.",nameof(mesh));
|
||||
if(mesh.Draws.Any(d=>(ulong)d.FirstIndex+d.IndexCount>(ulong)mesh.Indices.Length))
|
||||
throw new ArgumentException("Scene draw exceeds index buffer.",nameof(mesh));
|
||||
ValidateDrawMaterials(mesh.Draws);
|
||||
ValidateTexture(fallback);
|
||||
foreach(var image in mesh.Textures.Values)ValidateTexture(image);
|
||||
|
||||
@@ -142,6 +143,17 @@ public sealed unsafe class VulkanRenderer : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
private static void ValidateDrawMaterials(MeshDraw[] draws)
|
||||
{
|
||||
foreach(var draw in draws)
|
||||
{
|
||||
if(!float.IsFinite(draw.Opacity) || draw.Opacity<0 || draw.Opacity>1)
|
||||
throw new ArgumentException("Draw opacity must be finite and between zero and one.",nameof(draws));
|
||||
if(draw.Blend is not (MeshBlend.Opaque or MeshBlend.Alpha or MeshBlend.Additive))
|
||||
throw new ArgumentException("Draw blend mode is invalid.",nameof(draws));
|
||||
}
|
||||
}
|
||||
|
||||
private SceneResources DetachScene()
|
||||
{
|
||||
var scene=new SceneResources(mesh,vertexBuffer,vertexMemory,indexBuffer,indexMemory,
|
||||
@@ -425,7 +437,7 @@ public sealed unsafe class VulkanRenderer : IDisposable
|
||||
var vertex=Shader("mesh.vert.spv");var fragment=Shader("mesh.frag.spv");var main=Marshal.StringToCoTaskMemUTF8("main");
|
||||
try
|
||||
{
|
||||
var push=new PushConstantRange(ShaderStageFlags.VertexBit,0,64);var dl=descriptorLayout;
|
||||
var push=new PushConstantRange(ShaderStageFlags.VertexBit|ShaderStageFlags.FragmentBit,0,68);var dl=descriptorLayout;
|
||||
var layout=new PipelineLayoutCreateInfo{SType=StructureType.PipelineLayoutCreateInfo,SetLayoutCount=1,PSetLayouts=&dl,PushConstantRangeCount=1,PPushConstantRanges=&push};
|
||||
Check(vk.CreatePipelineLayout(device,&layout,null,out pipelineLayout),"CreatePipelineLayout");
|
||||
var stages=stackalloc PipelineShaderStageCreateInfo[2];
|
||||
@@ -450,6 +462,23 @@ public sealed unsafe class VulkanRenderer : IDisposable
|
||||
Check(vk.CreateGraphicsPipelines(device,default,1,&info,null,out pipeline),"CreateGraphicsPipeline");
|
||||
ds.DepthTestEnable=false;ds.DepthWriteEnable=false;
|
||||
Check(vk.CreateGraphicsPipelines(device,default,1,&info,null,out overlayPipeline),"CreateOverlayPipeline");
|
||||
// Blended world geometry tests opaque depth but does not write it.
|
||||
// Preserve draw order; general transparent-surface sorting is separate work.
|
||||
blendAttachment.BlendEnable=true;
|
||||
blendAttachment.SrcColorBlendFactor=BlendFactor.SrcAlpha;
|
||||
blendAttachment.DstColorBlendFactor=BlendFactor.OneMinusSrcAlpha;
|
||||
blendAttachment.ColorBlendOp=BlendOp.Add;
|
||||
blendAttachment.SrcAlphaBlendFactor=BlendFactor.SrcAlpha;
|
||||
blendAttachment.DstAlphaBlendFactor=BlendFactor.OneMinusSrcAlpha;
|
||||
blendAttachment.AlphaBlendOp=BlendOp.Add;
|
||||
Check(vk.CreateGraphicsPipelines(device,default,1,&info,null,out overlayAlphaPipeline),"CreateOverlayAlphaPipeline");
|
||||
ds.DepthTestEnable=true;
|
||||
Check(vk.CreateGraphicsPipelines(device,default,1,&info,null,out alphaPipeline),"CreateAlphaPipeline");
|
||||
blendAttachment.DstColorBlendFactor=BlendFactor.One;
|
||||
blendAttachment.DstAlphaBlendFactor=BlendFactor.One;
|
||||
Check(vk.CreateGraphicsPipelines(device,default,1,&info,null,out additivePipeline),"CreateAdditivePipeline");
|
||||
ds.DepthTestEnable=false;
|
||||
Check(vk.CreateGraphicsPipelines(device,default,1,&info,null,out overlayAdditivePipeline),"CreateOverlayAdditivePipeline");
|
||||
}
|
||||
finally{Marshal.FreeCoTaskMem(main);vk.DestroyShaderModule(device,vertex,null);vk.DestroyShaderModule(device,fragment,null);}
|
||||
}
|
||||
@@ -502,6 +531,9 @@ public sealed unsafe class VulkanRenderer : IDisposable
|
||||
}
|
||||
public void Render(Matrix4x4 mvp)
|
||||
{
|
||||
// Draw materials are mutable between frames. Reject invalid state before
|
||||
// acquiring an image or beginning a command buffer, even on hidden draws.
|
||||
ValidateDrawMaterials(mesh.Draws);
|
||||
uint imageIndex=0;
|
||||
if(window!=0)
|
||||
{
|
||||
@@ -517,14 +549,30 @@ public sealed unsafe class VulkanRenderer : IDisposable
|
||||
vk.CmdSetViewport(command,0,1,&viewport);vk.CmdSetScissor(command,0,1,&scissor);
|
||||
var vb=vertexBuffer;ulong offset=0;vk.CmdBindVertexBuffers(command,0,1,&vb,&offset);vk.CmdBindIndexBuffer(command,indexBuffer,0,IndexType.Uint32);
|
||||
var draws=mesh.HasExplicitDraws?mesh.Draws:[new MeshDraw(0,(uint)mesh.Indices.Length,-1)];
|
||||
foreach(var screenSpace in new[]{false,true})
|
||||
// Opaque world first, blended world second, UI last. Within each pass,
|
||||
// retain source order so existing UI composition remains unchanged.
|
||||
for(var pass=0;pass<3;pass++)
|
||||
{
|
||||
vk.CmdBindPipeline(command,PipelineBindPoint.Graphics,screenSpace?overlayPipeline:pipeline);
|
||||
var screenSpace=pass==2;
|
||||
var transform=screenSpace?Matrix4x4.Identity:mvp;
|
||||
vk.CmdPushConstants(command,pipelineLayout,ShaderStageFlags.VertexBit,0,64,&transform);
|
||||
var stages=ShaderStageFlags.VertexBit|ShaderStageFlags.FragmentBit;
|
||||
vk.CmdPushConstants(command,pipelineLayout,stages,0,64,&transform);
|
||||
foreach(var draw in draws)
|
||||
{
|
||||
if (!draw.Visible || draw.ScreenSpace!=screenSpace) continue;
|
||||
if(!screenSpace && (draw.Blend==MeshBlend.Opaque)!=(pass==0))continue;
|
||||
var selectedPipeline=(screenSpace,draw.Blend) switch
|
||||
{
|
||||
(false,MeshBlend.Alpha)=>alphaPipeline,
|
||||
(false,MeshBlend.Additive)=>additivePipeline,
|
||||
(true,MeshBlend.Alpha)=>overlayAlphaPipeline,
|
||||
(true,MeshBlend.Additive)=>overlayAdditivePipeline,
|
||||
(true,_)=>overlayPipeline,
|
||||
_=>pipeline
|
||||
};
|
||||
vk.CmdBindPipeline(command,PipelineBindPoint.Graphics,selectedPipeline);
|
||||
var opacity=draw.Opacity;
|
||||
vk.CmdPushConstants(command,pipelineLayout,stages,64,4,&opacity);
|
||||
var descriptor=descriptors.GetValueOrDefault(draw.TextureId,descriptors[-1]);
|
||||
vk.CmdBindDescriptorSets(command,PipelineBindPoint.Graphics,pipelineLayout,0,1,&descriptor,0,null);
|
||||
vk.CmdDrawIndexed(command,draw.IndexCount,1,draw.FirstIndex,0,0);
|
||||
@@ -565,6 +613,10 @@ public sealed unsafe class VulkanRenderer : IDisposable
|
||||
DestroyTarget();
|
||||
if(pipeline.Handle!=0)vk.DestroyPipeline(device,pipeline,null);
|
||||
if(overlayPipeline.Handle!=0)vk.DestroyPipeline(device,overlayPipeline,null);
|
||||
if(alphaPipeline.Handle!=0)vk.DestroyPipeline(device,alphaPipeline,null);
|
||||
if(additivePipeline.Handle!=0)vk.DestroyPipeline(device,additivePipeline,null);
|
||||
if(overlayAlphaPipeline.Handle!=0)vk.DestroyPipeline(device,overlayAlphaPipeline,null);
|
||||
if(overlayAdditivePipeline.Handle!=0)vk.DestroyPipeline(device,overlayAdditivePipeline,null);
|
||||
if(pipelineLayout.Handle!=0)vk.DestroyPipelineLayout(device,pipelineLayout,null);
|
||||
if(renderPass.Handle!=0)vk.DestroyRenderPass(device,renderPass,null);
|
||||
DestroySceneResources();
|
||||
|
||||
Reference in new issue
Block a user