Skip to content

Commit

Permalink
Add godbolt links for code snippets (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
spencer-lunarg authored Nov 3, 2024
1 parent 2a98e56 commit 4efc2a2
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 93 deletions.
12 changes: 6 additions & 6 deletions chapters/high_level_shader_language_comparison.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ HLSL
----
struct VSOutput
{
// The SV_POSITION semantic declares the Pos member as the vertex output position
// The SV_POSITION semantic declares the Pos member as the vertex output position
float4 Pos : SV_POSITION;
};
Expand All @@ -144,7 +144,7 @@ Reading the vertex index:
GLSL:
[source,glsl]
----
void main()
void main()
{
// The vertex index is stored in the gl_VertexIndex built-in
outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2);
Expand Down Expand Up @@ -254,7 +254,7 @@ Examples:
[source,glsl]
----
// Uniform buffer
layout (set = 0, binding = 0) uniform UBO
layout (set = 0, binding = 0) uniform UBO
{
mat4 projection;
} ubo;
Expand Down Expand Up @@ -990,20 +990,20 @@ These shader stages share several functions and built-ins
| imageAtomicExchange | InterlockedExchange |
| nonuniformEXT | NonUniformResourceIndex |
| gl_BaryCoordEXT | SV_Barycentrics |
| gl_BaryCoordNoPerspEXT | SV_Barycentrics with noperspective |
| gl_BaryCoordNoPerspEXT | SV_Barycentrics with noperspective |
|====

== Functions

[NOTE]
====
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.
====
====

[options="header"]
|====
| *GLSL* | *HLSL*
| dFdx | ddx
| dFdx | ddx
| dFdxCoarse | ddx_coarse
| dFdxFine | ddx_fine
| dFdy | ddy
Expand Down
6 changes: 4 additions & 2 deletions chapters/hlsl.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Similar to regular programming languages, HLSL and GLSL differ in their syntax.
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:

=== GLSL
link:https://godbolt.org/z/jcPofTK9j[Try Online]
[source,glsl]
----
#version 450
Expand All @@ -66,6 +67,7 @@ void main()
----

=== HLSL
link:https://godbolt.org/z/Y4sd9anMY[Try Online]
[source,hlsl]
----
struct VSInput
Expand Down Expand Up @@ -93,7 +95,7 @@ VSOutput main(VSInput input, uint VertexIndex : SV_VertexID)
{
VSOutput output = (VSOutput)0;
output.Color = input.Color * float(VertexIndex);
output.Position = mul(ubo.projectionMatrix, mul(ubo.viewMatrix, mul(ubo.modelMatrix, float4(input.Position.xyz, 1.0))));
output.Pos = mul(ubo.projectionMatrix, mul(ubo.viewMatrix, mul(ubo.modelMatrix, float4(input.Position.xyz, 1.0))));
return output;
}
----
Expand Down Expand Up @@ -198,7 +200,7 @@ std::vector<LPCWSTR> arguments = {
// Shader target profile
L"-T", targetProfile,
// Compile to SPIRV
L"-spirv"
L"-spirv"
};
// Compile shader
Expand Down
69 changes: 43 additions & 26 deletions chapters/mapping_data_to_shaders.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ The only shader stage in core Vulkan that has an input attribute controlled by V

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

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

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

[source,swift]
----
Name 18 "inPosition"
Decorate 18(inPosition) Location 0
OpDecorate %inPosition Location 0
17: TypePointer Input 16(fvec3)
18(inPosition): 17(ptr) Variable Input
19: 16(fvec3) Load 18(inPosition)
%ptr = OpTypePointer Input %v3float
%inPosition = OpVariable %ptr Input
%20 = OpLoad %v3float %inPosition
----

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

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

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

[source,glsl]
----
Expand All @@ -140,23 +139,23 @@ The corresponding SPIR-V assembly:

[source,swift]
----
Decorate 19(myTextureSampler) DescriptorSet 0
Decorate 19(myTextureSampler) Binding 0
OpDecorate %myTextureSampler DescriptorSet 0
OpDecorate %myTextureSampler Binding 0
MemberDecorate 29(uniformBuffer0) 0 Offset 0
Decorate 29(uniformBuffer0) Block
Decorate 31(ubo_0) DescriptorSet 0
Decorate 31(ubo_0) Binding 2
OpMemberDecorate %uniformBuffer0 0 Offset 0
OpDecorate %uniformBuffer0 Block
OpDecorate %ubo_0 DescriptorSet 0
OpDecorate %ubo_0 Binding 2
MemberDecorate 38(uniformBuffer1) 0 Offset 0
Decorate 38(uniformBuffer1) Block
Decorate 40(ubo_1) DescriptorSet 0
Decorate 40(ubo_1) Binding 3
OpMemberDecorate %uniformBuffer1 0 Offset 0
OpDecorate %uniformBuffer1 Block
OpDecorate %ubo_1 DescriptorSet 0
OpDecorate %ubo_1 Binding 3
MemberDecorate 44(storageBuffer) 0 Offset 0
Decorate 44(storageBuffer) BufferBlock
Decorate 46(ssbo) DescriptorSet 2
Decorate 46(ssbo) Binding 0
OpMemberDecorate %storageBuffer 0 Offset 0
OpDecorate %storageBuffer BufferBlock
OpDecorate %ssbo DescriptorSet 2
OpDecorate %ssbo Binding 0
----

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:
Expand Down Expand Up @@ -194,6 +193,8 @@ For GLSL, more information can be found in the link:https://registry.khronos.org

`VK_DESCRIPTOR_TYPE_STORAGE_IMAGE`

link:https://godbolt.org/z/7KPe11GPs[Try Online]

[source,glsl]
----
// VK_FORMAT_R32_UINT
Expand All @@ -219,6 +220,8 @@ OpDecorate %storageImage Binding 0

`VK_DESCRIPTOR_TYPE_SAMPLER` and `VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE`

link:https://godbolt.org/z/zbb3TW19x[Try Online]

[source,glsl]
----
layout(set = 0, binding = 0) uniform sampler samplerDescriptor;
Expand Down Expand Up @@ -262,6 +265,8 @@ OpDecorate %samplerDescriptor Binding 0
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.
====

link:https://godbolt.org/z/aTrajsrY3[Try Online]

[source,glsl]
----
layout(set = 0, binding = 0) uniform sampler2D combinedImageSampler;
Expand Down Expand Up @@ -294,6 +299,8 @@ OpDecorate %combinedImageSampler Binding 0
Uniform buffers can also have xref:{chapters}descriptor_dynamic_offset.adoc[dynamic offsets at bind time] (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC)
====

link:https://godbolt.org/z/qz6dcndxd[Try Online]

[source,glsl]
----
layout(set = 0, binding = 0) uniform uniformBuffer {
Expand Down Expand Up @@ -329,6 +336,8 @@ OpDecorate %ubo Binding 0
Storage buffers can also have xref:{chapters}descriptor_dynamic_offset.adoc[dynamic offsets at bind time] (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)
====

link:https://godbolt.org/z/hEfe8PhfY[Try Online]

[source,glsl]
----
layout(set = 0, binding = 0) buffer storageBuffer {
Expand Down Expand Up @@ -365,6 +374,8 @@ OpDecorate %ssbo Binding 0

`VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER`

link:https://godbolt.org/z/ob4T9d3E4[Try Online]

[source,glsl]
----
layout(set = 0, binding = 0) uniform textureBuffer uniformTexelBuffer;
Expand All @@ -389,6 +400,8 @@ OpDecorate %uniformTexelBuffer Binding 0

`VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER`

link:https://godbolt.org/z/zoeMxsKjq[Try Online]

[source,glsl]
----
// VK_FORMAT_R8G8B8A8_UINT
Expand All @@ -415,6 +428,8 @@ OpDecorate %storageTexelBuffer Binding 0

`VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT`

link:https://godbolt.org/z/aMncGWajG[Try Online]

[source,glsl]
----
layout (input_attachment_index = 0, set = 0, binding = 0) uniform subpassInput inputAttachment;
Expand Down Expand Up @@ -476,6 +491,8 @@ void main() {

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.

link:https://godbolt.org/z/xnncjdf3z[Try Online]

[source,glsl]
----
#version 450
Expand All @@ -489,13 +506,13 @@ void main() {

Resulting SPIR-V assembly:

[source,spswiftirv]
[source,swift]
----
Decorate 9(outColor) Location 0
Decorate 10(myColor) SpecId 0
OpDecorate %outColor Location 0
OpDecorate %myColor SpecId 0
// 0x3f800000 as decimal which is 1.0 for a 32 bit float
10(myColor): 6(float) SpecConstant 1065353216
// 0x3f800000 as decimal which is 1.0 for a 32 bit float
%myColor = OpSpecConstant %float 1065353216
----

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.
Expand Down
2 changes: 1 addition & 1 deletion chapters/push_constants.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ A small bank of values writable via the API and accessible in shaders. Push cons

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.

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

[source,glsl]
----
Expand Down
10 changes: 10 additions & 0 deletions chapters/shader_memory_layout.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ This extension allows the use of `std430` memory layout in UBOs. link:https://re

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`

(link:https://godbolt.org/z/j11d58hcs[Try Online])

[source,glsl]
----
layout(std140, binding = 0) uniform ubo140 {
Expand Down Expand Up @@ -140,6 +142,8 @@ The following are some GLSL to SPIR-V examples to help better understand the dif

=== Alignment Example 1

(link:https://godbolt.org/z/9rWKEdf1W[Try Online])

[source,glsl]
----
layout(binding = 0) buffer block {
Expand All @@ -165,6 +169,8 @@ OpMemberDecorate %block 1 Offset 32

=== Alignment Example 2

(link:https://godbolt.org/z/YMr6P749b[Try Online])

[source,glsl]
----
layout(binding = 0) buffer block {
Expand All @@ -191,6 +197,8 @@ OpMemberDecorate %block 2 Offset 12

=== Alignment Example 3

(link:https://godbolt.org/z/c4Pe4KvG9[Try Online])

[source,glsl]
----
layout(binding = 0) buffer block {
Expand All @@ -217,6 +225,8 @@ OpMemberDecorate %block 2 Offset 20

=== Alignment Example 4

(link:https://godbolt.org/z/rG17jorf8[Try Online])

[source,glsl]
----
layout (binding = 0) buffer block {
Expand Down
4 changes: 3 additions & 1 deletion lang/jp/chapters/hlsl.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ HLSL を Vulkan に対応させるため、Vulkan 固有の機能に対するイ
ここでは、両言語で書かれた同じシェーダを、前述した名前空間による明示的な位置指定などを含めて、どのように異なるかを簡単に比較します。

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

=== HLSL
https://godbolt.org/z/Y4sd9anMY[オンラインで試す]
[source,hlsl]
----
struct VSInput
Expand Down Expand Up @@ -94,7 +96,7 @@ VSOutput main(VSInput input, uint VertexIndex : SV_VertexID)
{
VSOutput output = (VSOutput)0;
output.Color = input.Color * float(VertexIndex);
output.Position = mul(ubo.projectionMatrix, mul(ubo.viewMatrix, mul(ubo.modelMatrix, float4(input.Position.xyz, 1.0))));
output.Pos = mul(ubo.projectionMatrix, mul(ubo.viewMatrix, mul(ubo.modelMatrix, float4(input.Position.xyz, 1.0))));
return output;
}
----
Expand Down
Loading

0 comments on commit 4efc2a2

Please sign in to comment.