private void refreshResources() { closeExistingResources(); if (material == null || model == null || cs == null || context == null) { return; } ct = new MycomputeShader(cs); traceDisposable(ct.ComputeTask); BufferDescription bd = new BufferDescription(); bd.CpuAccess = ResourceCpuAccess.None; bd.Usage = ResourceUsage.Default; bd.Flags = BufferFlags.ShaderResource | BufferFlags.UnorderedAccess | BufferFlags.BufferStructured; bd.StructureByteStride = 12; bd.SizeInBytes = (uint)(tmp.Length * bd.StructureByteStride); dataBuffer = context.Factory.CreateBuffer <Vector3>(null, ref bd); ct.Time = 2f; ct.Stride = 8; ct.Data = dataBuffer; //Buffer= //ct.TextureSlots }
protected override async void InternalLoad() { var swapChainDescription = this.swapChain?.SwapChainDescription; this.width = swapChainDescription.Value.Width; this.height = swapChainDescription.Value.Height; // Create Scene BasicScene(); // Compute Resources var spheresBufferDescription = new BufferDescription( (uint)(Unsafe.SizeOf <Sphere>() * spheres.Length), BufferFlags.UnorderedAccess | BufferFlags.ShaderResource | BufferFlags.BufferStructured, ResourceUsage.Default, ResourceCpuAccess.None, Unsafe.SizeOf <Sphere>()); spheresBuffer = this.graphicsContext.Factory.CreateBuffer(spheres, ref spheresBufferDescription); var materialsBufferDescription = new BufferDescription( (uint)(Unsafe.SizeOf <Material>() * spheres.Length), BufferFlags.UnorderedAccess | BufferFlags.ShaderResource | BufferFlags.BufferStructured, ResourceUsage.Default, ResourceCpuAccess.None, Unsafe.SizeOf <Material>()); materialsBuffer = this.graphicsContext.Factory.CreateBuffer(materials, ref materialsBufferDescription); this.threadGroupX = this.width / 16u; this.threadGroupY = this.height / 16u; CompilerParameters parameters = new CompilerParameters() { CompilationMode = CompilationMode.None, Profile = GraphicsProfile.Level_11_0, }; var computeShaderDescription = await this.assetsDirectory.ReadAndCompileShader(this.graphicsContext, "CS", "ComputeShader", ShaderStages.Compute, "CS", parameters); var computeShader = this.graphicsContext.Factory.CreateShader(ref computeShaderDescription); var textureDescription = new TextureDescription() { Type = TextureType.Texture2D, Usage = ResourceUsage.Default, Flags = TextureFlags.UnorderedAccess | TextureFlags.ShaderResource, Format = PixelFormat.R8G8B8A8_UNorm, Width = this.width, Height = this.height, Depth = 1, MipLevels = 1, ArraySize = 1, CpuAccess = ResourceCpuAccess.None, SampleCount = TextureSampleCount.None, }; Texture texture2D = this.graphicsContext.Factory.CreateTexture(ref textureDescription); this.computeData = new ComputeData() { time = 0, width = this.width, height = this.height, framecount = 0, samples = 25, recursion = 5, spherecount = (uint)spheres.Length, cam = this.cam }; var constantBufferDescription = new BufferDescription((uint)Unsafe.SizeOf <ComputeData>(), BufferFlags.ConstantBuffer, ResourceUsage.Default); this.constantBuffer = this.graphicsContext.Factory.CreateBuffer(ref this.computeData, ref constantBufferDescription); ResourceLayoutDescription computeLayoutDescription = new ResourceLayoutDescription( new LayoutElementDescription(0, ResourceType.StructuredBuffer, ShaderStages.Compute), new LayoutElementDescription(1, ResourceType.StructuredBuffer, ShaderStages.Compute), new LayoutElementDescription(0, ResourceType.ConstantBuffer, ShaderStages.Compute), new LayoutElementDescription(0, ResourceType.TextureReadWrite, ShaderStages.Compute)); ResourceLayout computeResourceLayout = this.graphicsContext.Factory.CreateResourceLayout(ref computeLayoutDescription); ResourceSetDescription computeResourceSetDescription = new ResourceSetDescription(computeResourceLayout, this.spheresBuffer, this.materialsBuffer, this.constantBuffer, texture2D); this.computeResourceSet = this.graphicsContext.Factory.CreateResourceSet(ref computeResourceSetDescription); var computePipelineDescription = new ComputePipelineDescription() { ComputeShader = computeShader, ResourceLayouts = new[] { computeResourceLayout }, ThreadGroupSizeX = 16, ThreadGroupSizeY = 16, ThreadGroupSizeZ = 1, }; this.computePipelineState = this.graphicsContext.Factory.CreateComputePipeline(ref computePipelineDescription); // Graphics Resources var vertexShaderDescription = await this.assetsDirectory.ReadAndCompileShader(this.graphicsContext, "HLSL", "VertexShader", ShaderStages.Vertex, "VS"); var pixelShaderDescription = await this.assetsDirectory.ReadAndCompileShader(this.graphicsContext, "HLSL", "FragmentShader", ShaderStages.Pixel, "PS"); var vertexShader = this.graphicsContext.Factory.CreateShader(ref vertexShaderDescription); var pixelShader = this.graphicsContext.Factory.CreateShader(ref pixelShaderDescription); var samplerDescription = SamplerStates.LinearClamp; var samplerState = this.graphicsContext.Factory.CreateSamplerState(ref samplerDescription); // Set Params this.paramsData.Samples = 1; this.paramsData.IsPathTracing = false; var paramsBufferDescription = new BufferDescription((uint)Unsafe.SizeOf <Params>(), BufferFlags.ConstantBuffer, ResourceUsage.Default); this.paramsBuffer = this.graphicsContext.Factory.CreateBuffer(ref paramsBufferDescription); ResourceLayoutDescription layoutDescription = new ResourceLayoutDescription( new LayoutElementDescription(0, ResourceType.ConstantBuffer, ShaderStages.Pixel), new LayoutElementDescription(0, ResourceType.Texture, ShaderStages.Pixel), new LayoutElementDescription(0, ResourceType.Sampler, ShaderStages.Pixel)); ResourceLayout resourceLayout = this.graphicsContext.Factory.CreateResourceLayout(ref layoutDescription); ResourceSetDescription resourceSetDescription = new ResourceSetDescription(resourceLayout, this.paramsBuffer, texture2D, samplerState); this.resourceSet = this.graphicsContext.Factory.CreateResourceSet(ref resourceSetDescription); BlendStateDescription blend = BlendStateDescription.Default; if (this.paramsData.IsPathTracing) { blend.RenderTarget0.BlendEnable = true; blend.RenderTarget0.SourceBlendColor = Blend.SourceAlpha; blend.RenderTarget0.DestinationBlendColor = Blend.InverseSourceAlpha; } else { blend = BlendStates.Opaque; } var pipelineDescription = new GraphicsPipelineDescription() { PrimitiveTopology = PrimitiveTopology.TriangleList, InputLayouts = null, ResourceLayouts = new[] { resourceLayout }, Shaders = new ShaderStateDescription() { VertexShader = vertexShader, PixelShader = pixelShader, }, RenderStates = new RenderStateDescription() { RasterizerState = RasterizerStates.CullBack, BlendState = blend, DepthStencilState = DepthStencilStates.Read, }, Outputs = this.frameBuffer.OutputDescription, }; this.graphicsPipelineState = this.graphicsContext.Factory.CreateGraphicsPipeline(ref pipelineDescription); this.graphicsCommandQueue = this.graphicsContext.Factory.CreateCommandQueue(CommandQueueType.Graphics); this.computeCommandQueue = this.graphicsContext.Factory.CreateCommandQueue(CommandQueueType.Compute); this.viewports = new Viewport[1]; this.viewports[0] = new Viewport(0, 0, this.width, this.height); this.scissors = new Rectangle[1]; this.scissors[0] = new Rectangle(0, 0, (int)this.width, (int)this.height); }
protected override async void InternalLoad() { // Compile Vertex and Pixel shaders var vertexShaderDescription = await this.assetsDirectory.ReadAndCompileShader(this.graphicsContext, "HLSL", "VertexShader", ShaderStages.Vertex, "VS"); var pixelShaderDescription = await this.assetsDirectory.ReadAndCompileShader(this.graphicsContext, "HLSL", "FragmentShader", ShaderStages.Pixel, "PS"); var vertexShader = this.graphicsContext.Factory.CreateShader(ref vertexShaderDescription); var pixelShader = this.graphicsContext.Factory.CreateShader(ref pixelShaderDescription); var vertexBufferDescription = new BufferDescription((uint)Unsafe.SizeOf <VertexPositionNormalTexture>() * (uint)vertexData.Length, BufferFlags.VertexBuffer, ResourceUsage.Default); var vertexBuffer = this.graphicsContext.Factory.CreateBuffer(vertexData, ref vertexBufferDescription); this.view = Matrix4x4.CreateLookAt(new Vector3(0, 0, 4f), new Vector3(0, 0, 0), Vector3.UnitY); this.proj = Matrix4x4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)this.frameBuffer.Width / (float)this.frameBuffer.Height, 0.1f, 100f); // Parameters float density = 0.4f; float minHairLength = 0.5f; this.parameters = new Parameters(); this.parameters.displacement = new Vector3(0, -0.05f, 0); this.parameters.numLayers = 50f; this.parameters.startShadowValue = 0.2f; this.parameters.MaxHairLength = 0.2f; this.parameters.viewProj = Matrix4x4.Multiply(this.view, this.proj); // Constant Buffer var constantBufferDescription = new BufferDescription((uint)Unsafe.SizeOf <Parameters>(), BufferFlags.ConstantBuffer, ResourceUsage.Default); this.constantBuffer = this.graphicsContext.Factory.CreateBuffer(ref this.parameters, ref constantBufferDescription); // Create FurTexture uint size = 1024; var description = new TextureDescription() { Type = TextureType.Texture2D, Width = size, Height = size, Depth = 1, ArraySize = 1, Faces = 1, Usage = ResourceUsage.Default, CpuAccess = ResourceCpuAccess.None, Flags = TextureFlags.ShaderResource, Format = PixelFormat.R8_UNorm, MipLevels = 1, SampleCount = TextureSampleCount.None, }; var textureFur = this.graphicsContext.Factory.CreateTexture(ref description); uint totalPixels = size * size; byte[] data = new byte[totalPixels]; int strands = (int)(density * totalPixels); int minValue = (int)(minHairLength * 255f); for (int i = 0; i < strands; i++) { int x = rand.Next((int)size); int y = rand.Next((int)size); data[(x * size) + y] = (byte)rand.Next(minValue, 255); } this.graphicsContext.UpdateTextureData(textureFur, data); // Color Texture Texture texture2D = null; ////using (var stream = this.assetsDirectory.Open("Cat.ktx")) using (var stream = this.assetsDirectory.Open("Leopard.ktx")) ////using (var stream = this.assetsDirectory.Open("Cheetah.ktx")) ////using (var stream = this.assetsDirectory.Open("GrayLeopard.ktx")) ////using (var stream = this.assetsDirectory.Open("Tiger.ktx")) ////using (var stream = this.assetsDirectory.Open("Zebra.ktx")) { if (stream != null) { VisualTests.LowLevel.Images.Image image = VisualTests.LowLevel.Images.Image.Load(stream); var textureDescription = image.TextureDescription; texture2D = graphicsContext.Factory.CreateTexture(image.DataBoxes, ref textureDescription); } } SamplerStateDescription sampler1Description = SamplerStates.LinearClamp; var sampler1 = this.graphicsContext.Factory.CreateSamplerState(ref sampler1Description); SamplerStateDescription sampler2Description = SamplerStates.PointClamp; var sampler2 = this.graphicsContext.Factory.CreateSamplerState(ref sampler2Description); // Prepare Pipeline var vertexLayouts = new InputLayouts() .Add(VertexPositionNormalTexture.VertexFormat); ResourceLayoutDescription layoutDescription = new ResourceLayoutDescription( new LayoutElementDescription(0, ResourceType.ConstantBuffer, ShaderStages.Vertex), new LayoutElementDescription(0, ResourceType.Texture, ShaderStages.Pixel), new LayoutElementDescription(1, ResourceType.Texture, ShaderStages.Pixel), new LayoutElementDescription(0, ResourceType.Sampler, ShaderStages.Pixel), new LayoutElementDescription(1, ResourceType.Sampler, ShaderStages.Pixel)); ResourceLayout resourcesLayout = this.graphicsContext.Factory.CreateResourceLayout(ref layoutDescription); ResourceSetDescription resourceSetDescription = new ResourceSetDescription(resourcesLayout, this.constantBuffer, texture2D, textureFur, sampler1, sampler2); this.resourceSet = this.graphicsContext.Factory.CreateResourceSet(ref resourceSetDescription); var pipelineDescription = new GraphicsPipelineDescription() { PrimitiveTopology = PrimitiveTopology.TriangleList, InputLayouts = vertexLayouts, ResourceLayouts = new[] { resourcesLayout }, Shaders = new ShaderStateDescription() { VertexShader = vertexShader, PixelShader = pixelShader, }, RenderStates = new RenderStateDescription() { RasterizerState = RasterizerStates.None, BlendState = BlendStates.Opaque, DepthStencilState = DepthStencilStates.None, }, Outputs = this.frameBuffer.OutputDescription, }; this.graphicsPipelineState = this.graphicsContext.Factory.CreateGraphicsPipeline(ref pipelineDescription); this.graphicsCommandQueue = this.graphicsContext.Factory.CreateCommandQueue(); var swapChainDescription = this.swapChain?.SwapChainDescription; var width = swapChainDescription.HasValue ? swapChainDescription.Value.Width : this.surface.Width; var height = swapChainDescription.HasValue ? swapChainDescription.Value.Height : this.surface.Height; this.viewports = new Viewport[1]; this.viewports[0] = new Viewport(0, 0, width, height); this.scissors = new Rectangle[1]; this.scissors[0] = new Rectangle(0, 0, (int)width, (int)height); this.vertexBuffers = new Buffer[1]; this.vertexBuffers[0] = vertexBuffer; }