public void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc, ResourceScope scope) { var factory = new DisposeCollectorResourceFactory(gd.ResourceFactory, _disposeCollector); BonesBuffer = factory.CreateBuffer(new BufferDescription((uint)(Marshal.SizeOf <Matrix4x4>() * MDLConstants.MaxBones), BufferUsage.UniformBuffer | BufferUsage.Dynamic)); RenderArgumentsBuffer = factory.CreateBuffer(new BufferDescription((uint)Marshal.SizeOf <StudioRenderArguments>(), BufferUsage.UniformBuffer | BufferUsage.Dynamic)); TextureDataBuffer = factory.CreateBuffer(new BufferDescription((uint)Marshal.SizeOf <StudioTextureData>(), BufferUsage.UniformBuffer | BufferUsage.Dynamic)); _sharedLayout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("WorldAndInverse", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("Bones", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("RenderArguments", ResourceKind.UniformBuffer, ShaderStages.Vertex | ShaderStages.Fragment), new ResourceLayoutElementDescription("TextureData", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("Sampler", ResourceKind.Sampler, ShaderStages.Fragment), new ResourceLayoutElementDescription("LightingInfo", ResourceKind.UniformBuffer, ShaderStages.Fragment))); TextureLayout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("Texture", ResourceKind.TextureReadOnly, ShaderStages.Fragment))); var vertexLayouts = new[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3), new VertexElementDescription("TextureCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), new VertexElementDescription("Normal", VertexElementSemantic.Normal, VertexElementFormat.Float3), new VertexElementDescription("VertexBoneIndex", VertexElementSemantic.TextureCoordinate, VertexElementFormat.UInt1), new VertexElementDescription("NormalBoneIndex", VertexElementSemantic.TextureCoordinate, VertexElementFormat.UInt1)) }; for (var cullMode = CullBack; cullMode < CullModeCount; ++cullMode) { for (var maskMode = MaskDisabled; maskMode < MaskModeCount; ++maskMode) { for (var additiveMode = AdditiveDisabled; additiveMode < AdditiveModeCount; ++additiveMode) { _pipelines[cullMode, maskMode, additiveMode] = CreatePipelines( gd, sc, vertexLayouts, _sharedLayout, TextureLayout, factory, cullMode == CullBack, maskMode == MaskEnabled, additiveMode == AdditiveEnabled); } } } SharedResourceSet = factory.CreateResourceSet(new ResourceSetDescription( _sharedLayout, sc.ProjectionMatrixBuffer, sc.ViewMatrixBuffer, sc.WorldAndInverseBuffer, BonesBuffer, RenderArgumentsBuffer, TextureDataBuffer, sc.MainSampler, sc.LightingInfoBuffer )); }
public void UpdateResourceLayout() { if (Layout == null) { Layout = factory.CreateResourceLayout(layoutDescription); } }
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, SceneContext sc, ResourceScope scope) { if ((scope & ResourceScope.Global) == 0) { return; } 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 shaderName = gd.IsUvOriginTopLeft ? "FinalPass_inverted" : "FinalPass"; (var vs, var fs) = sc.GlobalResourceCache.GetShaders(gd, gd.ResourceFactory, shaderName); GraphicsPipelineDescription pd = new GraphicsPipelineDescription( new BlendStateDescription( RgbaFloat.Black, 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)) }, new[] { vs, fs, }), new ResourceLayout[] { resourceLayout }, gd.SwapchainFramebuffer.OutputDescription); _pipeline = factory.CreateGraphicsPipeline(ref pd); float[] verts = Util.GetFullScreenQuadVerts(gd); _vb = factory.CreateBuffer(new BufferDescription(verts.SizeInBytes() * sizeof(float), BufferUsage.VertexBuffer)); cl.UpdateBuffer(_vb, 0, verts); _ib = factory.CreateBuffer( new BufferDescription((uint)s_quadIndices.Length * sizeof(ushort), BufferUsage.IndexBuffer)); cl.UpdateBuffer(_ib, 0, s_quadIndices); }
public void CreateDeviceObjects(IRendererContext context) { var c = (VeldridRendererContext)context; var cl = c.CommandList; var gd = c.GraphicsDevice; var factory = new DisposeCollectorResourceFactory(gd.ResourceFactory); _disposeCollector = factory.DisposeCollector; var layout = factory.CreateResourceLayout(new ResourceLayoutDescription( ResourceLayoutHelper.Texture("SourceTexture"), ResourceLayoutHelper.Sampler("SourceSampler"))); var shaderCache = Resolve <IShaderCache>(); _shaders = shaderCache.GetShaderPair(gd.ResourceFactory, VertexShaderName, FragmentShaderName, shaderCache.GetGlsl(VertexShaderName), shaderCache.GetGlsl(FragmentShaderName)); var rasterizerState = new RasterizerStateDescription( FaceCullMode.Back, PolygonFillMode.Solid, FrontFace.Clockwise, true, false); var pd = new GraphicsPipelineDescription( new BlendStateDescription(RgbaFloat.Black, BlendAttachmentDescription.OverrideBlend), DepthStencilStateDescription.Disabled, rasterizerState, PrimitiveTopology.TriangleList, new ShaderSetDescription(new[] { VertexLayoutHelper.Vertex2DTextured }, _shaders, ShaderHelper.GetSpecializations(gd)), new[] { layout }, gd.SwapchainFramebuffer.OutputDescription); _pipeline = factory.CreateGraphicsPipeline(ref pd); _pipeline.Name = "P_FullScreenQuad"; 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(QuadIndices.SizeInBytes(), BufferUsage.IndexBuffer)); cl.UpdateBuffer(_ib, 0, QuadIndices); }
public override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc) { 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))); (Shader vs, Shader fs) = StaticResourceCache.GetShaders(gd, gd.ResourceFactory, "ScreenDuplicator"); 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)) }, new[] { vs, fs, }, ShaderHelper.GetSpecializations(gd)), new ResourceLayout[] { resourceLayout }, sc.DuplicatorFramebuffer.OutputDescription); _pipeline = factory.CreateGraphicsPipeline(ref pd); float[] verts = Util.GetFullScreenQuadVerts(gd); _vb = factory.CreateBuffer(new BufferDescription(verts.SizeInBytes() * sizeof(float), BufferUsage.VertexBuffer)); cl.UpdateBuffer(_vb, 0, verts); _ib = factory.CreateBuffer( new BufferDescription((uint)s_quadIndices.Length * sizeof(ushort), BufferUsage.IndexBuffer)); cl.UpdateBuffer(_ib, 0, s_quadIndices); }
public override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc) { 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))); GraphicsPipelineDescription pd = new GraphicsPipelineDescription( new BlendStateDescription( RgbaFloat.Black, BlendAttachmentDescription.OverrideBlend, BlendAttachmentDescription.OverrideBlend), DepthStencilStateDescription.DepthOnlyLessEqual, RasterizerStateDescription.Default, PrimitiveTopology.TriangleList, new ShaderSetDescription( new[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float2), new VertexElementDescription("TexCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2)) }, new[] { ShaderHelper.LoadShader(gd, factory, "ScreenDuplicator", ShaderStages.Vertex, "VS"), ShaderHelper.LoadShader(gd, factory, "ScreenDuplicator", ShaderStages.Fragment, "FS"), }), new ResourceLayout[] { resourceLayout }, sc.DuplicatorFramebuffer.OutputDescription); _pipeline = factory.CreateGraphicsPipeline(ref pd); _vb = factory.CreateBuffer(new BufferDescription((uint)s_quadVerts.Length * sizeof(float), BufferUsage.VertexBuffer)); cl.UpdateBuffer(_vb, 0, s_quadVerts); _ib = factory.CreateBuffer( new BufferDescription((uint)s_quadIndices.Length * sizeof(ushort), BufferUsage.IndexBuffer)); cl.UpdateBuffer(_ib, 0, s_quadIndices); }
public void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, GraphicsSystem sc) { 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))); (Shader vs, Shader fs) = StaticResourceCache.GetShaders(gd, gd.ResourceFactory, "FullScreenQuad"); GraphicsPipelineDescription pd = new GraphicsPipelineDescription( new BlendStateDescription( RgbaFloat.Black, BlendAttachmentDescription.OverrideBlend), DepthStencilStateDescription.Disabled, new RasterizerStateDescription(FaceCullMode.Back, PolygonFillMode.Solid, FrontFace.Clockwise, true, false), PrimitiveTopology.TriangleList, new ShaderSetDescription( new[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), new VertexElementDescription("TexCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2)) }, new[] { vs, fs }), new ResourceLayout[] { resourceLayout }, gd.SwapchainFramebuffer.OutputDescription); _pipeline = factory.CreateGraphicsPipeline(ref pd); float[] verts = Util.GetFullScreenQuadVerts(gd.BackendType); _vb = factory.CreateBuffer(new BufferDescription(verts.SizeInBytes() * sizeof(float), BufferUsage.VertexBuffer)); cl.UpdateBuffer(_vb, 0, verts); _ib = factory.CreateBuffer( new BufferDescription(s_quadIndices.SizeInBytes(), BufferUsage.IndexBuffer)); cl.UpdateBuffer(_ib, 0, s_quadIndices); }
public void CreateDeviceObjects(GraphicsDevice gd, CommandList cl) { if (gd == null) { throw new ArgumentNullException(nameof(gd)); } if (cl == null) { throw new ArgumentNullException(nameof(cl)); } var factory = new DisposeCollectorResourceFactory(gd.ResourceFactory, _disposer); DeviceBuffer MakeBuffer(uint size, string name) { var buffer = factory.CreateBuffer( new BufferDescription(size, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); buffer.Name = name; return(buffer); } ProjectionMatrixBuffer = MakeBuffer(64, "M_Projection"); ModelViewMatrixBuffer = MakeBuffer(64, "M_View"); IdentityMatrixBuffer = MakeBuffer(64, "M_Id"); DepthLimitsBuffer = MakeBuffer((uint)Unsafe.SizeOf <DepthCascadeLimits>(), "B_DepthLimits"); CameraInfoBuffer = MakeBuffer((uint)Unsafe.SizeOf <CameraInfo>(), "B_CameraInfo"); cl.UpdateBuffer(IdentityMatrixBuffer, 0, Matrix4x4.Identity); var commonLayoutDescription = new ResourceLayoutDescription( ResourceLayoutHelper.Uniform("_Shared"), // CameraInfo / common data buffer ResourceLayoutHelper.UniformV("_Projection"), // Perspective Matrix ResourceLayoutHelper.UniformV("_View"), // View Matrix ResourceLayoutHelper.Texture("uPalette")); // PaletteTexture CommonResourceLayout = factory.CreateResourceLayout(commonLayoutDescription); CommonResourceLayout.Name = "RL_Common"; }
public void Start() { WindowCreateInfo wci = new WindowCreateInfo { X = 100, Y = 100, WindowWidth = 1280, WindowHeight = 720, WindowTitle = _runner.Name, }; _window = VeldridStartup.CreateWindow(ref wci); _window.Resized += () => { _windowResized = true; }; _ft = _runner.GetSubSystem <FrameTimer>(); _loader = _runner.GetSubSystem <ResourceLoader>(); GraphicsDeviceOptions options = new GraphicsDeviceOptions( debug: false, swapchainDepthFormat: PixelFormat.R16_UNorm, syncToVerticalBlank: true, resourceBindingModel: ResourceBindingModel.Improved); #if DEBUG options.Debug = true; #endif _gd = VeldridStartup.CreateGraphicsDevice(_window, options); _factory = new DisposeCollectorResourceFactory(_gd.ResourceFactory); ResourceLayout projViewLayout = _factory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex))); ResourceLayout worldTextureLayout = _factory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription("World", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("SurfaceTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("SurfaceSampler", ResourceKind.Sampler, ShaderStages.Fragment))); ShaderSetDescription shaderSet = new ShaderSetDescription( new[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3), new VertexElementDescription("TexCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2)) }, new[] { _loader.LoadShader(_factory, "Cube", ShaderStages.Vertex, "VS"), _loader.LoadShader(_factory, "Cube", ShaderStages.Fragment, "FS") }); _pipeline = _factory.CreateGraphicsPipeline(new GraphicsPipelineDescription( BlendStateDescription.SingleOverrideBlend, DepthStencilStateDescription.DepthOnlyLessEqual, RasterizerStateDescription.Default, PrimitiveTopology.TriangleList, shaderSet, new[] { projViewLayout, worldTextureLayout }, MainSwapchain.Framebuffer.OutputDescription)); _cl = _factory.CreateCommandList(); LastInput = _window.PumpEvents(); }
private void CreateResources() { _factory = new DisposeCollectorResourceFactory(_gd.ResourceFactory); _cl = _factory.CreateCommandList(); _cl.Begin(); _projectionBuffer = _factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer)); _viewBuffer = _factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer)); _modelBuffer = _factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer)); _projectionBufferForShadowShader = _factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer)); _viewBufferShadow = _factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer)); _modelBufferShadow = _factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer)); // TODO: no idea why this buffer requires 48 bytes instead of 32 bytes, padding? //_directionLightBuffer = _factory.CreateBuffer(new BufferDescription(48, BufferUsage.UniformBuffer)); // addition of shadowmatrix, now requires (48+64=) 112 bytes _directionLightBuffer = _factory.CreateBuffer(new BufferDescription(112, BufferUsage.UniformBuffer)); _cl.End(); _gd.SubmitCommands(_cl); _gd.WaitForIdle(); ShaderSetDescription shaderSet = new ShaderSetDescription( new[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3), new VertexElementDescription("Color", VertexElementSemantic.Color, VertexElementFormat.Float3), new VertexElementDescription("Normal", VertexElementSemantic.Normal, VertexElementFormat.Float3)) }, new[] { LoadShader(_factory, "ColorShader", ShaderStages.Vertex, "VS"), LoadShader(_factory, "ColorShader", ShaderStages.Fragment, "FS") }); ShaderSetDescription shaderSetShadow = new ShaderSetDescription( new[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3), new VertexElementDescription("Color", VertexElementSemantic.Color, VertexElementFormat.Float3), new VertexElementDescription("Normal", VertexElementSemantic.Normal, VertexElementFormat.Float3)) }, new[] { LoadShader(_factory, "ShadowShader", ShaderStages.Vertex, "VS"), LoadShader(_factory, "ShadowShader", ShaderStages.Fragment, "FS") }); ResourceLayout projectionViewMatricesLightLayout = _factory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("DirectionalLight", ResourceKind.UniformBuffer, ShaderStages.Vertex | ShaderStages.Fragment), new ResourceLayoutElementDescription("ShadowMap", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("ShadowMapSampler", ResourceKind.Sampler, ShaderStages.Fragment) )); ResourceLayout perObjectLayout = _factory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription("Model", ResourceKind.UniformBuffer, ShaderStages.Vertex) )); ResourceLayout projectionViewMatricesLayoutShadow = _factory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex) )); ResourceLayout modelLayoutShadow = _factory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription("Model", ResourceKind.UniformBuffer, ShaderStages.Vertex) )); _pipeline = _factory.CreateGraphicsPipeline(new GraphicsPipelineDescription( BlendStateDescription.SingleOverrideBlend, DepthStencilStateDescription.DepthOnlyLessEqual, RasterizerStateDescription.Default, PrimitiveTopology.TriangleList, shaderSet, new[] { projectionViewMatricesLightLayout, perObjectLayout }, _gd.SwapchainFramebuffer.OutputDescription)); TextureDescription desc = TextureDescription.Texture2D(2048, 2048, 1, 1, PixelFormat.D32_Float_S8_UInt, TextureUsage.DepthStencil | TextureUsage.Sampled); _shadowMap = _factory.CreateTexture(desc); _shadowMap.Name = "Shadow Map"; _shadowMapView = _factory.CreateTextureView(_shadowMap); _shadowMapFramebuffer = _factory.CreateFramebuffer(new FramebufferDescription( new FramebufferAttachmentDescription(_shadowMap, 0), Array.Empty <FramebufferAttachmentDescription>())); _pipelineShadow = _factory.CreateGraphicsPipeline(new GraphicsPipelineDescription( BlendStateDescription.Empty, DepthStencilStateDescription.DepthOnlyLessEqual, RasterizerStateDescription.Default, PrimitiveTopology.TriangleList, shaderSetShadow, new[] { projectionViewMatricesLayoutShadow, modelLayoutShadow }, _shadowMapFramebuffer.OutputDescription)); _projectionViewMatricesLightSet = _factory.CreateResourceSet(new ResourceSetDescription( projectionViewMatricesLightLayout, _projectionBuffer, _viewBuffer, _directionLightBuffer, _shadowMapView, _gd.PointSampler)); _perObjectSet = _factory.CreateResourceSet(new ResourceSetDescription( perObjectLayout, _modelBuffer)); _projectionViewMatricesSetShadow = _factory.CreateResourceSet(new ResourceSetDescription( projectionViewMatricesLayoutShadow, _projectionBufferForShadowShader, _viewBufferShadow)); _modelMatrixSetShadow = _factory.CreateResourceSet(new ResourceSetDescription( modelLayoutShadow, _modelBufferShadow)); }
static int Main(string[] args) { if (args.Length != 2) { Console.WriteLine($"ImageTint <image-path> <out>: Tints the image at <image-path> and saves it to <out>."); return(1); } string inPath = args[0]; string outPath = args[1]; // This demo uses WindowState.Hidden to avoid popping up an unnecessary window to the user. VeldridStartup.CreateWindowAndGraphicsDevice( new WindowCreateInfo { WindowInitialState = WindowState.Hidden, }, new GraphicsDeviceOptions() { ResourceBindingModel = ResourceBindingModel.Improved }, out Sdl2Window window, out GraphicsDevice gd); DisposeCollectorResourceFactory factory = new DisposeCollectorResourceFactory(gd.ResourceFactory); ImageSharpTexture inputImage = new ImageSharpTexture(inPath, false); Texture inputTexture = inputImage.CreateDeviceTexture(gd, factory); TextureView view = factory.CreateTextureView(inputTexture); Texture output = factory.CreateTexture(TextureDescription.Texture2D( inputImage.Width, inputImage.Height, 1, 1, PixelFormat.R8_G8_B8_A8_UNorm, TextureUsage.RenderTarget)); Framebuffer framebuffer = factory.CreateFramebuffer(new FramebufferDescription(null, output)); DeviceBuffer vertexBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.VertexBuffer)); Vector4[] quadVerts = { new Vector4(-1, 1, 0, 0), new Vector4(1, 1, 1, 0), new Vector4(-1, -1, 0, 1), new Vector4(1, -1, 1, 1), }; gd.UpdateBuffer(vertexBuffer, 0, quadVerts); ShaderSetDescription shaderSet = new ShaderSetDescription( new[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), new VertexElementDescription("TextureCoordinates", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2)) }, factory.CreateFromSpirv( new ShaderDescription(ShaderStages.Vertex, ReadEmbeddedAssetBytes("TintShader-vertex.glsl"), "main"), new ShaderDescription(ShaderStages.Fragment, ReadEmbeddedAssetBytes("TintShader-fragment.glsl"), "main"))); ResourceLayout layout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("Input", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("Sampler", ResourceKind.Sampler, ShaderStages.Fragment), new ResourceLayoutElementDescription("Tint", ResourceKind.UniformBuffer, ShaderStages.Fragment))); Pipeline pipeline = factory.CreateGraphicsPipeline(new GraphicsPipelineDescription( BlendStateDescription.SingleOverrideBlend, DepthStencilStateDescription.Disabled, RasterizerStateDescription.Default, PrimitiveTopology.TriangleStrip, shaderSet, layout, framebuffer.OutputDescription)); DeviceBuffer tintInfoBuffer = factory.CreateBuffer(new BufferDescription(16, BufferUsage.UniformBuffer)); gd.UpdateBuffer( tintInfoBuffer, 0, new TintInfo( new Vector3(1f, 0.2f, 0.1f), // Change this to modify the tint color. 0.25f)); ResourceSet resourceSet = factory.CreateResourceSet( new ResourceSetDescription(layout, view, gd.PointSampler, tintInfoBuffer)); // RenderTarget textures are not CPU-visible, so to get our tinted image back, we need to first copy it into // a "staging Texture", which is a Texture that is CPU-visible (it can be Mapped). Texture stage = factory.CreateTexture(TextureDescription.Texture2D( inputImage.Width, inputImage.Height, 1, 1, PixelFormat.R8_G8_B8_A8_UNorm, TextureUsage.Staging)); CommandList cl = factory.CreateCommandList(); cl.Begin(); cl.SetFramebuffer(framebuffer); cl.SetFullViewports(); cl.SetVertexBuffer(0, vertexBuffer); cl.SetPipeline(pipeline); cl.SetGraphicsResourceSet(0, resourceSet); cl.Draw(4, 1, 0, 0); cl.CopyTexture( output, 0, 0, 0, 0, 0, stage, 0, 0, 0, 0, 0, stage.Width, stage.Height, 1, 1); cl.End(); gd.SubmitCommands(cl); gd.WaitForIdle(); // When a texture is mapped into a CPU-visible region, it is often not laid out linearly. // Instead, it is laid out as a series of rows, which are all spaced out evenly by a "row pitch". // This spacing is provided in MappedResource.RowPitch. // It is also possible to obtain a "structured view" of a mapped data region, which is what is done below. // With a structured view, you can read individual elements from the region. // The code below simply iterates over the two-dimensional region and places each texel into a linear buffer. // ImageSharp requires the pixel data be contained in a linear buffer. MappedResourceView <Rgba32> map = gd.Map <Rgba32>(stage, MapMode.Read); // Rgba32 is synonymous with PixelFormat.R8_G8_B8_A8_UNorm. Rgba32[] pixelData = new Rgba32[stage.Width * stage.Height]; for (int y = 0; y < stage.Height; y++) { for (int x = 0; x < stage.Width; x++) { int index = (int)(y * stage.Width + x); pixelData[index] = map[x, y]; } } gd.Unmap(stage); // Resources should be Unmapped when the region is no longer used. Image <Rgba32> outputImage = Image.LoadPixelData(pixelData, (int)stage.Width, (int)stage.Height); outputImage.Save(outPath); factory.DisposeCollector.DisposeAll(); gd.Dispose(); window.Close(); return(0); }
public ResourceLayout CreateResourceLayout(ResourceLayoutDescription resourceLayoutDescription) => _factory.CreateResourceLayout(resourceLayoutDescription);
public void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc, ResourceScope scope) { if ((scope & ResourceScope.Map) == 0) { return; } var disposeFactory = new DisposeCollectorResourceFactory(gd.ResourceFactory, _disposeCollector); _sharedLayout = disposeFactory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("WorldAndInverse", ResourceKind.UniformBuffer, ShaderStages.Vertex | ShaderStages.Fragment), new ResourceLayoutElementDescription("LightingInfo", ResourceKind.UniformBuffer, ShaderStages.Fragment), new ResourceLayoutElementDescription("LightStyles", ResourceKind.UniformBuffer, ShaderStages.Fragment), new ResourceLayoutElementDescription("RenderColor", ResourceKind.UniformBuffer, ShaderStages.Fragment))); TextureLayout = disposeFactory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("Texture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("Sampler", ResourceKind.Sampler, ShaderStages.Fragment))); LightmapLayout = disposeFactory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("Lightmaps", ResourceKind.TextureReadOnly, ShaderStages.Fragment))); var vertexLayouts = new VertexLayoutDescription[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3), new VertexElementDescription("TextureCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), new VertexElementDescription("LightmapCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), //Used for multiple light styles; this is the offset to apply to lightmap X coordinates new VertexElementDescription("LightmapXOffset", VertexElementSemantic.Position, VertexElementFormat.Float1), new VertexElementDescription("StyleIndices", VertexElementSemantic.Position, VertexElementFormat.Int4)) }; (var vs, var fs) = sc.MapResourceCache.GetShaders(gd, gd.ResourceFactory, "LightMappedGeneric"); //Create render mode pipelines var rasterizerState = new RasterizerStateDescription(FaceCullMode.Back, PolygonFillMode.Solid, FrontFace.Clockwise, true, true); const PrimitiveTopology primitiveTopology = PrimitiveTopology.TriangleList; var shaderSets = new ShaderSetDescription(vertexLayouts, new[] { vs, fs }); var resourceLayouts = new ResourceLayout[] { _sharedLayout, TextureLayout, LightmapLayout }; var outputDescription = sc.MainSceneFramebuffer.OutputDescription; var pipelines = new Pipeline[(int)RenderMode.Last + 1]; var pd = new GraphicsPipelineDescription( BlendStateDescription.SingleDisabled, gd.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyGreaterEqual : DepthStencilStateDescription.DepthOnlyLessEqual, rasterizerState, primitiveTopology, shaderSets, resourceLayouts, outputDescription); pipelines[(int)RenderMode.Normal] = disposeFactory.CreateGraphicsPipeline(ref pd); pipelines[(int)RenderMode.TransColor] = disposeFactory.CreateGraphicsPipeline(ref pd); pd = new GraphicsPipelineDescription( BlendStateDescription.SingleAlphaBlend, gd.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyGreaterEqualRead : DepthStencilStateDescription.DepthOnlyLessEqualRead, rasterizerState, primitiveTopology, shaderSets, resourceLayouts, outputDescription); pipelines[(int)RenderMode.TransTexture] = disposeFactory.CreateGraphicsPipeline(ref pd); //Glow uses the same pipeline as texture pipelines[(int)RenderMode.Glow] = pipelines[(int)RenderMode.TransTexture]; pd = new GraphicsPipelineDescription( BlendStateDescription.SingleDisabled, gd.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyGreaterEqual : DepthStencilStateDescription.DepthOnlyLessEqual, rasterizerState, primitiveTopology, shaderSets, resourceLayouts, outputDescription); pipelines[(int)RenderMode.TransAlpha] = disposeFactory.CreateGraphicsPipeline(ref pd); pd = new GraphicsPipelineDescription( BlendStates.SingleAdditiveOneOneBlend, gd.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyGreaterEqualRead : DepthStencilStateDescription.DepthOnlyLessEqualRead, rasterizerState, primitiveTopology, shaderSets, resourceLayouts, outputDescription); pipelines[(int)RenderMode.TransAdd] = disposeFactory.CreateGraphicsPipeline(ref pd); Pipelines = new RenderModePipelines(pipelines); RenderArgumentsBuffer = disposeFactory.CreateBuffer(new BufferDescription((uint)Marshal.SizeOf <BSPRenderArguments>(), BufferUsage.UniformBuffer | BufferUsage.Dynamic)); _sharedResourceSet = disposeFactory.CreateResourceSet(new ResourceSetDescription( _sharedLayout, sc.ProjectionMatrixBuffer, sc.ViewMatrixBuffer, sc.WorldAndInverseBuffer, sc.LightingInfoBuffer, sc.LightStylesBuffer, RenderArgumentsBuffer)); }
public void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc, ResourceScope scope) { if ((scope & ResourceScope.Map) == 0) { return; } var disposeFactory = new DisposeCollectorResourceFactory(gd.ResourceFactory, _disposeCollector); //Create the layout and pipelines used by sprites var vertexLayouts = new VertexLayoutDescription[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3), new VertexElementDescription("TextureCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2) ) }; (var vs, var fs) = sc.MapResourceCache.GetShaders(gd, gd.ResourceFactory, "SpriteGeneric"); Layout = disposeFactory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("WorldAndInverse", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("Texture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("Sampler", ResourceKind.Sampler, ShaderStages.Fragment), new ResourceLayoutElementDescription("LightingInfo", ResourceKind.UniformBuffer, ShaderStages.Fragment), new ResourceLayoutElementDescription("RenderColor", ResourceKind.UniformBuffer, ShaderStages.Fragment) )); //Vanilla GoldSource has a cvar gl_spriteblend that disables blending and makes normal sprites render differently //This is purely a performance setting and lowers the quality of sprites //We won't do that here var pd = new GraphicsPipelineDescription( BlendStateDescription.SingleAlphaBlend, gd.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyGreaterEqual : DepthStencilStateDescription.DepthOnlyLessEqual, new RasterizerStateDescription(FaceCullMode.Back, PolygonFillMode.Solid, FrontFace.Clockwise, true, true), PrimitiveTopology.TriangleList, new ShaderSetDescription(vertexLayouts, new[] { vs, fs }), new ResourceLayout[] { Layout }, sc.MainSceneFramebuffer.OutputDescription); var pipelines = new Pipeline[(int)RenderMode.Last + 1]; pipelines[(int)RenderMode.Normal] = disposeFactory.CreateGraphicsPipeline(ref pd); //Same pipeline as normal when sprite blending is enabled in vanilla pipelines[(int)RenderMode.TransTexture] = pipelines[(int)RenderMode.Normal]; //Identical to Texture, vanilla GoldSource uses an invalid texture env mode that happens to use GL_MODULATE so for consistency this is required pipelines[(int)RenderMode.TransColor] = pipelines[(int)RenderMode.TransTexture]; pd = new GraphicsPipelineDescription( BlendStates.SingleAdditiveOneOneBlend, DepthStencilStateDescription.Disabled, new RasterizerStateDescription(FaceCullMode.Back, PolygonFillMode.Solid, FrontFace.Clockwise, true, true), PrimitiveTopology.TriangleList, new ShaderSetDescription(vertexLayouts, new[] { vs, fs }), new ResourceLayout[] { Layout }, sc.MainSceneFramebuffer.OutputDescription); pipelines[(int)RenderMode.Glow] = disposeFactory.CreateGraphicsPipeline(ref pd); //Identical to Texture, but does not write to the depth buffer pd = new GraphicsPipelineDescription( BlendStateDescription.SingleAlphaBlend, gd.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyGreaterEqualRead : DepthStencilStateDescription.DepthOnlyLessEqualRead, new RasterizerStateDescription(FaceCullMode.Back, PolygonFillMode.Solid, FrontFace.Clockwise, true, true), PrimitiveTopology.TriangleList, new ShaderSetDescription(vertexLayouts, new[] { vs, fs }), new ResourceLayout[] { Layout }, sc.MainSceneFramebuffer.OutputDescription); pipelines[(int)RenderMode.TransAlpha] = disposeFactory.CreateGraphicsPipeline(ref pd); //Identical to Glow, but still uses depth testing pd = new GraphicsPipelineDescription( BlendStates.SingleAdditiveOneOneBlend, gd.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyGreaterEqualRead : DepthStencilStateDescription.DepthOnlyLessEqualRead, new RasterizerStateDescription(FaceCullMode.Back, PolygonFillMode.Solid, FrontFace.Clockwise, true, true), PrimitiveTopology.TriangleList, new ShaderSetDescription(vertexLayouts, new[] { vs, fs }), new ResourceLayout[] { Layout }, sc.MainSceneFramebuffer.OutputDescription); pipelines[(int)RenderMode.TransAdd] = disposeFactory.CreateGraphicsPipeline(ref pd); Pipelines = new RenderModePipelines(pipelines); }
public void CreateDeviceResources(GraphicsDevice gd) { var factory = new DisposeCollectorResourceFactory(gd.ResourceFactory, disposeCollector); vertexBuffer = model.CreateVertexBuffer(gd); indexBuffer = model.CreateIndexBuffer(gd, out indexCount); transformationBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer)); projectionBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer)); viewBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer)); texture = textureData.CreateDeviceTexture(gd, factory); lightDirectionBuffer = factory.CreateBuffer(new BufferDescription(16, BufferUsage.UniformBuffer)); lightColorBuffer = factory.CreateBuffer(new BufferDescription(16, BufferUsage.UniformBuffer)); shineDamperBuffer = factory.CreateBuffer(new BufferDescription(16, BufferUsage.UniformBuffer)); reflectivityBuffer = factory.CreateBuffer(new BufferDescription(16, BufferUsage.UniformBuffer)); var vertexLayoutDesc = new VertexLayoutDescription[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementFormat.Float3, VertexElementSemantic.TextureCoordinate), new VertexElementDescription("TexCoord", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate), new VertexElementDescription("Normal", VertexElementFormat.Float3, VertexElementSemantic.TextureCoordinate) ) }; var transformationResourceLayout = factory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("TransformationBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("ProjectionBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("ViewBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex) } )); var textureResourceLayout = factory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("Texture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("Sampler", ResourceKind.Sampler, ShaderStages.Fragment), new ResourceLayoutElementDescription("LightDirectionBuffer", ResourceKind.UniformBuffer, ShaderStages.Fragment), new ResourceLayoutElementDescription("LightColorBuffer", ResourceKind.UniformBuffer, ShaderStages.Fragment), new ResourceLayoutElementDescription("ShineDamperBuffer", ResourceKind.UniformBuffer, ShaderStages.Fragment), new ResourceLayoutElementDescription("ReflectivityBuffer", ResourceKind.UniformBuffer, ShaderStages.Fragment) } )); transformationResourceSet = factory.CreateResourceSet( new ResourceSetDescription( transformationResourceLayout, transformationBuffer, projectionBuffer, viewBuffer)); textureResourceSet = factory.CreateResourceSet( new ResourceSetDescription( textureResourceLayout, texture, gd.Aniso4xSampler, lightDirectionBuffer, lightColorBuffer, shineDamperBuffer, reflectivityBuffer)); var pipelineDesc = new GraphicsPipelineDescription { BlendState = BlendStateDescription.SingleOverrideBlend, DepthStencilState = new DepthStencilStateDescription( depthTestEnabled: true, depthWriteEnabled: true, comparisonKind: ComparisonKind.LessEqual ), RasterizerState = new RasterizerStateDescription( cullMode: FaceCullMode.Back, fillMode: PolygonFillMode.Solid, frontFace: FrontFace.CounterClockwise, depthClipEnabled: true, scissorTestEnabled: false), PrimitiveTopology = PrimitiveTopology.TriangleList, ResourceLayouts = new ResourceLayout[] { transformationResourceLayout, textureResourceLayout }, ShaderSet = new ShaderSetDescription( vertexLayouts: vertexLayoutDesc, shaders: new[] { vertexShader, fragmentShader }), Outputs = gd.SwapchainFramebuffer.OutputDescription }; pipeline = factory.CreateGraphicsPipeline(pipelineDesc); }