Пример #1
0
        // Event-handler for DeviceManager.OnInitialize
        protected override void CreateDeviceDependentResources(DeviceManager deviceManager)
        {
            base.CreateDeviceDependentResources(deviceManager);

            // Release all resources
            RemoveAndDispose(ref vertexShader);
            RemoveAndDispose(ref vertexShaderBytecode);
            RemoveAndDispose(ref pixelShader);
            RemoveAndDispose(ref pixelShaderBytecode);

            RemoveAndDispose(ref depthVertexShader);
            RemoveAndDispose(ref depthVertexShaderBytecode);
            RemoveAndDispose(ref depthPixelShader);
            RemoveAndDispose(ref depthPixelShaderBytecode);

            RemoveAndDispose(ref vertexLayout);
            RemoveAndDispose(ref worldViewProjectionBuffer);
            RemoveAndDispose(ref depthStencilState);

            // Get a reference to the Device1 instance and context
            var device  = deviceManager.Direct3DDevice;
            var context = deviceManager.Direct3DContext;

            ShaderFlags shaderFlags = ShaderFlags.None;

#if DEBUG
            shaderFlags = ShaderFlags.Debug;
#endif
            // compile shader
            vertexShaderBytecode = ToDispose(ShaderBytecode.CompileFromFile("Primitive.hlsl", "VSMain", "vs_5_0", shaderFlags));
            vertexShader         = ToDispose(new VertexShader(device, vertexShaderBytecode));

            pixelShaderBytecode = ToDispose(ShaderBytecode.CompileFromFile("Primitive.hlsl", "PSMain", "ps_5_0", shaderFlags));
            pixelShader         = ToDispose(new PixelShader(device, pixelShaderBytecode));

            depthVertexShaderBytecode = ToDispose(ShaderBytecode.CompileFromFile("Depth.hlsl", "VSMain", "vs_5_0", shaderFlags));
            depthVertexShader         = ToDispose(new VertexShader(device, depthVertexShaderBytecode));
            depthPixelShaderBytecode  = ToDispose(ShaderBytecode.CompileFromFile("Depth.hlsl", "PSMain", "ps_5_0", shaderFlags));
            depthPixelShader          = ToDispose(new PixelShader(device, depthPixelShaderBytecode));

            // vertex input signature
            vertexLayout = ToDispose(new InputLayout(device,
                                                     vertexShaderBytecode.GetPart(ShaderBytecodePart.InputSignatureBlob),
                                                     new[]
            {
                new InputElement("SV_Position", 0, Format.R32G32B32A32_Float, 0, 0),
                new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0)
            }));

            // mvp matrix
            worldViewProjectionBuffer = ToDispose(new SharpDX.Direct3D11.Buffer(device, Utilities.SizeOf <Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0));

            // depth & stencil
            depthStencilState = ToDispose(new DepthStencilState(device,
                                                                new DepthStencilStateDescription()
            {
                IsDepthEnabled   = true,
                DepthComparison  = Comparison.Less,
                DepthWriteMask   = SharpDX.Direct3D11.DepthWriteMask.All,
                IsStencilEnabled = false,
                StencilReadMask  = 0xff,    // 0xff(no mask)
                StencilWriteMask = 0xff,    // 0xf(no mask)

                FrontFace = new DepthStencilOperationDescription()
                {
                    Comparison         = Comparison.Always,
                    PassOperation      = StencilOperation.Keep,
                    FailOperation      = StencilOperation.Keep,
                    DepthFailOperation = StencilOperation.Increment
                },

                BackFace = new DepthStencilOperationDescription()
                {
                    Comparison         = Comparison.Always,
                    PassOperation      = StencilOperation.Keep,
                    FailOperation      = StencilOperation.Keep,
                    DepthFailOperation = StencilOperation.Decrement
                }
            }));

            // assign input layout, constant buffer, vertex and pixel shaders and depth stencil state to appropriate graphics pipeline stages
            context.InputAssembler.InputLayout = vertexLayout;
            context.VertexShader.SetConstantBuffer(0, worldViewProjectionBuffer);
            context.VertexShader.Set(vertexShader);
            context.PixelShader.Set(pixelShader);
            context.OutputMerger.DepthStencilState = depthStencilState;
        }
