void Initialize(ComObject coreWindow, AntiAliasingMode antiAliasingMode)
        {
            InitializeInner(antiAliasingMode);

            var swapChainDesc = device.CreateSwapChainDescription();

            DXGI.Factory2 factory = device.QueryInterface <DXGI.Device1>().Adapter.GetParent <DXGI.Factory2>();
            swapChain = new DXGI.SwapChain1(factory, device, coreWindow, ref swapChainDesc);
        }
Exemple #2
0
    private int GetAntiAliasingMethod(AntiAliasingMode mode)
    {
        switch (mode)
        {
        case AntiAliasingMode.Off:
        case AntiAliasingMode.Fxaa:
            return((int)AntiAliasing.Method.Fxaa);

        case AntiAliasingMode.Smaa:
            return((int)AntiAliasing.Method.Smaa);

        default:
            throw new ArgumentOutOfRangeException("mode", mode, null);
        }
    }
Exemple #3
0
 public void GetImage(int width, int height, EventHandler taskEndEventHandler = null, int threadCount = 1,
                      bool depthFlag = false, int rayTracingMaxReflect = 0, AntiAliasingMode antiAliasing = AntiAliasingMode.SuperSampling)
 {
     this.width  = width;
     this.height = height;
     this.taskEndEventHandler  = taskEndEventHandler;
     this.threadCount          = threadCount;
     this.depthFlag            = depthFlag;
     this.rayTracingMaxReflect = rayTracingMaxReflect;
     this.antiAliasing         = antiAliasing;
     image = new MultiThreadImage(new Bitmap(width, height));
     dx    = 1.0 / width;
     dy    = 1.0 / height;
     StartTask();
 }
        public static DXGI.SampleDescription GetMultisamplingLevel(this D3D.Device device, AntiAliasingMode antiAliasingMode)
        {
            int desiredSampleCount = (int)((uint)antiAliasingMode & 0x0000FFFFu);
            int desiredQuality = (int)((uint)antiAliasingMode >> 16);

            int sampleCount = desiredSampleCount;
            int quality = desiredQuality;
            bool csaaModeFound = false;

            if (desiredQuality > 0) //CSAA is queried
            {
                for (; sampleCount > 0x4; sampleCount >>= 1)
                {
                    quality = device.CheckMultisampleQualityLevels(DXGI.Format.R8G8B8A8_UNorm, sampleCount) - 1;
                    if (quality >= desiredQuality)
                    {
                        csaaModeFound = true;
                        break;
                    }
                    if ((quality >>= 1) >= desiredQuality)
                    {
                        csaaModeFound = true;
                        break;
                    }
                    quality = desiredQuality;
                }
            }

            if (!csaaModeFound) //no CSAA available or not queried
            {
                sampleCount = desiredSampleCount;
                if (desiredQuality > 0) sampleCount <<= 1; //double queried sample count for equal quality

                for (; sampleCount > 0x1; sampleCount >>= 1)
                {
                    quality = device.CheckMultisampleQualityLevels(DXGI.Format.R8G8B8A8_UNorm, sampleCount) - 1;
                    if (quality >= 0) break;
                }
            }

            return new DXGI.SampleDescription(desiredSampleCount, quality);
        }
        /// <summary>
        /// Creates a renderer for a surface.
        /// </summary>
        /// <param name="surface">The target surface.</param>
        /// <param name="antiAliasingMode">The desired level of multisampling to be used by the renderer.</param>
        /// <remarks>The specified multisampling level is not guaranteed to be used if it is not supported by the current hardware. Instead the highest possible level is used.</remarks>
        /// <exception cref="System.ArgumentException">The specified surface is not a valid Win32 Window or Core Window.</exception>
        public Renderer(ISurface surface, AntiAliasingMode antiAliasingMode = AntiAliasingMode.None)
        {
            this.surface    = surface;
            surface.Resize += (sender, e) => resized = true;
            resized         = true;

            IWin32Window win32Window = surface.OutputTarget as IWin32Window;

            if (win32Window != null)
            {
                Initialize(win32Window, antiAliasingMode);
                return;
            }

            ComObject coreWindow = surface.OutputTarget as ComObject;

            if (coreWindow != null)
            {
                Initialize(coreWindow, antiAliasingMode);
                return;
            }

            throw new ArgumentException("Invalid output target, must be either a Win32 Window or a Core Window.");
        }
