public ChunkBuffer() { //Setup all buffer allocators VertexBuffers = new UniformBuffer(false); BufferAllocators = new BufferAllocator[Sizes.Length]; unsafe { long *buf = (long *)VertexBuffers.Update(); for (int i = 0; i < Sizes.Length; i++) { BufferAllocators[i] = new BufferAllocator(Sizes[i], Counts[i], false, PixelInternalFormat.Rgba8ui); buf[i * 2] = BufferAllocators[i].BufferTex.View.GetTextureHandle().SetResidency(Residency.Resident); } VertexBuffers.UpdateDone(); } }
//Deferred Renderer Rework - New GBuffer - //32:32:32 WorldPos, (16b:SPARE, 16b:materialIdx) //8:8:8 Norm, 8:SPARE //reproject z buffer and recompute chain for culling public DeferredRenderer(Framebuffer[] fbufs, LightManager man) { lMan = man; InfoBindings = new TextureBinding[fbufs.Length]; InfoBindings2 = new TextureBinding[fbufs.Length]; DepthBindings = new TextureBinding[fbufs.Length]; Framebuffers = new Framebuffer[fbufs.Length]; HiZMap = new TextureView[fbufs.Length][]; views = new ViewData[fbufs.Length]; HiZMapUBO = new UniformBuffer(false); unsafe { int off = 0; float *fp = (float *)HiZMapUBO.Update(); fp += 4; for (int i = 0; i < views.Length; i++) { views[i] = new ViewData() { depthBuf = new Texture() { Width = fbufs[i].Width, Height = fbufs[i].Height, Depth = 1, Format = PixelInternalFormat.DepthComponent32f, GenerateMipmaps = false, LayerCount = 1, LevelCount = 1, Target = TextureTarget.Texture2D }.Build(), infoTex = new Texture() { Width = fbufs[i].Width, Height = fbufs[i].Height, Depth = 1, Format = PixelInternalFormat.Rgba32f, GenerateMipmaps = false, LayerCount = 1, LevelCount = 1, Target = TextureTarget.Texture2D }.Build(), infoTex2 = new Texture() { Width = fbufs[i].Width, Height = fbufs[i].Height, Depth = 1, Format = PixelInternalFormat.Rgba8, GenerateMipmaps = false, LayerCount = 1, LevelCount = 1, Target = TextureTarget.Texture2D }.Build(), hiZ = new Texture() { Width = fbufs[i].Width, Height = fbufs[i].Height, Depth = 1, Format = PixelInternalFormat.Rg32f, GenerateMipmaps = false, LayerCount = 1, LevelCount = (int)(MathHelper.Log2((ulong)Math.Max(fbufs[i].Width, fbufs[i].Height)) + 1), Target = TextureTarget.Texture2D }.Build(), gbuffer = new Framebuffer(fbufs[i].Width, fbufs[i].Height), }; views[i].depthView = new TextureView() { BaseLayer = 0, BaseLevel = 0, Format = PixelInternalFormat.DepthComponent32f, LayerCount = 1, LevelCount = 1, Target = TextureTarget.Texture2D }.Build(views[i].depthBuf); views[i].infoView = new TextureView() { BaseLayer = 0, BaseLevel = 0, Format = PixelInternalFormat.Rgba32f, LayerCount = 1, LevelCount = 1, Target = TextureTarget.Texture2D }.Build(views[i].infoTex); views[i].infoView2 = new TextureView() { BaseLayer = 0, BaseLevel = 0, Format = PixelInternalFormat.Rgba8, LayerCount = 1, LevelCount = 1, Target = TextureTarget.Texture2D }.Build(views[i].infoTex2); views[i].hiZView = new TextureView[views[i].hiZ.LevelCount]; for (int j = 0; j < views[i].hiZView.Length; j++) { views[i].hiZView[j] = new TextureView() { BaseLayer = 0, BaseLevel = j, Format = PixelInternalFormat.Rg32f, LayerCount = 1, LevelCount = 1, Target = TextureTarget.Texture2D, }.Build(views[i].hiZ); var f_arr = (float[])views[i].hiZView[j].GetImageHandle().SetResidency(Residency.Resident, AccessMode.ReadWrite); for (int q = 0; q < f_arr.Length; q++) { *(fp++) = f_arr[q]; } fp += 2; } views[i].hiZTex = new TextureView() { BaseLayer = 0, BaseLevel = 0, Format = PixelInternalFormat.Rg32f, LayerCount = 1, LevelCount = views[i].hiZView.Length, Target = TextureTarget.Texture2D }.Build(views[i].hiZ); var sampler = new TextureSampler(); sampler.SetEnableLinearFilter(false, true, false); sampler.MinReadLevel = 0; sampler.MaxReadLevel = views[i].hiZView.Length; sampler.SetTileMode(TileMode.ClampToBorder, TileMode.ClampToBorder); views[i].hiZBinding = new TextureBinding() { View = views[i].hiZTex, Sampler = sampler }; var f_arr_ = (float[])views[i].hiZBinding.GetTextureHandle().SetResidency(Residency.Resident); for (int q = 0; q < f_arr_.Length; q++) { *(fp++) = f_arr_[q]; } fp += 2; views[i].gbuffer[FramebufferAttachment.DepthAttachment] = views[i].depthView; views[i].gbuffer[FramebufferAttachment.ColorAttachment0] = views[i].infoView; views[i].gbuffer[FramebufferAttachment.ColorAttachment1] = views[i].infoView2; Framebuffers[i] = views[i].gbuffer; views[i].program = new ShaderProgram( ShaderSource.Load(ShaderType.VertexShader, "Shaders/RenderToTexture/FrameBufferTriangle/vertex.glsl"), ShaderSource.Load(ShaderType.FragmentShader, "Shaders/RenderToTexture/FrameBufferTriangle/fragment.glsl") ); views[i].copy = new ShaderProgram( ShaderSource.Load(ShaderType.ComputeShader, "Shaders/HiZ/copy.glsl", $"#define MIP_COUNT {views[i].hiZView.Length}") ); views[i].copyState = new RenderState(null, views[i].copy, null, new UniformBuffer[] { Engine.GlobalParameters, HiZMapUBO }, false, false, DepthFunc.Always, InverseDepth.Far, InverseDepth.Near, BlendFactor.SrcAlpha, BlendFactor.OneMinusSrcAlpha, Vector4.Zero, InverseDepth.ClearDepth, CullFaceMode.Back); views[i].mipchain = new ShaderProgram( ShaderSource.Load(ShaderType.ComputeShader, "Shaders/HiZ/mipchain.glsl", $"#define MIP_COUNT {views[i].hiZView.Length}") ); views[i].mipchainState = new RenderState(null, views[i].mipchain, null, new UniformBuffer[] { Engine.GlobalParameters, HiZMapUBO }, false, false, DepthFunc.Always, InverseDepth.Far, InverseDepth.Near, BlendFactor.SrcAlpha, BlendFactor.OneMinusSrcAlpha, Vector4.Zero, InverseDepth.ClearDepth, CullFaceMode.Back); views[i].state = new RenderState(fbufs[i], views[i].program, null, new UniformBuffer[] { Engine.GlobalParameters }, false, true, DepthFunc.Always, InverseDepth.Far, InverseDepth.Near, BlendFactor.SrcAlpha, BlendFactor.OneMinusSrcAlpha, Vector4.Zero, InverseDepth.ClearDepth, CullFaceMode.None); InfoBindings[i] = new TextureBinding() { View = views[i].infoView, Sampler = TextureSampler.Default }; InfoBindings[i].GetTextureHandle().SetResidency(Residency.Resident); InfoBindings2[i] = new TextureBinding() { View = views[i].infoView2, Sampler = TextureSampler.Default }; InfoBindings2[i].GetTextureHandle().SetResidency(Residency.Resident); DepthBindings[i] = new TextureBinding() { View = views[i].depthView, Sampler = TextureSampler.Default, }; DepthBindings[i].GetTextureHandle().SetResidency(Residency.Resident); HiZMap[i] = views[i].hiZView; views[i].cBuffer = new CommandBuffer(); views[i].cBuffer.SetRenderState(views[i].state); views[i].cBuffer.Draw(PrimitiveType.Triangles, 0, 3, 1, 0); } HiZMapUBO.UpdateDone(); } }