private void InitPipeline() { var pipelineDescription = new GraphicsPipelineDescription(); var vertexLayout = new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), new VertexElementDescription("TexturePosition", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2)); var textureLayout = _graphicsDevice.ResourceFactory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription("SkeletonTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment))); _resourceSet = _graphicsDevice.ResourceFactory.CreateResourceSet(new ResourceSetDescription(textureLayout, _skeletonTextureView)); pipelineDescription.BlendState = BlendStateDescription.SingleAlphaBlend; pipelineDescription.DepthStencilState = new DepthStencilStateDescription( depthTestEnabled: true, depthWriteEnabled: true, comparisonKind: ComparisonKind.LessEqual); pipelineDescription.RasterizerState = new RasterizerStateDescription( cullMode: FaceCullMode.None, fillMode: PolygonFillMode.Solid, frontFace: FrontFace.Clockwise, depthClipEnabled: true, scissorTestEnabled: false); pipelineDescription.PrimitiveTopology = PrimitiveTopology.TriangleList; pipelineDescription.ResourceLayouts = new [] { textureLayout }; pipelineDescription.ShaderSet = new ShaderSetDescription( vertexLayouts: new [] { vertexLayout }, shaders: _shaders); pipelineDescription.Outputs = _graphicsDevice.SwapchainFramebuffer.OutputDescription; _pipeline = _graphicsDevice.ResourceFactory.CreateGraphicsPipeline(pipelineDescription); }
public Gui(Sdl2Window window, GraphicsDevice device) { LoadSettings(); Window = window; Device = device; Factory = device.ResourceFactory; TextRenderer = new TextRenderer(Device); ColorShader = new ColorShader(Factory); TextureShader = new TextureShader(Factory); // Create pipeline var pipelineDescription = new GraphicsPipelineDescription( BlendStateDescription.SingleAlphaBlend, new DepthStencilStateDescription( depthTestEnabled: true, depthWriteEnabled: true, comparisonKind: ComparisonKind.LessEqual), new RasterizerStateDescription( cullMode: FaceCullMode.Back, fillMode: PolygonFillMode.Solid, frontFace: FrontFace.Clockwise, depthClipEnabled: true, scissorTestEnabled: false), PrimitiveTopology.TriangleStrip, new ShaderSetDescription( vertexLayouts: new VertexLayoutDescription[] { ColorShader.Layout }, shaders: new Shader[] { ColorShader.VertexShader, ColorShader.FragmentShader }), new[] { ColorShader.ResourceLayout }, Device.SwapchainFramebuffer.OutputDescription ); Pipeline = Factory.CreateGraphicsPipeline(pipelineDescription); var texturePipelineDesc = new GraphicsPipelineDescription( BlendStateDescription.SingleAlphaBlend, new DepthStencilStateDescription( depthTestEnabled: true, depthWriteEnabled: true, comparisonKind: ComparisonKind.LessEqual), new RasterizerStateDescription( cullMode: FaceCullMode.Back, fillMode: PolygonFillMode.Solid, frontFace: FrontFace.Clockwise, depthClipEnabled: true, scissorTestEnabled: false), PrimitiveTopology.TriangleStrip, new ShaderSetDescription( vertexLayouts: new VertexLayoutDescription[] { TextureShader.Layout }, shaders: new Shader[] { TextureShader.VertexShader, TextureShader.FragmentShader }), new[] { TextureShader.ProjViewLayout, TextureShader.TextureLayout }, Device.SwapchainFramebuffer.OutputDescription ); TexturePipeline = Factory.CreateGraphicsPipeline(texturePipelineDesc); CommandList = Factory.CreateCommandList(); }
public ShaderProgramDescription(ShaderData vertShaderData, ShaderData fragShaderData) { if (vertShaderData is null) { throw new ArgumentNullException(nameof(vertShaderData)); } if (fragShaderData is null) { throw new ArgumentNullException(nameof(fragShaderData)); } UseSpirV = true; VertexShader = vertShaderData.ForStage(ShaderStages.Vertex); FragmentShader = fragShaderData.ForStage(ShaderStages.Fragment); VertexLayout = VertexTypeCache.GetDescription <VertexT>(); ValidateVertShaderInputsMatchVertLayout(VertexShader, VertexLayout); ValidateVertShaderOutputsMatchFragShaderOutputs(VertexShader, FragmentShader); PipelineOptions = new GraphicsPipelineDescription { BlendState = BlendStateDescription.SingleOverrideBlend, DepthStencilState = DepthStencilStateDescription.DepthOnlyLessEqual, RasterizerState = RasterizerStateDescription.Default, PrimitiveTopology = PrimitiveTopology.TriangleList, ResourceBindingModel = ResourceBindingModel.Improved }; }
public TextRenderer(GraphicsDevice device) { Device = device; var factory = device.ResourceFactory; Shader = AddDisposable(new TextShader(factory)); // Create pipeline var pipelineDescription = new GraphicsPipelineDescription(); pipelineDescription.BlendState = BlendStateDescription.SingleAlphaBlend; pipelineDescription.DepthStencilState = new DepthStencilStateDescription( depthTestEnabled: true, depthWriteEnabled: true, comparisonKind: ComparisonKind.LessEqual); pipelineDescription.RasterizerState = new RasterizerStateDescription( cullMode: FaceCullMode.Back, fillMode: PolygonFillMode.Solid, frontFace: FrontFace.Clockwise, depthClipEnabled: true, scissorTestEnabled: false); pipelineDescription.PrimitiveTopology = PrimitiveTopology.TriangleStrip; pipelineDescription.ResourceLayouts = new[] { Shader.ProjViewLayout, Shader.TextureLayout }; pipelineDescription.ShaderSet = new ShaderSetDescription( vertexLayouts: new VertexLayoutDescription[] { Shader.Layout }, shaders: new Shader[] { Shader.VertexShader, Shader.FragmentShader }); pipelineDescription.Outputs = Device.SwapchainFramebuffer.OutputDescription; _pipeline = AddDisposable(factory.CreateGraphicsPipeline(pipelineDescription)); CommandList = AddDisposable(factory.CreateCommandList()); }
private Pipeline CreatePipeline( Material material, FrontFace frontFace, PrimitiveTopology primitiveTopology, ResourceLayout resourceLayout, VertexLayoutDescription vertexLayout) { var pipelineDesc = new GraphicsPipelineDescription { BlendState = material.BlendState, DepthStencilState = material.DepthStencilState, RasterizerState = new RasterizerStateDescription( cullMode: material.CullMode, fillMode: material.FillMode, frontFace: frontFace, depthClipEnabled: true, scissorTestEnabled: false ), PrimitiveTopology = primitiveTopology, ResourceLayouts = new ResourceLayout[] { resourceLayout }, ShaderSet = new ShaderSetDescription( new VertexLayoutDescription[] { vertexLayout }, material.Shaders ), Outputs = _framebuffer.OutputDescription }; return(_factory.CreateGraphicsPipeline(pipelineDesc)); }
public Pipeline CreateAPipeline(ResourceLayout[] resourceLayouts, ShaderSetDescription shaderSetDescription, OutputDescription outputDescription, BlendStateDescription blendStateDescription, bool depthTest, FaceCullMode faceCullMode = FaceCullMode.None, PolygonFillMode polygonFillMode = PolygonFillMode.Solid, FrontFace frontFaceWindingOrder = FrontFace.Clockwise) { var pipelineDescription = new GraphicsPipelineDescription() { BlendState = blendStateDescription, DepthStencilState = new DepthStencilStateDescription( depthTestEnabled: depthTest, depthWriteEnabled: depthTest, comparisonKind: ComparisonKind.LessEqual), RasterizerState = new RasterizerStateDescription( cullMode: faceCullMode, fillMode: polygonFillMode, frontFace: frontFaceWindingOrder, depthClipEnabled: depthTest, scissorTestEnabled: false ), PrimitiveTopology = PrimitiveTopology.TriangleList, ResourceLayouts = resourceLayouts, ShaderSet = shaderSetDescription, Outputs = outputDescription }; return(_components.Factory.CreateGraphicsPipeline(pipelineDescription)); }
private Pipeline createPipeline(VertexLayoutDescription vertexLayout) { GraphicsPipelineDescription pipelineDescription = new GraphicsPipelineDescription(); pipelineDescription.BlendState = BlendStateDescription.SingleOverrideBlend; pipelineDescription.DepthStencilState = new DepthStencilStateDescription( depthTestEnabled: true, depthWriteEnabled: true, comparisonKind: ComparisonKind.LessEqual); pipelineDescription.RasterizerState = new RasterizerStateDescription( cullMode: FaceCullMode.Back, fillMode: PolygonFillMode.Solid, frontFace: FrontFace.Clockwise, depthClipEnabled: true, scissorTestEnabled: false); pipelineDescription.PrimitiveTopology = PrimitiveTopology.TriangleStrip; pipelineDescription.ResourceLayouts = System.Array.Empty <ResourceLayout>(); pipelineDescription.ShaderSet = new ShaderSetDescription( vertexLayouts: new VertexLayoutDescription[] { vertexLayout }, shaders: shaders); pipelineDescription.Outputs = GraphicsDevice.SwapchainFramebuffer.OutputDescription; return(GraphicsDevice.ResourceFactory.CreateGraphicsPipeline(pipelineDescription)); }
private static void CreateResources() { ResourceFactory factory = _graphicsDevice.ResourceFactory; // Create pipeline GraphicsPipelineDescription pipelineDescription = new GraphicsPipelineDescription(); pipelineDescription.BlendState = BlendStateDescription.SingleOverrideBlend; pipelineDescription.DepthStencilState = new DepthStencilStateDescription( depthTestEnabled: true, depthWriteEnabled: true, comparisonKind: ComparisonKind.LessEqual); pipelineDescription.RasterizerState = new RasterizerStateDescription( cullMode: FaceCullMode.Back, fillMode: PolygonFillMode.Solid, frontFace: FrontFace.Clockwise, depthClipEnabled: true, scissorTestEnabled: false); pipelineDescription.PrimitiveTopology = PrimitiveTopology.TriangleStrip; pipelineDescription.ResourceLayouts = System.Array.Empty <ResourceLayout>(); pipelineDescription.ShaderSet = new ShaderSetDescription( vertexLayouts: new VertexLayoutDescription[] { Gui.ColorShader.Layout }, shaders: new Shader[] { Gui.ColorShader.VertexShader, Gui.ColorShader.FragmentShader }); pipelineDescription.Outputs = _graphicsDevice.SwapchainFramebuffer.OutputDescription; _pipeline = factory.CreateGraphicsPipeline(pipelineDescription); _commandList = factory.CreateCommandList(); }
public void CreateDeviceObjects(IRendererContext context) { var c = (VeldridRendererContext)context; var cl = c.CommandList; var gd = c.GraphicsDevice; var sc = c.SceneContext; ResourceFactory factory = gd.ResourceFactory; _vb = factory.CreateBuffer(new BufferDescription(Vertices.SizeInBytes(), BufferUsage.VertexBuffer)); _ib = factory.CreateBuffer(new BufferDescription(Indices.SizeInBytes(), BufferUsage.IndexBuffer)); _miscUniformBuffer = factory.CreateBuffer(new BufferDescription(MiscUniformData.SizeInBytes, BufferUsage.UniformBuffer)); _vb.Name = "TileMapVertexBuffer"; _ib.Name = "TileMapIndexBuffer"; _miscUniformBuffer.Name = "TileMapMiscBuffer"; cl.UpdateBuffer(_vb, 0, Vertices); cl.UpdateBuffer(_ib, 0, Indices); var shaderCache = Resolve <IShaderCache>(); _shaders = shaderCache.GetShaderPair(gd.ResourceFactory, VertexShaderName, FragmentShaderName, shaderCache.GetGlsl(VertexShaderName), shaderCache.GetGlsl(FragmentShaderName)); var shaderSet = new ShaderSetDescription(new[] { VertexLayout, InstanceLayout }, _shaders); _layout = factory.CreateResourceLayout(PerSpriteLayoutDescription); _textureSampler = gd.ResourceFactory.CreateSampler(new SamplerDescription( SamplerAddressMode.Clamp, SamplerAddressMode.Clamp, SamplerAddressMode.Clamp, SamplerFilter.MinPoint_MagPoint_MipPoint, null, 1, 0, 0, 0, SamplerBorderColor.TransparentBlack )); var depthStencilMode = gd.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyLessEqual : DepthStencilStateDescription.DepthOnlyGreaterEqual; var rasterizerMode = new RasterizerStateDescription( FaceCullMode.Front, PolygonFillMode.Solid, FrontFace.Clockwise, true, true); var pd = new GraphicsPipelineDescription( BlendStateDescription.SingleAlphaBlend, depthStencilMode, rasterizerMode, PrimitiveTopology.TriangleList, new ShaderSetDescription(new[] { VertexLayout, InstanceLayout }, shaderSet.Shaders, ShaderHelper.GetSpecializations(gd)), new[] { _layout, sc.CommonResourceLayout }, sc.MainSceneFramebuffer.OutputDescription); _pipeline = factory.CreateGraphicsPipeline(ref pd); _pipeline.Name = "P_TileMapRenderer"; _disposeCollector.Add(_vb, _ib, _layout, _textureSampler, _pipeline); }
public PipelineBuilder BuildDefault() { _pipelineDescription = new GraphicsPipelineDescription(); _pipelineDescription.BlendState = BlendStateDescription.SingleOverrideBlend; _pipelineDescription.DepthStencilState = new DepthStencilStateDescription( depthTestEnabled: true, depthWriteEnabled: true, comparisonKind: ComparisonKind.LessEqual); _pipelineDescription.RasterizerState = new RasterizerStateDescription( cullMode: FaceCullMode.Back, fillMode: PolygonFillMode.Solid, frontFace: FrontFace.Clockwise, depthClipEnabled: true, scissorTestEnabled: false); _pipelineDescription.ResourceBindingModel = ResourceBindingModel.Improved; _pipelineDescription.PrimitiveTopology = PrimitiveTopology.TriangleStrip; _shadersSetup = false; _pipelineDescription.Outputs = _graphicsDevice.SwapchainFramebuffer.OutputDescription; return(this); }
private static CachedPipeline CreateAndCachePipeline(this ResourceFactory resourceFactory, SimplePipelineDescription simplePipelineDescription) { var faceCullMode = simplePipelineDescription.LayerShading.HasFlag(LayerShading.TwoSided) ? FaceCullMode.None : FaceCullMode.Back; var shaderSet = resourceFactory.GetShaderSet(simplePipelineDescription.ShaderSettings); var pipelineDescription = new GraphicsPipelineDescription( simplePipelineDescription.FilterMode.ToBlendStateDescription(), DepthStencilStateDescription.DepthOnlyLessEqual, new RasterizerStateDescription(faceCullMode, PolygonFillMode.Solid, FrontFace.CounterClockwise, true, false), simplePipelineDescription.FaceType.ToPrimitiveTopology(), shaderSet.ShaderSetDescription, shaderSet.ResourceLayout, simplePipelineDescription.OutputDescription); var pipeline = resourceFactory.CreateGraphicsPipeline(ref pipelineDescription); var cachedPipeline = new CachedPipeline { Pipeline = pipeline, ResourceLayout = shaderSet.ResourceLayout, }; _cachedPipelines[resourceFactory].Add(simplePipelineDescription, cachedPipeline); return(cachedPipeline); }
public BuiltPipeline(Pipeline pipeline, GraphicsPipelineDescription descr, ResourceLayoutDescription[] resLayoutDescriptions, string shaderSetName) { Pipeline = pipeline; Description = descr; ResourceLayoutDescriptions = resLayoutDescriptions; ShaderSetName = shaderSetName; }
protected override Pipeline CreateGraphicsPipelineCore(ref GraphicsPipelineDescription description) { Pipeline pipeline = Factory.CreateGraphicsPipeline(ref description); DisposeCollector.Add(pipeline); return(pipeline); }
/// <summary> /// Creates a new <see cref="Pipeline"/> object. /// </summary> /// <param name="description">The desired properties of the created object.</param> /// <returns>A new <see cref="Pipeline"/> which, when bound to a CommandList, is used to dispatch draw commands.</returns> public Pipeline CreateGraphicsPipeline(ref GraphicsPipelineDescription description) { #if VALIDATE_USAGE if (!description.RasterizerState.DepthClipEnabled && !Features.DepthClipDisable) { throw new VeldridException( "RasterizerState.DepthClipEnabled must be true if GraphicsDeviceFeatures.DepthClipDisable is not supported."); } if (description.RasterizerState.FillMode == PolygonFillMode.Wireframe && !Features.FillModeWireframe) { throw new VeldridException( "PolygonFillMode.Wireframe requires GraphicsDeviceFeatures.FillModeWireframe."); } if (!Features.IndependentBlend) { if (description.BlendState.AttachmentStates.Length > 0) { BlendAttachmentDescription attachmentState = description.BlendState.AttachmentStates[0]; for (int i = 1; i < description.BlendState.AttachmentStates.Length; i++) { if (!attachmentState.Equals(description.BlendState.AttachmentStates[i])) { throw new VeldridException( $"If GraphcsDeviceFeatures.IndependentBlend is false, then all members of BlendState.AttachmentStates must be equal."); } } } } #endif return(CreateGraphicsPipelineCore(ref description)); }
private void Initialize(BlendStateDescription?blendStateDescription = null) { commandList = factory.CreateCommandList(); var indexBufferDescription = new BufferDescription(sizeof(ushort) * MAX_BATCH * 6, BufferUsage.IndexBuffer | BufferUsage.Dynamic); indexBuffer = factory.CreateBuffer(indexBufferDescription); var vertexBufferDescription = new BufferDescription(VERTEX_SIZE * MAX_BATCH * 4, BufferUsage.VertexBuffer | BufferUsage.Dynamic); vertexBuffer = factory.CreateBuffer(vertexBufferDescription); var worldMatrixBufferDescription = new BufferDescription(64, BufferUsage.UniformBuffer); worldMatrixBuffer = factory.CreateBuffer(worldMatrixBufferDescription); viewResourceLayout = factory.CreateResourceLayout(new ResourceLayoutDescription(new ResourceLayoutElementDescription("WorldView", ResourceKind.UniformBuffer, ShaderStages.Vertex))); graphicsResourceLayout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("Texture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("TextureSampler", ResourceKind.Sampler, ShaderStages.Fragment))); var rasterizerStateDescription = new RasterizerStateDescription(FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, true, true); var pipelineDescription = new GraphicsPipelineDescription( blendStateDescription ?? BlendStateDescription.SingleAlphaBlend, DepthStencilStateDescription.DepthOnlyGreaterEqual, rasterizerStateDescription, PrimitiveTopology.TriangleList, shaderSet, new[] { viewResourceLayout, graphicsResourceLayout }, framebuffer.OutputDescription); pipeline = factory.CreateGraphicsPipeline(pipelineDescription); }
public void Create(RenderContext context) { (_vertex, _fragment) = context.ResourceLoader.LoadShaders(Type.ToString()); var pDesc = new GraphicsPipelineDescription { BlendState = BlendStateDescription.SingleDisabled, DepthStencilState = DepthStencilStateDescription.DepthOnlyLessEqual, RasterizerState = RasterizerStateDescription.Default, PrimitiveTopology = PrimitiveTopology.TriangleList, ResourceLayouts = new[] { context.ResourceLoader.ProjectionLayout, context.ResourceLoader.TextureLayout }, ShaderSet = new ShaderSetDescription(new[] { context.ResourceLoader.VertexStandardLayoutDescription }, new[] { _vertex, _fragment }), Outputs = new OutputDescription { ColorAttachments = new[] { new OutputAttachmentDescription(PixelFormat.B8_G8_R8_A8_UNorm) }, DepthAttachment = new OutputAttachmentDescription(PixelFormat.R32_Float), SampleCount = TextureSampleCount.Count1 } }; _pipeline = context.Device.ResourceFactory.CreateGraphicsPipeline(ref pDesc); _projectionBuffer = context.Device.ResourceFactory.CreateBuffer( new BufferDescription((uint)Unsafe.SizeOf <UniformProjection>(), BufferUsage.UniformBuffer) ); _projectionResourceSet = context.Device.ResourceFactory.CreateResourceSet( new ResourceSetDescription(context.ResourceLoader.ProjectionLayout, _projectionBuffer) ); }
public void CreateDeviceResources(GraphicsDevice gd, OutputDescription outputDescription, ColorSpaceHandling colorSpaceHandling) { _gd = gd; _colorSpaceHandling = colorSpaceHandling; ResourceFactory factory = gd.ResourceFactory; _vertexBuffer = factory.CreateBuffer(new BufferDescription(10000, BufferUsage.VertexBuffer | BufferUsage.Dynamic)); _vertexBuffer.Name = "ImGui.NET Vertex Buffer"; _indexBuffer = factory.CreateBuffer(new BufferDescription(2000, BufferUsage.IndexBuffer | BufferUsage.Dynamic)); _indexBuffer.Name = "ImGui.NET Index Buffer"; _projMatrixBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); _projMatrixBuffer.Name = "ImGui.NET Projection Buffer"; var res = StaticResourceCache.GetShaders(gd, gd.ResourceFactory, "imgui").ToTuple(); _vertexShader = res.Item1; _fragmentShader = res.Item2; VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[] { new VertexLayoutDescription( new VertexElementDescription("in_position", VertexElementSemantic.Position, VertexElementFormat.Float2), new VertexElementDescription("in_texCoord", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), new VertexElementDescription("in_color", VertexElementSemantic.Color, VertexElementFormat.Byte4_Norm)) }; _layout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("ProjectionMatrixBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("MainSampler", ResourceKind.Sampler, ShaderStages.Fragment))); _textureLayout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("MainTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment))); GraphicsPipelineDescription pd = new GraphicsPipelineDescription( BlendStateDescription.SingleAlphaBlend, new DepthStencilStateDescription(false, false, ComparisonKind.Always), new RasterizerStateDescription(FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, true, true), PrimitiveTopology.TriangleList, new ShaderSetDescription( vertexLayouts, new[] { _vertexShader, _fragmentShader }, new[] { new SpecializationConstant(0, gd.IsClipSpaceYInverted), new SpecializationConstant(1, _colorSpaceHandling == ColorSpaceHandling.Legacy), }), new ResourceLayout[] { _layout, Renderer.GlobalTexturePool.GetLayout() }, outputDescription, ResourceBindingModel.Default); _pipeline = factory.CreateGraphicsPipeline(ref pd); _pipeline.Name = "ImGuiPipeline"; _mainResourceSet = factory.CreateResourceSet(new ResourceSetDescription(_layout, _projMatrixBuffer, gd.PointSampler)); RecreateFontDeviceTexture(gd); }
/// <summary> /// Creates a new <see cref="Pipeline"/> object. /// </summary> /// <param name="description">The desired properties of the created object.</param> /// <returns>A new <see cref="Pipeline"/> which, when bound to a CommandList, is used to dispatch draw commands.</returns> public Pipeline CreateGraphicsPipeline(ref GraphicsPipelineDescription description) { #if VALIDATE_USAGE if (!description.RasterizerState.DepthClipEnabled && !Features.DepthClipDisable) { throw new VeldridException( "RasterizerState.DepthClipEnabled must be true if GraphicsDeviceFeatures.DepthClipDisable is not supported."); } if (description.RasterizerState.FillMode == PolygonFillMode.Wireframe && !Features.FillModeWireframe) { throw new VeldridException( "PolygonFillMode.Wireframe requires GraphicsDeviceFeatures.FillModeWireframe."); } if (!Features.IndependentBlend) { if (description.BlendState.AttachmentStates.Length > 0) { BlendAttachmentDescription attachmentState = description.BlendState.AttachmentStates[0]; for (int i = 1; i < description.BlendState.AttachmentStates.Length; i++) { if (!attachmentState.Equals(description.BlendState.AttachmentStates[i])) { throw new VeldridException( $"If GraphcsDeviceFeatures.IndependentBlend is false, then all members of BlendState.AttachmentStates must be equal."); } } } } foreach (VertexLayoutDescription layoutDesc in description.ShaderSet.VertexLayouts) { bool hasExplicitLayout = false; uint minOffset = 0; foreach (VertexElementDescription elementDesc in layoutDesc.Elements) { if (hasExplicitLayout && elementDesc.Offset == 0) { throw new VeldridException( $"If any vertex element has an explicit offset, then all elements must have an explicit offset."); } if (elementDesc.Offset != 0 && elementDesc.Offset < minOffset) { throw new VeldridException( $"Vertex element \"{elementDesc.Name}\" has an explicit offset which overlaps with the previous element."); } minOffset = elementDesc.Offset + FormatHelpers.GetSizeInBytes(elementDesc.Format); hasExplicitLayout |= elementDesc.Offset != 0; } if (minOffset > layoutDesc.Stride) { throw new VeldridException( $"The vertex layout's stride ({layoutDesc.Stride}) is less than the full size of the vertex ({minOffset})"); } } #endif return(CreateGraphicsPipelineCore(ref description)); }
//TODO: Add color space handling override public void CreateResources(SceneRuntimeDescriptor sceneRuntimeDescriptor, ModelRuntimeDescriptor <VertexPositionNormalTextureTangentBitangent>[] modelPNTTBDescriptorArray, ModelRuntimeDescriptor <VertexPositionNormal>[] modelPNDescriptorArray, ModelRuntimeDescriptor <VertexPositionTexture>[] modelPTDescriptorArray, ModelRuntimeDescriptor <VertexPositionColor>[] modelPCDescriptorArray) { //ResourceFactory factory = GraphicsDevice.ResourceFactory; _vertexBuffer = _factory.CreateBuffer(new BufferDescription(10000, BufferUsage.VertexBuffer | BufferUsage.Dynamic)); _vertexBuffer.Name = "ImGui.NET Vertex Buffer"; _indexBuffer = _factory.CreateBuffer(new BufferDescription(2000, BufferUsage.IndexBuffer | BufferUsage.Dynamic)); _indexBuffer.Name = "ImGui.NET Index Buffer"; _projMatrixBuffer = _factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); _projMatrixBuffer.Name = "ImGui.NET Projection Buffer"; byte[] vertexShaderBytes = LoadEmbeddedShaderCode(GraphicsDevice.ResourceFactory, "imgui-vertex"); byte[] fragmentShaderBytes = LoadEmbeddedShaderCode(GraphicsDevice.ResourceFactory, "imgui-frag"); _vertexShader = _factory.CreateShader(new ShaderDescription(ShaderStages.Vertex, vertexShaderBytes, GraphicsDevice.BackendType == GraphicsBackend.Vulkan ? "main" : "VS")); _fragmentShader = _factory.CreateShader(new ShaderDescription(ShaderStages.Fragment, fragmentShaderBytes, GraphicsDevice.BackendType == GraphicsBackend.Vulkan ? "main" : "FS")); VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[] { new VertexLayoutDescription( new VertexElementDescription("in_position", VertexElementSemantic.Position, VertexElementFormat.Float2), new VertexElementDescription("in_texCoord", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), new VertexElementDescription("in_color", VertexElementSemantic.Color, VertexElementFormat.Byte4_Norm)) }; _layout = _factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("ProjectionMatrixBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("MainSampler", ResourceKind.Sampler, ShaderStages.Fragment))); _textureLayout = _factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("MainTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment))); GraphicsPipelineDescription pd = new GraphicsPipelineDescription( BlendStateDescription.SingleAlphaBlend, new DepthStencilStateDescription(false, false, ComparisonKind.Always), new RasterizerStateDescription(FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, true, true), PrimitiveTopology.TriangleList, new ShaderSetDescription( vertexLayouts, new[] { _vertexShader, _fragmentShader }, new[] { new SpecializationConstant(0, GraphicsDevice.IsClipSpaceYInverted), new SpecializationConstant(1, true), }), new ResourceLayout[] { _layout, _textureLayout }, _frameBuffer.OutputDescription, ResourceBindingModel.Improved); _pipeline = _factory.CreateGraphicsPipeline(ref pd); _mainResourceSet = _factory.CreateResourceSet(new ResourceSetDescription(_layout, _projMatrixBuffer, GraphicsDevice.PointSampler)); RecreateFontDeviceTexture(GraphicsDevice); }
private static void CreateResources() { ResourceFactory factory = _graphicsDevice.ResourceFactory; VertexPositionColor[] quadVertices = { new VertexPositionColor(new Vector2(-.75f, .75f), RgbaFloat.Red), new VertexPositionColor(new Vector2(.75f, .75f), RgbaFloat.Green), new VertexPositionColor(new Vector2(-.75f, -.75f), RgbaFloat.Blue), new VertexPositionColor(new Vector2(.75f, -.75f), RgbaFloat.Yellow) }; BufferDescription vbDescription = new BufferDescription( 4 * VertexPositionColor.SizeInBytes, BufferUsage.VertexBuffer); _vertexBuffer = factory.CreateBuffer(vbDescription); _graphicsDevice.UpdateBuffer(_vertexBuffer, 0, quadVertices); ushort[] quadIndices = { 0, 1, 2, 3 }; BufferDescription ibDescription = new BufferDescription( 4 * sizeof(ushort), BufferUsage.IndexBuffer); _indexBuffer = factory.CreateBuffer(ibDescription); _graphicsDevice.UpdateBuffer(_indexBuffer, 0, quadIndices); VertexLayoutDescription vertexLayout = new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float2), new VertexElementDescription("Color", VertexElementSemantic.Color, VertexElementFormat.Float4)); _vertexShader = LoadShader(ShaderStages.Vertex); _fragmentShader = LoadShader(ShaderStages.Fragment); // Create pipeline GraphicsPipelineDescription pipelineDescription = new GraphicsPipelineDescription(); pipelineDescription.BlendState = BlendStateDescription.SingleOverrideBlend; pipelineDescription.DepthStencilState = new DepthStencilStateDescription( depthTestEnabled: true, depthWriteEnabled: true, comparisonKind: ComparisonKind.LessEqual); pipelineDescription.RasterizerState = new RasterizerStateDescription( cullMode: FaceCullMode.Back, fillMode: PolygonFillMode.Solid, frontFace: FrontFace.Clockwise, depthClipEnabled: true, scissorTestEnabled: false); pipelineDescription.PrimitiveTopology = PrimitiveTopology.TriangleStrip; pipelineDescription.ResourceLayouts = System.Array.Empty <ResourceLayout>(); pipelineDescription.ShaderSet = new ShaderSetDescription( vertexLayouts: new VertexLayoutDescription[] { vertexLayout }, shaders: new Shader[] { _vertexShader, _fragmentShader }); pipelineDescription.Outputs = _graphicsDevice.SwapchainFramebuffer.OutputDescription; _pipeline = factory.CreateGraphicsPipeline(pipelineDescription); _commandList = factory.CreateCommandList(); }
public SkyboxRenderer(GraphicsDevice device, Framebuffer target, Image <Rgba32> positiveXImage, Image <Rgba32> negativeXImage, Image <Rgba32> positiveYImage, Image <Rgba32> negativeYImage, Image <Rgba32> positiveZImage, Image <Rgba32> negativeZImage) { _skyboxTexture = new ImageSharpCubemapTexture(positiveXImage, negativeXImage, positiveYImage, negativeYImage, positiveZImage, negativeZImage); var factory = device.ResourceFactory; var deviceTexture = _skyboxTexture.CreateDeviceTexture(device, factory); _skyboxTexture = null; TextureView textureView = factory.CreateTextureView(new TextureViewDescription(deviceTexture)); _vb = factory.CreateBuffer(new BufferDescription(VertexPosition.SizeInBytes * (uint)s_vertices.Length, BufferUsage.VertexBuffer)); device.UpdateBuffer(_vb, 0, s_vertices); _ib = factory.CreateBuffer(new BufferDescription(sizeof(ushort) * (uint)s_indices.Length, BufferUsage.IndexBuffer)); device.UpdateBuffer(_ib, 0, s_indices); VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3)) }; _layout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("CubeTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("CubeSampler", ResourceKind.Sampler, ShaderStages.Fragment))); GraphicsPipelineDescription pd = new GraphicsPipelineDescription( BlendStateDescription.SingleAlphaBlend, //device.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyGreaterEqual : DepthStencilStateDescription.DepthOnlyLessEqual, new DepthStencilStateDescription(false, false, ComparisonKind.LessEqual), new RasterizerStateDescription(FaceCullMode.Back, PolygonFillMode.Solid, FrontFace.Clockwise, true, true), PrimitiveTopology.TriangleList, new ShaderSetDescription( vertexLayouts, factory.CreateFromSpirv( new ShaderDescription(ShaderStages.Vertex, Encoding.UTF8.GetBytes(SkyboxShader.VertexCode), "main"), new ShaderDescription(ShaderStages.Fragment, Encoding.UTF8.GetBytes(SkyboxShader.FragmentCode), "main"))), new ResourceLayout[] { _layout }, target.OutputDescription); _pipeline = factory.CreateGraphicsPipeline(ref pd); ProjectionMatrixBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer)); ViewMatrixBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer)); _resourceSet = factory.CreateResourceSet(new ResourceSetDescription( _layout, ProjectionMatrixBuffer, ViewMatrixBuffer, textureView, device.Aniso4xSampler)); }
public MIDIPatternIO(GraphicsDevice gd, ImGuiView view, Func <Vector2> computeSize, MIDIPatternConnect pattern) : base(gd, view, computeSize) { Buffers = new BufferList <VertexPositionColor> [257]; // first buffer for general purpose, others for keys for (int i = 0; i < 257; i++) { Buffers[i] = dispose.Add(new BufferList <VertexPositionColor>(gd, 6 * 2048 * 16, new[] { 0, 3, 2, 0, 2, 1 })); } PatternHandler = pattern; CurrentInteraction = new MIDIPatternInteractionIdle(PatternHandler); ProjMatrix = Factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); dispose.Add(ProjMatrix); VertexLayoutDescription vertexLayout = new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), new VertexElementDescription("Color", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float4)); Layout = Factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("ProjectionMatrixBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex))); dispose.Add(Layout); ShaderDescription vertexShaderDesc = new ShaderDescription( ShaderStages.Vertex, Encoding.UTF8.GetBytes(VertexCode), "main"); ShaderDescription fragmentShaderDesc = new ShaderDescription( ShaderStages.Fragment, Encoding.UTF8.GetBytes(FragmentCode), "main"); Shaders = dispose.AddArray(Factory.CreateFromSpirv(vertexShaderDesc, fragmentShaderDesc)); var pipelineDescription = new GraphicsPipelineDescription(); pipelineDescription.BlendState = BlendStateDescription.SingleAlphaBlend; pipelineDescription.DepthStencilState = new DepthStencilStateDescription( depthTestEnabled: true, depthWriteEnabled: true, comparisonKind: ComparisonKind.LessEqual); pipelineDescription.RasterizerState = new RasterizerStateDescription( cullMode: FaceCullMode.Front, fillMode: PolygonFillMode.Solid, frontFace: FrontFace.Clockwise, depthClipEnabled: true, scissorTestEnabled: false); pipelineDescription.PrimitiveTopology = PrimitiveTopology.TriangleList; pipelineDescription.ResourceLayouts = new ResourceLayout[] { Layout }; pipelineDescription.ShaderSet = new ShaderSetDescription( vertexLayouts: new VertexLayoutDescription[] { vertexLayout }, shaders: Shaders); pipelineDescription.Outputs = Canvas.FrameBuffer.OutputDescription; Pipeline = Factory.CreateGraphicsPipeline(pipelineDescription); MainResourceSet = Factory.CreateResourceSet(new ResourceSetDescription(Layout, ProjMatrix)); }
Pipeline BuildPipeline(GraphicsDevice gd, SceneContext sc, SpriteShaderKey key) { var shaderCache = Resolve <IShaderCache>(); var vertexShaderName = "SpriteSV.vert"; var fragmentShaderName = "SpriteSF.frag"; var vertexShaderContent = shaderCache.GetGlsl(vertexShaderName); var fragmentShaderContent = shaderCache.GetGlsl(fragmentShaderName); if (key.UseArrayTexture) { fragmentShaderName += ".array"; fragmentShaderContent = @"#define USE_ARRAY_TEXTURE " + fragmentShaderContent; } var shaders = shaderCache.GetShaderPair( gd.ResourceFactory, vertexShaderName, fragmentShaderName, vertexShaderContent, fragmentShaderContent); _shaders.AddRange(shaders); var shaderSet = new ShaderSetDescription(new[] { VertexLayout, InstanceLayout }, shaders); Console.WriteLine($"-- Build SpriteRenderer-- IsDepth0..1: {gd.IsDepthRangeZeroToOne} YInvert: {gd.IsClipSpaceYInverted} UV TopLeft: {gd.IsUvOriginTopLeft}"); var depthStencilMode = key.PerformDepthTest ? gd.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyLessEqual : DepthStencilStateDescription.DepthOnlyGreaterEqual : DepthStencilStateDescription.Disabled; var rasterizerMode = new RasterizerStateDescription( FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, key.PerformDepthTest, // depth test true); // scissor test var pipelineDescription = new GraphicsPipelineDescription( BlendStateDescription.SingleAlphaBlend, depthStencilMode, rasterizerMode, PrimitiveTopology.TriangleList, new ShaderSetDescription(new[] { VertexLayout, InstanceLayout }, shaderSet.Shaders, ShaderHelper.GetSpecializations(gd)), new[] { _perSpriteResourceLayout, sc.CommonResourceLayout }, sc.MainSceneFramebuffer.OutputDescription); var pipeline = gd.ResourceFactory.CreateGraphicsPipeline(ref pipelineDescription); pipeline.Name = $"P_Sprite_{key}"; return(pipeline); }
public void CreateDeviceObjects(IRendererContext context) { var c = (VeldridRendererContext)context; var cl = c.CommandList; var gd = c.GraphicsDevice; var sc = c.SceneContext; DisposeCollectorResourceFactory factory = new DisposeCollectorResourceFactory(gd.ResourceFactory); _disposeCollector = factory.DisposeCollector; ResourceLayout resourceLayout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("SourceTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("SourceSampler", ResourceKind.Sampler, ShaderStages.Fragment))); var shaderCache = Resolve <IShaderCache>(); _shaders = shaderCache.GetShaderPair(gd.ResourceFactory, VertexShaderName, FragmentShaderName, shaderCache.GetGlsl(VertexShaderName), shaderCache.GetGlsl(FragmentShaderName)); GraphicsPipelineDescription pd = new GraphicsPipelineDescription( new BlendStateDescription( RgbaFloat.Black, BlendAttachmentDescription.OverrideBlend, BlendAttachmentDescription.OverrideBlend), gd.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyGreaterEqual : DepthStencilStateDescription.DepthOnlyLessEqual, RasterizerStateDescription.Default, PrimitiveTopology.TriangleList, new ShaderSetDescription( new[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), new VertexElementDescription("TexCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2)) }, _shaders, ShaderHelper.GetSpecializations(gd)), new[] { resourceLayout }, sc.DuplicatorFramebuffer.OutputDescription); _pipeline = factory.CreateGraphicsPipeline(ref pd); _pipeline.Name = "P_ScreenDuplicator"; float[] verts = CoreUtil.GetFullScreenQuadVerts(gd.IsClipSpaceYInverted); _vb = factory.CreateBuffer(new BufferDescription(verts.SizeInBytes() * sizeof(float), BufferUsage.VertexBuffer)); cl.UpdateBuffer(_vb, 0, verts); _ib = factory.CreateBuffer( new BufferDescription((uint)QuadIndices.Length * sizeof(ushort), BufferUsage.IndexBuffer)); cl.UpdateBuffer(_ib, 0, QuadIndices); }
public override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneRenderPipeline sp) { var factory = gd.ResourceFactory; WorldBuffer = Renderer.UniformBufferAllocator.Allocate(128, 128); WorldBuffer.FillBuffer(cl, ref _World); VertexLayoutDescription[] mainVertexLayouts = new VertexLayoutDescription[] { new VertexLayoutDescription( new VertexElementDescription("position", VertexElementSemantic.TextureCoordinate, Veldrid.VertexElementFormat.Float3), new VertexElementDescription("normal", VertexElementSemantic.TextureCoordinate, Veldrid.VertexElementFormat.SByte4), new VertexElementDescription("color", VertexElementSemantic.TextureCoordinate, Veldrid.VertexElementFormat.Byte4)) }; var res = StaticResourceCache.GetShaders(gd, gd.ResourceFactory, "Collision").ToTuple(); Shaders = new Shader[] { res.Item1, res.Item2 }; ResourceLayout projViewLayout = StaticResourceCache.GetResourceLayout( gd.ResourceFactory, StaticResourceCache.SceneParamLayoutDescription); ResourceLayout mainPerObjectLayout = StaticResourceCache.GetResourceLayout(gd.ResourceFactory, new ResourceLayoutDescription( new ResourceLayoutElementDescription("WorldBuffer", ResourceKind.StructuredBufferReadWrite, ShaderStages.Vertex | ShaderStages.Fragment, ResourceLayoutElementOptions.None))); PerObjRS = StaticResourceCache.GetResourceSet(factory, new ResourceSetDescription(mainPerObjectLayout, Renderer.UniformBufferAllocator._backingBuffer)); bool isTriStrip = false; var fres = ColResource.Get(); if (fres != null) { var mesh = fres.GPUMeshes[ColMeshIndex]; } GraphicsPipelineDescription pipelineDescription = new GraphicsPipelineDescription(); pipelineDescription.BlendState = BlendStateDescription.SingleOverrideBlend; pipelineDescription.DepthStencilState = DepthStencilStateDescription.DepthOnlyGreaterEqual; pipelineDescription.RasterizerState = new RasterizerStateDescription( cullMode: FaceCullMode.Back, fillMode: PolygonFillMode.Solid, frontFace: WindClockwise ? FrontFace.Clockwise : FrontFace.CounterClockwise, depthClipEnabled: true, scissorTestEnabled: false); pipelineDescription.PrimitiveTopology = isTriStrip ? PrimitiveTopology.TriangleStrip : PrimitiveTopology.TriangleList; pipelineDescription.ShaderSet = new ShaderSetDescription( vertexLayouts: mainVertexLayouts, shaders: Shaders); pipelineDescription.ResourceLayouts = new ResourceLayout[] { projViewLayout, mainPerObjectLayout, Renderer.GlobalTexturePool.GetLayout(), Renderer.GlobalCubeTexturePool.GetLayout(), Renderer.MaterialBufferAllocator.GetLayout(), SamplerSet.SamplersLayout }; pipelineDescription.Outputs = gd.SwapchainFramebuffer.OutputDescription; RenderPipeline = StaticResourceCache.GetPipeline(factory, ref pipelineDescription); }
public void ResourceSet_IncompatibleSet_Fails() { DeviceBuffer infoBuffer = RF.CreateBuffer(new BufferDescription(16, BufferUsage.UniformBuffer)); DeviceBuffer orthoBuffer = RF.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer)); ShaderSetDescription shaderSet = new ShaderSetDescription( new VertexLayoutDescription[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float2), new VertexElementDescription("Color_UInt", VertexElementSemantic.Color, VertexElementFormat.UInt4)) }, new Shader[] { TestShaders.Load(RF, "UIntVertexAttribs", ShaderStages.Vertex, "VS"), TestShaders.Load(RF, "UIntVertexAttribs", ShaderStages.Fragment, "FS") }); ResourceLayout layout = RF.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("InfoBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("Ortho", ResourceKind.UniformBuffer, ShaderStages.Vertex))); ResourceLayout layout2 = RF.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("InfoBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("Tex", ResourceKind.TextureReadOnly, ShaderStages.Fragment))); ResourceLayout layout3 = RF.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("InfoBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex))); Texture tex = RF.CreateTexture(TextureDescription.Texture2D(16, 16, 1, 1, PixelFormat.R32_G32_B32_A32_Float, TextureUsage.Sampled)); TextureView texView = RF.CreateTextureView(tex); ResourceSet set = RF.CreateResourceSet(new ResourceSetDescription(layout, infoBuffer, orthoBuffer)); ResourceSet set2 = RF.CreateResourceSet(new ResourceSetDescription(layout2, infoBuffer, texView)); ResourceSet set3 = RF.CreateResourceSet(new ResourceSetDescription(layout3, infoBuffer)); GraphicsPipelineDescription gpd = new GraphicsPipelineDescription( BlendStateDescription.SingleOverrideBlend, DepthStencilStateDescription.Disabled, RasterizerStateDescription.Default, PrimitiveTopology.PointList, shaderSet, layout, new OutputDescription(null, new OutputAttachmentDescription(PixelFormat.B8_G8_R8_A8_UNorm))); Pipeline pipeline = RF.CreateGraphicsPipeline(ref gpd); CommandList cl = RF.CreateCommandList(); cl.Begin(); cl.SetPipeline(pipeline); cl.SetGraphicsResourceSet(0, set); Assert.Throws <VeldridException>(() => cl.SetGraphicsResourceSet(0, set2)); // Wrong type Assert.Throws <VeldridException>(() => cl.SetGraphicsResourceSet(0, set3)); // Wrong count }
private VxContext(GraphicsDevice gd, Sdl2Window window) { Device = gd; Window = window; Factory = new DisposeCollectorResourceFactory(gd.ResourceFactory); _cl = Factory.CreateCommandList(); Window.Resized += () => { Device.MainSwapchain.Resize((uint)Window.Width, (uint)Window.Height); _imguiRenderer.WindowResized(Window.Width, Window.Height); }; ShaderSetDescription meshShaderSet = new ShaderSetDescription( new[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3), new VertexElementDescription("Normal", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3)) }, ShaderHelpers.LoadSet(Device, Factory, "Model")); ResourceLayout viewProjLayout = Factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("ViewProjection", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("SceneInfo", ResourceKind.UniformBuffer, ShaderStages.Fragment))); ResourceLayout worldLayout = Factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("World", ResourceKind.UniformBuffer, ShaderStages.Vertex))); ResourceLayout modelParamsLayout = Factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("ModelParams", ResourceKind.UniformBuffer, ShaderStages.Fragment))); GraphicsPipelineDescription meshPipelineDescription = new GraphicsPipelineDescription( BlendStateDescription.SingleOverrideBlend, DepthStencilStateDescription.DepthOnlyGreaterEqual, RasterizerStateDescription.Default, PrimitiveTopology.TriangleList, meshShaderSet, new[] { viewProjLayout, worldLayout, modelParamsLayout }, Device.MainSwapchain.Framebuffer.OutputDescription); _modelPipeline = Factory.CreateGraphicsPipeline(meshPipelineDescription); _viewProjectionBuffer = Factory.CreateBufferFor <Matrix4x4>(BufferUsage.Dynamic | BufferUsage.UniformBuffer); _sceneInfoBuffer = Factory.CreateBufferFor <SceneInfo>(BufferUsage.Dynamic | BufferUsage.UniformBuffer); _viewProjectionSet = Factory.CreateResourceSet(new ResourceSetDescription(viewProjLayout, _viewProjectionBuffer, _sceneInfoBuffer)); _worldBuffer = Factory.CreateBuffer(new BufferDescription(128, BufferUsage.Dynamic | BufferUsage.UniformBuffer)); _worldSet = Factory.CreateResourceSet(new ResourceSetDescription(worldLayout, _worldBuffer)); _modelParamsBuffer = Factory.CreateBufferFor <Vector4>(BufferUsage.Dynamic | BufferUsage.UniformBuffer); _modelParamsSet = Factory.CreateResourceSet(new ResourceSetDescription(modelParamsLayout, _modelParamsBuffer)); _imguiRenderer = new ImGuiRenderer(Device, Device.MainSwapchain.Framebuffer.OutputDescription, Window.Width, Window.Height); _sw = Stopwatch.StartNew(); }
public void CreateDeviceResources(GraphicsDevice gd, OutputDescription outputDescription) { _gd = gd; ResourceFactory factory = gd.ResourceFactory; _vertexBuffer = factory.CreateBuffer(new BufferDescription(10000, BufferUsage.VertexBuffer | BufferUsage.Dynamic)); _vertexBuffer.Name = "ImGui.NET Vertex Buffer"; _indexBuffer = factory.CreateBuffer(new BufferDescription(2000, BufferUsage.IndexBuffer | BufferUsage.Dynamic)); _indexBuffer.Name = "ImGui.NET Index Buffer"; var fontMr = new FontManager(); fontMr.SetupFonts(); RecreateFontDeviceTexture(gd); Log.Debug("Fonts OK!"); _projMatrixBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); _projMatrixBuffer.Name = "ImGui.NET Projection Buffer"; byte[] vertexShaderBytes = LoadEmbeddedShaderCode(gd.ResourceFactory, "imgui-vertex", ShaderStages.Vertex); byte[] fragmentShaderBytes = LoadEmbeddedShaderCode(gd.ResourceFactory, "imgui-frag", ShaderStages.Fragment); _vertexShader = factory.CreateShader(new ShaderDescription(ShaderStages.Vertex, vertexShaderBytes, gd.BackendType == GraphicsBackend.Metal ? "VS" : "main")); _fragmentShader = factory.CreateShader(new ShaderDescription(ShaderStages.Fragment, fragmentShaderBytes, gd.BackendType == GraphicsBackend.Metal ? "FS" : "main")); VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[] { new VertexLayoutDescription( new VertexElementDescription("in_position", VertexElementSemantic.Position, VertexElementFormat.Float2), new VertexElementDescription("in_texCoord", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), new VertexElementDescription("in_color", VertexElementSemantic.Color, VertexElementFormat.Byte4_Norm)) }; _layout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("ProjectionMatrixBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("MainSampler", ResourceKind.Sampler, ShaderStages.Fragment))); _textureLayout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("MainTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment))); GraphicsPipelineDescription pd = new GraphicsPipelineDescription( BlendStateDescription.SingleAlphaBlend, new DepthStencilStateDescription(false, false, ComparisonKind.Always), new RasterizerStateDescription(FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, false, true), PrimitiveTopology.TriangleList, new ShaderSetDescription(vertexLayouts, new[] { _vertexShader, _fragmentShader }), new ResourceLayout[] { _layout, _textureLayout }, outputDescription, ResourceBindingModel.Default); _pipeline = factory.CreateGraphicsPipeline(ref pd); _mainResourceSet = factory.CreateResourceSet(new ResourceSetDescription(_layout, _projMatrixBuffer, gd.PointSampler)); _fontTextureResourceSet = factory.CreateResourceSet(new ResourceSetDescription(_textureLayout, _fontTextureView)); }
private IList <IDisposable> createTexturedQuadResources() { Mesh <VertexPositionTexture> quad = GeometryFactory.GenerateQuadPT_XY(); if (_indexBufferQuad == null) { throw new ApplicationException("_indexBufferQuad should have been created"); } _vertexBufferTexturedQuad = _factory.CreateBuffer(new BufferDescription(quad.Vertices.LengthUnsigned() * VertexPositionTexture.SizeInBytes, BufferUsage.VertexBuffer)); GraphicsDevice.UpdateBuffer(_vertexBufferTexturedQuad, 0, quad.Vertices); VertexLayoutDescription vertexLayout = new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3), new VertexElementDescription("UV", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2) ); _worldTransTexturedQuad = Matrix4x4.CreateWorld(new Vector3(5, 0, 0), -Vector3.UnitZ, Vector3.UnitY); _vertexShaderTexturedQuad = IO.LoadShader("QuadTexture", ShaderStages.Vertex, GraphicsDevice); _fragmentShaderTexturedQuad = IO.LoadShader("QuadTexture", ShaderStages.Fragment, GraphicsDevice); GraphicsPipelineDescription pipelineDescription = new GraphicsPipelineDescription() { BlendState = BlendStateDescription.SingleOverrideBlend, DepthStencilState = DepthStencilStateDescription.DepthOnlyLessEqual, RasterizerState = new RasterizerStateDescription( cullMode: FaceCullMode.Back, fillMode: PolygonFillMode.Solid, frontFace: FrontFace.Clockwise, depthClipEnabled: true, scissorTestEnabled: false ), PrimitiveTopology = PrimitiveTopology.TriangleStrip, ResourceLayouts = new ResourceLayout[] { _transformationPipelineResourceLayout, _offscreenLayout }, ShaderSet = new ShaderSetDescription( vertexLayouts: new VertexLayoutDescription[] { vertexLayout }, shaders: new Shader[] { _vertexShaderTexturedQuad, _fragmentShaderTexturedQuad } ), Outputs = GraphicsDevice.SwapchainFramebuffer.OutputDescription }; _pipelineTexturedQuad = _factory.CreateGraphicsPipeline(pipelineDescription); return(new List <IDisposable>() { _textureOffscreenResourceSet, _vertexBufferTexturedQuad, _vertexShaderTexturedQuad, _fragmentShaderTexturedQuad, _pipelineTexturedQuad }); }
public IBuiltPipeline Build() { if (shaderSetName == null) { throw new InvalidOperationException("No shader set was specified"); } if (depthTarget == null && colorTargets.Count == 0) { throw new InvalidOperationException("Neither a depth target nor a color target was specified"); } if (vertexElements.Last().Count() == 0) { throw new InvalidOperationException("Last vertex layout has no elements"); } if (resLayoutElements.Last().Count() == 0) { throw new InvalidOperationException("Last resource layout has no elements"); } lock (Collection) { var builtPipeline = Collection.pipelines.FirstOrDefault(Equals); if (builtPipeline != null) { return(builtPipeline); } var vertexLayouts = vertexElements .Select(set => new VertexLayoutDescription(set.ToArray())) .Select((set, i) => { set.InstanceStepRate = vertexLayoutInstanceStepRates[i]; return(set); }) .ToArray(); var resLayoutDescrs = resLayoutElements .Select(set => new ResourceLayoutDescription(set.ToArray())) .ToArray(); var resourceLayouts = resLayoutDescrs .Select(layoutDescr => Collection.Factory.CreateResourceLayout(layoutDescr)) .ToArray(); var shaders = Collection.LoadShaderSet(shaderSetName); var pipelineDescr = new GraphicsPipelineDescription( blendState, depthStencil, rasterizer, primitiveTopology, new ShaderSetDescription(vertexLayouts, shaders), resourceLayouts, OutputDescription); var pipeline = Collection.Factory.CreateGraphicsPipeline(pipelineDescr); builtPipeline = new BuiltPipeline(pipeline, pipelineDescr, resLayoutDescrs, shaderSetName); Collection.pipelines.Add(builtPipeline); return(builtPipeline); } }