Sample.Renderer.Renderer C# (CSharp) Method

Renderer() public method

public Renderer ( SharpDX.Direct3D11.Device device, Form window ) : System
device SharpDX.Direct3D11.Device
window System.Windows.Forms.Form
return System
        public Renderer(Device device, Form window)
        {
            // Default parameter values

            P1Color = new Vector3(1.0f, 0.0f, 0.0f);
            P2Color = new Vector3(0.0f, 1.0f, 0.0f);
            P3Color = new Vector3(0.0f, 0.0f, 1.0f);
            Wireframe = false;
            Scale = 0.5f;

            // Create swapchain for device and window

            this.window = window;
            this.device = device;

            this.factory = device.QueryInterface<DXGIDevice>().GetParent<Adapter>().GetParent<Factory>();

            this.swapChain = new SwapChain(factory, device, new SwapChainDescription()
            {
                BufferCount = 1,
                IsWindowed = true,
                Flags = SwapChainFlags.None,
                OutputHandle = window.Handle,
                SwapEffect = SwapEffect.Discard,
                Usage = Usage.RenderTargetOutput,
                SampleDescription = new SampleDescription(1, 0),
                ModeDescription = new ModeDescription()
                {
                    Width = 0,
                    Height = 0,
                    Format = Format.R8G8B8A8_UNorm,
                    RefreshRate = new Rational(60, 1),
                }
            });

            context = device.ImmediateContext;
            factory.MakeWindowAssociation(window.Handle, WindowAssociationFlags.IgnoreAll);

            // Load shaders, create vertex buffers and stuff

            vertexBuffer = new Buffer(device, new BufferDescription()
            {
                BindFlags = BindFlags.VertexBuffer,
                CpuAccessFlags = CpuAccessFlags.Write,
                OptionFlags = ResourceOptionFlags.None,
                SizeInBytes = (32 * 3),
                StructureByteStride = 16,
                Usage = ResourceUsage.Dynamic
            });

            var vertexShaderByteCode = ShaderBytecode.CompileFromFile("MiniTri.fx", "VS", "vs_4_0");
            vertexShader = new VertexShader(device, vertexShaderByteCode);

            var pixelShaderByteCode = ShaderBytecode.CompileFromFile("MiniTri.fx", "PS", "ps_4_0");
            pixelShader = new PixelShader(device, pixelShaderByteCode);

            // Get input layout from the vertex shader
            
            layout = new InputLayout(device, ShaderSignature.GetInputSignature(vertexShaderByteCode), new[] {
                new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
                new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0)
            });

            Resize(window.ClientSize); // first time resize
        }