/// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public override void Dispose()
        {
            D3D11.ComputeShader shader = Interlocked.Exchange(ref _shader, null);

            if (shader != null)
            {
                Graphics.Log.Print($"Destroying {ShaderType} '{Name}' ({ID})", LoggingLevel.Verbose);
                shader.Dispose();
            }

            base.Dispose();
        }
Exemplo n.º 2
0
        private void GenerateHeightMaps()
        {
            ShaderBytecode shaderByteCode = ShaderBytecode.CompileFromFile(@"Data/Shaders/TerrainCompute.hlsl", "initTerrain", "cs_5_0", Shader.ShaderFlags);
            ComputeShader initTerrain = new ComputeShader(_context.DirectX.Device, shaderByteCode);
            shaderByteCode.Dispose();
            shaderByteCode = ShaderBytecode.CompileFromFile(@"Data/Shaders/TerrainCompute.hlsl", "initWater", "cs_5_0", Shader.ShaderFlags);
            ComputeShader initWater = new ComputeShader(_context.DirectX.Device, shaderByteCode);
            shaderByteCode.Dispose();
            shaderByteCode = ShaderBytecode.CompileFromFile(@"Data/Shaders/TerrainCompute.hlsl", "applyRandomDisplacement", "cs_5_0", Shader.ShaderFlags);
            _baseTerrainGeneration = new ComputeShader(_context.DirectX.Device, shaderByteCode);
            shaderByteCode.Dispose();
            shaderByteCode = ShaderBytecode.CompileFromFile(@"Data/Shaders/TerrainCompute.hlsl", "flowsCalculation", "cs_5_0", Shader.ShaderFlags);
            _flowsCalculation = new ComputeShader(_context.DirectX.Device, shaderByteCode);
            shaderByteCode.Dispose();
            shaderByteCode = ShaderBytecode.CompileFromFile(@"Data/Shaders/TerrainCompute.hlsl", "updateWaterLevel", "cs_5_0", Shader.ShaderFlags);
            _updateWaterLevel = new ComputeShader(_context.DirectX.Device, shaderByteCode);
            shaderByteCode.Dispose();

            Texture2DDescription textureDescription = new Texture2DDescription
            {
                ArraySize = 1,
                BindFlags = BindFlags.ShaderResource | BindFlags.UnorderedAccess,
                CpuAccessFlags = CpuAccessFlags.None,
                Format = Format.R32_Float,
                Height = TextureSize,
                Width = TextureSize,
                MipLevels = 1,
                OptionFlags = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Default
            };

            ConstantBuffer<ComputeData> computeBuffer = new ConstantBuffer<ComputeData>(_context);
            _context.DirectX.DeviceContext.ComputeShader.SetConstantBuffer(1, computeBuffer.Buffer);

            foreach (Face face in _faces)
            {
                Texture2D terrainTexture = new Texture2D(_context.DirectX.Device, textureDescription);
                face.TerrainSrv = new ShaderResourceView(_context.DirectX.Device, terrainTexture);
                face.TerrainUav = new UnorderedAccessView(_context.DirectX.Device, terrainTexture);
                terrainTexture.Dispose();

                Texture2D waterTexture = new Texture2D(_context.DirectX.Device, textureDescription);
                face.WaterSrv = new ShaderResourceView(_context.DirectX.Device, waterTexture);
                face.WaterUav = new UnorderedAccessView(_context.DirectX.Device, waterTexture);
                waterTexture.Dispose();

                Texture2D flowsTexture = new Texture2D(_context.DirectX.Device, textureDescription);
                face.FlowsLeftUav = new UnorderedAccessView(_context.DirectX.Device, flowsTexture);
                flowsTexture.Dispose();

                flowsTexture = new Texture2D(_context.DirectX.Device, textureDescription);
                face.FlowsTopUav = new UnorderedAccessView(_context.DirectX.Device, flowsTexture);
                flowsTexture.Dispose();

                flowsTexture = new Texture2D(_context.DirectX.Device, textureDescription);
                face.FlowsRightUav = new UnorderedAccessView(_context.DirectX.Device, flowsTexture);
                flowsTexture.Dispose();

                flowsTexture = new Texture2D(_context.DirectX.Device, textureDescription);
                face.FlowsBottomUav = new UnorderedAccessView(_context.DirectX.Device, flowsTexture);
                flowsTexture.Dispose();

                _context.DirectX.DeviceContext.ComputeShader.SetUnorderedAccessView(0, face.TerrainUav);
                _context.DirectX.DeviceContext.ComputeShader.SetUnorderedAccessView(1, face.WaterUav);

                _context.DirectX.DeviceContext.ComputeShader.Set(initTerrain);
                computeBuffer.Update(new ComputeData(TextureSize - 1 - BatchSize, 0, 0, 0.0f));
                _context.DirectX.DeviceContext.Dispatch(TextureSize / BatchSize, TextureSize / BatchSize, 1);

                _context.DirectX.DeviceContext.ComputeShader.Set(initWater);
                computeBuffer.Update(new ComputeData(TextureSize - 1 - BatchSize, 0, 0, 0.05f));
                _context.DirectX.DeviceContext.Dispatch(TextureSize / BatchSize, TextureSize / BatchSize, 1);

                _context.DirectX.DeviceContext.ComputeShader.Set(initTerrain);
                computeBuffer.Update(new ComputeData(TextureSize - 1 - BatchSize, BatchSize / 2, BatchSize / 2, 0.5f));
                _context.DirectX.DeviceContext.Dispatch(TextureSize / BatchSize - 1, TextureSize / BatchSize - 1, 1);
            }

             _planeBuffer = new ConstantBuffer<PlaneData>(_context);

            initTerrain.Dispose();
            computeBuffer.Dispose();
        }