Пример #2
0
        // Handler for DeviceManager.OnInitialize
        protected override void CreateDeviceDependentResources(DeviceManager deviceManager)
        {
            base.CreateDeviceDependentResources(deviceManager);

            // First release all ressources
            RemoveAndDispose(ref texture2D);
            RemoveAndDispose(ref vsByteCode);
            RemoveAndDispose(ref vsShader);
            RemoveAndDispose(ref psByteCode);
            RemoveAndDispose(ref psShader);
            RemoveAndDispose(ref vsLayout);
            RemoveAndDispose(ref depthStencilState);
            RemoveAndDispose(ref mvpBuffer);

            ShaderFlags flag = ShaderFlags.None;

#if DEBUG
            flag = ShaderFlags.Debug;
#endif
            var device  = deviceManager.Direct3DDevice;
            var context = deviceManager.Direct3DContext;

            // Compile and create vs shader
            vsByteCode = ToDispose(ShaderBytecode.CompileFromFile("Shaders/Simple.hlsl", "VSMain", "vs_5_0", flag));
            vsShader   = ToDispose(new VertexShader(device, vsByteCode));

            // Compile and create ps shader
            psByteCode = ToDispose(ShaderBytecode.CompileFromFile("Shaders/Simple.hlsl", "PSMain", "ps_5_0", flag));
            psShader   = ToDispose(new PixelShader(device, psByteCode));

            // Compile and create the depth vertex and pixel shaders
            // These shaders are for checking what the depth buffer should look like
            //depthVertexShaderBytecode = ToDispose(ShaderBytecode.CompileFromFile("Depth.hlsl", "VSMain", "vs_5_0", flag));
            //depthVertexShader = ToDispose(new VertexShader(device, depthVertexShaderBytecode));
            //depthPixelShaderBytecode = ToDispose(ShaderBytecode.CompileFromFile("Depth.hlsl", "PSMain", "ps_5_0", flag));
            //depthPixelShader = ToDispose(new PixelShader(device, depthPixelShaderBytecode));

            // Initialize vertex layout to match vs input structure
            // Input structure definition
            var input = new[]
            {
                // Position
                new InputElement("SV_Position", 0, Format.R32G32B32A32_Float, 0, 0),
                // Color
                new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0),
            };
            vsLayout = ToDispose(new InputLayout(device, vsByteCode.GetPart(ShaderBytecodePart.InputSignatureBlob), input));

            // Create the constant buffer to store the MVP matrix

            mvpBuffer = ToDispose(new Buffer(device, Utilities.SizeOf <Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0));

            // Create depth stencil state for OM

            depthStencilState = ToDispose(new DepthStencilState(device, new DepthStencilStateDescription
            {
                IsDepthEnabled   = true,
                DepthComparison  = Comparison.Less,
                DepthWriteMask   = DepthWriteMask.All,
                IsStencilEnabled = false,
                StencilReadMask  = 0xff, // no mask
                StencilWriteMask = 0xff,
                // Face culling
                FrontFace = new DepthStencilOperationDescription {
                    Comparison         = Comparison.Always,
                    PassOperation      = StencilOperation.Keep,
                    DepthFailOperation = StencilOperation.Increment,
                    FailOperation      = StencilOperation.Keep
                },

                BackFace = new DepthStencilOperationDescription
                {
                    Comparison         = Comparison.Always,
                    PassOperation      = StencilOperation.Keep,
                    DepthFailOperation = StencilOperation.Decrement,
                    FailOperation      = StencilOperation.Keep
                },
            }));

            // Tell IA what vertices will look like
            context.InputAssembler.InputLayout = vsLayout;

            // Bind buffers to vs
            context.VertexShader.SetConstantBuffer(0, mvpBuffer);

            // Set vs to run
            context.VertexShader.Set(vsShader);

            // Set pixel shader to run
            context.PixelShader.Set(psShader);

            // Set depth stencil to OM
            context.OutputMerger.DepthStencilState = depthStencilState;

            InitializeMatricies();
        }
        protected override void CreateDeviceDependentResources(DeviceManager deviceManager)
        {
            base.CreateDeviceDependentResources(deviceManager);

            // Release all resources
            RemoveAndDispose(ref vertexShader);
            RemoveAndDispose(ref vertexShaderBytecode);
            RemoveAndDispose(ref pixelShader);
            RemoveAndDispose(ref pixelShaderBytecode);

            RemoveAndDispose(ref depthVertexShader);
            RemoveAndDispose(ref depthVertexShaderBytecode);
            RemoveAndDispose(ref depthPixelShader);
            RemoveAndDispose(ref depthPixelShaderBytecode);

            RemoveAndDispose(ref vertexLayout);
            RemoveAndDispose(ref worldViewProjectionBuffer);
            RemoveAndDispose(ref depthStencilState);

            // Get a reference to the Device1 instance and immediate context
            var device  = deviceManager.Direct3DDevice;
            var context = deviceManager.Direct3DContext;

            ShaderFlags shaderFlags = ShaderFlags.None;

#if DEBUG
            shaderFlags = ShaderFlags.Debug;
#endif

            // Compile and create the vertex shader
            vertexShaderBytecode = ToDispose(ShaderBytecode.CompileFromFile("Simple.hlsl", "VSMain", "vs_5_0", shaderFlags));
            vertexShader         = ToDispose(new VertexShader(device, vertexShaderBytecode));

            // Compile and create the pixel shader
            pixelShaderBytecode = ToDispose(ShaderBytecode.CompileFromFile("Simple.hlsl", "PSMain", "ps_5_0", shaderFlags));
            pixelShader         = ToDispose(new PixelShader(device, pixelShaderBytecode));

            // Compile and create the depth vertex and pixel shaders
            // These shaders are for checking what the depth buffer should look like
            depthVertexShaderBytecode = ToDispose(ShaderBytecode.CompileFromFile("Depth.hlsl", "VSMain", "vs_5_0", shaderFlags));
            depthVertexShader         = ToDispose(new VertexShader(device, depthVertexShaderBytecode));
            depthPixelShaderBytecode  = ToDispose(ShaderBytecode.CompileFromFile("Depth.hlsl", "PSMain", "ps_5_0", shaderFlags));
            depthPixelShader          = ToDispose(new PixelShader(device, depthPixelShaderBytecode));

            // Layout from VertexShader input signature
            vertexLayout = ToDispose(new InputLayout(device,
                                                     vertexShaderBytecode.GetPart(ShaderBytecodePart.InputSignatureBlob),
                                                     //ShaderSignature.GetInputSignature(vertexShaderBytecode),
                                                     new[]
            {
                // input semantic SV_Position = vertex coordinate in object space
                new InputElement("SV_Position", 0, Format.R32G32B32A32_Float, 0, 0),
                // input semantic COLOR = vertex color
                new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0)
            }));

            // Create the constant buffer that will
            // store our worldViewProjection matrix
            worldViewProjectionBuffer = ToDispose(new SharpDX.Direct3D11.Buffer(device, Utilities.SizeOf <Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0));

            // Configure the depth buffer to discard pixels that are
            // further than the current pixel.
            depthStencilState = ToDispose(new DepthStencilState(device,
                                                                new DepthStencilStateDescription()
            {
                IsDepthEnabled   = true,   // enable depth?
                DepthComparison  = Comparison.Less,
                DepthWriteMask   = SharpDX.Direct3D11.DepthWriteMask.All,
                IsStencilEnabled = false,   // enable stencil?
                StencilReadMask  = 0xff,    // 0xff (no mask)
                StencilWriteMask = 0xff,    // 0xff (no mask)
                // Configure FrontFace depth/stencil operations
                FrontFace = new DepthStencilOperationDescription()
                {
                    Comparison         = Comparison.Always,
                    PassOperation      = StencilOperation.Keep,
                    FailOperation      = StencilOperation.Keep,
                    DepthFailOperation = StencilOperation.Increment
                },
                // Configure BackFace depth/stencil operations
                BackFace = new DepthStencilOperationDescription()
                {
                    Comparison         = Comparison.Always,
                    PassOperation      = StencilOperation.Keep,
                    FailOperation      = StencilOperation.Keep,
                    DepthFailOperation = StencilOperation.Decrement
                },
            }));

            // Tell the IA what the vertices will look like
            // in this case two 4-component 32bit floats
            // (32 bytes in total)
            context.InputAssembler.InputLayout = vertexLayout;

            // Set our constant buffer (to store worldViewProjection)
            context.VertexShader.SetConstantBuffer(0, worldViewProjectionBuffer);

            // Set the vertex shader to run
            context.VertexShader.Set(vertexShader);

            // Set the pixel shader to run
            context.PixelShader.Set(pixelShader);

            // Set our depth stencil state
            context.OutputMerger.DepthStencilState = depthStencilState;
        }
