Skip to content

Commit 4efc2a2

Browse files
Add godbolt links for code snippets (#282)
1 parent 2a98e56 commit 4efc2a2

13 files changed

+181
-93
lines changed

chapters/high_level_shader_language_comparison.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ HLSL
127127
----
128128
struct VSOutput
129129
{
130-
// The SV_POSITION semantic declares the Pos member as the vertex output position
130+
// The SV_POSITION semantic declares the Pos member as the vertex output position
131131
float4 Pos : SV_POSITION;
132132
};
133133
@@ -144,7 +144,7 @@ Reading the vertex index:
144144
GLSL:
145145
[source,glsl]
146146
----
147-
void main()
147+
void main()
148148
{
149149
// The vertex index is stored in the gl_VertexIndex built-in
150150
outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2);
@@ -254,7 +254,7 @@ Examples:
254254
[source,glsl]
255255
----
256256
// Uniform buffer
257-
layout (set = 0, binding = 0) uniform UBO
257+
layout (set = 0, binding = 0) uniform UBO
258258
{
259259
mat4 projection;
260260
} ubo;
@@ -990,20 +990,20 @@ These shader stages share several functions and built-ins
990990
| imageAtomicExchange | InterlockedExchange |
991991
| nonuniformEXT | NonUniformResourceIndex |
992992
| gl_BaryCoordEXT | SV_Barycentrics |
993-
| gl_BaryCoordNoPerspEXT | SV_Barycentrics with noperspective |
993+
| gl_BaryCoordNoPerspEXT | SV_Barycentrics with noperspective |
994994
|====
995995

996996
== Functions
997997

998998
[NOTE]
999999
====
10001000
Most GLSL functions are also available in HLSL and vice-versa. This chapter lists functions with divergent names. Functions that have a 1:1 counterpart (e.g. `isNan`) aren't listed.
1001-
====
1001+
====
10021002

10031003
[options="header"]
10041004
|====
10051005
| *GLSL* | *HLSL*
1006-
| dFdx | ddx
1006+
| dFdx | ddx
10071007
| dFdxCoarse | ddx_coarse
10081008
| dFdxFine | ddx_fine
10091009
| dFdy | ddy

chapters/hlsl.adoc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Similar to regular programming languages, HLSL and GLSL differ in their syntax.
4242
Here is the same shader written in both languages to give quick comparison on how they basically differ, including the aforementioned namespace that e.g. adds explicit locations:
4343

4444
=== GLSL
45+
link:https://godbolt.org/z/jcPofTK9j[Try Online]
4546
[source,glsl]
4647
----
4748
#version 450
@@ -66,6 +67,7 @@ void main()
6667
----
6768

6869
=== HLSL
70+
link:https://godbolt.org/z/Y4sd9anMY[Try Online]
6971
[source,hlsl]
7072
----
7173
struct VSInput
@@ -93,7 +95,7 @@ VSOutput main(VSInput input, uint VertexIndex : SV_VertexID)
9395
{
9496
VSOutput output = (VSOutput)0;
9597
output.Color = input.Color * float(VertexIndex);
96-
output.Position = mul(ubo.projectionMatrix, mul(ubo.viewMatrix, mul(ubo.modelMatrix, float4(input.Position.xyz, 1.0))));
98+
output.Pos = mul(ubo.projectionMatrix, mul(ubo.viewMatrix, mul(ubo.modelMatrix, float4(input.Position.xyz, 1.0))));
9799
return output;
98100
}
99101
----
@@ -198,7 +200,7 @@ std::vector<LPCWSTR> arguments = {
198200
// Shader target profile
199201
L"-T", targetProfile,
200202
// Compile to SPIRV
201-
L"-spirv"
203+
L"-spirv"
202204
};
203205
204206
// Compile shader

chapters/mapping_data_to_shaders.adoc

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ The only shader stage in core Vulkan that has an input attribute controlled by V
3939

4040
Before calling `vkCreateGraphicsPipelines` a `VkPipelineVertexInputStateCreateInfo` struct will need to be filled out with a list of `VkVertexInputAttributeDescription` mappings to the shader.
4141

42-
An example GLSL vertex shader:
42+
An example GLSL vertex shader (link:https://godbolt.org/z/x3b3ceTa6[Try Online]):
4343

4444
[source,glsl]
4545
----
@@ -55,12 +55,11 @@ There is only a single input attribute at location 0. This can also be seen in t
5555

5656
[source,swift]
5757
----
58-
Name 18 "inPosition"
59-
Decorate 18(inPosition) Location 0
58+
OpDecorate %inPosition Location 0
6059
61-
17: TypePointer Input 16(fvec3)
62-
18(inPosition): 17(ptr) Variable Input
63-
19: 16(fvec3) Load 18(inPosition)
60+
%ptr = OpTypePointer Input %v3float
61+
%inPosition = OpVariable %ptr Input
62+
%20 = OpLoad %v3float %inPosition
6463
----
6564

6665
In this example, the following could be used for the `VkVertexInputAttributeDescription`:
@@ -115,7 +114,7 @@ In this example, there are the following 3 descriptor sets:
115114

116115
image::{images}mapping_data_to_shaders_descriptor_1.png[mapping_data_to_shaders_descriptor_1.png]
117116

118-
The GLSL of the shader:
117+
The GLSL of the shader (link:https://godbolt.org/z/oMz58a78T[Try Online]):
119118

120119
[source,glsl]
121120
----
@@ -140,23 +139,23 @@ The corresponding SPIR-V assembly:
140139

141140
[source,swift]
142141
----
143-
Decorate 19(myTextureSampler) DescriptorSet 0
144-
Decorate 19(myTextureSampler) Binding 0
142+
OpDecorate %myTextureSampler DescriptorSet 0
143+
OpDecorate %myTextureSampler Binding 0
145144
146-
MemberDecorate 29(uniformBuffer0) 0 Offset 0
147-
Decorate 29(uniformBuffer0) Block
148-
Decorate 31(ubo_0) DescriptorSet 0
149-
Decorate 31(ubo_0) Binding 2
145+
OpMemberDecorate %uniformBuffer0 0 Offset 0
146+
OpDecorate %uniformBuffer0 Block
147+
OpDecorate %ubo_0 DescriptorSet 0
148+
OpDecorate %ubo_0 Binding 2
150149
151-
MemberDecorate 38(uniformBuffer1) 0 Offset 0
152-
Decorate 38(uniformBuffer1) Block
153-
Decorate 40(ubo_1) DescriptorSet 0
154-
Decorate 40(ubo_1) Binding 3
150+
OpMemberDecorate %uniformBuffer1 0 Offset 0
151+
OpDecorate %uniformBuffer1 Block
152+
OpDecorate %ubo_1 DescriptorSet 0
153+
OpDecorate %ubo_1 Binding 3
155154
156-
MemberDecorate 44(storageBuffer) 0 Offset 0
157-
Decorate 44(storageBuffer) BufferBlock
158-
Decorate 46(ssbo) DescriptorSet 2
159-
Decorate 46(ssbo) Binding 0
155+
OpMemberDecorate %storageBuffer 0 Offset 0
156+
OpDecorate %storageBuffer BufferBlock
157+
OpDecorate %ssbo DescriptorSet 2
158+
OpDecorate %ssbo Binding 0
160159
----
161160

162161
The binding of descriptors is done while recording the command buffer. The descriptors must be bound at the time of a draw/dispatch call. The following is some pseudo code to better represent this:
@@ -194,6 +193,8 @@ For GLSL, more information can be found in the link:https://registry.khronos.org
194193

195194
`VK_DESCRIPTOR_TYPE_STORAGE_IMAGE`
196195

196+
link:https://godbolt.org/z/7KPe11GPs[Try Online]
197+
197198
[source,glsl]
198199
----
199200
// VK_FORMAT_R32_UINT
@@ -219,6 +220,8 @@ OpDecorate %storageImage Binding 0
219220

220221
`VK_DESCRIPTOR_TYPE_SAMPLER` and `VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE`
221222

223+
link:https://godbolt.org/z/zbb3TW19x[Try Online]
224+
222225
[source,glsl]
223226
----
224227
layout(set = 0, binding = 0) uniform sampler samplerDescriptor;
@@ -262,6 +265,8 @@ OpDecorate %samplerDescriptor Binding 0
262265
On some implementations, it **may** be more efficient to sample from an image using a combination of sampler and sampled image that are stored together in the descriptor set in a combined descriptor.
263266
====
264267

268+
link:https://godbolt.org/z/aTrajsrY3[Try Online]
269+
265270
[source,glsl]
266271
----
267272
layout(set = 0, binding = 0) uniform sampler2D combinedImageSampler;
@@ -294,6 +299,8 @@ OpDecorate %combinedImageSampler Binding 0
294299
Uniform buffers can also have xref:{chapters}descriptor_dynamic_offset.adoc[dynamic offsets at bind time] (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC)
295300
====
296301

302+
link:https://godbolt.org/z/qz6dcndxd[Try Online]
303+
297304
[source,glsl]
298305
----
299306
layout(set = 0, binding = 0) uniform uniformBuffer {
@@ -329,6 +336,8 @@ OpDecorate %ubo Binding 0
329336
Storage buffers can also have xref:{chapters}descriptor_dynamic_offset.adoc[dynamic offsets at bind time] (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)
330337
====
331338

339+
link:https://godbolt.org/z/hEfe8PhfY[Try Online]
340+
332341
[source,glsl]
333342
----
334343
layout(set = 0, binding = 0) buffer storageBuffer {
@@ -365,6 +374,8 @@ OpDecorate %ssbo Binding 0
365374

366375
`VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER`
367376

377+
link:https://godbolt.org/z/ob4T9d3E4[Try Online]
378+
368379
[source,glsl]
369380
----
370381
layout(set = 0, binding = 0) uniform textureBuffer uniformTexelBuffer;
@@ -389,6 +400,8 @@ OpDecorate %uniformTexelBuffer Binding 0
389400

390401
`VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER`
391402

403+
link:https://godbolt.org/z/zoeMxsKjq[Try Online]
404+
392405
[source,glsl]
393406
----
394407
// VK_FORMAT_R8G8B8A8_UINT
@@ -415,6 +428,8 @@ OpDecorate %storageTexelBuffer Binding 0
415428

416429
`VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT`
417430

431+
link:https://godbolt.org/z/aMncGWajG[Try Online]
432+
418433
[source,glsl]
419434
----
420435
layout (input_attachment_index = 0, set = 0, binding = 0) uniform subpassInput inputAttachment;
@@ -476,6 +491,8 @@ void main() {
476491

477492
Using specialization constants, the decision can instead be made when calling `vkCreateGraphicsPipelines` to compile the shader. This means there only needs to be a single shader.
478493

494+
link:https://godbolt.org/z/xnncjdf3z[Try Online]
495+
479496
[source,glsl]
480497
----
481498
#version 450
@@ -489,13 +506,13 @@ void main() {
489506

490507
Resulting SPIR-V assembly:
491508

492-
[source,spswiftirv]
509+
[source,swift]
493510
----
494-
Decorate 9(outColor) Location 0
495-
Decorate 10(myColor) SpecId 0
511+
OpDecorate %outColor Location 0
512+
OpDecorate %myColor SpecId 0
496513
497-
// 0x3f800000 as decimal which is 1.0 for a 32 bit float
498-
10(myColor): 6(float) SpecConstant 1065353216
514+
// 0x3f800000 as decimal which is 1.0 for a 32 bit float
515+
%myColor = OpSpecConstant %float 1065353216
499516
----
500517

501518
With specialization constants, the value is still a constant inside the shader, but for example, if another `VkPipeline` uses the same shader, but wants to set the `myColor` value to `0.5f`, it is possible to do so at runtime.

chapters/push_constants.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ A small bank of values writable via the API and accessible in shaders. Push cons
3535

3636
From a shader perspective, push constant are similar to a uniform buffer. The spec provides details for the link:https://www.khronos.org/registry/vulkan/specs/1.3-extensions/html/vkspec.html#interfaces-resources-pushconst[push constant interface] between Vulkan and SPIR-V.
3737

38-
A simple GLSL fragment shader example:
38+
A simple GLSL fragment shader example (link:https://godbolt.org/z/93WaYd8dE[Try Online]):
3939

4040
[source,glsl]
4141
----

chapters/shader_memory_layout.adoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ This extension allows the use of `std430` memory layout in UBOs. link:https://re
3939

4040
One example of when the `uniformBufferStandardLayout` feature is needed is when an application doesn't want the array stride for a UBO to be restricted to `extended alignment`
4141

42+
(link:https://godbolt.org/z/j11d58hcs[Try Online])
43+
4244
[source,glsl]
4345
----
4446
layout(std140, binding = 0) uniform ubo140 {
@@ -140,6 +142,8 @@ The following are some GLSL to SPIR-V examples to help better understand the dif
140142

141143
=== Alignment Example 1
142144

145+
(link:https://godbolt.org/z/9rWKEdf1W[Try Online])
146+
143147
[source,glsl]
144148
----
145149
layout(binding = 0) buffer block {
@@ -165,6 +169,8 @@ OpMemberDecorate %block 1 Offset 32
165169

166170
=== Alignment Example 2
167171

172+
(link:https://godbolt.org/z/YMr6P749b[Try Online])
173+
168174
[source,glsl]
169175
----
170176
layout(binding = 0) buffer block {
@@ -191,6 +197,8 @@ OpMemberDecorate %block 2 Offset 12
191197

192198
=== Alignment Example 3
193199

200+
(link:https://godbolt.org/z/c4Pe4KvG9[Try Online])
201+
194202
[source,glsl]
195203
----
196204
layout(binding = 0) buffer block {
@@ -217,6 +225,8 @@ OpMemberDecorate %block 2 Offset 20
217225

218226
=== Alignment Example 4
219227

228+
(link:https://godbolt.org/z/rG17jorf8[Try Online])
229+
220230
[source,glsl]
221231
----
222232
layout (binding = 0) buffer block {

lang/jp/chapters/hlsl.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ HLSL を Vulkan に対応させるため、Vulkan 固有の機能に対するイ
4343
ここでは、両言語で書かれた同じシェーダを、前述した名前空間による明示的な位置指定などを含めて、どのように異なるかを簡単に比較します。
4444

4545
=== GLSL
46+
link:https://godbolt.org/z/jcPofTK9j[オンラインで試す]
4647
[source,glsl]
4748
----
4849
#version 450
@@ -67,6 +68,7 @@ void main()
6768
----
6869

6970
=== HLSL
71+
https://godbolt.org/z/Y4sd9anMY[オンラインで試す]
7072
[source,hlsl]
7173
----
7274
struct VSInput
@@ -94,7 +96,7 @@ VSOutput main(VSInput input, uint VertexIndex : SV_VertexID)
9496
{
9597
VSOutput output = (VSOutput)0;
9698
output.Color = input.Color * float(VertexIndex);
97-
output.Position = mul(ubo.projectionMatrix, mul(ubo.viewMatrix, mul(ubo.modelMatrix, float4(input.Position.xyz, 1.0))));
99+
output.Pos = mul(ubo.projectionMatrix, mul(ubo.viewMatrix, mul(ubo.modelMatrix, float4(input.Position.xyz, 1.0))));
98100
return output;
99101
}
100102
----

0 commit comments

Comments
 (0)