/// <summary> /// Used to override Detach /// </summary> protected virtual void OnDetach() { IsAttached = false; renderTechnique = null; effect = null; renderHost = null; }
/// <summary> /// Attaches the element to the specified host. /// </summary> /// <param name="host">The host.</param> public virtual void Attach(IRenderHost host) { renderTechnique = this.renderTechnique == null ? host.RenderTechnique : this.renderTechnique; renderHost = host; effect = renderHost.EffectsManager.GetEffect(renderTechnique); InvalidateRender(); }
/// <summary> /// Attaches the element to the specified host. /// </summary> /// <param name="host">The host.</param> public virtual void Attach(IRenderHost host) { this.renderTechnique = this.renderTechnique == null ? host.RenderTechnique : this.renderTechnique; this.effect = EffectsManager.Instance.GetEffect(renderTechnique); this.renderHost = host; this.InvalidateRender(); }
public void AddRenderTechnique(string techniqueName, byte[] techniqueSource) { var technique = new RenderTechnique(techniqueName); if (!TechniquesSourceDict.ContainsKey(technique)) { TechniquesSourceDict.Add(technique, techniqueSource); } renderTechniques.Add(techniqueName, technique); }
/// <summary> /// <para>Attaches the element to the specified host. To overide Attach, please override <see cref="OnAttach(IRenderHost)"/> function.</para> /// <para>To set different render technique instead of using technique from host, override <see cref="SetRenderTechnique"/></para> /// <para>Attach Flow: <see cref="SetRenderTechnique(IRenderHost)"/> -> Set RenderHost -> Get Effect -> <see cref="OnAttach(IRenderHost)"/> -> <see cref="OnAttached"/> -> <see cref="InvalidateRender"/></para> /// </summary> /// <param name="host">The host.</param> public void Attach(IRenderHost host) { if (IsAttached || host == null) { return; } renderHost = host; if (host.EffectsManager == null) { throw new ArgumentException("EffectManger does not exist. Please make sure the proper EffectManager has been bind from view model."); } this.renderTechnique = SetRenderTechnique(host); effect = renderHost.EffectsManager.GetEffect(renderTechnique); IsAttached = OnAttach(host); if (IsAttached) { OnAttached(); } InvalidateRender(); }
/// <summary> /// Register an InputLayout for a set of RenderTechniques /// </summary> /// <param name="techniques">An array of RenderTechnique objects.</param> /// <param name="layout">An InputLayout object.</param> protected void RegisterLayout(RenderTechnique[] techniques, InputLayout layout) { foreach (var tech in techniques) data[tech.Name + "Layout"] = layout; }
/// <summary> /// Register an InputLayout for a RenderTechnique. /// </summary> /// <param name="technique">A RenderTechnique object.</param> /// <param name="layout">An InputLayout object.</param> private void RegisterLayout(RenderTechnique technique, InputLayout layout) { data[technique.Name + "Layout"] = layout; }
/// <summary> /// Register an effect for a RenderTechnique. /// /// Override in a derived class to control how effects are registered. /// </summary> /// <param name="shaderEffectString">A string representing the shader code.</param> /// <param name="techniqueName"></param> /// <param name="sFlags"></param> /// <param name="eFlags"></param> protected void RegisterEffect(string shaderEffectString, RenderTechnique technique, ShaderFlags sFlags = ShaderFlags.None, EffectFlags eFlags = EffectFlags.None) { RegisterEffect(shaderEffectString, new[] { technique }, sFlags, eFlags); }
/// <summary> /// Register an effect for a set of RenderTechniques. /// </summary> /// <param name="shaderEffectString">A string representing the shader code.</param> /// <param name="techniques">A set of RenderTechnique objects for which to associate the Effect.</param> /// <param name="sFlags"></param> /// <param name="eFlags"></param> protected void RegisterEffect(string shaderEffectString, RenderTechnique[] techniques, ShaderFlags sFlags = ShaderFlags.None, EffectFlags eFlags = EffectFlags.None) { #if PRECOMPILED_SHADERS try { var shaderBytes = ((DefaultRenderTechniquesManager)renderTechniquesManager).TechniquesSourceDict[techniques[0]]; RegisterEffect(shaderBytes, techniques); } catch (Exception ex) { //System.Windows.MessageBox.Show(string.Format("Error registering effect: {0}", ex.Message), "Error"); Debug.WriteLine(string.Format("Error registering effect: {0}", ex.Message), "Error"); throw; } #else #if DEBUG sFlags |= ShaderFlags.Debug; eFlags |= EffectFlags.None; #else sFlags |= ShaderFlags.OptimizationLevel3; eFlags |= EffectFlags.None; #endif var preposessMacros = new List<ShaderMacro>(); #if DEFERRED #if DEFERRED_MSAA preposessMacros.Add(new ShaderMacro("DEFERRED_MSAA", true)); #endif #if SSAO preposessMacros.Add(new ShaderMacro("SSAO", true)); #endif #endif var preprocess = ShaderBytecode.Preprocess(shaderEffectString, preposessMacros.ToArray(), new IncludeHandler()); var hashCode = preprocess.GetHashCode(); if (!File.Exists(hashCode.ToString())) { try { var shaderBytes = ShaderBytecode.Compile(preprocess, "fx_5_0", sFlags, eFlags); shaderBytes.Bytecode.Save(hashCode.ToString()); this.RegisterEffect(shaderBytes.Bytecode, techniques); } catch (Exception ex) { //System.Windows.MessageBox.Show(string.Format("Error compiling effect: {0}", ex.Message), "Error"); Debug.WriteLine(string.Format("Error compiling effect: {0}", ex.Message), "Error"); throw; } } else { var shaderBytes = ShaderBytecode.FromFile(hashCode.ToString()); this.RegisterEffect(shaderBytes, techniques); } #endif }
/// <summary> /// Register an effect for a set of RenderTechniques. /// </summary> /// <param name="shaderEffectBytecode">A byte array representing the compiled shader.</param> /// <param name="techniques">A set of RenderTechnique objects for which to associate the Effect.</param> /// <param name="eFlags"></param> protected void RegisterEffect(byte[] shaderEffectBytecode, RenderTechnique[] techniques, EffectFlags eFlags = EffectFlags.None) { var effect = new Effect(device, shaderEffectBytecode, eFlags); foreach (var tech in techniques) data[tech.Name] = effect; }
/// <summary> /// Detaches the element from the host. /// </summary> public virtual void Detach() { renderTechnique = null; effect = null; renderHost = null; }
private void Render() { var device = this.device; if (device == null) return; var renderTarget = colorBuffer; if (renderTarget == null) return; if (Renderable != null) { /// --------------------------------------------------------------------------- /// this part is done only if the scene is not attached /// it is an attach and init pass for all elements in the scene-graph if (!sceneAttached) { try { Light3D.LightCount = 0; sceneAttached = true; ClearColor = renderRenderable.BackgroundColor; IsShadowMapEnabled = renderRenderable.IsShadowMappingEnabled; var blinn = RenderTechniquesManager.RenderTechniques[DefaultRenderTechniqueNames.Blinn]; RenderTechnique = renderRenderable.RenderTechnique == null ? blinn : renderRenderable.RenderTechnique; if (renderContext != null) { renderContext.Dispose(); } renderContext = new RenderContext(this, EffectsManager.GetEffect(RenderTechnique)); renderRenderable.Attach(this); if (SupportDeferredRender) { gbuffer = RenderTechniquesManager.RenderTechniques[DeferredRenderTechniqueNames.GBuffer]; deferred = RenderTechniquesManager.RenderTechniques[DeferredRenderTechniqueNames.Deferred]; if (RenderTechnique == deferred) { deferredRenderer.InitBuffers(this, Format.R32G32B32A32_Float); } if (RenderTechnique == gbuffer) { deferredRenderer.InitBuffers(this, Format.B8G8R8A8_UNorm); } } } catch (Exception ex) { //MessageBox.Show("DPFCanvas: Error attaching element: " + string.Format(ex.Message), "Error"); Debug.WriteLine("DPFCanvas: Error attaching element: " + string.Format(ex.Message), "Error"); throw; } } SetDefaultRenderTargets(); /// --------------------------------------------------------------------------- /// this part is per frame if (SupportDeferredRender) { if (RenderTechnique == deferred) { /// set G-Buffer deferredRenderer.SetGBufferTargets(); /// render G-Buffer pass renderRenderable.Render(renderContext); /// call deferred render deferredRenderer.RenderDeferred(renderContext, renderRenderable); } else if (RenderTechnique == gbuffer) { /// set G-Buffer deferredRenderer.SetGBufferTargets(targetWidth / 2, targetHeight / 2); /// render G-Buffer pass renderRenderable.Render(renderContext); /// reset render targets and run lighting pass #if MSAA deferredRenderer.RenderGBufferOutput(ref renderTargetNMS); #else this.deferredRenderer.RenderGBufferOutput(ref this.colorBuffer); #endif } } else { this.device.ImmediateContext.ClearRenderTargetView(colorBufferView, ClearColor); this.device.ImmediateContext.ClearDepthStencilView(depthStencilBufferView, DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 1.0f, 0); renderRenderable.Render(renderContext); } } this.device.ImmediateContext.Flush(); }
/// <summary> /// Register an effect for a RenderTechnique. /// </summary> /// <param name="shaderEffectString">A string representing the shader code.</param> /// <param name="techniqueName"></param> /// <param name="sFlags"></param> /// <param name="eFlags"></param> public void RegisterEffect(string shaderEffectString, RenderTechnique technique, ShaderFlags sFlags = ShaderFlags.None, EffectFlags eFlags = EffectFlags.None) { RegisterEffect(shaderEffectString, new[] { technique }, sFlags, eFlags); }
/// <summary> /// Detaches the element from the host. /// </summary> public virtual void Detach() { this.renderTechnique = null; this.effect = null; this.renderHost = null; }
public DynamoGeometryModel3D(RenderTechnique renderTechnique) { renderTechniqueInternal = renderTechnique; }
/// <summary> /// Get the Effect associated with a RenderTechnique. /// </summary> /// <param name="technique">A RenderTechnique object.</param> /// <returns></returns> public Effect GetEffect(RenderTechnique technique) { return (Effect)data[technique.Name]; }
/// <summary> /// Get the InputLayout associated with a RenderTechnique /// </summary> /// <param name="technique">A RenderTechnique object.</param> /// <returns></returns> public InputLayout GetLayout(RenderTechnique technique) { return (InputLayout)data[technique.Name + "Layout"]; }
/// <summary> /// Get the Effect associated with a RenderTechnique. /// </summary> /// <param name="technique">A RenderTechnique object.</param> /// <returns></returns> public Effect GetEffect(RenderTechnique technique) { return((Effect)data[technique.Name]); }
/// <summary> /// Register an InputLayout for a RenderTechnique. /// </summary> /// <param name="technique">A RenderTechnique object.</param> /// <param name="layout">An InputLayout object.</param> public void RegisterLayout(RenderTechnique technique, InputLayout layout) { data[technique.Name + "Layout"] = layout; }
static Techniques() { /// <summary> /// Names of techniques which are implemented by default /// </summary> RenderBlinn = new RenderTechnique("RenderBlinn"); RenderPhong = new RenderTechnique("RenderPhong"); RenderDiffuse = new RenderTechnique("RenderDiffuse"); RenderColors = new RenderTechnique("RenderColors"); RenderPositions = new RenderTechnique("RenderPositions"); RenderNormals = new RenderTechnique("RenderNormals"); RenderPerturbedNormals = new RenderTechnique("RenderPerturbedNormals"); RenderTangents = new RenderTechnique("RenderTangents"); RenderTexCoords = new RenderTechnique("RenderTexCoords"); RenderWires = new RenderTechnique("RenderWires"); #if DEFERRED RenderDeferred = new RenderTechnique("RenderDeferred"); RenderGBuffer = new RenderTechnique("RenderGBuffer"); RenderDeferredLighting = new RenderTechnique("RenderDeferredLighting"); RenderScreenSpace = new RenderTechnique("RenderScreenSpace"); #endif #if TESSELLATION RenderPNTriangs = new RenderTechnique("RenderPNTriangs"); RenderPNQuads = new RenderTechnique("RenderPNQuads"); #endif RenderCubeMap = new RenderTechnique("RenderCubeMap"); RenderLines = new RenderTechnique("RenderLines"); RenderPoints = new RenderTechnique("RenderPoints"); RenderBillboard = new RenderTechnique("RenderBillboard"); RenderTechniques = new List <RenderTechnique> { RenderBlinn, RenderPhong, RenderColors, RenderDiffuse, RenderPositions, RenderNormals, RenderPerturbedNormals, RenderTangents, RenderTexCoords, RenderWires, #if DEFERRED RenderDeferred, RenderGBuffer, #endif #if TESSELLATION RenderPNTriangs, RenderPNQuads, #endif }; TechniquesSourceDict = new Dictionary <RenderTechnique, byte[]>() { { Techniques.RenderPhong, Properties.Resources._default }, { Techniques.RenderBlinn, Properties.Resources._default }, { Techniques.RenderCubeMap, Properties.Resources._default }, { Techniques.RenderColors, Properties.Resources._default }, { Techniques.RenderDiffuse, Properties.Resources._default }, { Techniques.RenderPositions, Properties.Resources._default }, { Techniques.RenderNormals, Properties.Resources._default }, { Techniques.RenderPerturbedNormals, Properties.Resources._default }, { Techniques.RenderTangents, Properties.Resources._default }, { Techniques.RenderTexCoords, Properties.Resources._default }, { Techniques.RenderWires, Properties.Resources._default }, { Techniques.RenderLines, Properties.Resources._default }, { Techniques.RenderPoints, Properties.Resources._default }, { Techniques.RenderBillboard, Properties.Resources._default }, #if TESSELLATION { Techniques.RenderPNTriangs, Properties.Resources._default }, { Techniques.RenderPNQuads, Properties.Resources._default }, #endif #if DEFERRED { Techniques.RenderDeferred, Properties.Resources._deferred }, { Techniques.RenderGBuffer, Properties.Resources._deferred }, { Techniques.RenderDeferredLighting, Properties.Resources._deferred }, #endif }; }
/// <summary> /// Get the InputLayout associated with a RenderTechnique /// </summary> /// <param name="technique">A RenderTechnique object.</param> /// <returns></returns> public InputLayout GetLayout(RenderTechnique technique) { return((InputLayout)data[technique.Name + "Layout"]); }
static Techniques() { /// <summary> /// Names of techniques which are implemented by default /// </summary> RenderBlinn = new RenderTechnique("RenderBlinn"); RenderPhong = new RenderTechnique("RenderPhong"); RenderDiffuse = new RenderTechnique("RenderDiffuse"); RenderColors = new RenderTechnique("RenderColors"); RenderPositions = new RenderTechnique("RenderPositions"); RenderNormals = new RenderTechnique("RenderNormals"); RenderPerturbedNormals = new RenderTechnique("RenderPerturbedNormals"); RenderTangents = new RenderTechnique("RenderTangents"); RenderTexCoords = new RenderTechnique("RenderTexCoords"); RenderWires = new RenderTechnique("RenderWires"); #if DEFERRED RenderDeferred = new RenderTechnique("RenderDeferred"); RenderGBuffer = new RenderTechnique("RenderGBuffer"); RenderDeferredLighting = new RenderTechnique("RenderDeferredLighting"); RenderScreenSpace = new RenderTechnique("RenderScreenSpace"); #endif #if TESSELLATION RenderPNTriangs = new RenderTechnique("RenderPNTriangs"); RenderPNQuads = new RenderTechnique("RenderPNQuads"); #endif RenderCubeMap = new RenderTechnique("RenderCubeMap"); RenderLines = new RenderTechnique("RenderLines"); RenderPoints = new RenderTechnique("RenderPoints"); RenderBillboard = new RenderTechnique("RenderBillboard"); RenderTechniques = new List<RenderTechnique> { RenderBlinn, RenderPhong, RenderColors, RenderDiffuse, RenderPositions, RenderNormals, RenderPerturbedNormals, RenderTangents, RenderTexCoords, RenderWires, #if DEFERRED RenderDeferred, RenderGBuffer, #endif #if TESSELLATION RenderPNTriangs, RenderPNQuads, #endif }; TechniquesSourceDict = new Dictionary<RenderTechnique, byte[]>() { { Techniques.RenderPhong, Properties.Resources._default}, { Techniques.RenderBlinn, Properties.Resources._default}, { Techniques.RenderCubeMap, Properties.Resources._default}, { Techniques.RenderColors, Properties.Resources._default}, { Techniques.RenderDiffuse, Properties.Resources._default}, { Techniques.RenderPositions, Properties.Resources._default}, { Techniques.RenderNormals, Properties.Resources._default}, { Techniques.RenderPerturbedNormals, Properties.Resources._default}, { Techniques.RenderTangents, Properties.Resources._default}, { Techniques.RenderTexCoords, Properties.Resources._default}, { Techniques.RenderWires, Properties.Resources._default}, { Techniques.RenderLines, Properties.Resources._default}, { Techniques.RenderPoints, Properties.Resources._default}, { Techniques.RenderBillboard, Properties.Resources._default}, #if TESSELLATION { Techniques.RenderPNTriangs, Properties.Resources._default}, { Techniques.RenderPNQuads, Properties.Resources._default}, #endif #if DEFERRED { Techniques.RenderDeferred, Properties.Resources._deferred}, { Techniques.RenderGBuffer, Properties.Resources._deferred}, { Techniques.RenderDeferredLighting , Properties.Resources._deferred}, #endif }; }