-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathProgram.cs
More file actions
198 lines (167 loc) · 6.97 KB
/
Copy pathProgram.cs
File metadata and controls
198 lines (167 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System.IO;
using System.Numerics;
using Veldrid;
using Veldrid.Sdl2;
using Veldrid.StartupUtilities;
using Veldrid.SPIRV;
using System.Text;
namespace GettingStarted
{
class Program
{
private static GraphicsDevice _graphicsDevice;
private static CommandList _commandList;
private static DeviceBuffer _vertexBuffer;
private static DeviceBuffer _indexBuffer;
private static Shader[] _shaders;
private static Pipeline _pipeline;
private const string VertexCode = @"
#version 450
layout(location = 0) in vec2 Position;
layout(location = 1) in vec4 Color;
layout(location = 0) out vec4 fsin_Color;
void main()
{
gl_Position = vec4(Position, 0, 1);
fsin_Color = Color;
}";
private const string FragmentCode = @"
#version 450
layout(location = 0) in vec4 fsin_Color;
layout(location = 0) out vec4 fsout_Color;
void main()
{
fsout_Color = fsin_Color;
}";
static void Main(string[] args)
{
WindowCreateInfo windowCI = new WindowCreateInfo()
{
X = 100,
Y = 100,
WindowWidth = 960,
WindowHeight = 540,
WindowTitle = "Veldrid Tutorial"
};
Sdl2Window window = VeldridStartup.CreateWindow(ref windowCI);
GraphicsDeviceOptions options = new GraphicsDeviceOptions
{
PreferStandardClipSpaceYDirection = true,
PreferDepthRangeZeroToOne = true
};
_graphicsDevice = VeldridStartup.CreateGraphicsDevice(window, options, GraphicsBackend.Vulkan);
CreateResources();
while (window.Exists)
{
window.PumpEvents();
if (window.Exists)
{
Draw();
}
}
DisposeResources();
}
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.TextureCoordinate, VertexElementFormat.Float2),
new VertexElementDescription("Color", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float4));
ShaderDescription vertexShaderDesc = new ShaderDescription(
ShaderStages.Vertex,
Encoding.UTF8.GetBytes(VertexCode),
"main");
ShaderDescription fragmentShaderDesc = new ShaderDescription(
ShaderStages.Fragment,
Encoding.UTF8.GetBytes(FragmentCode),
"main");
_shaders = factory.CreateFromSpirv(vertexShaderDesc, fragmentShaderDesc);
// 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: _shaders);
pipelineDescription.Outputs = _graphicsDevice.SwapchainFramebuffer.OutputDescription;
_pipeline = factory.CreateGraphicsPipeline(pipelineDescription);
_commandList = factory.CreateCommandList();
}
private static void Draw()
{
// Begin() must be called before commands can be issued.
_commandList.Begin();
// We want to render directly to the output window.
_commandList.SetFramebuffer(_graphicsDevice.SwapchainFramebuffer);
_commandList.ClearColorTarget(0, RgbaFloat.Black);
// Set all relevant state to draw our quad.
_commandList.SetVertexBuffer(0, _vertexBuffer);
_commandList.SetIndexBuffer(_indexBuffer, IndexFormat.UInt16);
_commandList.SetPipeline(_pipeline);
// Issue a Draw command for a single instance with 4 indices.
_commandList.DrawIndexed(
indexCount: 4,
instanceCount: 1,
indexStart: 0,
vertexOffset: 0,
instanceStart: 0);
// End() must be called before commands can be submitted for execution.
_commandList.End();
_graphicsDevice.SubmitCommands(_commandList);
// Once commands have been submitted, the rendered image can be presented to the application window.
_graphicsDevice.SwapBuffers();
}
private static void DisposeResources()
{
_pipeline.Dispose();
foreach (Shader shader in _shaders)
{
shader.Dispose();
}
_commandList.Dispose();
_vertexBuffer.Dispose();
_indexBuffer.Dispose();
_graphicsDevice.Dispose();
}
}
struct VertexPositionColor
{
public const uint SizeInBytes = 24;
public Vector2 Position;
public RgbaFloat Color;
public VertexPositionColor(Vector2 position, RgbaFloat color)
{
Position = position;
Color = color;
}
}
}