Пример #4
0
        /// <summary>
        /// Create any device dependent resources here.
        /// This method will be called when the device is first
        /// initialized or recreated after being removed or reset.
        /// </summary>
        protected override void CreateDeviceDependentResources()
        {
            // Ensure that if already set the device resources
            // are correctly disposed of before recreating
            RemoveAndDispose(ref vertexShader);
            RemoveAndDispose(ref vertexLayout);
            RemoveAndDispose(ref vertexBuffer);

            // Retrieve our SharpDX.Direct3D11.Device1 instance
            var device = DeviceManager.Direct3DDevice;

            ShaderFlags shaderFlags = ShaderFlags.None;

#if DEBUG
            shaderFlags = ShaderFlags.Debug | ShaderFlags.SkipOptimization;
#endif
            // Use our HLSL file include handler to resolve #include directives in the HLSL source
            var includeHandler = new HLSLFileIncludeHandler(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "Shaders"));

            // Compile and create the vertex shader
            using (ShaderBytecode vertexShaderBytecode = ShaderBytecode.CompileFromFile(@"Shaders\SAQuad.hlsl", "VSMain", "vs_5_0", shaderFlags, EffectFlags.None, null, includeHandler))
            {
                vertexShader = ToDispose(new VertexShader(device, vertexShaderBytecode));
                // Layout from VertexShader input signature
                vertexLayout = ToDispose(new InputLayout(device,
                                                         vertexShaderBytecode.GetPart(ShaderBytecodePart.InputSignatureBlob),
                                                         //ShaderSignature.GetInputSignature(vertexShaderBytecode),
                                                         new[]
                {
                    // "SV_Position" = vertex coordinate
                    new InputElement("SV_Position", 0, Format.R32G32B32_Float, 0, 0),
                }));

                // Create vertex buffer
                vertexBuffer = ToDispose(Buffer.Create(device, BindFlags.VertexBuffer, new Vector3[] {
                    /*  Position: float x 3 */
                    new Vector3(-1.0f, -1.0f, -1.0f),
                    new Vector3(-1.0f, 1.0f, -1.0f),
                    new Vector3(1.0f, -1.0f, -1.0f),
                    new Vector3(1.0f, 1.0f, -1.0f),
                }));
                vertexBinding = new VertexBufferBinding(vertexBuffer, Utilities.SizeOf <Vector3>(), 0);
                // Triangle strip:
                // v1   v3
                // |\   |
                // | \ B|
                // | A\ |
                // |   \|
                // v0   v2
            }

            // Compile and create the pixel shader
            using (var bytecode = ToDispose(ShaderBytecode.CompileFromFile(@"Shaders\SAQuad.hlsl", "PSMain", "ps_5_0", shaderFlags, EffectFlags.None, null, includeHandler)))
                pixelShader = ToDispose(new PixelShader(device, bytecode));

            using (var bytecode = ToDispose(ShaderBytecode.CompileFromFile(@"Shaders\SAQuad.hlsl", "PSMainMultisample", "ps_5_0", shaderFlags, EffectFlags.None, null, includeHandler)))
                pixelShaderMS = ToDispose(new PixelShader(device, bytecode));

            samplerState = ToDispose(new SamplerState(device, new SamplerStateDescription
            {
                Filter             = Filter.MinMagMipLinear,
                AddressU           = TextureAddressMode.Wrap,
                AddressV           = TextureAddressMode.Wrap,
                AddressW           = TextureAddressMode.Wrap,
                ComparisonFunction = Comparison.Never,
                MinimumLod         = 0,
                MaximumLod         = float.MaxValue
            }));

            //pointSamplerState = ToDispose(new SamplerState(device, new SamplerStateDescription
            //{
            //    Filter = Filter.MinMagMipPoint,
            //    AddressU = TextureAddressMode.Wrap,
            //    AddressV = TextureAddressMode.Wrap,
            //    AddressW = TextureAddressMode.Wrap,
            //    ComparisonFunction = Comparison.Never,
            //    MinimumLod = 0,
            //    MaximumLod = float.MaxValue
            //}));
        }