Skip to content
This repository has been archived by the owner on Mar 30, 2019. It is now read-only.

Commit

Permalink
[Toolkit.Graphics] Fix issue with BasicEffect used with an effect tha…
Browse files Browse the repository at this point in the history
…t is using Geometry

Shader. Make sure to reset Domain, Hull and Geometry shaders before applying a BasicEffect or PrimitiveQuad.
  • Loading branch information
xoofx committed Mar 30, 2013
1 parent 28ebc6b commit 0560464
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 30 deletions.
70 changes: 40 additions & 30 deletions Source/Toolkit/SharpDX.Toolkit.Graphics/GraphicsDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,15 @@ public class GraphicsDevice : Component
/// </summary>
public readonly RasterizerStateCollection RasterizerStates;

private InputAssemblerStage inputAssemblerStage;
private RasterizerStage rasterizerStage;
internal InputAssemblerStage InputAssemblerStage;
internal VertexShaderStage VertexShaderStage;
internal DomainShaderStage DomainShaderStage;
internal HullShaderStage HullShaderStage;
internal GeometryShaderStage GeometryShaderStage;
internal RasterizerStage RasterizerStage;
internal PixelShaderStage PixelShaderStage;
private OutputMergerStage outputMergerStage;
internal OutputMergerStage OutputMergerStage;
internal ComputeShaderStage ComputeShaderStage;

internal readonly bool needWorkAroundForUpdateSubResource;

Expand Down Expand Up @@ -215,10 +220,15 @@ private void Initialize()
resetVertexBuffersPointer = ToDispose(Utilities.AllocateClearedMemory(Utilities.SizeOf<IntPtr>() * InputAssemblerStage.VertexInputResourceSlotCount));
}

inputAssemblerStage = Context.InputAssembler;
outputMergerStage = Context.OutputMerger;
rasterizerStage = Context.Rasterizer;
InputAssemblerStage = Context.InputAssembler;
VertexShaderStage = Context.VertexShader;
DomainShaderStage = Context.DomainShader;
HullShaderStage = Context.HullShader;
GeometryShaderStage = Context.GeometryShader;
RasterizerStage = Context.Rasterizer;
PixelShaderStage = Context.PixelShader;
OutputMergerStage = Context.OutputMerger;
ComputeShaderStage = Context.ComputeShader;

// Precompute shader stages
ShaderStages = new CommonShaderStage[]
Expand Down Expand Up @@ -588,7 +598,7 @@ private PrimitiveTopology PrimitiveType
{
set
{
inputAssemblerStage.PrimitiveTopology = value;
InputAssemblerStage.PrimitiveTopology = value;
}
}

Expand Down Expand Up @@ -960,11 +970,11 @@ public void SetBlendState(BlendState blendState)
{
if (blendState == null)
{
outputMergerStage.SetBlendState(null, Color.White, -1);
OutputMergerStage.SetBlendState(null, Color.White, -1);
}
else
{
outputMergerStage.SetBlendState(blendState, blendState.BlendFactor, blendState.MultiSampleMask);
OutputMergerStage.SetBlendState(blendState, blendState.BlendFactor, blendState.MultiSampleMask);
}
}

