Example #1
0
        /// <summary>
        /// Function to rebuild the volumetric data used to render the volume texture.
        /// </summary>
        private void RebuildVolumeData()
        {
            for (int i = 0; i < _volumeRtSections.Length; i++)
            {
                _volumeSections[i]?.Dispose();
                _volumeRtSections[i]?.Dispose();
                _volumeRtSections[i] = GorgonRenderTarget2DView.CreateRenderTarget(_graphics, new GorgonTexture2DInfo($"Vol_RTV_{i}")
                {
                    Binding = TextureBinding.ShaderResource,
                    Format  = BufferFormat.R16G16B16A16_Float,
                    Width   = (int)VolumeRegion.Width,
                    Height  = (int)VolumeRegion.Height
                });
                _volumeSections[i] = _volumeRtSections[i].GetShaderResourceView();
            }

            var drawBuilder = new GorgonDrawIndexCallBuilder();

            _cubeDirDrawCall = drawBuilder
                               .ResetTo(_cubeDirDrawCall)
                               .ShaderResource(ShaderType.Pixel, _textureView, 0)
                               .SamplerState(ShaderType.Pixel, GorgonSamplerState.Default, 0)
                               .ShaderResource(ShaderType.Pixel, _volumeSections[0], 1)
                               .SamplerState(ShaderType.Pixel, GorgonSamplerState.Default, 1)
                               .ShaderResource(ShaderType.Pixel, _volumeSections[1], 2)
                               .SamplerState(ShaderType.Pixel, GorgonSamplerState.Default, 2)
                               .Build();
        }
Example #2
0
        /// <summary>Initializes a new instance of the <see cref="PlanetLayer"/> class.</summary>
        /// <param name="graphics">The graphics interface for the application.</param>
        /// <param name="resources">The resources for the application.</param>
        public PlanetLayer(GorgonGraphics graphics, ResourceManagement resources)
        {
            _graphics  = graphics;
            _resources = resources;

            _stateBuilder    = new GorgonPipelineStateBuilder(graphics);
            _drawCallBuilder = new GorgonDrawIndexCallBuilder();
        }
Example #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DrawCallFactory"/> class.
 /// </summary>
 /// <param name="graphics">The graphics interface to use when creating states.</param>
 /// <param name="defaultTexture">The default texture to use as a fallback when no texture is passed by renderable.</param>
 /// <param name="inputLayout">The input layout defining a vertex type.</param>
 public DrawCallFactory(GorgonGraphics graphics, GorgonTexture2DView defaultTexture, GorgonInputLayout inputLayout)
 {
     _inputLayout        = inputLayout;
     _drawIndexAllocator = new GorgonDrawCallPoolAllocator <GorgonDrawIndexCall>(128);
     _drawAllocator      = new GorgonDrawCallPoolAllocator <GorgonDrawCall>(128);
     _drawIndexBuilder   = new GorgonDrawIndexCallBuilder();
     _drawBuilder        = new GorgonDrawCallBuilder();
     _stateBuilder       = new GorgonPipelineStateBuilder(graphics);
     _defaultTexture     = defaultTexture;
 }
