diff options
| author | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2019-08-24 21:46:41 +0200 |
|---|---|---|
| committer | j-p <jp_bruyere@hotmail.com> | 2019-08-28 11:32:06 +0200 |
| commit | 8a8b76927ee1b5477001e62379baf546d2fae978 (patch) | |
| tree | 3339bde35b4503abbc7583e4988ba213cfefa9a5 | |
| parent | fbd236002664df62f8f3480a1863b92538fe2c6e (diff) | |
debug memory pools
| -rw-r--r-- | samples/Directory.Build.props | 2 | ||||
| -rw-r--r-- | samples/TexturedCube/TexturedCube.csproj | 3 | ||||
| -rw-r--r-- | samples/TexturedCube/main.cs | 137 | ||||
| -rw-r--r-- | samples/TexturedCube/shaders/FullScreenQuad.vert | 10 | ||||
| -rw-r--r-- | samples/TexturedCube/shaders/simpletexture.frag | 15 | ||||
| -rw-r--r-- | samples/deferred/deferred.csproj | 12 | ||||
| -rw-r--r-- | samples/deferred/main-crow.cs | 8 | ||||
| -rw-r--r-- | samples/deferred/main.cs | 7 | ||||
| -rw-r--r-- | samples/hdr/Program.cs | 66 | ||||
| -rw-r--r-- | samples/hdr/hdr.csproj | 6 | ||||
| -rw-r--r-- | samples/hdr/shaders/main.vert | 28 | ||||
| -rw-r--r-- | samples/pbr/PbrModel.cs | 26 | ||||
| -rw-r--r-- | samples/pbr/PbrPipeline.cs | 6 | ||||
| -rw-r--r-- | samples/tests/Program.cs | 40 | ||||
| -rw-r--r-- | vk.net.sln | 11 | ||||
| -rw-r--r-- | vke/CVKL.csproj | 4 | ||||
| -rw-r--r-- | vke/src/MemoryPool.cs | 78 | ||||
| -rw-r--r-- | vke/src/ResourceManager.cs | 2 | ||||
| -rw-r--r-- | vke/src/VkWindow.cs | 2 | ||||
| -rw-r--r-- | vke/src/model/glTFLoader.cs | 2 |
20 files changed, 373 insertions, 92 deletions
diff --git a/samples/Directory.Build.props b/samples/Directory.Build.props index a2cc0c7..8e85c1e 100644 --- a/samples/Directory.Build.props +++ b/samples/Directory.Build.props @@ -15,7 +15,7 @@ </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> - <DefineConstants>TRACE;DEBUG;NETSTANDARD;NETSTANDARD2_0;_WITH_SHADOWS;_WITH_VKVG</DefineConstants> + <DefineConstants>TRACE;DEBUG;NETSTANDARD;NETSTANDARD2_0;_WITH_SHADOWS;WITH_VKVG</DefineConstants> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <DefineConstants>NETSTANDARD;NETSTANDARD2_0;WITH_SHADOWS;_WITH_VKVG</DefineConstants> diff --git a/samples/TexturedCube/TexturedCube.csproj b/samples/TexturedCube/TexturedCube.csproj index 2caad9c..5608d58 100644 --- a/samples/TexturedCube/TexturedCube.csproj +++ b/samples/TexturedCube/TexturedCube.csproj @@ -1,4 +1,7 @@ <Project Sdk="Microsoft.NET.Sdk"> <Import Project="$(RootDirectory)netfx.props" /> + <ItemGroup> + <PackageReference Include="vkvg.net" Version="0.2.2-beta" /> + </ItemGroup> </Project> diff --git a/samples/TexturedCube/main.cs b/samples/TexturedCube/main.cs index 435d527..e348128 100644 --- a/samples/TexturedCube/main.cs +++ b/samples/TexturedCube/main.cs @@ -94,12 +94,109 @@ namespace TextureCube { VkSampleCountFlags samples = VkSampleCountFlags.SampleCount1; + vkvg.Device vkvgDev; + vkvg.Surface vkvgSurf; + Image uiImage; + GraphicPipeline uiPipeline; + DescriptorSet dsVKVG; + + void initUISurface () { + uiImage?.Dispose (); + vkvgSurf?.Dispose (); + vkvgSurf = new vkvg.Surface (vkvgDev, (int)swapChain.Width, (int)swapChain.Height); + uiImage = new Image (dev, new VkImage ((ulong)vkvgSurf.VkImage.ToInt64 ()), VkFormat.B8g8r8a8Unorm, + VkImageUsageFlags.ColorAttachment, (uint)vkvgSurf.Width, (uint)vkvgSurf.Height); + uiImage.CreateView (VkImageViewType.ImageView2D, VkImageAspectFlags.Color); + uiImage.CreateSampler (VkFilter.Nearest, VkFilter.Nearest, VkSamplerMipmapMode.Nearest, VkSamplerAddressMode.ClampToBorder); + uiImage.Descriptor.imageLayout = VkImageLayout.ShaderReadOnlyOptimal; + + DescriptorSetWrites uboUpdate = new DescriptorSetWrites (dsVKVG, dsLayout.Bindings[1]); + uboUpdate.Write (dev, uiImage.Descriptor); + } + + void vkvgDraw () { + using (vkvg.Context ctx = new vkvg.Context (vkvgSurf)) { + ctx.Operator = vkvg.Operator.Clear; + ctx.Paint (); + ctx.Operator = vkvg.Operator.Over; + + drawResources (ctx); + } + } + + void drawResources (vkvg.Context ctx) { + ResourceManager rm = dev.resourceManager; + + int margin = 5, memPoolHeight=15; + int drawingWidth = (int)swapChain.Width - 4 * margin; + int drawingHeight = (memPoolHeight + margin) * (rm.memoryPools.Length) + margin; + int y = (int)swapChain.Height - drawingHeight - margin; + int x = 2 * margin; + ctx.LineWidth = 1; + ctx.SetSource (0.1, 0.1, 0.1, 0.8); + ctx.Rectangle (0.5+margin, 0.5+y, swapChain.Width - 2*margin, drawingHeight); + ctx.FillPreserve (); + ctx.Flush (); + ctx.SetSource (0.8, 0.8, 0.8); + ctx.Stroke (); + + + + foreach (MemoryPool mp in rm.memoryPools) { + float byteWidth = (float)drawingWidth / mp.Size; + + y += margin; + ctx.Rectangle (x, y, drawingWidth, memPoolHeight); + ctx.SetSource (0.3, 0.3, 0.3, 0.4); + ctx.Fill (); + + if (mp.Last == null) + return; + + Resource r = mp.Last; + do { + float width = Math.Max (1f, byteWidth * r.AllocatedDeviceMemorySize); + + Vector3 c = new Vector3 (0); + Image img = r as Image; + if (img != null) { + c.Z = 1f; + if (img.CreateInfo.usage.HasFlag (VkImageUsageFlags.InputAttachment)) + c.Y += 0.3f; + if (img.CreateInfo.usage.HasFlag (VkImageUsageFlags.ColorAttachment)) + c.Y += 0.1f; + if (img.CreateInfo.usage.HasFlag (VkImageUsageFlags.Sampled)) + c.X += 0.3f; + } else { + CVKL.Buffer buff = r as CVKL.Buffer; + c.X = 1f; + if (buff.Infos.usage.HasFlag (VkBufferUsageFlags.IndexBuffer)) + c.Y += 0.2f; + if (buff.Infos.usage.HasFlag (VkBufferUsageFlags.VertexBuffer)) + c.Y += 0.4f; + if (buff.Infos.usage.HasFlag (VkBufferUsageFlags.UniformBuffer)) + c.Z += 0.3f; + } + ctx.SetSource (c.X, c.Y, c.Z, 0.5); + ctx.Rectangle (0.5f + x + byteWidth * r.poolOffset, 0.5f + y, width, memPoolHeight); + ctx.FillPreserve (); + ctx.SetSource (0.01f, 0.01f, 0.01f); + ctx.Stroke (); + r = r.next; + } while (r != mp.Last && r != null); + y += memPoolHeight; + } + } + Program () : base () { + vkvgDev = new vkvg.Device (instance.Handle, phy.Handle, dev.VkDev.Handle, presentQueue.qFamIndex, + vkvg.SampleCount.Sample_4, presentQueue.index); + vbo = new GPUBuffer<float> (presentQueue, cmdPool, VkBufferUsageFlags.VertexBuffer, g_vertex_buffer_data); - descriptorPool = new DescriptorPool (dev, 1, - new VkDescriptorPoolSize (VkDescriptorType.UniformBuffer), - new VkDescriptorPoolSize (VkDescriptorType.CombinedImageSampler) + descriptorPool = new DescriptorPool (dev, 2, + new VkDescriptorPoolSize (VkDescriptorType.UniformBuffer,2), + new VkDescriptorPoolSize (VkDescriptorType.CombinedImageSampler,2) ); dsLayout = new DescriptorSetLayout (dev, 0, @@ -119,10 +216,20 @@ namespace TextureCube { pipeline = new GraphicPipeline (cfg); + cfg.ResetShadersAndVerticesInfos (); + cfg.AddShader (VkShaderStageFlags.Vertex, "shaders/FullScreenQuad.vert.spv"); + cfg.AddShader (VkShaderStageFlags.Fragment, "shaders/simpletexture.frag.spv"); + + cfg.blendAttachments[0] = new VkPipelineColorBlendAttachmentState (true); + + uiPipeline = new GraphicPipeline (cfg); + + uboMats = new HostBuffer (dev, VkBufferUsageFlags.UniformBuffer, matrices); uboMats.Map ();//permanent map descriptorSet = descriptorPool.Allocate (dsLayout); + dsVKVG = descriptorPool.Allocate (dsLayout); DescriptorSetWrites uboUpdate = new DescriptorSetWrites (descriptorSet, dsLayout.Bindings[0]); uboUpdate.Write (dev, uboMats.Descriptor); @@ -130,8 +237,12 @@ namespace TextureCube { loadTexture (imgPathes[currentImgIndex]); if (nextTexture != null) updateTextureSet (); - } + initUISurface (); + } + public override void Update () { + vkvgDraw (); + } void buildCommandBuffers () { for (int i = 0; i < swapChain.ImageCount; ++i) { cmds[i]?.Free (); @@ -155,6 +266,17 @@ namespace TextureCube { cmd.BindVertexBuffer (vbo, 0); cmd.Draw (36); + uiPipeline.Bind (cmd); + cmd.BindDescriptorSet (pipeline.Layout, dsVKVG); + + uiImage.SetLayout (cmd, VkImageAspectFlags.Color, VkImageLayout.ColorAttachmentOptimal, VkImageLayout.ShaderReadOnlyOptimal, + VkPipelineStageFlags.ColorAttachmentOutput, VkPipelineStageFlags.FragmentShader); + + cmd.Draw (3, 1, 0, 0); + + uiImage.SetLayout (cmd, VkImageAspectFlags.Color, VkImageLayout.ShaderReadOnlyOptimal, VkImageLayout.ColorAttachmentOptimal, + VkPipelineStageFlags.FragmentShader, VkPipelineStageFlags.BottomOfPipe); + pipeline.RenderPass.End (cmd); } @@ -243,6 +365,8 @@ namespace TextureCube { protected override void OnResize () { dev.WaitIdle (); + initUISurface (); + updateMatrices (); if (frameBuffers!=null) @@ -273,6 +397,7 @@ namespace TextureCube { if (!isDisposed) { dev.WaitIdle (); pipeline.Dispose (); + uiPipeline.Dispose (); dsLayout.Dispose (); for (int i = 0; i < swapChain.ImageCount; i++) frameBuffers[i].Dispose (); @@ -281,6 +406,10 @@ namespace TextureCube { uboMats.Dispose (); vbo.Dispose (); + + uiImage?.Dispose (); + vkvgSurf?.Dispose (); + vkvgDev.Dispose (); } } diff --git a/samples/TexturedCube/shaders/FullScreenQuad.vert b/samples/TexturedCube/shaders/FullScreenQuad.vert new file mode 100644 index 0000000..826720b --- /dev/null +++ b/samples/TexturedCube/shaders/FullScreenQuad.vert @@ -0,0 +1,10 @@ +#version 450 + +layout (location = 0) out vec2 outUV; + +void main() +{ + outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); + gl_Position = vec4(outUV * 2.0f + -1.0f, 0.0f, 1.0f); + //gl_Position = vec4(outUV -1.0f, 0.0f, 1.0f); +} diff --git a/samples/TexturedCube/shaders/simpletexture.frag b/samples/TexturedCube/shaders/simpletexture.frag new file mode 100644 index 0000000..cee63cb --- /dev/null +++ b/samples/TexturedCube/shaders/simpletexture.frag @@ -0,0 +1,15 @@ +#version 450 + +#extension GL_ARB_separate_shader_objects : enable +#extension GL_ARB_shading_language_420pack : enable + +layout (set = 0, binding = 1) uniform sampler2D samplerColor; + +layout (location = 0) in vec2 inUV; + +layout (location = 0) out vec4 outFragColor; + +void main() +{ + outFragColor = texture(samplerColor, inUV); +} diff --git a/samples/deferred/deferred.csproj b/samples/deferred/deferred.csproj index ac9eed1..4a034f6 100644 --- a/samples/deferred/deferred.csproj +++ b/samples/deferred/deferred.csproj @@ -8,18 +8,22 @@ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DefineConstants>TRACE;DEBUG;NETSTANDARD;NETSTANDARD2_0;MEMORY_POOLS;WITH_SHADOWS;_WITH_VKVG</DefineConstants> </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugCrow|AnyCPU' "> + <DefineConstants>TRACE;DEBUG;NETSTANDARD;NETSTANDARD2_0;MEMORY_POOLS;WITH_SHADOWS;_WITH_VKVG</DefineConstants> + </PropertyGroup> <ItemGroup> <Compile Include="EnvironmentPipeline.cs;deferredPbrRenderer.cs;shadowMapRenderer.cs" /> </ItemGroup> - <ItemGroup Condition="'$(Configuration)' == 'DebugCrow' "> + + <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugCrow|AnyCPU' "> <Compile Include="../common/CrowWin.cs" /> <Compile Include="main-crow.cs" /> <EmbeddedResource Include="ui\**\*.*"> <LogicalName>deferred.%(Filename)%(Extension)</LogicalName> </EmbeddedResource> - </ItemGroup> + </ItemGroup> <ItemGroup Condition="$(Configuration) != 'DebugCrow' "> <Compile Include="main.cs" /> @@ -28,6 +32,6 @@ <ItemGroup> <PackageReference Include="Crow.VK" Version="0.9.0-beta" /> - </ItemGroup> - + </ItemGroup> + </Project>
\ No newline at end of file diff --git a/samples/deferred/main-crow.cs b/samples/deferred/main-crow.cs index 8f9e3e4..83170b7 100644 --- a/samples/deferred/main-crow.cs +++ b/samples/deferred/main-crow.cs @@ -9,10 +9,14 @@ using System.Linq; namespace deferred { class Program : Crow.CrowWin { static void Main (string[] args) { - Instance.DEBUG_UTILS = true; + /*Instance.DEBUG_UTILS = true; Instance.VALIDATION = true; - Instance.RENDER_DOC_CAPTURE = true; + Instance.RENDER_DOC_CAPTURE = true;*/ DeferredPbrRenderer.TEXTURE_ARRAY = true; + DeferredPbrRenderer.NUM_SAMPLES = VkSampleCountFlags.SampleCount1; + DeferredPbrRenderer.HDR_FORMAT = VkFormat.R32g32b32a32Sfloat; + DeferredPbrRenderer.MRT_FORMAT = VkFormat.R16g16b16a16Sfloat; + PbrModelTexArray.TEXTURE_DIM = 1024; using (Program vke = new Program ()) { diff --git a/samples/deferred/main.cs b/samples/deferred/main.cs index fd815b0..5e4c28f 100644 --- a/samples/deferred/main.cs +++ b/samples/deferred/main.cs @@ -8,15 +8,14 @@ namespace deferred { class Deferred : VkWindow { static void Main (string[] args) { - Instance.VALIDATION = true; - Instance.DEBUG_UTILS = true; + //Instance.VALIDATION = true; + //Instance.DEBUG_UTILS = true; //Instance.RENDER_DOC_CAPTURE = true; DeferredPbrRenderer.TEXTURE_ARRAY = true; DeferredPbrRenderer.NUM_SAMPLES = VkSampleCountFlags.SampleCount1; DeferredPbrRenderer.HDR_FORMAT = VkFormat.R32g32b32a32Sfloat; - DeferredPbrRenderer.MRT_FORMAT = VkFormat.R32g32b32a32Sfloat; - DeferredPbrRenderer.NUM_SAMPLES = VkSampleCountFlags.SampleCount1; + DeferredPbrRenderer.MRT_FORMAT = VkFormat.R16g16b16a16Sfloat; PbrModelTexArray.TEXTURE_DIM = 1024; diff --git a/samples/hdr/Program.cs b/samples/hdr/Program.cs new file mode 100644 index 0000000..0f34ddf --- /dev/null +++ b/samples/hdr/Program.cs @@ -0,0 +1,66 @@ +using System; +using CVKL; +using VK; +using static VK.Vk; + +namespace tests { + class Program : VkWindow { + static void Main (string[] args) { + Instance.VALIDATION = true; + Instance.DEBUG_UTILS = true; + using (Program vke = new Program ()) { + vke.Run (); + } + } + + DebugReport dbgReport; + CVKL.DebugUtils.Messenger msg; + + Image img; + + Program () : base () { + msg = new CVKL.DebugUtils.Messenger (instance); + + dbgReport = new DebugReport (instance, + VkDebugReportFlagsEXT.DebugEXT | + VkDebugReportFlagsEXT.ErrorEXT | + VkDebugReportFlagsEXT.WarningEXT | + VkDebugReportFlagsEXT.PerformanceWarningEXT | + VkDebugReportFlagsEXT.InformationEXT); + + + CommandBuffer cmd = cmdPool.AllocateAndStart (VkCommandBufferUsageFlags.OneTimeSubmit); + + using (StbImage stbi = new StbImage ("data/textures/texture.jpg")) { + + img = new Image (dev, VkFormat.R8g8b8a8Unorm, VkImageUsageFlags.TransferSrc | VkImageUsageFlags.TransferDst, + VkMemoryPropertyFlags.DeviceLocal, (uint)stbi.Width, (uint)stbi.Height, VkImageType.Image2D, + VkSampleCountFlags.SampleCount1, VkImageTiling.Optimal, Image.ComputeMipLevels (stbi.Width, stbi.Height)); + + using (HostBuffer stag = new HostBuffer (dev, VkBufferUsageFlags.TransferSrc, (ulong)stbi.Size, stbi.Handle)) { + + stag.CopyTo (cmd, img, VkImageLayout.TransferSrcOptimal); + img.BuildMipmaps (cmd); + + cmd.End (); + presentQueue.Submit (cmd); + presentQueue.WaitIdle (); + cmd.Free (); + + } + } + + + } + + + protected override void Dispose (bool disposing) { + if (disposing) { + if (!isDisposed) { + dbgReport?.Dispose (); + } + } + base.Dispose (); + } + } +} diff --git a/samples/hdr/hdr.csproj b/samples/hdr/hdr.csproj new file mode 100644 index 0000000..f4c1c6b --- /dev/null +++ b/samples/hdr/hdr.csproj @@ -0,0 +1,6 @@ +<Project Sdk="Microsoft.NET.Sdk"> + <Import Project="$(RootDirectory)netfx.props" /> + <ItemGroup> + <Folder Include="shaders\" /> + </ItemGroup> +</Project> diff --git a/samples/hdr/shaders/main.vert b/samples/hdr/shaders/main.vert new file mode 100644 index 0000000..3b395bf --- /dev/null +++ b/samples/hdr/shaders/main.vert @@ -0,0 +1,28 @@ +#version 450 + +#extension GL_ARB_separate_shader_objects : enable +#extension GL_ARB_shading_language_420pack : enable + +layout (location = 0) in vec3 inPos; +layout (location = 1) in vec3 inColor; + +layout (binding = 0) uniform UBO +{ + mat4 projectionMatrix; + mat4 viewMatrix; + mat4 modelMatrix; +} ubo; + +layout (location = 0) out vec3 outColor; + +out gl_PerVertex +{ + vec4 gl_Position; +}; + + +void main() +{ + outColor = inColor; + gl_Position = ubo.projectionMatrix * ubo.viewMatrix * ubo.modelMatrix * vec4(inPos.xyz, 1.0); +} diff --git a/samples/pbr/PbrModel.cs b/samples/pbr/PbrModel.cs index 80476e9..3ed1cf3 100644 --- a/samples/pbr/PbrModel.cs +++ b/samples/pbr/PbrModel.cs @@ -1,28 +1,6 @@ -// -// Model.cs +// Copyright (c) 2019 Jean-Philippe Bruyère <jp_bruyere@hotmail.com> // -// Author: -// Jean-Philippe Bruyère <jp_bruyere@hotmail.com> -// -// Copyright (c) 2019 jp -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) using System; using System.Diagnostics; using System.Numerics; diff --git a/samples/pbr/PbrPipeline.cs b/samples/pbr/PbrPipeline.cs index 4ca7e7a..f0bdc9d 100644 --- a/samples/pbr/PbrPipeline.cs +++ b/samples/pbr/PbrPipeline.cs @@ -1,4 +1,8 @@ -using System; +// Copyright (c) 2019 Jean-Philippe Bruyère <jp_bruyere@hotmail.com> +// +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) + +using System; using System.Numerics; using System.Runtime.InteropServices; using VK; diff --git a/samples/tests/Program.cs b/samples/tests/Program.cs index 0f34ddf..f05fdbd 100644 --- a/samples/tests/Program.cs +++ b/samples/tests/Program.cs @@ -1,4 +1,13 @@ -using System; +/* Copyright (c) 2019 Jean-Philippe Bruyère <jp_bruyere@hotmail.com> + * + * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) + * + * port of the 'High dynamic range rendering' sample from + * Copyright by Sascha Willems - www.saschawillems.de + * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) +*/ + +using System; using CVKL; using VK; using static VK.Vk; @@ -8,49 +17,22 @@ namespace tests { static void Main (string[] args) { Instance.VALIDATION = true; Instance.DEBUG_UTILS = true; + using (Program vke = new Program ()) { vke.Run (); } } DebugReport dbgReport; - CVKL.DebugUtils.Messenger msg; - Image img; Program () : base () { - msg = new CVKL.DebugUtils.Messenger (instance); - dbgReport = new DebugReport (instance, VkDebugReportFlagsEXT.DebugEXT | VkDebugReportFlagsEXT.ErrorEXT | VkDebugReportFlagsEXT.WarningEXT | VkDebugReportFlagsEXT.PerformanceWarningEXT | VkDebugReportFlagsEXT.InformationEXT); - - - CommandBuffer cmd = cmdPool.AllocateAndStart (VkCommandBufferUsageFlags.OneTimeSubmit); - - using (StbImage stbi = new StbImage ("data/textures/texture.jpg")) { - - img = new Image (dev, VkFormat.R8g8b8a8Unorm, VkImageUsageFlags.TransferSrc | VkImageUsageFlags.TransferDst, - VkMemoryPropertyFlags.DeviceLocal, (uint)stbi.Width, (uint)stbi.Height, VkImageType.Image2D, - VkSampleCountFlags.SampleCount1, VkImageTiling.Optimal, Image.ComputeMipLevels (stbi.Width, stbi.Height)); - - using (HostBuffer stag = new HostBuffer (dev, VkBufferUsageFlags.TransferSrc, (ulong)stbi.Size, stbi.Handle)) { - - stag.CopyTo (cmd, img, VkImageLayout.TransferSrcOptimal); - img.BuildMipmaps (cmd); - - cmd.End (); - presentQueue.Submit (cmd); - presentQueue.WaitIdle (); - cmd.Free (); - - } - } - - } @@ -29,6 +29,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Model", "samples\Model\Mode EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "tests", "samples\tests\tests.csproj", "{1972F90F-9995-4B5D-9696-1B0692F8F650}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hdr", "samples\hdr\hdr.csproj", "{77D1C316-5BFE-4646-A465-EFAF1813F910}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -123,6 +125,14 @@ Global {1972F90F-9995-4B5D-9696-1B0692F8F650}.temp|Any CPU.Build.0 = Debug|Any CPU {1972F90F-9995-4B5D-9696-1B0692F8F650}.DebugCrow|Any CPU.ActiveCfg = Debug|Any CPU {1972F90F-9995-4B5D-9696-1B0692F8F650}.DebugCrow|Any CPU.Build.0 = Debug|Any CPU + {77D1C316-5BFE-4646-A465-EFAF1813F910}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {77D1C316-5BFE-4646-A465-EFAF1813F910}.Debug|Any CPU.Build.0 = Debug|Any CPU + {77D1C316-5BFE-4646-A465-EFAF1813F910}.Release|Any CPU.ActiveCfg = Release|Any CPU + {77D1C316-5BFE-4646-A465-EFAF1813F910}.Release|Any CPU.Build.0 = Release|Any CPU + {77D1C316-5BFE-4646-A465-EFAF1813F910}.temp|Any CPU.ActiveCfg = Debug|Any CPU + {77D1C316-5BFE-4646-A465-EFAF1813F910}.temp|Any CPU.Build.0 = Debug|Any CPU + {77D1C316-5BFE-4646-A465-EFAF1813F910}.DebugCrow|Any CPU.ActiveCfg = Debug|Any CPU + {77D1C316-5BFE-4646-A465-EFAF1813F910}.DebugCrow|Any CPU.Build.0 = Debug|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {96A2E0B8-91AE-4EB9-A435-185D01E24067} = {A2A2849A-0032-4AA8-BC0E-82C69D7DE354} @@ -134,6 +144,7 @@ Global {790642C5-7AD1-437D-9F9C-3F721133AFEF} = {A2A2849A-0032-4AA8-BC0E-82C69D7DE354} {0E4346C3-87C0-4C0A-8CDB-DBF1E29F5385} = {A2A2849A-0032-4AA8-BC0E-82C69D7DE354} {1972F90F-9995-4B5D-9696-1B0692F8F650} = {A2A2849A-0032-4AA8-BC0E-82C69D7DE354} + {77D1C316-5BFE-4646-A465-EFAF1813F910} = {A2A2849A-0032-4AA8-BC0E-82C69D7DE354} EndGlobalSection GlobalSection(MonoDevelopProperties) = preSolution Policies = $0 diff --git a/vke/CVKL.csproj b/vke/CVKL.csproj index 850a3a2..a5bf978 100644 --- a/vke/CVKL.csproj +++ b/vke/CVKL.csproj @@ -27,12 +27,12 @@ <DebugType>full</DebugType> <Optimize>false</Optimize> <CheckForOverflowUnderflow>true</CheckForOverflowUnderflow> - <DefineConstants>_MEMORY_POOLS;NETSTANDARD;NETSTANDARD2_0;DEBUG;NETFRAMEWORK;NET471</DefineConstants> + <DefineConstants>MEMORY_POOLS;NETSTANDARD;NETSTANDARD2_0;DEBUG;NETFRAMEWORK;NET471</DefineConstants> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <Optimize>true</Optimize> - <DefineConstants>_MEMORY_POOLS</DefineConstants> + <DefineConstants>MEMORY_POOLS</DefineConstants> </PropertyGroup> <ItemGroup Condition="$(TargetFramework.StartsWith('net4'))"> diff --git a/vke/src/MemoryPool.cs b/vke/src/MemoryPool.cs index e2c27cd..297b61a 100644 --- a/vke/src/MemoryPool.cs +++ b/vke/src/MemoryPool.cs @@ -16,7 +16,7 @@ namespace CVKL { internal VkDeviceMemory vkMemory; VkMemoryAllocateInfo memInfo = VkMemoryAllocateInfo.New (); - Resource firstResource; + //Resource firstResource; Resource lastResource; //Resource mappedFrom; @@ -28,7 +28,7 @@ namespace CVKL { public ulong Size => memInfo.allocationSize; public bool IsMapped => mappedPointer != IntPtr.Zero; public IntPtr MappedData => mappedPointer; - public Resource First => firstResource; + public Resource Last => lastResource; public MemoryPool (Device dev, uint memoryTypeIndex, UInt64 size) { this.dev = dev; @@ -40,35 +40,77 @@ namespace CVKL { public void Add (Resource resource) { resource.memoryPool = this; - if (lastResource == null) { - firstResource = lastResource = resource; - resource.poolOffset = 0; - } else { + ulong limit = Size; + ulong offset = 0; + Resource previous = lastResource; - resource.poolOffset = lastResource.poolOffset + lastResource.AllocatedDeviceMemorySize; - resource.poolOffset += resource.MemoryAlignment - (resource.poolOffset % resource.MemoryAlignment); + if (previous != null) { + do { + offset = previous.poolOffset + previous.AllocatedDeviceMemorySize; + offset += resource.MemoryAlignment - (offset % resource.MemoryAlignment); + + if (previous.next == null) { + if (offset + resource.AllocatedDeviceMemorySize >= limit) { + offset = 0; + limit = previous.poolOffset; + } + break; + } + + if (previous.next.poolOffset < previous.poolOffset) { + limit = Size; + if (offset + resource.AllocatedDeviceMemorySize < Size) + break; + offset = 0; + limit = previous.next.poolOffset; + }else + limit = previous.next.poolOffset; + + if (offset + resource.AllocatedDeviceMemorySize < limit) + break; + + previous = previous.next; + + } while (previous != lastResource); - lastResource.next = resource; - resource.previous = lastResource; - lastResource = resource; } + + if (offset + resource.AllocatedDeviceMemorySize >= limit) + throw new Exception ($"Out of Memory pool: {memInfo.memoryTypeIndex}"); + + resource.poolOffset = offset; + resource.previous = previous; + if (previous != null) { + if (previous.next == null) { + resource.next = resource.previous = previous; + previous.next = previous.previous = resource; + } else { + resource.next = previous.next; + previous.next = resource; + } + } + lastResource = resource; + resource.bindMemory (); } + + public void Defrag () { } public void Remove (Resource resource) { - if (resource == firstResource) - firstResource = resource.next; if (resource == lastResource) lastResource = resource.previous; - if (resource.previous != null) - resource.previous.next = resource.next; - if (resource.next != null) - resource.next.previous = resource.previous; - + if (lastResource != null) { + if (resource.previous == resource.next)//only 1 resources remaining + lastResource.next = lastResource.previous = null; + else { + resource.previous.next = resource.next; + resource.next.previous = resource.previous; + } + } resource.next = resource.previous = null; } diff --git a/vke/src/ResourceManager.cs b/vke/src/ResourceManager.cs index 490f591..e450754 100644 --- a/vke/src/ResourceManager.cs +++ b/vke/src/ResourceManager.cs @@ -16,7 +16,7 @@ namespace CVKL { VkMemoryHeap getHeapFromMemoryIndex (uint i) => memoryProperties.memoryHeaps[memoryProperties.memoryTypes[i].heapIndex]; - public ResourceManager (Device dev, ulong defaultPoolsBlockDivisor = 16) { + public ResourceManager (Device dev, ulong defaultPoolsBlockDivisor = 4) { memoryProperties = dev.phy.memoryProperties; memoryPools = new MemoryPool[memoryProperties.memoryTypeCount]; reservedHeapMemory = new ulong[memoryProperties.memoryHeapCount]; diff --git a/vke/src/VkWindow.cs b/vke/src/VkWindow.cs index d0f3f61..d9d1cf8 100644 --- a/vke/src/VkWindow.cs +++ b/vke/src/VkWindow.cs @@ -81,7 +81,7 @@ namespace CVKL { public uint Width => width; public uint Height => height; - public VkWindow (string name = "VkWindow", uint _width = 1024, uint _height = 768, bool vSync = false) { + public VkWindow (string name = "VkWindow", uint _width = 1024, uint _height = 1024, bool vSync = false) { currentWindow = this; width = _width; diff --git a/vke/src/model/glTFLoader.cs b/vke/src/model/glTFLoader.cs index 29af594..db9adf5 100644 --- a/vke/src/model/glTFLoader.cs +++ b/vke/src/model/glTFLoader.cs @@ -470,7 +470,7 @@ namespace CVKL { cmd = cmdPool.AllocateAndStart (VkCommandBufferUsageFlags.OneTimeSubmit); vkimg.SetLayout (cmd, VkImageAspectFlags.Color, VkImageLayout.Undefined, VkImageLayout.TransferSrcOptimal, - VkPipelineStageFlags.Transfer, VkPipelineStageFlags.Transfer); + VkPipelineStageFlags.BottomOfPipe, VkPipelineStageFlags.Transfer); Vk.vkCmdBlitImage (cmd.Handle, vkimg.handle, VkImageLayout.TransferSrcOptimal, texArray.handle, VkImageLayout.TransferDstOptimal, 1, ref imageBlit, VkFilter.Linear); |