Exemple #6
0
        public static DXGI.SampleDescription GetMultisamplingLevel(this D3D.Device device, AntiAliasingMode antiAliasingMode)
        {
            int desiredSampleCount = (int)((uint)antiAliasingMode & 0x0000FFFFu);
            int desiredQuality     = (int)((uint)antiAliasingMode >> 16);

            int  sampleCount   = desiredSampleCount;
            int  quality       = desiredQuality;
            bool csaaModeFound = false;

            if (desiredQuality > 0) //CSAA is queried
            {
                for (; sampleCount > 0x4; sampleCount >>= 1)
                {
                    quality = device.CheckMultisampleQualityLevels(DXGI.Format.R8G8B8A8_UNorm, sampleCount) - 1;
                    if (quality >= desiredQuality)
                    {
                        csaaModeFound = true;
                        break;
                    }
                    if ((quality >>= 1) >= desiredQuality)
                    {
                        csaaModeFound = true;
                        break;
                    }
                    quality = desiredQuality;
                }
            }

            if (!csaaModeFound) //no CSAA available or not queried
            {
                sampleCount = desiredSampleCount;
                if (desiredQuality > 0)
                {
                    sampleCount <<= 1;                     //double queried sample count for equal quality
                }
                for (; sampleCount > 0x1; sampleCount >>= 1)
                {
                    quality = device.CheckMultisampleQualityLevels(DXGI.Format.R8G8B8A8_UNorm, sampleCount) - 1;
                    if (quality >= 0)
                    {
                        break;
                    }
                }
            }

            return(new DXGI.SampleDescription(desiredSampleCount, quality));
        }
        unsafe void InitializeInner(AntiAliasingMode antiAliasingMode)
        {
            worldMatrix = Matrix.Identity;
            textMatrix = Matrix.Identity;

            device = new D3D11.Device(DriverType.Hardware,
                D3D11.DeviceCreationFlags.None,
                new[] { FeatureLevel.Level_11_1, FeatureLevel.Level_11_0, FeatureLevel.Level_10_1, FeatureLevel.Level_10_0 });
            tkDevice = TK.GraphicsDevice.New(device);
            deviceContext = device.ImmediateContext;
            deviceContext.Rasterizer.State = device.CreateRasterizerState();

            //TODO: replace with precompiled bytecode
            const string shaderFile = @"..\..\..\GameUtils\Graphics\shaders.fx";
            ShaderBytecode vertexShaderBytecode = ShaderBytecode.CompileFromFile(shaderFile, "VS", "vs_4_0", ShaderFlags.Debug);
            ShaderBytecode pixelShaderBytecode = ShaderBytecode.CompileFromFile(shaderFile, "PS", "ps_4_0", ShaderFlags.Debug);
            InitializeShaders(vertexShaderBytecode, pixelShaderBytecode);

            indexBuffers = new D3D11.Buffer[BufferCount];
            vertexBuffers = new D3D11.Buffer[BufferCount];
            for (int i = 0, indexBufferSize = IndexBufferStartSize, vertexBufferSize = VertexBufferStartSize;
                i < BufferCount;
                i++, indexBufferSize <<= BufferSizeStep, vertexBufferSize <<= BufferSizeStep)
            {
                indexBuffers[i] = device.CreateDynamicBuffer(sizeof(int) * indexBufferSize, D3D11.BindFlags.IndexBuffer);
                vertexBuffers[i] = device.CreateDynamicBuffer(sizeof(Vertex) * vertexBufferSize, D3D11.BindFlags.VertexBuffer);
            }
            currentBufferIndex = 0;
            indexBuffer = indexBuffers[0];
            vertexBuffer = vertexBuffers[0];

            //indexBuffer = device.CreateDynamicBuffer(sizeof(int) * IndexBufferSize, D3D11.BindFlags.IndexBuffer);
            //vertexBuffer = device.CreateDynamicBuffer(sizeof(Vertex) * VertexBufferSize, D3D11.BindFlags.VertexBuffer);
            matrixBuffer = device.CreateConstantBuffer(sizeof(MatrixBuffer));
            brushBuffer = device.CreateConstantBuffer(sizeof(Brush.BrushBuffer));

            deviceContext.InputAssembler.SetIndexBuffer(indexBuffer, DXGI.Format.R32_UInt, 0);
            deviceContext.InputAssembler.SetVertexBuffers(0, new D3D11.VertexBufferBinding(vertexBuffer, sizeof(Vertex), 0));
            deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;

            blendState = device.CreateBlendState();
            deviceContext.OutputMerger.SetBlendState(blendState);

            currentWrapMode = WrapMode.Clamp;
            currentInterpolationMode = InterpolationMode.Linear;
            samplerState = device.CreateSamplerState(WrapMode.Clamp, InterpolationMode.Linear);
            deviceContext.PixelShader.SetSampler(0, samplerState);

            sampleDescription = device.GetMultisamplingLevel(antiAliasingMode);

            defaultDepthStencilState = device.CreateDepthStencilState(D3D11.Comparison.Always,
                D3D11.StencilOperation.Keep,
                D3D11.StencilOperation.Keep);
            clipDepthStencilState = device.CreateDepthStencilState(D3D11.Comparison.Never,
                D3D11.StencilOperation.Replace,
                D3D11.StencilOperation.Keep);
            clippingDepthStencilState = device.CreateDepthStencilState(D3D11.Comparison.Equal,
                D3D11.StencilOperation.Keep,
                D3D11.StencilOperation.Keep);
            deviceContext.OutputMerger.SetDepthStencilState(defaultDepthStencilState);
        }
        /// <summary>
        /// Creates a renderer for a surface.
        /// </summary>
        /// <param name="surface">The target surface.</param>
        /// <param name="antiAliasingMode">The desired level of multisampling to be used by the renderer.</param>
        /// <remarks>The specified multisampling level is not guaranteed to be used if it is not supported by the current hardware. Instead the highest possible level is used.</remarks>
        /// <exception cref="System.ArgumentException">The specified surface is not a valid Win32 Window or Core Window.</exception>
        public Renderer(ISurface surface, AntiAliasingMode antiAliasingMode = AntiAliasingMode.None)
        {
            this.surface = surface;
            surface.Resize += (sender, e) => resized = true;
            resized = true;

            IWin32Window win32Window = surface.OutputTarget as IWin32Window;
            if (win32Window != null)
            {
                Initialize(win32Window, antiAliasingMode);
                return;
            }

            ComObject coreWindow = surface.OutputTarget as ComObject;
            if (coreWindow != null)
            {
                Initialize(coreWindow, antiAliasingMode);
                return;
            }

            throw new ArgumentException("Invalid output target, must be either a Win32 Window or a Core Window.");
        }
        void Initialize(ComObject coreWindow, AntiAliasingMode antiAliasingMode)
        {
            InitializeInner(antiAliasingMode);

            var swapChainDesc = device.CreateSwapChainDescription();
            DXGI.Factory2 factory = device.QueryInterface<DXGI.Device1>().Adapter.GetParent<DXGI.Factory2>();
            swapChain = new DXGI.SwapChain1(factory, device, coreWindow, ref swapChainDesc);
        }
        unsafe void InitializeInner(AntiAliasingMode antiAliasingMode)
        {
            worldMatrix = Matrix.Identity;
            textMatrix  = Matrix.Identity;

            device = new D3D11.Device(DriverType.Hardware,
                                      D3D11.DeviceCreationFlags.None,
                                      new[] { FeatureLevel.Level_11_1, FeatureLevel.Level_11_0, FeatureLevel.Level_10_1, FeatureLevel.Level_10_0 });
            tkDevice      = TK.GraphicsDevice.New(device);
            deviceContext = device.ImmediateContext;
            deviceContext.Rasterizer.State = device.CreateRasterizerState();

            //TODO: replace with precompiled bytecode
            const string   shaderFile           = @"..\..\..\GameUtils\Graphics\shaders.fx";
            ShaderBytecode vertexShaderBytecode = ShaderBytecode.CompileFromFile(shaderFile, "VS", "vs_4_0", ShaderFlags.Debug);
            ShaderBytecode pixelShaderBytecode  = ShaderBytecode.CompileFromFile(shaderFile, "PS", "ps_4_0", ShaderFlags.Debug);

            InitializeShaders(vertexShaderBytecode, pixelShaderBytecode);

            indexBuffers  = new D3D11.Buffer[BufferCount];
            vertexBuffers = new D3D11.Buffer[BufferCount];
            for (int i = 0, indexBufferSize = IndexBufferStartSize, vertexBufferSize = VertexBufferStartSize;
                 i < BufferCount;
                 i++, indexBufferSize <<= BufferSizeStep, vertexBufferSize <<= BufferSizeStep)
            {
                indexBuffers[i]  = device.CreateDynamicBuffer(sizeof(int) * indexBufferSize, D3D11.BindFlags.IndexBuffer);
                vertexBuffers[i] = device.CreateDynamicBuffer(sizeof(Vertex) * vertexBufferSize, D3D11.BindFlags.VertexBuffer);
            }
            currentBufferIndex = 0;
            indexBuffer        = indexBuffers[0];
            vertexBuffer       = vertexBuffers[0];

            //indexBuffer = device.CreateDynamicBuffer(sizeof(int) * IndexBufferSize, D3D11.BindFlags.IndexBuffer);
            //vertexBuffer = device.CreateDynamicBuffer(sizeof(Vertex) * VertexBufferSize, D3D11.BindFlags.VertexBuffer);
            matrixBuffer = device.CreateConstantBuffer(sizeof(MatrixBuffer));
            brushBuffer  = device.CreateConstantBuffer(sizeof(Brush.BrushBuffer));

            deviceContext.InputAssembler.SetIndexBuffer(indexBuffer, DXGI.Format.R32_UInt, 0);
            deviceContext.InputAssembler.SetVertexBuffers(0, new D3D11.VertexBufferBinding(vertexBuffer, sizeof(Vertex), 0));
            deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;

            blendState = device.CreateBlendState();
            deviceContext.OutputMerger.SetBlendState(blendState);

            currentWrapMode          = WrapMode.Clamp;
            currentInterpolationMode = InterpolationMode.Linear;
            samplerState             = device.CreateSamplerState(WrapMode.Clamp, InterpolationMode.Linear);
            deviceContext.PixelShader.SetSampler(0, samplerState);

            sampleDescription = device.GetMultisamplingLevel(antiAliasingMode);

            defaultDepthStencilState = device.CreateDepthStencilState(D3D11.Comparison.Always,
                                                                      D3D11.StencilOperation.Keep,
                                                                      D3D11.StencilOperation.Keep);
            clipDepthStencilState = device.CreateDepthStencilState(D3D11.Comparison.Never,
                                                                   D3D11.StencilOperation.Replace,
                                                                   D3D11.StencilOperation.Keep);
            clippingDepthStencilState = device.CreateDepthStencilState(D3D11.Comparison.Equal,
                                                                       D3D11.StencilOperation.Keep,
                                                                       D3D11.StencilOperation.Keep);
            deviceContext.OutputMerger.SetDepthStencilState(defaultDepthStencilState);
        }