Example #4
0
        /// <summary>
        /// Function to initialize the states for the objects to draw.
        /// </summary>
        private static void InitializeStates()
        {
            var drawBuilder            = new GorgonDrawIndexCallBuilder();
            var stateBuilder           = new GorgonPipelineStateBuilder(_graphics);
            GorgonSamplerState sampler = new GorgonSamplerStateBuilder(_graphics)
                                         .Filter(SampleFilter.MinMagMipPoint)
                                         .Wrapping(TextureWrap.Wrap, TextureWrap.Wrap)
                                         .Build();


            // Initialize our draw call so we can render the objects.
            // All objects are using triangle lists, so we must tell the draw call that's what we need to render.
            for (int i = 0; i < 2; ++i)
            {
                _planes[i].Material.TextureSampler = sampler;
                _drawCalls[i] = drawBuilder.VertexBuffer(_inputLayout, _planes[i].VertexBufferBindings[0])
                                .IndexBuffer(_planes[i].IndexBuffer, 0, _planes[i].IndexBuffer.IndexCount)
                                .SamplerState(ShaderType.Pixel, sampler)
                                .ShaderResource(ShaderType.Pixel, _planes[i].Material.Texture)
                                .ConstantBuffer(ShaderType.Vertex, _wvpBuffer)
                                .ConstantBuffer(ShaderType.Pixel, _materialBuffer)
                                .PipelineState(stateBuilder.DepthStencilState(GorgonDepthStencilState.DepthStencilEnabledGreaterEqual)
                                               .PixelShader(_pixelShader)
                                               .VertexShader(_vertexShader))
                                .Build();
            }

            // For our sphere, we can just reuse the builder(s) since only a small part of the resources have changed.
            _sphere.Material.TextureSampler = sampler;
            _drawCalls[2] = drawBuilder.VertexBuffer(_inputLayout, _sphere.VertexBufferBindings[0])
                            .ShaderResource(ShaderType.Pixel, _sphere.Material.Texture)
                            .IndexBuffer(_sphere.IndexBuffer, 0, _sphere.IndexBuffer.IndexCount)
                            .Build();

            // Set up our view matrix.
            // Move the camera (view matrix) back 2.2 units.  This will give us enough room to see what's
            // going on.
            DX.Matrix.Translation(0, 0, 2.2f, out _viewMatrix);

            // Set up our projection matrix.
            // This matrix is probably the cause of almost EVERY problem you'll ever run into in 3D programming. Basically we're telling the renderer that we
            // want to have a vertical FOV of 75 degrees, with the aspect ratio based on our form width and height.  The final values indicate how to
            // distribute Z values across depth (tip: it's not linear).
            _projMatrix = DX.Matrix.PerspectiveFovLH((75.0f).ToRadians(), _mainForm.ClientSize.Width / (float)_mainForm.ClientSize.Height, 500.0f, 0.125f);
        }
Example #5
0
        /// <summary>
        /// Function used to build the resources required by the volume renderer.
        /// </summary>
        public void CreateResources()
        {
            _cubeVs        = GorgonShaderFactory.Compile <GorgonVertexShader>(_graphics, Resources.VolumeRenderShaders, "VolumeVS", true);
            _cubePosShader = GorgonShaderFactory.Compile <GorgonPixelShader>(_graphics, Resources.VolumeRenderShaders, "VolumePositionPS", true);
            _cubeDirShader = GorgonShaderFactory.Compile <GorgonPixelShader>(_graphics, Resources.VolumeRenderShaders, "VolumeRayCastPS", true);
            _inputLayout   = GorgonInputLayout.CreateUsingType <CubeVertex>(_graphics, _cubeVs);
            _cube          = new Cube(_graphics, _inputLayout);

            _cubeTransform = new GorgonConstantBuffer(_graphics, new GorgonConstantBufferInfo
            {
                SizeInBytes = DX.Matrix.SizeInBytes
            });

            _volumeRayParams = new GorgonConstantBuffer(_graphics, new GorgonConstantBufferInfo
            {
                SizeInBytes = Unsafe.SizeOf <VolumeRayParameters>()
            });

            _volumeScaleFactor = new GorgonConstantBuffer(_graphics, new GorgonConstantBufferInfo
            {
                SizeInBytes = DX.Vector4.SizeInBytes
            });

            // Our camera is never changing, so we only need to define it here.
            DX.Matrix.Translation(0, 0, 1.5f, out _view);
            ResizeRenderRegion();

            UpdateCubeTransform();

            var pipelineBuilder = new GorgonPipelineStateBuilder(_graphics);
            var drawBuilder     = new GorgonDrawIndexCallBuilder();

            pipelineBuilder
            .PixelShader(_cubePosShader)
            .VertexShader(_cubeVs);

            // Position draw calls.
            _cubePosDrawCull = drawBuilder
                               .ConstantBuffer(ShaderType.Vertex, _cubeTransform.GetView())
                               .ConstantBuffer(ShaderType.Vertex, _volumeScaleFactor.GetView(), 1)
                               .PipelineState(pipelineBuilder)
                               .IndexBuffer(_cube.IndexBuffer, indexCount: _cube.IndexBuffer.IndexCount)
                               .VertexBuffer(_inputLayout, _cube.VertexBuffer[0])
                               .Build();

            pipelineBuilder
            .RasterState(GorgonRasterState.CullFrontFace);

            _cubePosDrawFrontCull = drawBuilder
                                    .PipelineState(pipelineBuilder)
                                    .Build();

            // Raycasting draw call.
            pipelineBuilder
            .PixelShader(_cubeDirShader)
            .RasterState(GorgonRasterState.Default);

            _cubeDirDrawCall = drawBuilder
                               .ConstantBuffer(ShaderType.Pixel, _volumeRayParams.GetView(), 0)
                               .PipelineState(pipelineBuilder)
                               .Build();
        }