Expand All @@ -984,11 +994,11 @@ public void SetBlendState(BlendState blendState, Color4 blendFactor, int multiSa
{
if (blendState == null)
{
outputMergerStage.SetBlendState(null, blendFactor, multiSampleMask);
OutputMergerStage.SetBlendState(null, blendFactor, multiSampleMask);
}
else
{
outputMergerStage.SetBlendState(blendState, blendFactor, multiSampleMask);
OutputMergerStage.SetBlendState(blendState, blendFactor, multiSampleMask);
}
}

Expand Down Expand Up @@ -1022,7 +1032,7 @@ public void SetBlendState(BlendState blendState, Color4 blendFactor, uint multiS
/// <unmanaged-short>ID3D11DeviceContext::OMSetDepthStencilState</unmanaged-short>
public void SetDepthStencilState(DepthStencilState depthStencilState, int stencilReference = 0)
{
outputMergerStage.SetDepthStencilState(depthStencilState, stencilReference);
OutputMergerStage.SetDepthStencilState(depthStencilState, stencilReference);
}

/// <summary>
Expand All @@ -1034,7 +1044,7 @@ public void SetDepthStencilState(DepthStencilState depthStencilState, int stenci
/// <unmanaged-short>ID3D11DeviceContext::RSSetState</unmanaged-short>
public void SetRasterizerState(RasterizerState rasterizerState)
{
rasterizerStage.State = rasterizerState;
RasterizerStage.State = rasterizerState;
}

/// <summary>
Expand All @@ -1052,7 +1062,7 @@ public void SetRasterizerState(RasterizerState rasterizerState)
/// <unmanaged-short>ID3D11DeviceContext::RSSetScissorRects</unmanaged-short>
public void SetScissorRectangles(int left, int top, int right, int bottom)
{
rasterizerStage.SetScissorRectangle(left, top, right, bottom);
RasterizerStage.SetScissorRectangle(left, top, right, bottom);
}

/// <summary>
Expand All @@ -1067,7 +1077,7 @@ public void SetScissorRectangles(int left, int top, int right, int bottom)
/// <unmanaged-short>ID3D11DeviceContext::RSSetScissorRects</unmanaged-short>
public void SetScissorRectangles(params Rectangle[] scissorRectangles)
{
rasterizerStage.SetScissorRectangles(scissorRectangles);
RasterizerStage.SetScissorRectangles(scissorRectangles);
}

/// <summary>
Expand All @@ -1078,7 +1088,7 @@ public ViewportF Viewport
{
get
{
rasterizerStage.GetViewports(viewports);
RasterizerStage.GetViewports(viewports);
return viewports[0];
}

Expand All @@ -1095,7 +1105,7 @@ public ViewportF Viewport
/// <returns>Returns a viewport bind on a specified mulrendertarget</returns>
public ViewportF GetViewport(int index)
{
rasterizerStage.GetViewports(viewports);
RasterizerStage.GetViewports(viewports);
return viewports[index];
}

Expand All @@ -1117,7 +1127,7 @@ public ViewportF GetViewport(int index)
public void SetViewport(float x, float y, float width, float height, float minZ = 0.0f, float maxZ = 1.0f)
{
viewports[0] = new ViewportF(x, y, width, height, minZ, maxZ);
rasterizerStage.SetViewport(x, y, width, height, minZ, maxZ);
RasterizerStage.SetViewport(x, y, width, height, minZ, maxZ);
}

/// <summary>
Expand All @@ -1133,7 +1143,7 @@ public void SetViewport(float x, float y, float width, float height, float minZ
public void SetViewport(ViewportF viewport)
{
viewports[0] = viewport;
rasterizerStage.SetViewport(viewport);
RasterizerStage.SetViewport(viewport);
}

/// <summary>
Expand All @@ -1151,7 +1161,7 @@ public void SetViewports(params ViewportF[] viewports)
for (int i = 0; i < viewports.Length; i++)
this.viewports[i] = viewports[i];

rasterizerStage.SetViewports(this.viewports, viewports.Length);
RasterizerStage.SetViewports(this.viewports, viewports.Length);
}

/// <summary>
Expand All @@ -1164,7 +1174,7 @@ public void ResetTargets()
{
currentRenderTargetView = null;
currentDepthStencilView = null;
outputMergerStage.ResetTargets();
OutputMergerStage.ResetTargets();
}

/// <summary>
Expand All @@ -1186,7 +1196,7 @@ public void SetRenderTargets(params RenderTargetView[] renderTargetViews)

CommonSetRenderTargets(renderTargetViews);
currentDepthStencilView = null;
outputMergerStage.SetTargets(renderTargetViews);
OutputMergerStage.SetTargets(renderTargetViews);
}

/// <summary>
Expand All @@ -1203,7 +1213,7 @@ public void SetRenderTargets(RenderTargetView renderTargetView)
{
CommonSetRenderTargets(renderTargetView);
currentDepthStencilView = null;
outputMergerStage.SetTargets(renderTargetView);
OutputMergerStage.SetTargets(renderTargetView);
}

/// <summary>
Expand All @@ -1226,7 +1236,7 @@ public void SetRenderTargets(DepthStencilView depthStencilView, params RenderTar

CommonSetRenderTargets(renderTargetViews);
currentDepthStencilView = depthStencilView;
outputMergerStage.SetTargets(depthStencilView, renderTargetViews);
OutputMergerStage.SetTargets(depthStencilView, renderTargetViews);
}

/// <summary>
Expand All @@ -1244,7 +1254,7 @@ public void SetRenderTargets(DepthStencilView depthStencilView, RenderTargetView
{
CommonSetRenderTargets(renderTargetView);
currentDepthStencilView = depthStencilView;
outputMergerStage.SetTargets(depthStencilView, renderTargetView);
OutputMergerStage.SetTargets(depthStencilView, renderTargetView);
}

/// <summary>
Expand All @@ -1261,7 +1271,7 @@ public void SetRenderTargets(DepthStencilView depthStencilView, RenderTargetView
/// <unmanaged-short>ID3D11DeviceContext::IASetIndexBuffer</unmanaged-short>
public void SetIndexBuffer(Buffer indexBuffer, bool is32Bit, int offset = 0)
{
inputAssemblerStage.SetIndexBuffer(indexBuffer, is32Bit ? DXGI.Format.R32_UInt : DXGI.Format.R16_UInt, offset);
InputAssemblerStage.SetIndexBuffer(indexBuffer, is32Bit ? DXGI.Format.R32_UInt : DXGI.Format.R16_UInt, offset);
}

/// <summary>
Expand Down Expand Up @@ -1317,7 +1327,7 @@ public unsafe void SetVertexBuffer<T>(int slot, Buffer<T> vertexBuffer, int vert
if ((slot + 1) > maxSlotCountForVertexBuffer)
maxSlotCountForVertexBuffer = slot + 1;
}
inputAssemblerStage.SetVertexBuffers(slot, 1, new IntPtr(&vertexBufferPtr), new IntPtr(&stride), new IntPtr(&offset));
InputAssemblerStage.SetVertexBuffers(slot, 1, new IntPtr(&vertexBufferPtr), new IntPtr(&stride), new IntPtr(&offset));
}

/// <summary>
Expand All @@ -1344,7 +1354,7 @@ public unsafe void SetVertexBuffer(int slot, SharpDX.Direct3D11.Buffer vertexBuf
if ((slot+1) > maxSlotCountForVertexBuffer)
maxSlotCountForVertexBuffer = slot + 1;
}
inputAssemblerStage.SetVertexBuffers(slot, 1, new IntPtr(&vertexBufferPtr), new IntPtr(&vertexStride), new IntPtr(&offsetInBytes));
InputAssemblerStage.SetVertexBuffers(slot, 1, new IntPtr(&vertexBufferPtr), new IntPtr(&vertexStride), new IntPtr(&offsetInBytes));
}

/// <summary>
Expand All @@ -1362,7 +1372,7 @@ public void ResetVertexBuffers()
if (maxSlotCountForVertexBuffer == 0)
return;

inputAssemblerStage.SetVertexBuffers(0, maxSlotCountForVertexBuffer, resetVertexBuffersPointer, resetVertexBuffersPointer, resetVertexBuffersPointer);
InputAssemblerStage.SetVertexBuffers(0, maxSlotCountForVertexBuffer, resetVertexBuffersPointer, resetVertexBuffersPointer, resetVertexBuffersPointer);

maxSlotCountForVertexBuffer = 0;
}
Expand Down Expand Up @@ -1415,7 +1425,7 @@ private void SetupInputLayout()
throw new InvalidOperationException("Cannot perform a Draw/Dispatch operation without an EffectPass applied.");

var inputLayout = CurrentPass.GetInputLayout(currentVertexInputLayout);
inputAssemblerStage.SetInputLayout(inputLayout);
InputAssemblerStage.SetInputLayout(inputLayout);
}

/// <summary>
Expand Down
16 changes: 16 additions & 0 deletions Source/Toolkit/SharpDX.Toolkit.Graphics/PrimitiveQuad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public void Draw()
GraphicsDevice.SetVertexBuffer(sharedData.VertexBuffer);
GraphicsDevice.SetVertexInputLayout(sharedData.VertexInputLayout);

ResetShaderStages();

// Make sure that we are using our vertex shader
quadPass.Apply();
GraphicsDevice.Draw(PrimitiveType.TriangleStrip, 4);
Expand All @@ -102,6 +104,8 @@ public void Draw(SharpDX.Direct3D11.ShaderResourceView texture, SharpDX.Direct3D
GraphicsDevice.SetVertexBuffer(sharedData.VertexBuffer);
GraphicsDevice.SetVertexInputLayout(sharedData.VertexInputLayout);

ResetShaderStages();

// Make sure that we are using our vertex shader
textureParameter.SetResource(texture);
textureSamplerParameter.SetResource(samplerState ?? GraphicsDevice.SamplerStates.LinearClamp);
Expand All @@ -117,6 +121,8 @@ public void Draw(Effect effect)
{
GraphicsDevice.SetVertexBuffer(sharedData.VertexBuffer);
GraphicsDevice.SetVertexInputLayout(sharedData.VertexInputLayout);
ResetShaderStages();

foreach (var pass in effect.CurrentTechnique.Passes)
{
// Apply the Effect pass
Expand All @@ -135,11 +141,21 @@ public void Draw(Effect effect)
/// <param name="effectPass">The effect pass.</param>
public void Draw(EffectPass effectPass)
{
ResetShaderStages();

// Apply the Effect pass
effectPass.Apply();
Draw();
}

private void ResetShaderStages()
{
// Make sure that domain, hull and geometry shaders are disable.
GraphicsDevice.DomainShaderStage.Set(null);
GraphicsDevice.HullShaderStage.Set(null);
GraphicsDevice.GeometryShaderStage.Set(null);
}

/// <summary>
/// Internal structure used to store VertexBuffer and VertexInputLayout.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,11 @@ public void EnableDefaultLighting()
/// </summary>
protected internal override EffectPass OnApply(EffectPass pass)
{
// Make sure that domain, hull and geometry shaders are disable.
GraphicsDevice.DomainShaderStage.Set(null);
GraphicsDevice.HullShaderStage.Set(null);
GraphicsDevice.GeometryShaderStage.Set(null);

// Recompute the world+view+projection matrix or fog vector?
dirtyFlags = EffectHelpers.SetWorldViewProjAndFog(dirtyFlags, ref world, ref view, ref projection, ref worldView, fogEnabled, fogStart, fogEnd, worldViewProjParam, fogVectorParam);

Expand Down

0 comments on commit 0560464

Please sign in to comment.