public BlendState(bool blendEnabled, BlendOperation blendOperation, BlendOperation alphaBlendOperation, BlendOption sourceBlend, BlendOption destinationBlend, BlendOption sourceAlphhaBlend, BlendOption destinationAlphaBlend) { RenderTargetBlendDescription targetDesc = new RenderTargetBlendDescription() { AlphaBlendOperation = alphaBlendOperation, BlendOperation = blendOperation, DestinationAlphaBlend = destinationAlphaBlend, SourceAlphaBlend = sourceAlphhaBlend, SourceBlend = sourceBlend, DestinationBlend = destinationBlend, IsBlendEnabled = blendEnabled, RenderTargetWriteMask = ColorWriteMaskFlags.All }; BlendStateDescription desc = new BlendStateDescription() { IndependentBlendEnable = false, }; desc.RenderTarget[0] = targetDesc; NativeBlendState = new SharpDX.Direct3D11.BlendState(GraphicManager.Device, desc); }
/// <summary> /// Function to convert this render target blend state to a D3D blend description. /// </summary> /// <returns>The D3D render target blend description.</returns> internal D3D.RenderTargetBlendDescription Convert() { var desc = new D3D.RenderTargetBlendDescription { AlphaBlendOperation = (D3D.BlendOperation)AlphaOperation, BlendOperation = (D3D.BlendOperation)BlendingOperation, IsBlendEnabled = IsBlendingEnabled, DestinationAlphaBlend = (D3D.BlendOption)DestinationAlphaBlend, DestinationBlend = (D3D.BlendOption)DestinationBlend, RenderTargetWriteMask = (D3D.ColorWriteMaskFlags)WriteMask, SourceAlphaBlend = (D3D.BlendOption)SourceAlphaBlend, SourceBlend = (D3D.BlendOption)SourceBlend }; return(desc); }
static BlendStateDescriptions() { AlphaBlendDescription = new RenderTargetBlendDescription() { IsBlendEnabled = true, RenderTargetWriteMask = ColorWriteMaskFlags.All, AlphaBlendOperation = BlendOperation.Add, BlendOperation = BlendOperation.Add, SourceAlphaBlend = BlendOption.SourceAlpha, SourceBlend = BlendOption.SourceAlpha, DestinationAlphaBlend = BlendOption.Zero, DestinationBlend = BlendOption.InverseSourceAlpha }; OverwriteBlendDescription = new RenderTargetBlendDescription() { IsBlendEnabled = true, RenderTargetWriteMask = ColorWriteMaskFlags.All, AlphaBlendOperation = BlendOperation.Add, BlendOperation = BlendOperation.Add, SourceAlphaBlend = BlendOption.One, SourceBlend = BlendOption.One, DestinationAlphaBlend = BlendOption.Zero, DestinationBlend = BlendOption.Zero }; DisabledBlendDescription = new RenderTargetBlendDescription() { IsBlendEnabled = false }; }
/// <summary> /// <p>Create a blend-state object that encapsulates blend state for the output-merger stage.</p> /// </summary> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="renderTargetBlend0">The render target blend description for the first render target.</param> /// <param name="blendFactor">The blend factor.</param> /// <param name="mask">The mask.</param> /// <returns>A new <see cref="BlendState" /> instance.</returns> /// <msdn-id>ff476500</msdn-id> /// <unmanaged>HRESULT ID3D11Device::CreateBlendState([In] const D3D11_BLEND_DESC* pBlendStateDesc,[Out, Fast] ID3D11BlendState** ppBlendState)</unmanaged> /// <unmanaged-short>ID3D11Device::CreateBlendState</unmanaged-short> /// <remarks><p>An application can create up to 4096 unique blend-state objects. For each object created, the runtime checks to see if a previous object has the same state. If such a previous object exists, the runtime will return a reference to previous instance instead of creating a duplicate object.</p></remarks> public static BlendState New(GraphicsDevice device, RenderTargetBlendDescription renderTargetBlend0, Color4 blendFactor, int mask = -1) { var description = BlendStateDescription.Default(); description.RenderTarget[0] = renderTargetBlend0; return new BlendState(device, description, blendFactor, mask); }
/// <summary> /// <p>Create a blend-state object that encapsulates blend state for the output-merger stage.</p> /// </summary> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="renderTargetBlend0">The render target blend description for the first render target.</param> /// <param name="mask">The mask.</param> /// <returns>A new <see cref="BlendState" /> instance.</returns> /// <msdn-id>ff476500</msdn-id> /// <unmanaged>HRESULT ID3D11Device::CreateBlendState([In] const D3D11_BLEND_DESC* pBlendStateDesc,[Out, Fast] ID3D11BlendState** ppBlendState)</unmanaged> /// <unmanaged-short>ID3D11Device::CreateBlendState</unmanaged-short> /// <remarks><p>An application can create up to 4096 unique blend-state objects. For each object created, the runtime checks to see if a previous object has the same state. If such a previous object exists, the runtime will return a reference to previous instance instead of creating a duplicate object.</p></remarks> public static BlendState New(GraphicsDevice device, RenderTargetBlendDescription renderTargetBlend0, int mask = -1) { return New(device, renderTargetBlend0, Color.White, mask); }
/// <summary> /// SetupBlendState /// </summary> void SetupBlendState () { var rtbd = new RenderTargetBlendDescription(); bool enabled = true; if ( BlendState.DstAlpha==Blend.Zero && BlendState.SrcAlpha==Blend.One && BlendState.DstColor==Blend.Zero && BlendState.SrcColor==Blend.One ) { enabled = false; } rtbd.IsBlendEnabled = enabled ; rtbd.BlendOperation = Converter.Convert( BlendState.ColorOp ); rtbd.AlphaBlendOperation = Converter.Convert( BlendState.AlphaOp ); rtbd.RenderTargetWriteMask = (ColorWriteMaskFlags)(int)BlendState.WriteMask; rtbd.DestinationBlend = Converter.Convert( BlendState.DstColor ); rtbd.SourceBlend = Converter.Convert( BlendState.SrcColor ); rtbd.DestinationAlphaBlend = Converter.Convert( BlendState.DstAlpha ); rtbd.SourceAlphaBlend = Converter.Convert( BlendState.SrcAlpha ); var bsd = new BlendStateDescription(); bsd.AlphaToCoverageEnable = false; bsd.IndependentBlendEnable = false; bsd.RenderTarget[0] = rtbd; blendFactor = SharpDXHelper.Convert( BlendState.BlendFactor ); blendMsaaMask = BlendState.MultiSampleMask; blendState = new D3DBlendState( device.Device, bsd ); }
/// <summary> /// Initializes a new engine, sets view , tex manager /// camera , mesh manager , texture manager , line manager /// volume manager /// </summary> /// <param name="width">The width of the Render target</param> /// <param name="height">The height of the Render target</param> public Engine(int Width, int Height, Form form) { // pass the settings of resolution Settings.Resolution = new System.Drawing.Size(Width, Height); /// set the handles for full screen or windows this.form = form; this.FormHandle = form.Handle; /// Create the factory which manages general graphics resources g_factory = new Factory(); g_factory.MakeWindowAssociation(this.FormHandle, WindowAssociationFlags.IgnoreAll); // find correct adapter int adapterCount = g_factory.GetAdapterCount(); //MessageBox.Show(adapterCount.ToString()); // we try to select the PerfHUD adapter for (int i = 0; i < adapterCount; i++) { Adapter adapt = g_factory.GetAdapter(i); //MessageBox.Show(adapt.Description.Description); if (adapt.Description.Description == "NVIDIA PerfHUD") { g_device = new Device( adapt, DeviceCreationFlags.Debug); } Console.WriteLine(i.ToString() + adapt.Description.Description); } if (g_device == null) { #if true /// Create the DirectX Device g_device = new Device(g_factory.GetAdapter(1), (Settings.Debug) ? DeviceCreationFlags.Debug : DeviceCreationFlags.None, new FeatureLevel[] { FeatureLevel.Level_11_0 }); #else g_device = new Device(DriverType.Warp, (Settings.Debug) ? DeviceCreationFlags.Debug : DeviceCreationFlags.None, new FeatureLevel[] { FeatureLevel.Level_11_0 }); #endif // check if we have one device to our system if (!(((g_device.FeatureLevel & FeatureLevel.Level_10_0) != 0) || ((g_device.FeatureLevel & FeatureLevel.Level_10_1) != 0) || ((g_device.FeatureLevel & FeatureLevel.Level_11_0) != 0))) { // if we don't have we just simulate #region Create the device base on swapChain /// Create a description of the display mode g_modeDesc = new ModeDescription(); /// Standard 32-bit RGBA g_modeDesc.Format = Format.R8G8B8A8_UNorm; /// Refresh rate of 60Hz (60 / 1 = 60) g_modeDesc.RefreshRate = new Rational(60, 1); /// Default g_modeDesc.Scaling = DisplayModeScaling.Unspecified; g_modeDesc.ScanlineOrdering = DisplayModeScanlineOrder.Progressive; /// ClientSize is the size of the /// form without the title and borders g_modeDesc.Width = Width; g_modeDesc.Height = Height; /// Create a description of the samping /// for multisampling or antialiasing g_sampleDesc = new SampleDescription(); /// No multisampling g_sampleDesc.Count = 1; g_sampleDesc.Quality = 0; /// Create a description of the swap /// chain or front and back buffers g_swapDesc = new SwapChainDescription(); /// link the ModeDescription g_swapDesc.ModeDescription = g_modeDesc; /// link the SampleDescription g_swapDesc.SampleDescription = g_sampleDesc; /// Number of buffers (including the front buffer) g_swapDesc.BufferCount = 1; g_swapDesc.Flags = SwapChainFlags.AllowModeSwitch; g_swapDesc.IsWindowed = true; /// The output window (the windows being rendered to) g_swapDesc.OutputHandle = this.FormHandle; /// Scrap the contents of the buffer every frame g_swapDesc.SwapEffect = SwapEffect.Discard; /// Indicate that this SwapChain /// is going to be a Render target g_swapDesc.Usage = Usage.RenderTargetOutput; //g_swapChain = new SwapChain(g_factory, g_device, g_swapDesc); try { /// Create the actual swap chain /// Here we set and the device type Device.CreateWithSwapChain(DriverType.Warp, (Settings.Debug) ? DeviceCreationFlags.Debug : DeviceCreationFlags.None, new FeatureLevel[] { Settings.FeatureLevel }, g_swapDesc, out g_device, out g_swapChain); } catch (Exception ex) { /// Create the actual swap chain /// Here we set and the device type Device.CreateWithSwapChain(DriverType.Reference, (Settings.Debug) ? DeviceCreationFlags.Debug : DeviceCreationFlags.None, new FeatureLevel[] { Settings.FeatureLevel }, g_swapDesc, out g_device, out g_swapChain); } /// Create the factory which manages general graphics resources g_factory = g_swapChain.GetParent<Factory>(); g_factory.MakeWindowAssociation(this.FormHandle, WindowAssociationFlags.IgnoreAll); #endregion } else { #if false #region Create the device base on swapChain /// Create a description of the display mode g_modeDesc = new ModeDescription(); /// Standard 32-bit RGBA g_modeDesc.Format = Format.R8G8B8A8_UNorm; /// Refresh rate of 60Hz (60 / 1 = 60) g_modeDesc.RefreshRate = new Rational(60, 1); /// Default g_modeDesc.Scaling = DisplayModeScaling.Unspecified; g_modeDesc.ScanlineOrdering = DisplayModeScanlineOrdering.Progressive; /// ClientSize is the size of the /// form without the title and borders g_modeDesc.Width = Width; g_modeDesc.Height = Height; /// Create a description of the samping /// for multisampling or antialiasing g_sampleDesc = new SampleDescription(); /// No multisampling g_sampleDesc.Count = 1; g_sampleDesc.Quality = 0; /// Create a description of the swap /// chain or front and back buffers g_swapDesc = new SwapChainDescription(); /// link the ModeDescription g_swapDesc.ModeDescription = g_modeDesc; /// link the SampleDescription g_swapDesc.SampleDescription = g_sampleDesc; /// Number of buffers (including the front buffer) g_swapDesc.BufferCount = 1; g_swapDesc.Flags = SwapChainFlags.None; g_swapDesc.IsWindowed = true; /// The output window (the windows being rendered to) g_swapDesc.OutputHandle = FormHandle; /// Scrap the contents of the buffer every frame g_swapDesc.SwapEffect = SwapEffect.Discard; /// Indicate that this SwapChain /// is going to be a Render target g_swapDesc.Usage = Usage.RenderTargetOutput; g_swapChain = new SwapChain(g_factory, g_device, g_swapDesc); #endregion #endif } // set the feature level Settings.FeatureLevel = g_device.FeatureLevel; } /// init mesh manager g_MeshManager = new Object3DManager(); /// init deferred device //DeferredDev1 = new DeviceContext( Engine.g_device ); //DeferredDev2 = new DeviceContext( Engine.g_device ); // init the image factory... g_image_factory = new ImagingFactory(); /// Set flag to indicate that engine is running g_Running = true; Logger.WriteLine("The Graphing Engine have be start "); // set the event handler RenderLoopHandler = new EventHandler(RenderLoop); var rtb = new RenderTargetBlendDescription() { IsBlendEnabled = true, BlendOperation = BlendOperation.Add, AlphaBlendOperation = BlendOperation.Add, DestinationBlend = BlendOption.One, DestinationAlphaBlend = BlendOption.Zero, SourceBlend = BlendOption.One, SourceAlphaBlend = BlendOption.One, RenderTargetWriteMask = ColorWriteMaskFlags.All }; BlendStateDescription blendDesc = new BlendStateDescription(); blendDesc.AlphaToCoverageEnable = false; blendDesc.IndependentBlendEnable = false; blendDesc.RenderTarget[0] = rtb; g_blendState = new BlendState(g_device, blendDesc); DepthStencilStateDescription dssd = new DepthStencilStateDescription(); dssd.DepthComparison = Comparison.Less; dssd.IsDepthEnabled = true; g_depthStencilState = new DepthStencilState(g_device, new DepthStencilStateDescription()); }
private static void SetupRenderTargetBlendDescriptionAccordingToTheMode(BlendMode blendMode, ref RenderTargetBlendDescription targetDescription) { switch (blendMode) { case BlendMode.Normal: targetDescription.DestinationBlend = BlendOption.InverseSourceAlpha; break; case BlendMode.Additive: break; case BlendMode.Subtractive: targetDescription.BlendOperation = BlendOperation.ReverseSubtract; break; case BlendMode.LightEffect: targetDescription.SourceBlend = BlendOption.DestinationColor; targetDescription.BlendOperation = BlendOperation.Add; break; default: targetDescription.IsBlendEnabled = false; break; } }
public bool InitializeShader(string shaderName) { try { // Set shader flags ShaderFlags sFlags = ShaderFlags.EnableStrictness; #if DEBUG sFlags |= ShaderFlags.Debug; #endif // Compile shader code CompilationResult vertexShaderByteCode = ShaderBytecode.CompileFromFile(shaderName, "VS", "vs_4_0", sFlags, EffectFlags.None); CompilationResult pixelShaderByteCode = ShaderBytecode.CompileFromFile(shaderName, "PS", "ps_4_0", sFlags, EffectFlags.None); // Create signature ShaderSignature inputSignature = ShaderSignature.GetInputSignature(vertexShaderByteCode); // Set shaders Set(ref m_vertexShader, new VertexShader(Device, vertexShaderByteCode)); Set(ref m_pixelShader, new PixelShader(Device, pixelShaderByteCode)); // Define inputs m_inputElements = new InputLayout(Device, inputSignature, new[]{ new InputElement("POSITION", 0, Format.R32G32B32_Float, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 12, 0), }); Device.ImmediateContext.InputAssembler.InputLayout = (m_inputElements); // Release the shader buffers. vertexShaderByteCode.Dispose(); pixelShaderByteCode.Dispose(); // Vagrant Story meshes are written as TriangleLists Device.ImmediateContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; RasterizerStateDescription rasterDesc = new RasterizerStateDescription() { CullMode = CullMode.Back, FillMode = FillMode.Solid, IsFrontCounterClockwise = true, DepthBias = 0, DepthBiasClamp = 0, SlopeScaledDepthBias = 0, IsDepthClipEnabled = true, IsMultisampleEnabled = true, IsScissorEnabled = false, IsAntialiasedLineEnabled = true, }; rasterizerState = new RasterizerState(Device, rasterDesc); Device.ImmediateContext.Rasterizer.State = rasterizerState; RenderTargetBlendDescription RTBDesc = new RenderTargetBlendDescription(true, BlendOption.SourceAlpha, BlendOption.InverseSourceAlpha, BlendOperation.Add, BlendOption.One, BlendOption.Zero, BlendOperation.Add, ColorWriteMaskFlags.All); BlendStateDescription bsDesc = new BlendStateDescription(); bsDesc.RenderTarget[0] = RTBDesc; blendState = new BlendState(Device, bsDesc); Device.ImmediateContext.OutputMerger.SetBlendState(blendState, null, -1); // Create constant matrix buffer m_matrixBuffer = new ConstantBuffer<MatrixBuffer>(Device); Device.ImmediateContext.VertexShader.SetConstantBuffer(0, m_matrixBuffer.Buffer); // Create a texture sampler state description. SamplerStateDescription samplerDesc = new SamplerStateDescription { Filter = Filter.MinMagMipPoint, AddressU = TextureAddressMode.Wrap, AddressV = TextureAddressMode.Wrap, AddressW = TextureAddressMode.Wrap, ComparisonFunction = Comparison.Never, MinimumLod = 0, MaximumLod = 0, }; m_samplerState = new SamplerState(Device, samplerDesc); DepthStencilStateDescription depthStencilStateDesc = new DepthStencilStateDescription { IsDepthEnabled = true, DepthWriteMask = DepthWriteMask.All, DepthComparison = Comparison.Less, IsStencilEnabled = true, FrontFace = new DepthStencilOperationDescription { FailOperation = StencilOperation.Keep, DepthFailOperation = StencilOperation.Increment, PassOperation = StencilOperation.Keep, Comparison = Comparison.Always, }, BackFace = new DepthStencilOperationDescription { FailOperation = StencilOperation.Keep, DepthFailOperation = StencilOperation.Decrement, PassOperation = StencilOperation.Keep, Comparison = Comparison.Always, }, }; depthStencilState = new DepthStencilState(Device, depthStencilStateDesc); } catch (Exception ex) { MessageBox.Show("Error initializing shader. Error is " + ex.Message); return false; }; Camera = new VSViewerCamera(); Camera.SetProjParams(65 * VSTools.Deg2Rad, 1.5f, 25.0f, 10000f); // vagrant story is -y up. Probably an artifact of the hardware restrictions. Camera.SetViewParams(new Vector3(0.0f, 0.0f, -500.0f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0, -1, 0)); return true; }