/// <summary> /// Applies the given material to the renderer. /// </summary> /// <param name="resourceToApply">The material to apply.</param> /// <param name="instancingMode">The instancing mode for which to apply the material.</param> internal void ApplyMaterial(MaterialResource resourceToApply, MaterialApplyInstancingMode instancingMode) { // Use forced material if any set if ((m_forcedMaterial != null) && (resourceToApply != m_forcedMaterial)) { resourceToApply = m_forcedMaterial; } // Disable logic if given material is null if (resourceToApply == null) { m_lastAppliedMaterial = null; return; } if ((m_lastAppliedMaterial != resourceToApply) || (m_lastMaterialInstancingMode != instancingMode)) { // Apply material (material or instancing mode has changed) resourceToApply.Apply(this, instancingMode, m_lastAppliedMaterial); m_lastAppliedMaterial = resourceToApply; m_lastMaterialInstancingMode = instancingMode; } }
/// <summary> /// Applies the material to the given render state. /// </summary> /// <param name="renderState">Current render state</param> /// <param name="instancingMode">The instancing mode for which to apply the material.</param> /// <param name="previousMaterial">The previously applied material.</param> /// <exception cref="SeeingSharpGraphicsException">Effect + this.Effect + not supported!</exception> internal override void Apply(RenderState renderState, MaterialApplyInstancingMode instancingMode, MaterialResource previousMaterial) { D3D11.DeviceContext deviceContext = renderState.Device.DeviceImmediateContextD3D11; bool isResourceSameType = (previousMaterial != null) && (previousMaterial.ResourceType == base.ResourceType); if (!isResourceSameType) { deviceContext.PixelShader.SetSampler(0, m_defaultResources.GetSamplerState(TextureSamplerQualityLevel.Low)); } // Set texture resource (if set) if (m_textureResource != null) { deviceContext.PixelShader.SetShaderResource(0, m_textureResource.TextureView); } else { deviceContext.PixelShader.SetShaderResource(0, null); } // Set shader resources deviceContext.VertexShader.Set(m_vertexShader.VertexShader); deviceContext.PixelShader.Set(m_pixelShader.PixelShader); }
/// <summary> /// Applies the material to the given render state. /// </summary> /// <param name="renderState">Current render state</param> /// <param name="instancingMode">The instancing mode for which to apply the material.</param> /// <param name="previousMaterial">The previously applied material.</param> internal override void Apply(RenderState renderState, MaterialApplyInstancingMode instancingMode, MaterialResource previousMaterial) { D3D11.DeviceContext deviceContext = renderState.Device.DeviceImmediateContextD3D11; bool isResourceSameType = (previousMaterial != null) && (previousMaterial.ResourceType == base.ResourceType); // Apply local shader configuration if (m_cbPerMaterialDataChanged) { m_cbPerMaterial.SetData( deviceContext, new CBPerMaterial() { ClipFactor = m_clipFactor, MaxClipDistance = m_maxClipDistance, Texture0Factor = m_textureResource != null ? 1f : 0f, AdjustTextureCoordinates = m_adjustTextureCoordinates ? 1f : 0f, AddToAlpha = m_addToAlpha, MaterialDiffuseColor = m_materialDiffuseColor }); m_cbPerMaterialDataChanged = false; } // Apply sampler and constants if (!isResourceSameType) { deviceContext.PixelShader.SetSampler(0, m_defaultResources.GetSamplerState(TextureSamplerQualityLevel.Low)); } deviceContext.PixelShader.SetConstantBuffer(3, m_cbPerMaterial.ConstantBuffer); deviceContext.VertexShader.SetConstantBuffer(3, m_cbPerMaterial.ConstantBuffer); // Set texture resource (if set) if ((m_textureResource != null) && (renderState.ViewInformation.ViewConfiguration.ShowTexturesInternal)) { deviceContext.PixelShader.SetShaderResource(0, m_textureResource.TextureView); } else { deviceContext.PixelShader.SetShaderResource(0, null); } // Set shader resources if (!isResourceSameType) { deviceContext.VertexShader.Set(m_vertexShader.VertexShader); deviceContext.PixelShader.Set(m_pixelShader.PixelShader); } }
/// <summary> /// Applies current render target settings. /// </summary> public void ClearState() { if (m_disposed) { throw new ObjectDisposedException("RenderState"); } // Clear material properties m_lastAppliedMaterial = null; m_forcedMaterial = null; m_lastMaterialInstancingMode = MaterialApplyInstancingMode.SingleObject; m_device.DeviceImmediateContextD3D11.ClearState(); if (m_currentRenderSettings != null) { m_currentRenderSettings.Apply(m_device.DeviceImmediateContextD3D11); } }
/// <summary> /// Applies the material to the given render state. /// </summary> /// <param name="renderState">Current render state</param> /// <param name="instancingMode">The instancing mode for which to apply the material.</param> /// <param name="previousMaterial">The previously applied material.</param> internal override void Apply(RenderState renderState, MaterialApplyInstancingMode instancingMode, MaterialResource previousMaterial) { D3D11.DeviceContext deviceContext = renderState.Device.DeviceImmediateContextD3D11; // Apply local shader configuration if (m_cbPerMaterialDataChanged) { m_cbPerMaterial.SetData( deviceContext, new CBPerMaterial() { FadeIntensity = m_fadeIntensity }); m_cbPerMaterialDataChanged = false; } // Apply constants and shader resources deviceContext.VertexShader.Set(m_vertexShader.VertexShader); deviceContext.PixelShader.Set(m_pixelShader.PixelShader); deviceContext.PixelShader.SetConstantBuffer(3, m_cbPerMaterial.ConstantBuffer); deviceContext.VertexShader.SetConstantBuffer(3, m_cbPerMaterial.ConstantBuffer); }
/// <summary> /// Generates the requested input layout. /// </summary> /// <param name="device">The device on which to create the input layout.</param> /// <param name="inputElements">An array of InputElements describing vertex input structure.</param> /// <param name="instancingMode">Instancing mode for which to generate the input layout for.</param> /// <returns></returns> internal override D3D11.InputLayout GenerateInputLayout(EngineDevice device, D3D11.InputElement[] inputElements, MaterialApplyInstancingMode instancingMode) { switch (instancingMode) { case MaterialApplyInstancingMode.SingleObject: return(new D3D11.InputLayout(device.DeviceD3D11_1, m_vertexShader.ShaderBytecode, inputElements)); default: throw new SeeingSharpGraphicsException(this.GetType() + " does not support " + typeof(MaterialApplyInstancingMode) + "." + instancingMode + "!"); } }
/// <summary> /// An internal helper method which tells the RenderState to clear /// the cached material resource, which was applied lastly. /// This method musst be called if other parts (e. g. postprocessing) work /// with shaders or such like outside of the renderstate. /// </summary> internal void ClearChachedAppliedMaterial() { m_lastAppliedMaterial = null; m_lastMaterialInstancingMode = MaterialApplyInstancingMode.SingleObject; }
/// <summary> /// Generates the requested input layout. /// </summary> /// <param name="device">The device on which to create the input layout.</param> /// <param name="inputElements">An array of InputElements describing vertex input structure.</param> /// <param name="instancingMode">Instancing mode for which to generate the input layout for.</param> internal abstract D3D11.InputLayout GenerateInputLayout(EngineDevice device, D3D11.InputElement[] inputElements, MaterialApplyInstancingMode instancingMode);
/// <summary> /// Applies the material to the given render state. /// </summary> /// <param name="renderState">Current render state</param> /// <param name="instancingMode">The instancing mode for which to apply the material.</param> /// <param name="previousMaterial">The previously applied material.</param> internal virtual void Apply(RenderState renderState, MaterialApplyInstancingMode instancingMode, MaterialResource previousMaterial) { }