void UpdateShaderValues(ComputeData cdata) { // Set the qualities of the textures Shader.SetGlobalTexture("VSM_ShadowTex1", _backTarget1); Shader.SetGlobalTexture("VSM_ShadowTex2", _backTarget2); Shader.SetGlobalFloat("VSM_InvNumCascades", 1f / cdata.numCascades); UpdateShadowCamTransformShaderValues(cdata); }
void FinalizeUpdateSteps(ComputeData cdata) { CustomBlit(null, _renderBackTarget1, _blur_material, 1f - 1f / _renderBackTarget1.height, 1f); CustomBlit(null, _renderBackTarget2, _blur_material, 1f - 1f / _renderBackTarget2.height, 1f); Swap(ref _backTarget1, ref _renderBackTarget1); /* might be identical, if !incremental */ Swap(ref _backTarget2, ref _renderBackTarget2); UpdateShaderValues(cdata); }
void InitComputeData(out ComputeData cdata) { /* ComputeData stores parameters that we want to remain constant over several frames * in UpdateShadowsIncrementalCascade(), even if they are public fields that may be * modified at a random point. */ cdata.numCascades = numCascades; cdata.resolution = _resolution; cdata.firstCascadeLevelSize = firstCascadeLevelSize; cdata.depthOfShadowRange = depthOfShadowRange; }
public void BindComputeData(ComputeData data) { foreach (var kernel in kernels.Values) { foreach (var buffer in data.buffers) { kernel.SetBuffer(buffer.Key, buffer.Value); } foreach (var texture in data.textures) { kernel.SetTexture(texture.Key, texture.Value); } } }
private void SetupServer() { while (true) { if (udpClient == null) { udpClient = new UdpClient(6000); addtoLogs("Server online..."); } try { bytes = udpClient.Receive(ref groupEP); inputData = Encoding.UTF8.GetString(bytes); setConnected(); ComputeData.ComputeReceivedData(inputData, _decimalSeparator); ComputeData.DecodeByteMic(inputData, bytes); //msgType = ComputeReceivedData(inputData); //if (msgType == MessageType.Bye) //{ // SetDisconnectedInfoToUI(); // udpClient.Close(); // udpClient = null; //} receiveMsg(inputData); string[] array = inputData.Split('|'); if (array[0] == "info") { SetDeviceInfo(array); } } //On time out catch (SocketException) { if (udpClient != null) { udpClient.Close(); } udpClient = null; addtoLogs("Client disconnected..."); } } }
void UpdateShadowCamTransformShaderValues(ComputeData cdata) { Vector3 size; size.y = cdata.firstCascadeLevelSize * internal_scale * 2; size.x = _shadowCam.aspect * size.y; size.z = cdata.depthOfShadowRange * internal_scale * 2; size.x = 1f / size.x; size.y = 1f / size.y; size.z = 128f / size.z; var mat = _shadowCam.transform.worldToLocalMatrix; Shader.SetGlobalMatrix("VSM_LightMatrix", Matrix4x4.Scale(size) * mat); Shader.SetGlobalMatrix("VSM_LightMatrixNormal", Matrix4x4.Scale(Vector3.one * 1.2f / cdata.resolution) * mat); #if UNITY_EDITOR ShowRenderTexturesForDebugging(); #endif }
void ComputeCascade(int lvl, ComputeData cdata) { _shadowCam.orthographicSize = cdata.firstCascadeLevelSize * internal_scale * Mathf.Pow(2, lvl); _shadowCam.nearClipPlane = -cdata.depthOfShadowRange * internal_scale; _shadowCam.farClipPlane = cdata.depthOfShadowRange * internal_scale; _shadowCam.RenderWithShader(_depthShader, onlyOpaqueCasters ? "RenderType" : ""); var rt = RenderTexture.GetTemporary(_resolution, _resolution, 0, RenderTextureFormat.ARGBHalf); rt.wrapMode = TextureWrapMode.Clamp; _blur_material.EnableKeyword("BLUR_Y"); CustomBlit(_target, rt, _blur_material, 0, 1); _blur_material.DisableKeyword("BLUR_Y"); float y1 = lvl / (float)cdata.numCascades; float y2 = (lvl + 1) / (float)cdata.numCascades; _blur_material.EnableKeyword("BLUR_LINEAR_AND_SQUARE_PART"); CustomBlit2(rt, _renderBackTarget1, _renderBackTarget2, _blur_material, y1, y2); _blur_material.DisableKeyword("BLUR_LINEAR_AND_SQUARE_PART"); RenderTexture.ReleaseTemporary(rt); }
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); }