public void Draw(RenderContext11 renderContext, float Opacity, Color drawColor) { if (glyphCache == null || glyphCache.Version > glyphVersion) { PrepareBatch(); } //todo11 Use Shader renderContext.SetupBasicEffect(BasicEffect.TextureColorOpacity, Opacity, drawColor); renderContext.MainTexture = glyphCache.Texture; renderContext.devContext.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList; renderContext.PreDraw(); if (layout == null) { layout = new SharpDX.Direct3D11.InputLayout(renderContext.Device, renderContext.Shader.InputSignature, new[] { new SharpDX.Direct3D11.InputElement("POSITION", 0, SharpDX.DXGI.Format.R32G32B32_Float, 0, 0), new SharpDX.Direct3D11.InputElement("TEXCOORD", 0, SharpDX.DXGI.Format.R32G32_Float, 12, 0), }); } renderContext.Device.ImmediateContext.InputAssembler.InputLayout = layout; renderContext.SetVertexBuffer(vertexBuffer); renderContext.devContext.Draw(vertexBuffer.Count, 0); }
/// <summary> /// Prepares a draw call. This method is called before each Draw() method to setup the correct Primitive, InputLayout and VertexBuffers. /// </summary> /// <param name="primitiveType">Type of the primitive.</param> /// <exception cref="System.InvalidOperationException">Cannot GraphicsDevice.Draw*() without an effect being previously applied with Effect.Apply() method</exception> private void PrepareDraw(PrimitiveType primitiveType) { if (CurrentEffect == null) { throw new InvalidOperationException("Cannot GraphicsDevice.Draw*() without an effect being previously applied with Effect.Apply() method"); } // Setup the primitive type PrimitiveType = primitiveType; // If the vertex array object is null, simply set the InputLayout to null if (newVertexArrayObject == null) { if (currentVertexArrayObject != null) { currentVertexArrayObject = null; currentVertexArrayLayout = null; currentEffectInputSignature = null; inputAssembler.InputLayout = currentInputLayout = null; } } else { var newVertexArrayLayout = newVertexArrayObject.Layout; var newEffectInputSignature = CurrentEffect.InputSignature; var oldInputLayout = currentInputLayout; // Apply the VertexArrayObject if (newVertexArrayObject != currentVertexArrayObject) { currentVertexArrayObject = newVertexArrayObject; newVertexArrayObject.Apply(inputAssembler); } // If the input layout of the effect or the vertex buffer has changed, get the associated new input layout if (!ReferenceEquals(newVertexArrayLayout, currentVertexArrayLayout) || !ReferenceEquals(newEffectInputSignature, currentEffectInputSignature)) { currentVertexArrayLayout = newVertexArrayLayout; currentEffectInputSignature = newEffectInputSignature; if (newVertexArrayObject.InputLayout != null && ReferenceEquals(newEffectInputSignature, newVertexArrayObject.EffectInputSignature)) { // Default configuration currentInputLayout = newVertexArrayObject.InputLayout; } else if (ReferenceEquals(newEffectInputSignature, newVertexArrayObject.LastEffectInputSignature)) { // Reuse previous configuration currentInputLayout = newVertexArrayObject.LastInputLayout; } // Slow path if the current VertexArrayObject is not optimized for the particular input (or not used right before) else { currentInputLayout = InputLayoutManager.GetInputLayout(newEffectInputSignature, currentVertexArrayLayout); // Store it in VAO since it will likely be used with same effect later newVertexArrayObject.LastInputLayout = currentInputLayout; newVertexArrayObject.LastEffectInputSignature = newEffectInputSignature; } // Setup the input layout (if it changed) if (currentInputLayout != oldInputLayout) inputAssembler.InputLayout = currentInputLayout; } } SetViewportImpl(); }
public void Draw(RenderContext11 renderContext) { renderContext.devContext.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList; renderContext.devContext.InputAssembler.SetVertexBuffers(0, vertexBufferBinding); renderContext.devContext.InputAssembler.SetIndexBuffer(indexBuffer, SharpDX.DXGI.Format.R32_UInt, 0); renderContext.SunPosition = new Vector3d(500, 500, 0.0); renderContext.SunlightColor = System.Drawing.Color.White; renderContext.AmbientLightColor = System.Drawing.Color.DarkGray; renderContext.SetupBasicEffect(BasicEffect.TextureOnly, 1.0f, System.Drawing.Color.White); renderContext.MainTexture = texture; renderContext.PreDraw(); if (layout == null) { layout = new SharpDX.Direct3D11.InputLayout(renderContext.Device, renderContext.Shader.InputSignature, new[] { new SharpDX.Direct3D11.InputElement("POSITION", 0, SharpDX.DXGI.Format.R32G32B32_Float, 0, 0), new SharpDX.Direct3D11.InputElement("TEXCOORD", 0, SharpDX.DXGI.Format.R32G32_Float, 16, 0), }); renderContext.Device.ImmediateContext.InputAssembler.InputLayout = layout; } // Draw the cube renderContext.devContext.DrawIndexed(triangleCount * 3, 0, 0); }
private void ClearStateImpl() { NativeDeviceContext.ClearState(); for (int i = 0; i < samplerStates.Length; ++i) samplerStates[i] = null; for (int i = 0; i < constantBuffers.Length; ++i) constantBuffers[i] = null; for (int i = 0; i < unorderedAccessViews.Length; ++i) unorderedAccessViews[i] = null; for (int i = 0; i < currentRenderTargetViews.Length; i++) currentRenderTargetViews[i] = null; currentEffectInputSignature = null; currentVertexArrayLayout = null; currentInputLayout = null; currentVertexArrayObject = null; CurrentEffect = null; }
private void ReleaseDevice() { // Display D3D11 ref counting info ClearState(); NativeDevice.ImmediateContext.Flush(); NativeDevice.ImmediateContext.Dispose(); if (IsDebugMode) { var deviceDebug = new SharpDX.Direct3D11.DeviceDebug(NativeDevice); deviceDebug.ReportLiveDeviceObjects(SharpDX.Direct3D11.ReportingLevel.Detail); } currentInputLayout = null; currentEffectInputSignature = null; currentVertexArrayObject = null; currentVertexArrayLayout = null; nativeDevice.Dispose(); }
/// <summary> /// Creates device-based resources to store a constant buffer, cube /// geometry, and vertex and pixel shaders. In some cases this will also /// store a geometry shader. /// </summary> public async void CreateDeviceDependentResourcesAsync() { ReleaseDeviceDependentResources(); usingVprtShaders = deviceResources.D3DDeviceSupportsVprt; var folder = Windows.ApplicationModel.Package.Current.InstalledLocation; // On devices that do support the D3D11_FEATURE_D3D11_OPTIONS3:: // VPAndRTArrayIndexFromAnyShaderFeedingRasterizer optional feature // we can avoid using a pass-through geometry shader to set the render // target array index, thus avoiding any overhead that would be // incurred by setting the geometry shader stage. var vertexShaderFileName = usingVprtShaders ? "Content\\Shaders\\VPRTVertexShader.cso" : "Content\\Shaders\\VertexShader.cso"; // Load the compiled vertex shader. var vertexShaderByteCode = await DirectXHelper.ReadDataAsync(await folder.GetFileAsync(vertexShaderFileName)); // After the vertex shader file is loaded, create the shader and input layout. vertexShader = this.ToDispose(new SharpDX.Direct3D11.VertexShader( deviceResources.D3DDevice, vertexShaderByteCode)); SharpDX.Direct3D11.InputElement[] vertexDesc = { new SharpDX.Direct3D11.InputElement("POSITION", 0, SharpDX.DXGI.Format.R32G32B32_Float, 0, 0, SharpDX.Direct3D11.InputClassification.PerVertexData, 0), new SharpDX.Direct3D11.InputElement("COLOR", 0, SharpDX.DXGI.Format.R32G32B32_Float, 12, 0, SharpDX.Direct3D11.InputClassification.PerVertexData, 0), }; inputLayout = this.ToDispose(new SharpDX.Direct3D11.InputLayout( deviceResources.D3DDevice, vertexShaderByteCode, vertexDesc)); if (!usingVprtShaders) { // Load the compiled pass-through geometry shader. var geometryShaderByteCode = await DirectXHelper.ReadDataAsync(await folder.GetFileAsync("Content\\Shaders\\GeometryShader.cso")); // After the pass-through geometry shader file is loaded, create the shader. geometryShader = this.ToDispose(new SharpDX.Direct3D11.GeometryShader( deviceResources.D3DDevice, geometryShaderByteCode)); } // Load the compiled pixel shader. var pixelShaderByteCode = await DirectXHelper.ReadDataAsync(await folder.GetFileAsync("Content\\Shaders\\PixelShader.cso")); // After the pixel shader file is loaded, create the shader. pixelShader = this.ToDispose(new SharpDX.Direct3D11.PixelShader( deviceResources.D3DDevice, pixelShaderByteCode)); // Load mesh vertices. Each vertex has a position and a color. // Note that the cube size has changed from the default DirectX app // template. Windows Holographic is scaled in meters, so to draw the // cube at a comfortable size we made the cube width 0.2 m (20 cm). VertexPositionColor[] cubeVertices = { new VertexPositionColor(new Vector3(-0.1f, -0.1f, -0.1f), new Vector3(0.0f, 0.0f, 0.0f)), new VertexPositionColor(new Vector3(-0.1f, -0.1f, 0.1f), new Vector3(0.0f, 0.0f, 1.0f)), new VertexPositionColor(new Vector3(-0.1f, 0.1f, -0.1f), new Vector3(0.0f, 1.0f, 0.0f)), new VertexPositionColor(new Vector3(-0.1f, 0.1f, 0.1f), new Vector3(0.0f, 1.0f, 1.0f)), new VertexPositionColor(new Vector3(0.1f, -0.1f, -0.1f), new Vector3(1.0f, 0.0f, 0.0f)), new VertexPositionColor(new Vector3(0.1f, -0.1f, 0.1f), new Vector3(1.0f, 0.0f, 1.0f)), new VertexPositionColor(new Vector3(0.1f, 0.1f, -0.1f), new Vector3(1.0f, 1.0f, 0.0f)), new VertexPositionColor(new Vector3(0.1f, 0.1f, 0.1f), new Vector3(1.0f, 1.0f, 1.0f)), }; vertexBuffer = this.ToDispose(SharpDX.Direct3D11.Buffer.Create( deviceResources.D3DDevice, SharpDX.Direct3D11.BindFlags.VertexBuffer, cubeVertices)); // Load mesh indices. Each trio of indices represents // a triangle to be rendered on the screen. // For example: 0,2,1 means that the vertices with indexes // 0, 2 and 1 from the vertex buffer compose the // first triangle of this mesh. ushort[] cubeIndices = { 2, 1, 0, // -x 2, 3, 1, 6, 4, 5, // +x 6, 5, 7, 0, 1, 5, // -y 0, 5, 4, 2, 6, 7, // +y 2, 7, 3, 0, 4, 6, // -z 0, 6, 2, 1, 3, 7, // +z 1, 7, 5, }; indexCount = cubeIndices.Length; indexBuffer = this.ToDispose(SharpDX.Direct3D11.Buffer.Create( deviceResources.D3DDevice, SharpDX.Direct3D11.BindFlags.IndexBuffer, cubeIndices)); // Create a constant buffer to store the model matrix. modelConstantBuffer = this.ToDispose(SharpDX.Direct3D11.Buffer.Create( deviceResources.D3DDevice, SharpDX.Direct3D11.BindFlags.ConstantBuffer, ref modelConstantBufferData)); // Once the cube is loaded, the object is ready to be rendered. loadingComplete = true; }
public VertexDeclaration(SharpDX.Direct3D11.InputLayout inputLayout) { _InputLayout = inputLayout; }
internal void GenerateInputLayout( IGraphicsDevice graphicsDevice, Shader vertexShader ) { if ( layout != null ) return; layout = new SharpDX.Direct3D11.InputLayout ( graphicsDevice.Handle as SharpDX.Direct3D11.Device, ( vertexShader.Handle as Shader ).bytecode, e ); }
/// <summary> /// Creates device-based resources to store a constant buffer, cube /// geometry, and vertex and pixel shaders. In some cases this will also /// store a geometry shader. /// </summary> public async void CreateDeviceDependentResourcesAsync() { ReleaseDeviceDependentResources(); usingVprtShaders = deviceResources.D3DDeviceSupportsVprt; var folder = Windows.ApplicationModel.Package.Current.InstalledLocation; // On devices that do support the D3D11_FEATURE_D3D11_OPTIONS3:: // VPAndRTArrayIndexFromAnyShaderFeedingRasterizer optional feature // we can avoid using a pass-through geometry shader to set the render // target array index, thus avoiding any overhead that would be // incurred by setting the geometry shader stage. var vertexShaderFileName = usingVprtShaders ? "Content\\Shaders\\VPRTVertexShader.cso" : "Content\\Shaders\\VertexShader.cso"; // Load the compiled vertex shader. var vertexShaderByteCode = await DirectXHelper.ReadDataAsync(await folder.GetFileAsync(vertexShaderFileName)); // After the vertex shader file is loaded, create the shader and input layout. vertexShader = this.ToDispose(new SharpDX.Direct3D11.VertexShader( deviceResources.D3DDevice, vertexShaderByteCode)); SharpDX.Direct3D11.InputElement[] vertexDesc = { new SharpDX.Direct3D11.InputElement("POSITION", 0, SharpDX.DXGI.Format.R32G32B32_Float, 0, 0, SharpDX.Direct3D11.InputClassification.PerVertexData, 0), new SharpDX.Direct3D11.InputElement("COLOR", 0, SharpDX.DXGI.Format.R32G32_Float, 12, 0, SharpDX.Direct3D11.InputClassification.PerVertexData, 0), }; inputLayout = this.ToDispose(new SharpDX.Direct3D11.InputLayout( deviceResources.D3DDevice, vertexShaderByteCode, vertexDesc)); if (!usingVprtShaders) { // Load the compiled pass-through geometry shader. var geometryShaderByteCode = await DirectXHelper.ReadDataAsync(await folder.GetFileAsync("Content\\Shaders\\GeometryShader.cso")); // After the pass-through geometry shader file is loaded, create the shader. geometryShader = this.ToDispose(new SharpDX.Direct3D11.GeometryShader( deviceResources.D3DDevice, geometryShaderByteCode)); } // Load the compiled pixel shader. var pixelShaderByteCode = await DirectXHelper.ReadDataAsync(await folder.GetFileAsync("Content\\Shaders\\PixelShader.cso")); // After the pixel shader file is loaded, create the shader. pixelShader = this.ToDispose(new SharpDX.Direct3D11.PixelShader( deviceResources.D3DDevice, pixelShaderByteCode)); foreach (var sprite in sprites) { sprite.CreateDeviceDependentResourcesAsync(deviceResources); } // Once the cube is loaded, the object is ready to be rendered. loadingComplete = true; }
/// <summary> /// Prepares a draw call. This method is called before each Draw() method to setup the correct Primitive, InputLayout and VertexBuffers. /// </summary> /// <param name="primitiveType">Type of the primitive.</param> /// <exception cref="System.InvalidOperationException">Cannot GraphicsDevice.Draw*() without an effect being previously applied with Effect.Apply() method</exception> private void PrepareDraw(PrimitiveType primitiveType) { if (CurrentEffect == null) { throw new InvalidOperationException("Cannot GraphicsDevice.Draw*() without an effect being previously applied with Effect.Apply() method"); } // Setup the primitive type PrimitiveType = primitiveType; // If the vertex array object is null, simply set the InputLayout to null if (newVertexArrayObject == null) { if (currentVertexArrayObject != null) { currentVertexArrayObject = null; currentVertexArrayLayout = null; currentEffectInputSignature = null; inputAssembler.InputLayout = currentInputLayout = null; } } else { var newVertexArrayLayout = newVertexArrayObject.Layout; var newEffectInputSignature = CurrentEffect.InputSignature; var oldInputLayout = currentInputLayout; // Apply the VertexArrayObject if (newVertexArrayObject != currentVertexArrayObject) { currentVertexArrayObject = newVertexArrayObject; newVertexArrayObject.Apply(inputAssembler); } // If the input layout of the effect or the vertex buffer has changed, get the associated new input layout if (!ReferenceEquals(newVertexArrayLayout, currentVertexArrayLayout) || !ReferenceEquals(newEffectInputSignature, currentEffectInputSignature)) { currentVertexArrayLayout = newVertexArrayLayout; currentEffectInputSignature = newEffectInputSignature; if (newVertexArrayObject.InputLayout != null && ReferenceEquals(newEffectInputSignature, newVertexArrayObject.EffectInputSignature)) { // Default configuration currentInputLayout = newVertexArrayObject.InputLayout; } else if (ReferenceEquals(newEffectInputSignature, newVertexArrayObject.LastEffectInputSignature)) { // Reuse previous configuration currentInputLayout = newVertexArrayObject.LastInputLayout; } // Slow path if the current VertexArrayObject is not optimized for the particular input (or not used right before) else { currentInputLayout = InputLayoutManager.GetInputLayout(newEffectInputSignature, currentVertexArrayLayout); // Store it in VAO since it will likely be used with same effect later newVertexArrayObject.LastInputLayout = currentInputLayout; newVertexArrayObject.LastEffectInputSignature = newEffectInputSignature; } // Setup the input layout (if it changed) if (currentInputLayout != oldInputLayout) { inputAssembler.InputLayout = currentInputLayout; } } } SetViewportImpl(); }