Esempio n. 1
0
 // TODO: private protected
 /// <summary>
 /// </summary>
 /// <param name="description"></param>
 /// <returns></returns>
 protected abstract DeviceBuffer CreateBufferCore(ref BufferDescription description);
        unsafe public CommandList BuildCommandList(GraphicsDevice graphicsDevice, Framebuffer framebuffer)
        {
            ResourceFactory factory       = graphicsDevice.ResourceFactory;
            Shader          _vsShader     = null;
            Shader          _psShader     = null;
            DeviceBuffer    _vertexBuffer = null;
            Pipeline        _pipeline     = null;

            Veldrid.CommandList _commandList = null;
            try
            {
                #region --- try

                #region --- shaders
                {
                    string _vsname = null;
                    string _psname = null;

                    switch (graphicsDevice.BackendType)
                    {
                    case GraphicsBackend.Direct3D11:
                        _vsname = "Test_Colored2DVertices.vs";
                        _psname = "Test_Colored2DVertices.ps";
                        break;

                    case GraphicsBackend.Metal:
                        _vsname = "Test_Colored2DVertices_vs.msl";
                        _psname = "Test_Colored2DVertices_ps.msl";
                        break;

                    case GraphicsBackend.Vulkan:
                        _vsname = "Test_Colored2DVertices_vs.spv";
                        _psname = "Test_Colored2DVertices_ps.spv";
                        break;

                    case GraphicsBackend.OpenGL:
                        _vsname = "Test_Colored2DVertices_vs.glsl";
                        _psname = "Test_Colored2DVertices_ps.glsl";
                        break;

                    case GraphicsBackend.OpenGLES:
                        _vsname = "Test_Colored2DVertices_vs_es.glsl";
                        _psname = "Test_Colored2DVertices_ps_es.glsl";
                        break;
                    }

                    string _vs = loadResourceString(Assembly.GetExecutingAssembly(), _vsname);
                    if (string.IsNullOrEmpty(_vs))
                    {
                        return(null);
                    }
                    string _ps = loadResourceString(Assembly.GetExecutingAssembly(), _psname);
                    if (string.IsNullOrEmpty(_ps))
                    {
                        return(null);
                    }

                    ShaderDescription vertexShaderDesc = new ShaderDescription(
                        ShaderStages.Vertex,
                        Encoding.UTF8.GetBytes(_vs),
                        "_VertexShader");
                    ShaderDescription fragmentShaderDesc = new ShaderDescription(
                        ShaderStages.Fragment,
                        Encoding.UTF8.GetBytes(_ps),
                        "_PixelShader");

                    _vsShader = factory.CreateShader(vertexShaderDesc);
                    _psShader = factory.CreateShader(fragmentShaderDesc);
                }
                #endregion

                VertexLayoutDescription _vertexLayout = new VertexLayoutDescription();
                int _vertexCount;

                #region --- _vertexBuffer + _vertexLayout
                {
                    int    _count = 10;
                    byte[] _data  = Build_2DColoredQuads_16SNorm_8UNorm(_count, _count, out _vertexCount);
                    //
                    Veldrid.BufferDescription vbDescription = new Veldrid.BufferDescription(
                        (uint)_data.Length,
                        BufferUsage.VertexBuffer);
                    _vertexBuffer = factory.CreateBuffer(vbDescription);

                    fixed(byte *_pdata = _data)
                    {
                        graphicsDevice.UpdateBuffer(_vertexBuffer, bufferOffsetInBytes: 0, (IntPtr)_pdata, (uint)_data.Length);
                    }

                    _vertexLayout = new VertexLayoutDescription(
                        new VertexElementDescription("Position",
                                                     VertexElementSemantic.Position,
                                                     VertexElementFormat.Short2_Norm,
                                                     offset: 0),
                        new VertexElementDescription("Color",
                                                     VertexElementSemantic.Color,
                                                     VertexElementFormat.Byte4_Norm,
                                                     offset: 2 * sizeof(short)));
                }
                #endregion

                #region --- pipeline
                {
                    GraphicsPipelineDescription _pipelineDescription = new GraphicsPipelineDescription();

                    _pipelineDescription.BlendState = Veldrid.BlendStateDescription.SingleOverrideBlend;

                    _pipelineDescription.DepthStencilState = new Veldrid.DepthStencilStateDescription(
                        depthTestEnabled: false,
                        depthWriteEnabled: false,
                        comparisonKind: ComparisonKind.LessEqual);

                    _pipelineDescription.RasterizerState = new Veldrid.RasterizerStateDescription(
                        cullMode: FaceCullMode.None,
                        fillMode: PolygonFillMode.Solid,
                        frontFace: FrontFace.Clockwise,
                        depthClipEnabled: true, // Android ?
                        scissorTestEnabled: false);

                    _pipelineDescription.PrimitiveTopology = Veldrid.PrimitiveTopology.TriangleList;
                    _pipelineDescription.ResourceLayouts   = System.Array.Empty <ResourceLayout>();

                    _pipelineDescription.ShaderSet = new ShaderSetDescription(
                        vertexLayouts: new VertexLayoutDescription[] { _vertexLayout },
                        shaders: new Shader[] { _vsShader, _psShader });

                    _pipelineDescription.Outputs = framebuffer.OutputDescription; // _offscreenFB.OutputDescription;

                    _pipeline = factory.CreateGraphicsPipeline(_pipelineDescription);
                }
                #endregion

                #region --- commandList
                {
                    _commandList = factory.CreateCommandList();

                    // Begin() must be called before commands can be issued.
                    _commandList.Begin();

                    _commandList.SetPipeline(_pipeline);

                    // We want to render directly to the output window.
                    _commandList.SetFramebuffer(framebuffer);

                    _commandList.SetFullViewports();
                    _commandList.ClearColorTarget(0, RgbaFloat.DarkRed);

                    // Set all relevant state to draw our quad.
                    _commandList.SetVertexBuffer(0, _vertexBuffer);

                    _commandList.Draw(vertexCount: (uint)_vertexCount);

                    // End() must be called before commands can be submitted for execution.
                    _commandList.End();
                }
                #endregion

                CommandList vret = _commandList;
                _commandList = null;

                return(vret);

                #endregion
            }
            catch (Exception e)
            {
                return(null);
            }
            finally
            {
                _vsShader?.Dispose();
                _psShader?.Dispose();
                _vertexBuffer?.Dispose();
                _pipeline?.Dispose();
                _commandList?.Dispose();
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Creates a new <see cref="DeviceBuffer"/>.
 /// </summary>
 /// <param name="description">The desired properties of the created object.</param>
 /// <returns>A new <see cref="DeviceBuffer"/>.</returns>
 public DeviceBuffer CreateBuffer(BufferDescription description) => CreateBuffer(ref description);