/// <summary>
        /// Main drawing method to be executed per-frame
        /// Draw vertices and call the callback between setting object-specific shaders and object-specific buffers.
        /// </summary>
        public void Draw()
        {
            if (!renderDeviceStarted)
            {
                renderDeviceStarted = true;
                RenderDeviceStarted();
            }

            // Clear with a color
            Clear(Color.Black);

            // Set raymarch shader buffer data
            {
                raymarchShaderBufferData.cameraPosition  = Scene.CurrentScene.ActiveCamera.Movement.Position;
                raymarchShaderBufferData.cameraDirection = Scene.CurrentScene.ActiveCamera.Movement.Forward;
                raymarchShaderBufferData.aspectRatio     = Engine.AspectRatio();
                raymarchShaderBufferData.time            = Engine.ElapsedTime; // TODO reset time when it is too large

                raymarchShaderBuffer.UpdateValue(raymarchShaderBufferData);

                primitivesBuffer[0].UpdateValue(
                    Scene.CurrentScene.Components <RaymarchRenderer <Sphere> >()
                    .Select(primitive => primitive.GetBufferData()).ToArray()
                    );

                primitivesBuffer[1].UpdateValue(
                    Scene.CurrentScene.Components <RaymarchRenderer <Box> >()
                    .Select(primitive => primitive.GetBufferData()).ToArray()
                    );

                primitivesBuffer[2].UpdateValue(
                    Scene.CurrentScene.Components <RaymarchRenderer <Primitives.Plane> >()
                    .Select(primitive => primitive.GetBufferData()).ToArray()
                    );

                /*primitivesBuffer[3].UpdateValue(
                 *  Engine.CurrentScene.GroupedPrimitives.GetPrimitivesOfType<Ellipsoid>()
                 *      .Select(primitive => primitive.GetBufferData()).ToArray()
                 * );
                 *
                 * primitivesBuffer[4].UpdateValue(
                 *  Engine.CurrentScene.GroupedPrimitives.GetPrimitivesOfType<Torus>()
                 *      .Select(primitive => primitive.GetBufferData()).ToArray()
                 * );
                 *
                 * primitivesBuffer[5].UpdateValue(
                 *  Engine.CurrentScene.GroupedPrimitives.GetPrimitivesOfType<CappedTorus>()
                 *      .Select(primitive => primitive.GetBufferData()).ToArray()
                 * );*/
            }

            // Draw raymarch plane
            raymarchRenderPlane.Draw();

            // Draw rendered scene to screen
            swapChain.Present(1, PresentFlags.None);
        }
Exemple #2
0
        private void Run()
        {
            var window = new TesselationParameterForm();
            window.Show();

            var deviceManager = new DeviceManager();
            deviceManager.Initialize(deviceCreationFlags: DeviceCreationFlags.Debug);

            var settings = new RenderFormSettings(800, 600, false, false, "hello!", WindowAssociationFlags.IgnoreAll);

            var renderWindow = new PresentationWindow(settings);
            renderWindow.SwapChain = deviceManager.CreateSwapChainForWindow(renderWindow.Handle, settings);

            var rtCreationSettings = new RenderTarget.Configuration.CreationSettings(
                new Size(settings.Width, settings.Height),
                true,
                new SampleDescription(1, 0),
                RenderTarget.Configuration.RenderTargetClearSettings.Default,
                RenderTarget.Configuration.DepthStencilClearSettings.Default);

            var renderTarget = deviceManager.RenderTargetManager.CreateRenderTargetFromSwapChain(renderWindow.SwapChain, rtCreationSettings);

            var context = deviceManager.Context;

            context.Rasterizer.SetViewports(new Viewport(0, 0, settings.Width, settings.Height, 0.0f, 1.0f));

            var vertexData = new[]
                                 {
                                     new VertexPosition {Position = new Vector3(0.0f, 0.5f, 0.5f)},
                                     new VertexPosition {Position = new Vector3(0.5f, -0.5f, 0.5f)},
                                     new VertexPosition {Position = new Vector3(-0.5f, -0.5f, 0.5f)},
                                 };

            var stride = Marshal.SizeOf(typeof (VertexPosition));

            var vertexBufferDesc = new BufferDescription(
                stride*vertexData.Length,
                ResourceUsage.Immutable,
                BindFlags.VertexBuffer,
                CpuAccessFlags.None,
                ResourceOptionFlags.None,
                stride);

            var vertexBuffer = Buffer.Create(deviceManager.Device, vertexData, vertexBufferDesc);

            var binding = new VertexBufferBinding(vertexBuffer, stride, 0);
            var inputLayout = new InputLayoutCache(deviceManager.Device).GetLayout<VertexPosition>();

            var fileName = "../../Shaders/triangle.fx";
            var hullShader = new HullShader(deviceManager.Device, deviceManager.ShaderCompiler.LoadByteCodeFromFile(ShaderCompiler.ShaderType.HullShader, fileName, "HS"));
            var domainerShader = new DomainShader(deviceManager.Device, deviceManager.ShaderCompiler.LoadByteCodeFromFile(ShaderCompiler.ShaderType.DomainShader, fileName, "DS"));
            var vertexShader = new VertexShader(deviceManager.Device, deviceManager.ShaderCompiler.LoadByteCodeFromFile(ShaderCompiler.ShaderType.VertexShader, fileName, "VS"));
            var pixelShader = new PixelShader(deviceManager.Device, deviceManager.ShaderCompiler.LoadByteCodeFromFile(ShaderCompiler.ShaderType.PixelShader, fileName, "PS"));

            Console.Out.WriteLine("");

            var rasterizerState = new RasterizerState(deviceManager.Device, new RasterizerStateDescription
                                                                                {
                                                                                    CullMode = CullMode.None,
                                                                                    FillMode = FillMode.Wireframe,
                                                                                    IsDepthClipEnabled = false,
                                                                                    IsFrontCounterClockwise = false,
                                                                                    IsScissorEnabled = false
                                                                                });

            var constantBuffer = new ConstantBuffer<TessellationParameters>(deviceManager.Device);
            var parameters = new TessellationParameters
                                 {
                                     TessellationFactor = 5
                                 };

            RenderLoop.Run(renderWindow, () =>
                                             {
                                                 renderTarget.SetActive(context);
                                                 renderTarget.Clear(context);

                                                 context.InputAssembler.SetVertexBuffers(0, binding);
                                                 context.InputAssembler.PrimitiveTopology = PrimitiveTopology.PatchListWith3ControlPoints;
                                                 context.InputAssembler.InputLayout = inputLayout;

                                                 context.VertexShader.Set(vertexShader);
                                                 context.PixelShader.Set(pixelShader);
                                                 context.HullShader.Set(hullShader);
                                                 context.DomainShader.Set(domainerShader);

                                                 parameters.TessellationFactor = window.TesselationFactor;

                                                 constantBuffer.UpdateValue(parameters);
                                                 context.HullShader.SetConstantBuffer(0, constantBuffer.Buffer);

                                                 //context.OutputMerger.DepthStencilState = null;

                                                 context.Rasterizer.State = rasterizerState;

                                                 context.Draw(3, 0);

                                                 renderWindow.Present();
                                             });

            deviceManager.Dispose();
        }