protected override unsafe void DrawCore(RenderDrawContext context) { // Clear render targets if there is a dependency conflict (D3D11 warning) if (delaySetRenderTargets) { context.CommandList.ResetTargets(); } if (EffectInstance.UpdateEffect(GraphicsDevice) || pipelineStateDirty || previousBytecode != EffectInstance.Effect.Bytecode) { // The EffectInstance might have been updated from outside previousBytecode = EffectInstance.Effect.Bytecode; pipelineState.State.RootSignature = EffectInstance.RootSignature; pipelineState.State.EffectBytecode = EffectInstance.Effect.Bytecode; pipelineState.State.BlendState = blendState; pipelineState.State.DepthStencilState = depthStencilState; var renderTargetCount = OutputCount; if (renderTargetCount > 0) { // Special case: texture cube var isTextureCube = GetOutput(0).ViewDimension == TextureDimension.TextureCube; if (isTextureCube) { renderTargetCount = 6; } // Capture output state manually (since render targets might not be bound if delaySetRenderTargets is set to true) pipelineState.State.Output.RenderTargetCount = renderTargetCount; fixed(PixelFormat *pixelFormatStart = &pipelineState.State.Output.RenderTargetFormat0) for (int i = 0; i < renderTargetCount; ++i) { pixelFormatStart[i] = GetOutput(isTextureCube ? 0 : i).ViewFormat; } } if (HasDepthStencilOutput) { pipelineState.State.Output.DepthStencilFormat = DepthStencil.Format; } pipelineState.Update(); pipelineStateDirty = false; } context.CommandList.SetPipelineState(pipelineState.CurrentState); EffectInstance.Apply(context.GraphicsContext); // Now that resources are bound, set render targets if (delaySetRenderTargets) { SetRenderTargets(context); } // Draw a full screen quad context.GraphicsDevice.PrimitiveQuad.Draw(context.CommandList); }
private void PrepareForRendering() { var localSamplerState = SamplerState ?? GraphicsDevice.SamplerStates.PointClamp; // Sets the sampler state of the effect if (samplerUpdater.HasValue) { Parameters.Set(samplerUpdater.Value, localSamplerState); } Effect.UpdateEffect(GraphicsDevice); // Setup states (Blend, DepthStencil, Rasterizer) MutablePipeline.State.SetDefaults(); MutablePipeline.State.RootSignature = Effect.RootSignature; MutablePipeline.State.EffectBytecode = Effect.Effect.Bytecode; MutablePipeline.State.BlendState = BlendState ?? BlendStates.AlphaBlend; MutablePipeline.State.DepthStencilState = DepthStencilState ?? DepthStencilStates.Default; MutablePipeline.State.RasterizerState = RasterizerState ?? RasterizerStates.CullBack; MutablePipeline.State.InputElements = VertexPositionTexture.Layout.CreateInputElements(); MutablePipeline.State.PrimitiveType = PrimitiveType.TriangleList; MutablePipeline.State.Output.CaptureState(GraphicsContext.CommandList); MutablePipeline.Update(); }
/// <summary> /// Draws this <see cref="GeometricPrimitive" />. /// </summary> /// <param name="commandList">The command list.</param> public void Draw(CommandList commandList, EffectInstance effectInstance) { // Update pipeline state PipelineState.State.RootSignature = effectInstance.RootSignature; PipelineState.State.EffectBytecode = effectInstance.Effect.Bytecode; PipelineState.State.Output.CaptureState(commandList); PipelineState.Update(); commandList.SetPipelineState(PipelineState.CurrentState); // Setup the Vertex Buffer commandList.SetIndexBuffer(IndexBuffer, 0, IsIndex32Bits); commandList.SetVertexBuffer(0, VertexBuffer, 0, VertexBufferBinding.Stride); // Finally Draw this mesh commandList.DrawIndexed(IndexBuffer.ElementCount); }
public override void Draw(RenderDrawContext context, RenderView renderView, RenderViewStage renderViewStage, int startIndex, int endIndex) { // First do everything that doesn't change per individual render object var graphicsDevice = context.GraphicsDevice; var graphicsContext = context.GraphicsContext; var commandList = context.GraphicsContext.CommandList; // Refresh shader, might have changed during runtime myCustomShader.UpdateEffect(graphicsDevice); // Set common shader parameters if needed //myCustomShader.Parameters.Set(TransformationKeys.ViewProjection, renderView.ViewProjection); for (int index = startIndex; index < endIndex; index++) { var renderNodeReference = renderViewStage.SortedRenderNodes[index].RenderNode; var renderNode = GetRenderNode(renderNodeReference); var myRenderObject = (MyRenderObject)renderNode.RenderObject; if (myRenderObject.VertexBuffer == null) { continue; //next render object } // Assign shader parameters myCustomShader.Parameters.Set(TransformationKeys.WorldViewProjection, myRenderObject.WorldMatrix * renderView.ViewProjection); myCustomShader.Parameters.Set(TexturingKeys.Texture0, myRenderObject.Texture); myCustomShader.Parameters.Set(MyCustomShaderKeys.TextureScale, myRenderObject.TextureScale); myCustomShader.Parameters.Set(MyCustomShaderKeys.Color, myRenderObject.Color); // Prepare pipeline state pipelineState.State.RootSignature = myCustomShader.RootSignature; pipelineState.State.EffectBytecode = myCustomShader.Effect.Bytecode; pipelineState.State.Output.CaptureState(commandList); pipelineState.Update(); commandList.SetPipelineState(pipelineState.CurrentState); // Apply the effect myCustomShader.Apply(graphicsContext); // Set vertex buffer and draw commandList.SetVertexBuffer(0, myRenderObject.VertexBuffer, 0, MyRenderObject.VertexDeclaration.VertexStride); commandList.Draw(MyRenderObject.VertexCount, 0); } }
private void Draw(RenderDrawContext context, MeshDraw drawData, Vector4 color, DepthStencilStateDescription depthStencilState) { pipelineState.State.DepthStencilState = depthStencilState; pipelineState.State.Output.CaptureState(context.CommandList); pipelineState.Update(); context.CommandList.SetIndexBuffer(drawData.IndexBuffer.Buffer, drawData.IndexBuffer.Offset, drawData.IndexBuffer.Is32Bit); context.CommandList.SetPipelineState(pipelineState.CurrentState); shader.Parameters.Set(SelectionWireframeShaderKeys.LineColor, color); shader.Apply(context.GraphicsContext); if (drawData.IndexBuffer != null) { context.CommandList.DrawIndexed(drawData.DrawCount, drawData.StartLocation); } else { context.CommandList.Draw(drawData.DrawCount, drawData.StartLocation); } }
protected override void DrawCore(RenderDrawContext context) { if (EffectInstance.UpdateEffect(GraphicsDevice) || pipelineStateDirty || previousBytecode != EffectInstance.Effect.Bytecode) { // The EffectInstance might have been updated from outside previousBytecode = EffectInstance.Effect.Bytecode; pipelineState.State.RootSignature = EffectInstance.RootSignature; pipelineState.State.EffectBytecode = EffectInstance.Effect.Bytecode; pipelineState.State.BlendState = blendState; pipelineState.State.Output.CaptureState(context.CommandList); pipelineState.Update(); pipelineStateDirty = false; } context.CommandList.SetPipelineState(pipelineState.CurrentState); EffectInstance.Apply(context.GraphicsContext); // Draw a full screen quad context.GraphicsDevice.PrimitiveQuad.Draw(context.CommandList); }
protected override void DrawCore(RenderDrawContext context) { if (string.IsNullOrEmpty(ShaderSourceName)) { return; } Parameters.Set(ComputeEffectShaderKeys.ThreadNumbers, ThreadNumbers); Parameters.Set(ComputeEffectShaderKeys.ComputeShaderName, ShaderSourceName); Parameters.Set(ComputeShaderBaseKeys.ThreadGroupCountGlobal, ThreadGroupCounts); if (EffectInstance.UpdateEffect(GraphicsDevice) || pipelineStateDirty || previousBytecode != EffectInstance.Effect.Bytecode) { // The EffectInstance might have been updated from outside previousBytecode = EffectInstance.Effect.Bytecode; pipelineState.State.SetDefaults(); pipelineState.State.RootSignature = EffectInstance.RootSignature; pipelineState.State.EffectBytecode = EffectInstance.Effect.Bytecode; pipelineState.Update(); pipelineStateDirty = false; } // Apply pipeline state context.CommandList.SetPipelineState(pipelineState.CurrentState); // Apply the effect EffectInstance.Apply(context.GraphicsContext); // Draw a full screen quad context.CommandList.Dispatch(ThreadGroupCounts.X, ThreadGroupCounts.Y, ThreadGroupCounts.Z); // Un-apply //throw new InvalidOperationException(); //EffectInstance.Effect.UnbindResources(GraphicsDevice); }
private void DrawGeometry(GeometryBuffer buffer, PointF position, float depth) { if (isSpriteRenderInProgress) { spriteBatch.End(); } Matrix world = Matrix.Translation(position.X, position.Y, 0); Matrix worldView; Matrix.MultiplyTo(ref world, ref view, out worldView); Matrix worldViewProjection; UpdateProjection(graphicsContext.CommandList); Matrix.MultiplyTo(ref worldView, ref projection, out worldViewProjection); XenkoGeometryBuffer paradoxBuffer = buffer as XenkoGeometryBuffer; paradoxBuffer.EffectInstance.Parameters.Set(SpriteBaseKeys.MatrixTransform, worldViewProjection); if (isClipped) { geometryPipelineState.State.RasterizerState = scissorRasterizerStateDescription; } else { geometryPipelineState.State.RasterizerState = geometryRasterizerStateDescription; } switch (buffer.PrimitiveType) { case GeometryPrimitiveType.TriangleList: geometryPipelineState.State.PrimitiveType = PrimitiveType.TriangleList; break; case GeometryPrimitiveType.TriangleStrip: geometryPipelineState.State.PrimitiveType = PrimitiveType.TriangleStrip; break; case GeometryPrimitiveType.LineList: geometryPipelineState.State.PrimitiveType = PrimitiveType.LineList; break; case GeometryPrimitiveType.LineStrip: geometryPipelineState.State.PrimitiveType = PrimitiveType.LineStrip; break; default: break; } geometryPipelineState.State.RootSignature = paradoxBuffer.EffectInstance.RootSignature; geometryPipelineState.State.EffectBytecode = paradoxBuffer.EffectInstance.Effect.Bytecode; geometryPipelineState.State.InputElements = paradoxBuffer.InputElementDescriptions; geometryPipelineState.State.Output.CaptureState(graphicsContext.CommandList); geometryPipelineState.Update(); graphicsContext.CommandList.SetPipelineState(geometryPipelineState.CurrentState); paradoxBuffer.EffectInstance.Apply(graphicsContext); graphicsContext.CommandList.SetVertexBuffer(0, paradoxBuffer.VertexBufferBinding.Buffer, 0, paradoxBuffer.VertexBufferBinding.Stride); graphicsContext.CommandList.Draw(paradoxBuffer.PrimitiveCount); if (isSpriteRenderInProgress) { if (isClipped) { spriteBatch.Begin(graphicsContext, SpriteSortMode.Deferred, depthStencilState: DepthStencilStates.None, rasterizerState: scissorRasterizerStateDescription, effect: currentActiveEffect); } else { spriteBatch.Begin(graphicsContext, SpriteSortMode.Deferred, depthStencilState: DepthStencilStates.None, effect: currentActiveEffect); } } }
public override void Draw(RenderDrawContext context, RenderView renderView, RenderViewStage renderViewStage) { shader.UpdateEffect(context.GraphicsDevice); foreach (var renderNode in renderViewStage.SortedRenderNodes) { var renderMesh = renderNode.RenderObject as RenderMesh; if (renderMesh == null) { continue; } // get wireframe script WireframeScript wireframeScript = null; if (renderMesh.Source is ModelComponent) { wireframeScript = (renderMesh.Source as ModelComponent).Entity.Get <WireframeScript>(); } if (wireframeScript == null || !wireframeScript.Enabled) { continue; } MeshDraw drawData = renderMesh.ActiveMeshDraw; // bind VB for (int slot = 0; slot < drawData.VertexBuffers.Length; slot++) { var vertexBuffer = drawData.VertexBuffers[slot]; context.CommandList.SetVertexBuffer(slot, vertexBuffer.Buffer, vertexBuffer.Offset, vertexBuffer.Stride); } // set shader parameters shader.Parameters.Set(TransformationKeys.WorldViewProjection, renderMesh.World * renderView.ViewProjection); // matrix shader.Parameters.Set(TransformationKeys.WorldScale, new Vector3(ScaleAdjust + 1.0f)); // increase size to avoid z-fight shader.Parameters.Set(SinglePassWireframeShaderKeys.Viewport, new Vector4(context.RenderContext.RenderView.ViewSize, 0, 0)); shader.Parameters.Set(SinglePassWireframeShaderKeys.LineWidth, wireframeScript.LineWidth); shader.Parameters.Set(SinglePassWireframeShaderKeys.LineColor, (Vector3)wireframeScript.Color); // prepare pipeline state pipelineState.State.RootSignature = shader.RootSignature; pipelineState.State.EffectBytecode = shader.Effect.Bytecode; pipelineState.State.PrimitiveType = drawData.PrimitiveType; pipelineState.State.Output.CaptureState(context.CommandList); pipelineState.Update(); context.CommandList.SetIndexBuffer(drawData.IndexBuffer.Buffer, drawData.IndexBuffer.Offset, drawData.IndexBuffer.Is32Bit); context.CommandList.SetPipelineState(pipelineState.CurrentState); // apply the effect shader.Apply(context.GraphicsContext); if (drawData.IndexBuffer != null) { context.CommandList.DrawIndexed(drawData.DrawCount, drawData.StartLocation); } else { context.CommandList.Draw(drawData.DrawCount, drawData.StartLocation); } } }
protected override void DrawCore(RenderDrawContext context) { if (string.IsNullOrEmpty(ShaderSourceName) || FRefreshTime > context.RenderContext.Time.Total) { return; } Parameters.Set(ComputeEffectShaderKeys.ThreadNumbers, ThreadNumbers); Parameters.Set(ComputeEffectShaderKeys.ComputeShaderName, ShaderSourceName); Parameters.Set(ComputeShaderBaseKeys.ThreadGroupCountGlobal, ThreadGroupCounts); var effectUpdated = false; try { effectUpdated = EffectInstance.UpdateEffect(GraphicsDevice); FCompiled = true; LastError = string.Empty; } catch (Exception e) { LastError = e.InnermostException().Message; FCompiled = false; FRefreshTime = context.RenderContext.Time.Total + TimeSpan.FromSeconds(3); } if (!FCompiled) { return; } try { if (effectUpdated || pipelineStateDirty || previousBytecode != EffectInstance.Effect.Bytecode) { // The EffectInstance might have been updated from outside previousBytecode = EffectInstance.Effect.Bytecode; pipelineState.State.SetDefaults(); pipelineState.State.RootSignature = EffectInstance.RootSignature; pipelineState.State.EffectBytecode = EffectInstance.Effect.Bytecode; pipelineState.Update(); pipelineStateDirty = false; } } catch (Exception e) { LastError = e.InnermostException().Message; FCompiled = false; FRefreshTime = context.RenderContext.Time.Total + TimeSpan.FromSeconds(3); return; } // Apply pipeline state var commandList = context.CommandList; commandList.SetPipelineState(pipelineState.CurrentState); drawRenderDrawContext = context; drawCommandList = commandList; // Un-apply //throw new InvalidOperationException(); //EffectInstance.Effect.UnbindResources(GraphicsDevice); }