Moved shaders to new directory

This commit is contained in:
Sascha Willems 2023-05-09 21:03:02 +02:00
parent 0b3f8340e3
commit 99b226237a
1244 changed files with 0 additions and 0 deletions

18
shaders/hlsl/README.md Normal file
View file

@ -0,0 +1,18 @@
## HLSL Shaders
This directory contains a fork of the shaders found in [data/shaders/glsl](https://github.com/SaschaWillems/Vulkan/tree/master/data/shaders/glsl), re-written in HLSL.
These can be compiled with [DXC](https://github.com/microsoft/DirectXShaderCompiler) using the `compile.py` script.
### Known issues
- specialization constants can't be used to specify array size.
- `gl_PointCoord` not supported. HLSL has no equivalent. We changed the shaders to calulate the PointCoord manually in the shader. (`computenbody`, `computeparticles`, `particlefire` examples).
- HLSL doesn't have inverse operation (`deferred`, `hdr`, `instancing`, `skeletalanimation` & `texturecubemap` examples).
- `modf` causes compilation to fail without errors or warnings. (`modf` not used by any examples, easily confused with fmod)
- In `specializationconstants` example, shader compilation fails with error:
```
--- Error msg: fatal error: failed to optimize SPIR-V: Id 10 is defined more than once
```
When multiple constant ids are defined and have different types. We work around this problem by making all constant ids the same type, then use `asfloat`, `asint` or `asuint` to get the original value in the shader.
- `gl_RayTmaxNV` not supported. (`nv_ray_tracing_*` examples)
- HLSL interface for sparse residency textures is different from GLSL interface. After translating from HLSL to GLSL the shaders behave slightly different. Most important parts do behave identically though.

View file

@ -0,0 +1,10 @@
// Copyright 2020 Google LLC
Texture2D textureFont : register(t0);
SamplerState samplerFont : register(s0);
float4 main([[vk::location(0)]]float2 inUV : TEXCOORD0) : SV_TARGET
{
float color = textureFont.Sample(samplerFont, inUV).r;
return float4(color.xxx, 1.0);
}

Binary file not shown.

View file

@ -0,0 +1,21 @@
// Copyright 2020 Google LLC
struct VSInput
{
[[vk::location(0)]]float2 Pos : POSITION0;
[[vk::location(1)]]float2 UV : TEXCOORD0;
};
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]]float2 UV : TEXCOORD0;
};
VSOutput main(VSInput input)
{
VSOutput output = (VSOutput)0;
output.Pos = float4(input.Pos, 0.0, 1.0);
output.UV = input.UV;
return output;
}

Binary file not shown.

View file

@ -0,0 +1,15 @@
// Copyright 2020 Google LLC
Texture2D fontTexture : register(t0);
SamplerState fontSampler : register(s0);
struct VSOutput
{
[[vk::location(0)]]float2 UV : TEXCOORD0;
[[vk::location(1)]]float4 Color : COLOR0;
};
float4 main(VSOutput input) : SV_TARGET
{
return input.Color * fontTexture.Sample(fontSampler, input.UV);
}

Binary file not shown.

View file

@ -0,0 +1,33 @@
// Copyright 2020 Google LLC
struct VSInput
{
[[vk::location(0)]]float2 Pos : POSITION0;
[[vk::location(1)]]float2 UV : TEXCOORD0;
[[vk::location(2)]]float4 Color : COLOR0;
};
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]]float2 UV : TEXCOORD0;
[[vk::location(1)]]float4 Color : COLOR0;
};
struct PushConstants
{
float2 scale;
float2 translate;
};
[[vk::push_constant]]
PushConstants pushConstants;
VSOutput main(VSInput input)
{
VSOutput output = (VSOutput)0;
output.Pos = float4(input.Pos * pushConstants.scale + pushConstants.translate, 0.0, 1.0);
output.UV = input.UV;
output.Color = input.Color;
return output;
}

Binary file not shown.

View file

@ -0,0 +1,15 @@
// Copyright 2020 Google LLC
Texture2D colorMapTexture : register(t1);
SamplerState colorMapSampler : register(s1);
struct VSOutput
{
[[vk::location(0)]]float3 Color : COLOR0;
[[vk::location(1)]]float2 UV : TEXCOORD0;
};
float4 main(VSOutput input) : SV_TARGET
{
return float4(input.Color, 1);
}

Binary file not shown.

View file

@ -0,0 +1,31 @@
// Copyright 2020 Google LLC
struct VSInput
{
[[vk::location(0)]]float4 Pos : POSITION0;
[[vk::location(1)]]float2 UV : TEXCOORD0;
[[vk::location(2)]]float3 Color : COLOR0;
};
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]]float3 Color : COLOR0;
[[vk::location(1)]]float2 UV : TEXCOORD0;
};
cbuffer UBO : register(b0)
{
float4x4 projection;
float4x4 view;
float4x4 model;
};
VSOutput main(VSInput input)
{
VSOutput output = (VSOutput)0;
output.UV = input.UV;
output.Color = input.Color;
output.Pos = mul(projection, mul(view, mul(model, input.Pos)));
return output;
}

Binary file not shown.

View file

@ -0,0 +1,43 @@
// Copyright 2020 Google LLC
Texture2D textureColor : register(t1);
SamplerState samplerColor : register(s1);
cbuffer UBO : register(b0)
{
float blurScale;
float blurStrength;
};
[[vk::constant_id(0)]] const int blurdirection = 0;
float4 main([[vk::location(0)]] float2 inUV : TEXCOORD0) : SV_TARGET
{
float weight[5];
weight[0] = 0.227027;
weight[1] = 0.1945946;
weight[2] = 0.1216216;
weight[3] = 0.054054;
weight[4] = 0.016216;
float2 textureSize;
textureColor.GetDimensions(textureSize.x, textureSize.y);
float2 tex_offset = 1.0 / textureSize * blurScale; // gets size of single texel
float3 result = textureColor.Sample(samplerColor, inUV).rgb * weight[0]; // current fragment's contribution
for(int i = 1; i < 5; ++i)
{
if (blurdirection == 1)
{
// H
result += textureColor.Sample(samplerColor, inUV + float2(tex_offset.x * i, 0.0)).rgb * weight[i] * blurStrength;
result += textureColor.Sample(samplerColor, inUV - float2(tex_offset.x * i, 0.0)).rgb * weight[i] * blurStrength;
}
else
{
// V
result += textureColor.Sample(samplerColor, inUV + float2(0.0, tex_offset.y * i)).rgb * weight[i] * blurStrength;
result += textureColor.Sample(samplerColor, inUV - float2(0.0, tex_offset.y * i)).rgb * weight[i] * blurStrength;
}
}
return float4(result, 1.0);
}

Binary file not shown.

View file

@ -0,0 +1,15 @@
// Copyright 2020 Google LLC
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float2 UV : TEXCOORD0;
};
VSOutput main(uint VertexIndex : SV_VertexID)
{
VSOutput output = (VSOutput)0;
output.UV = float2((VertexIndex << 1) & 2, VertexIndex & 2);
output.Pos = float4(output.UV * 2.0f - 1.0f, 0.0f, 1.0f);
return output;
}

Binary file not shown.

View file

@ -0,0 +1,32 @@
// Copyright 2020 Google LLC
Texture2D colorMapTexture : register(t1);
SamplerState colorMapSampler : register(s1);
struct VSOutput
{
[[vk::location(0)]] float3 Normal : NORMAL0;
[[vk::location(1)]] float2 UV : TEXCOORD0;
[[vk::location(2)]] float3 Color : COLOR0;
[[vk::location(3)]] float3 ViewVec : TEXCOORD1;
[[vk::location(4)]] float3 LightVec : TEXCOORD2;
};
float4 main(VSOutput input) : SV_TARGET
{
float3 ambient = float3(0.0f, 0.0f, 0.0f);
// Adjust light calculations for glow color
if ((input.Color.r >= 0.9) || (input.Color.g >= 0.9) || (input.Color.b >= 0.9))
{
ambient = input.Color * 0.25;
}
float3 N = normalize(input.Normal);
float3 L = normalize(input.LightVec);
float3 V = normalize(input.ViewVec);
float3 R = reflect(-L, N);
float3 diffuse = max(dot(N, L), 0.0) * input.Color;
float3 specular = pow(max(dot(R, V), 0.0), 8.0) * float3(0.75f, 0.75f, 0.75f);
return float4(ambient + diffuse + specular, 1.0);
}

Binary file not shown.

View file

@ -0,0 +1,42 @@
// Copyright 2020 Google LLC
struct VSInput
{
[[vk::location(0)]] float4 Pos : POSITION0;
[[vk::location(1)]] float2 UV : TEXCOORD0;
[[vk::location(2)]] float3 Color : COLOR0;
[[vk::location(3)]] float3 Normal : NORMAL0;
};
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float3 Normal : NORMAL0;
[[vk::location(1)]] float2 UV : TEXCOORD0;
[[vk::location(2)]] float3 Color : COLOR0;
[[vk::location(3)]] float3 ViewVec : TEXCOORD1;
[[vk::location(4)]] float3 LightVec : TEXCOORD2;
};
cbuffer UBO : register(b0)
{
float4x4 projection;
float4x4 view;
float4x4 model;
};
VSOutput main(VSInput input)
{
VSOutput output = (VSOutput)0;
output.Normal = input.Normal;
output.Color = input.Color;
output.UV = input.UV;
output.Pos = mul(projection, mul(view, mul(model, input.Pos)));
float3 lightPos = float3(-5.0, -5.0, 0.0);
float4 pos = mul(view, mul(model, input.Pos));
output.Normal = mul((float4x3)mul(view, model), input.Normal).xyz;
output.LightVec = lightPos - pos.xyz;
output.ViewVec = -pos.xyz;
return output;
}

Binary file not shown.

View file

@ -0,0 +1,9 @@
// Copyright 2020 Google LLC
TextureCube textureCubeMap : register(t1);
SamplerState samplerCubeMap : register(s1);
float4 main([[vk::location(0)]] float3 inUVW : NORMAL0) : SV_TARGET
{
return textureCubeMap.Sample(samplerCubeMap, inUVW);
}

Binary file not shown.

View file

@ -0,0 +1,22 @@
// Copyright 2020 Google LLC
cbuffer UBO : register(b0)
{
float4x4 projection;
float4x4 view;
float4x4 model;
};
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float3 UVW : NORMAL0;
};
VSOutput main([[vk::location(0)]] float3 inPos : POSITION0)
{
VSOutput output = (VSOutput)0;
output.UVW = inPos;
output.Pos = mul(projection, mul(view, mul(model, float4(inPos.xyz, 1.0))));
return output;
}

Binary file not shown.

72
shaders/hlsl/compile.py Normal file
View file

@ -0,0 +1,72 @@
# Copyright 2020 Google LLC
import argparse
import fileinput
import os
import subprocess
import sys
parser = argparse.ArgumentParser(description='Compile all .hlsl shaders')
parser.add_argument('--dxc', type=str, help='path to DXC executable')
args = parser.parse_args()
def findDXC():
def isExe(path):
return os.path.isfile(path) and os.access(path, os.X_OK)
if args.dxc != None and isExe(args.dxc):
return args.dxc
exe_name = "dxc"
if os.name == "nt":
exe_name += ".exe"
for exe_dir in os.environ["PATH"].split(os.pathsep):
full_path = os.path.join(exe_dir, exe_name)
if isExe(full_path):
return full_path
sys.exit("Could not find DXC executable on PATH, and was not specified with --dxc")
dxc_path = findDXC()
dir_path = os.path.dirname(os.path.realpath(__file__))
dir_path = dir_path.replace('\\', '/')
for root, dirs, files in os.walk(dir_path):
for file in files:
if file.endswith(".vert") or file.endswith(".frag") or file.endswith(".comp") or file.endswith(".geom") or file.endswith(".tesc") or file.endswith(".tese") or file.endswith(".rgen") or file.endswith(".rchit") or file.endswith(".rmiss"):
hlsl_file = os.path.join(root, file)
spv_out = hlsl_file + ".spv"
target = ''
profile = ''
if(hlsl_file.find('.vert') != -1):
profile = 'vs_6_1'
elif(hlsl_file.find('.frag') != -1):
profile = 'ps_6_1'
elif(hlsl_file.find('.comp') != -1):
profile = 'cs_6_1'
elif(hlsl_file.find('.geom') != -1):
profile = 'gs_6_1'
elif(hlsl_file.find('.tesc') != -1):
profile = 'hs_6_1'
elif(hlsl_file.find('.tese') != -1):
profile = 'ds_6_1'
elif(hlsl_file.find('.rgen') != -1 or
hlsl_file.find('.rchit') != -1 or
hlsl_file.find('.rmiss') != -1):
target='-fspv-target-env=vulkan1.2'
profile = 'lib_6_3'
print('Compiling %s' % (hlsl_file))
subprocess.check_output([
dxc_path,
'-spirv',
'-T', profile,
'-E', 'main',
'-fspv-extension=SPV_KHR_ray_tracing',
'-fspv-extension=SPV_KHR_multiview',
'-fspv-extension=SPV_KHR_shader_draw_parameters',
'-fspv-extension=SPV_EXT_descriptor_indexing',
target,
hlsl_file,
'-Fo', spv_out])

View file

@ -0,0 +1,154 @@
// Copyright 2020 Google LLC
struct Particle {
float4 pos;
float4 vel;
float4 uv;
float4 normal;
float pinned;
};
[[vk::binding(0)]]
StructuredBuffer<Particle> particleIn;
[[vk::binding(1)]]
RWStructuredBuffer<Particle> particleOut;
struct UBO
{
float deltaT;
float particleMass;
float springStiffness;
float damping;
float restDistH;
float restDistV;
float restDistD;
float sphereRadius;
float4 spherePos;
float4 gravity;
int2 particleCount;
};
cbuffer ubo : register(b2)
{
UBO params;
};
struct PushConstants
{
uint calculateNormals;
};
[[vk::push_constant]]
PushConstants pushConstants;
float3 springForce(float3 p0, float3 p1, float restDist)
{
float3 dist = p0 - p1;
return normalize(dist) * params.springStiffness * (length(dist) - restDist);
}
[numthreads(10, 10, 1)]
void main(uint3 id : SV_DispatchThreadID)
{
uint index = id.y * params.particleCount.x + id.x;
if (index > params.particleCount.x * params.particleCount.y)
return;
// Pinned?
if (particleIn[index].pinned == 1.0) {
particleOut[index].pos = particleOut[index].pos;
particleOut[index].vel = float4(0, 0, 0, 0);
return;
}
// Initial force from gravity
float3 force = params.gravity.xyz * params.particleMass;
float3 pos = particleIn[index].pos.xyz;
float3 vel = particleIn[index].vel.xyz;
// Spring forces from neighboring particles
// left
if (id.x > 0) {
force += springForce(particleIn[index-1].pos.xyz, pos, params.restDistH);
}
// right
if (id.x < params.particleCount.x - 1) {
force += springForce(particleIn[index + 1].pos.xyz, pos, params.restDistH);
}
// upper
if (id.y < params.particleCount.y - 1) {
force += springForce(particleIn[index + params.particleCount.x].pos.xyz, pos, params.restDistV);
}
// lower
if (id.y > 0) {
force += springForce(particleIn[index - params.particleCount.x].pos.xyz, pos, params.restDistV);
}
// upper-left
if ((id.x > 0) && (id.y < params.particleCount.y - 1)) {
force += springForce(particleIn[index + params.particleCount.x - 1].pos.xyz, pos, params.restDistD);
}
// lower-left
if ((id.x > 0) && (id.y > 0)) {
force += springForce(particleIn[index - params.particleCount.x - 1].pos.xyz, pos, params.restDistD);
}
// upper-right
if ((id.x < params.particleCount.x - 1) && (id.y < params.particleCount.y - 1)) {
force += springForce(particleIn[index + params.particleCount.x + 1].pos.xyz, pos, params.restDistD);
}
// lower-right
if ((id.x < params.particleCount.x - 1) && (id.y > 0)) {
force += springForce(particleIn[index - params.particleCount.x + 1].pos.xyz, pos, params.restDistD);
}
force += (-params.damping * vel);
// Integrate
float3 f = force * (1.0 / params.particleMass);
particleOut[index].pos = float4(pos + vel * params.deltaT + 0.5 * f * params.deltaT * params.deltaT, 1.0);
particleOut[index].vel = float4(vel + f * params.deltaT, 0.0);
// Sphere collision
float3 sphereDist = particleOut[index].pos.xyz - params.spherePos.xyz;
if (length(sphereDist) < params.sphereRadius + 0.01) {
// If the particle is inside the sphere, push it to the outer radius
particleOut[index].pos.xyz = params.spherePos.xyz + normalize(sphereDist) * (params.sphereRadius + 0.01);
// Cancel out velocity
particleOut[index].vel = float4(0, 0, 0, 0);
}
// Normals
if (pushConstants.calculateNormals == 1) {
float3 normal = float3(0, 0, 0);
float3 a, b, c;
if (id.y > 0) {
if (id.x > 0) {
a = particleIn[index - 1].pos.xyz - pos;
b = particleIn[index - params.particleCount.x - 1].pos.xyz - pos;
c = particleIn[index - params.particleCount.x].pos.xyz - pos;
normal += cross(a,b) + cross(b,c);
}
if (id.x < params.particleCount.x - 1) {
a = particleIn[index - params.particleCount.x].pos.xyz - pos;
b = particleIn[index - params.particleCount.x + 1].pos.xyz - pos;
c = particleIn[index + 1].pos.xyz - pos;
normal += cross(a,b) + cross(b,c);
}
}
if (id.y < params.particleCount.y - 1) {
if (id.x > 0) {
a = particleIn[index + params.particleCount.x].pos.xyz - pos;
b = particleIn[index + params.particleCount.x - 1].pos.xyz - pos;
c = particleIn[index - 1].pos.xyz - pos;
normal += cross(a,b) + cross(b,c);
}
if (id.x < params.particleCount.x - 1) {
a = particleIn[index + 1].pos.xyz - pos;
b = particleIn[index + params.particleCount.x + 1].pos.xyz - pos;
c = particleIn[index + params.particleCount.x].pos.xyz - pos;
normal += cross(a,b) + cross(b,c);
}
}
particleOut[index].normal = float4(normalize(normal), 0.0f);
}
}

Binary file not shown.

View file

@ -0,0 +1,24 @@
// Copyright 2020 Google LLC
Texture2D textureColor : register(t1);
SamplerState samplerColor : register(s1);
struct VSOutput
{
[[vk::location(0)]]float2 UV : TEXCOORD0;
[[vk::location(1)]]float3 Normal : NORMAL0;
[[vk::location(2)]]float3 ViewVec : TEXCOORD1;
[[vk::location(3)]]float3 LightVec : TEXCOORD2;
};
float4 main (VSOutput input) : SV_TARGET
{
float3 color = textureColor.Sample(samplerColor, input.UV).rgb;
float3 N = normalize(input.Normal);
float3 L = normalize(input.LightVec);
float3 V = normalize(input.ViewVec);
float3 R = reflect(-L, N);
float3 diffuse = max(dot(N, L), 0.15) * float3(1, 1, 1);
float3 specular = pow(max(dot(R, V), 0.0), 8.0) * float3(0.2, 0.2, 0.2);
return float4(diffuse * color.rgb + specular, 1.0);
}

Binary file not shown.

View file

@ -0,0 +1,43 @@
// Copyright 2020 Google LLC
struct VSInput
{
[[vk::location(0)]] float3 Pos : POSITION0;
[[vk::location(1)]] float2 UV : TEXCOORD0;
[[vk::location(2)]] float3 Normal : NORMAL0;
};
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float2 UV : TEXCOORD0;
[[vk::location(1)]] float3 Normal : NORMAL0;
[[vk::location(2)]] float3 ViewVec : TEXCOORD1;
[[vk::location(3)]] float3 LightVec : TEXCOORD2;
};
struct UBO
{
float4x4 projection;
float4x4 modelview;
float4 lightPos;
};
cbuffer ubo : register(b0)
{
UBO ubo;
};
VSOutput main (VSInput input)
{
VSOutput output = (VSOutput)0;
output.UV = input.UV;
output.Normal = input.Normal.xyz;
float4 eyePos = mul(ubo.modelview, float4(input.Pos.x, input.Pos.y, input.Pos.z, 1.0));
output.Pos = mul(ubo.projection, eyePos);
float4 pos = float4(input.Pos, 1.0);
float3 lPos = ubo.lightPos.xyz;
output.LightVec = lPos - pos.xyz;
output.ViewVec = -pos.xyz;
return output;
}

Binary file not shown.

View file

@ -0,0 +1,20 @@
// Copyright 2020 Google LLC
struct VSOutput
{
[[vk::location(0)]] float3 Normal : NORMAL0;
[[vk::location(1)]] float3 ViewVec : TEXCOORD0;
[[vk::location(2)]] float3 LightVec : TEXCOORD1;
};
float4 main (VSOutput input) : SV_TARGET
{
float3 color = float3(0.5, 0.5, 0.5);
float3 N = normalize(input.Normal);
float3 L = normalize(input.LightVec);
float3 V = normalize(input.ViewVec);
float3 R = reflect(-L, N);
float3 diffuse = max(dot(N, L), 0.15);
float3 specular = pow(max(dot(R, V), 0.0), 32.0);
return float4(diffuse * color.rgb + specular, 1.0);
}

Binary file not shown.

View file

@ -0,0 +1,41 @@
// Copyright 2020 Google LLC
struct VSInput
{
[[vk::location(0)]]float3 Pos : POSITION0;
[[vk::location(2)]]float3 Normal : NORMAL0;
};
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float3 Normal : NORMAL0;
[[vk::location(1)]] float3 ViewVec : TEXCOORD0;
[[vk::location(2)]] float3 LightVec : TEXCOORD1;
};
struct UBO
{
float4x4 projection;
float4x4 modelview;
float4 lightPos;
};
cbuffer ubo : register(b0)
{
UBO ubo;
};
VSOutput main (VSInput input)
{
VSOutput output = (VSOutput)0;
float4 eyePos = mul(ubo.modelview, float4(input.Pos.x, input.Pos.y, input.Pos.z, 1.0));
output.Pos = mul(ubo.projection, eyePos);
float4 pos = float4(input.Pos, 1.0);
float3 lPos = ubo.lightPos.xyz;
output.LightVec = lPos - pos.xyz;
output.ViewVec = -pos.xyz;
output.Normal = input.Normal;
return output;
}

Binary file not shown.

View file

@ -0,0 +1,115 @@
// Copyright 2020 Google LLC
#define MAX_LOD_LEVEL_COUNT 6
[[vk::constant_id(0)]] const int MAX_LOD_LEVEL = 5;
struct InstanceData
{
float3 pos;
float scale;
};
StructuredBuffer<InstanceData> instances : register(t0);
// Same layout as VkDrawIndexedIndirectCommand
struct IndexedIndirectCommand
{
uint indexCount;
uint instanceCount;
uint firstIndex;
uint vertexOffset;
uint firstInstance;
};
RWStructuredBuffer<IndexedIndirectCommand> indirectDraws : register(u1);
// Binding 2: Uniform block object with matrices
struct UBO
{
float4x4 projection;
float4x4 modelview;
float4 cameraPos;
float4 frustumPlanes[6];
};
cbuffer ubo : register(b2) { UBO ubo; }
// Binding 3: Indirect draw stats
struct UBOOut
{
uint drawCount;
uint lodCount[MAX_LOD_LEVEL_COUNT];
};
RWStructuredBuffer<UBOOut> uboOut : register(u3);
// Binding 4: level-of-detail information
struct LOD
{
uint firstIndex;
uint indexCount;
float distance;
float _pad0;
};
StructuredBuffer<LOD> lods : register(t4);
[numthreads(16, 1, 1)]
bool frustumCheck(float4 pos, float radius)
{
// Check sphere against frustum planes
for (int i = 0; i < 6; i++)
{
if (dot(pos, ubo.frustumPlanes[i]) + radius < 0.0)
{
return false;
}
}
return true;
}
[numthreads(16, 1, 1)]
void main(uint3 GlobalInvocationID : SV_DispatchThreadID )
{
uint idx = GlobalInvocationID.x;
uint temp;
// Clear stats on first invocation
if (idx == 0)
{
InterlockedExchange(uboOut[0].drawCount, 0, temp);
for (uint i = 0; i < MAX_LOD_LEVEL + 1; i++)
{
InterlockedExchange(uboOut[0].lodCount[i], 0, temp);
}
}
float4 pos = float4(instances[idx].pos.xyz, 1.0);
// Check if object is within current viewing frustum
if (frustumCheck(pos, 1.0))
{
indirectDraws[idx].instanceCount = 1;
// Increase number of indirect draw counts
InterlockedAdd(uboOut[0].drawCount, 1, temp);
// Select appropriate LOD level based on distance to camera
uint lodLevel = MAX_LOD_LEVEL;
for (uint i = 0; i < MAX_LOD_LEVEL; i++)
{
if (distance(instances[idx].pos.xyz, ubo.cameraPos.xyz) < lods[i].distance)
{
lodLevel = i;
break;
}
}
indirectDraws[idx].firstIndex = lods[lodLevel].firstIndex;
indirectDraws[idx].indexCount = lods[lodLevel].indexCount;
// Update stats
InterlockedAdd(uboOut[0].lodCount[lodLevel], 1, temp);
}
else
{
indirectDraws[idx].instanceCount = 0;
}
}

Binary file not shown.

View file

@ -0,0 +1,18 @@
// Copyright 2020 Google LLC
struct VSOutput
{
[[vk::location(0)]] float3 Normal : NORMAL0;
[[vk::location(1)]] float3 Color : COLOR0;
[[vk::location(2)]] float3 ViewVec : TEXCOORD1;
[[vk::location(3)]] float3 LightVec : TEXCOORD2;
};
float4 main(VSOutput input) : SV_TARGET
{
float3 N = normalize(input.Normal);
float3 L = normalize(input.LightVec);
float3 ambient = float3(0.25, 0.25, 0.25);
float3 diffuse = max(dot(N, L), 0.0).xxx;
return float4((ambient + diffuse) * input.Color, 1.0);
}

Binary file not shown.

View file

@ -0,0 +1,46 @@
// Copyright 2020 Google LLC
struct VSInput
{
[[vk::location(0)]] float4 Pos : POSITION0;
[[vk::location(1)]] float3 Normal : NORMAL0;
[[vk::location(2)]] float3 Color : COLOR0;
// Instanced attributes
[[vk::location(4)]] float3 instancePos : TEXCOORD0;
[[vk::location(5)]] float instanceScale : TEXCOORD1;
};
struct UBO
{
float4x4 projection;
float4x4 modelview;
};
cbuffer ubo : register(b0) { UBO ubo; }
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float3 Normal : NORMAL0;
[[vk::location(1)]] float3 Color : COLOR0;
[[vk::location(2)]] float3 ViewVec : TEXCOORD1;
[[vk::location(3)]] float3 LightVec : TEXCOORD2;
};
VSOutput main(VSInput input)
{
VSOutput output = (VSOutput)0;
output.Color = input.Color;
output.Normal = input.Normal;
float4 pos = float4((input.Pos.xyz * input.instanceScale) + input.instancePos, 1.0);
output.Pos = mul(ubo.projection, mul(ubo.modelview, pos));
float4 wPos = mul(ubo.modelview, float4(pos.xyz, 1.0));
float4 lPos = float4(0.0, 10.0, 50.0, 1.0);
output.LightVec = lPos.xyz - pos.xyz;
output.ViewVec = -pos.xyz;
return output;
}

Binary file not shown.

View file

@ -0,0 +1,28 @@
// Copyright 2020 Google LLC
RWStructuredBuffer<uint> values : register(u0);
[[vk::constant_id(0)]] const uint BUFFER_ELEMENTS = 32;
uint fibonacci(uint n) {
if(n <= 1){
return n;
}
uint curr = 1;
uint prev = 1;
for(uint i = 2; i < n; ++i) {
uint temp = curr;
curr += prev;
prev = temp;
}
return curr;
}
[numthreads(1, 1, 1)]
void main(uint3 GlobalInvocationID : SV_DispatchThreadID)
{
uint index = GlobalInvocationID.x;
if (index >= BUFFER_ELEMENTS)
return;
values[index] = fibonacci(values[index]);
}

Binary file not shown.

View file

@ -0,0 +1,21 @@
// Copyright 2020 Google LLC
Texture2D textureColorMap : register(t0);
SamplerState samplerColorMap : register(s0);
Texture2D textureGradientRamp : register(t1);
SamplerState samplerGradientRamp : register(s1);
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float GradientPos : POSITION0;
[[vk::location(1)]] float2 CenterPos : POSITION1;
[[vk::location(2)]] float PointSize : TEXCOORD0;
};
float4 main (VSOutput input) : SV_TARGET
{
float3 color = textureGradientRamp.Sample(samplerGradientRamp, float2(input.GradientPos, 0.0)).rgb;
float2 PointCoord = (input.Pos.xy - input.CenterPos.xy) / input.PointSize + 0.5;
return float4(textureColorMap.Sample(samplerColorMap, PointCoord).rgb * color, 1);
}

Binary file not shown.

View file

@ -0,0 +1,41 @@
// Copyright 2020 Google LLC
struct VSInput
{
[[vk::location(0)]] float4 Pos : POSITION0;
[[vk::location(1)]] float4 Vel : TEXCOORD0;
};
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float GradientPos : POSITION0;
[[vk::location(1)]] float2 CenterPos : POSITION1;
[[vk::builtin("PointSize")]] float PSize : PSIZE;
[[vk::location(2)]] float PointSize : TEXCOORD0;
};
struct UBO
{
float4x4 projection;
float4x4 modelview;
float2 screendim;
};
cbuffer ubo : register(b2) { UBO ubo; }
VSOutput main (VSInput input)
{
VSOutput output = (VSOutput)0;
const float spriteSize = 0.005 * input.Pos.w; // Point size influenced by mass (stored in input.Pos.w);
float4 eyePos = mul(ubo.modelview, float4(input.Pos.x, input.Pos.y, input.Pos.z, 1.0));
float4 projectedCorner = mul(ubo.projection, float4(0.5 * spriteSize, 0.5 * spriteSize, eyePos.z, eyePos.w));
output.PSize = output.PointSize = clamp(ubo.screendim.x * projectedCorner.x / projectedCorner.w, 1.0, 128.0);
output.Pos = mul(ubo.projection, eyePos);
output.CenterPos = ((output.Pos.xy / output.Pos.w) + 1.0) * 0.5 * ubo.screendim;
output.GradientPos = input.Vel.w;
return output;
}

Binary file not shown.

View file

@ -0,0 +1,70 @@
// Copyright 2020 Google LLC
struct Particle
{
float4 pos;
float4 vel;
};
// Binding 0 : Position storage buffer
RWStructuredBuffer<Particle> particles : register(u0);
struct UBO
{
float deltaT;
int particleCount;
};
cbuffer ubo : register(b1) { UBO ubo; }
#define MAX_SHARED_DATA_SIZE 1024
[[vk::constant_id(0)]] const int SHARED_DATA_SIZE = 512;
[[vk::constant_id(1)]] const float GRAVITY = 0.002;
[[vk::constant_id(2)]] const float POWER = 0.75;
[[vk::constant_id(3)]] const float SOFTEN = 0.0075;
// Share data between computer shader invocations to speed up caluclations
groupshared float4 sharedData[MAX_SHARED_DATA_SIZE];
[numthreads(256, 1, 1)]
void main(uint3 GlobalInvocationID : SV_DispatchThreadID, uint3 LocalInvocationID : SV_GroupThreadID)
{
// Current SSBO index
uint index = GlobalInvocationID.x;
if (index >= ubo.particleCount)
return;
float4 position = particles[index].pos;
float4 velocity = particles[index].vel;
float4 acceleration = float4(0, 0, 0, 0);
for (int i = 0; i < ubo.particleCount; i += SHARED_DATA_SIZE)
{
if (i + LocalInvocationID.x < ubo.particleCount)
{
sharedData[LocalInvocationID.x] = particles[i + LocalInvocationID.x].pos;
}
else
{
sharedData[LocalInvocationID.x] = float4(0, 0, 0, 0);
}
GroupMemoryBarrierWithGroupSync();
for (int j = 0; j < 256; j++)
{
float4 other = sharedData[j];
float3 len = other.xyz - position.xyz;
acceleration.xyz += GRAVITY * len * other.w / pow(dot(len, len) + SOFTEN, POWER);
}
GroupMemoryBarrierWithGroupSync();
}
particles[index].vel.xyz += ubo.deltaT * acceleration.xyz;
// Gradient texture position
particles[index].vel.w += 0.1 * ubo.deltaT;
if (particles[index].vel.w > 1.0)
particles[index].vel.w -= 1.0;
}

Binary file not shown.

View file

@ -0,0 +1,28 @@
// Copyright 2020 Google LLC
struct Particle
{
float4 pos;
float4 vel;
};
// Binding 0 : Position storage buffer
RWStructuredBuffer<Particle> particles : register(u0);
struct UBO
{
float deltaT;
int particleCount;
};
cbuffer ubo : register(b1) { UBO ubo; }
[numthreads(256, 1, 1)]
void main(uint3 GlobalInvocationID : SV_DispatchThreadID)
{
int index = int(GlobalInvocationID.x);
float4 position = particles[index].pos;
float4 velocity = particles[index].vel;
position += ubo.deltaT * velocity;
particles[index].pos = position;
}

Binary file not shown.

View file

@ -0,0 +1,74 @@
// Copyright 2020 Google LLC
struct Particle
{
float2 pos;
float2 vel;
float4 gradientPos;
};
// Binding 0 : Position storage buffer
RWStructuredBuffer<Particle> particles : register(u0);
struct UBO
{
float deltaT;
float destX;
float destY;
int particleCount;
};
cbuffer ubo : register(b1) { UBO ubo; }
float2 attraction(float2 pos, float2 attractPos)
{
float2 delta = attractPos - pos;
const float damp = 0.5;
float dDampedDot = dot(delta, delta) + damp;
float invDist = 1.0f / sqrt(dDampedDot);
float invDistCubed = invDist*invDist*invDist;
return delta * invDistCubed * 0.0035;
}
float2 repulsion(float2 pos, float2 attractPos)
{
float2 delta = attractPos - pos;
float targetDistance = sqrt(dot(delta, delta));
return delta * (1.0 / (targetDistance * targetDistance * targetDistance)) * -0.000035;
}
[numthreads(256, 1, 1)]
void main(uint3 GlobalInvocationID : SV_DispatchThreadID)
{
// Current SSBO index
uint index = GlobalInvocationID.x;
// Don't try to write beyond particle count
if (index >= ubo.particleCount)
return;
// Read position and velocity
float2 vVel = particles[index].vel.xy;
float2 vPos = particles[index].pos.xy;
float2 destPos = float2(ubo.destX, ubo.destY);
float2 delta = destPos - vPos;
float targetDistance = sqrt(dot(delta, delta));
vVel += repulsion(vPos, destPos.xy) * 0.05;
// Move by velocity
vPos += vVel * ubo.deltaT;
// collide with boundary
if ((vPos.x < -1.0) || (vPos.x > 1.0) || (vPos.y < -1.0) || (vPos.y > 1.0))
vVel = (-vVel * 0.1) + attraction(vPos, destPos) * 12;
else
particles[index].pos.xy = vPos;
// Write back
particles[index].vel.xy = vVel;
particles[index].gradientPos.x += 0.02 * ubo.deltaT;
if (particles[index].gradientPos.x > 1.0)
particles[index].gradientPos.x -= 1.0;
}

Binary file not shown.

View file

@ -0,0 +1,22 @@
// Copyright 2020 Google LLC
Texture2D textureColorMap : register(t0);
SamplerState samplerColorMap : register(s0);
Texture2D textureGradientRamp : register(t1);
SamplerState samplerGradientRamp : register(s1);
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float4 Color : COLOR0;
[[vk::location(1)]] float GradientPos : POSITION0;
[[vk::location(2)]] float2 CenterPos : POSITION1;
[[vk::location(3)]] float PointSize : TEXCOORD0;
};
float4 main (VSOutput input) : SV_TARGET
{
float3 color = textureGradientRamp.Sample(samplerGradientRamp, float2(input.GradientPos, 0.0)).rgb;
float2 PointCoord = (input.Pos.xy - input.CenterPos.xy) / input.PointSize + 0.5;
return float4(textureColorMap.Sample(samplerColorMap, PointCoord).rgb * color, 1.0);
}

Binary file not shown.

View file

@ -0,0 +1,35 @@
// Copyright 2020 Google LLC
struct VSInput
{
[[vk::location(0)]] float2 Pos : POSITION0;
[[vk::location(1)]] float4 GradientPos : POSITION1;
};
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::builtin("PointSize")]] float PSize : PSIZE;
[[vk::location(0)]] float4 Color : COLOR0;
[[vk::location(1)]] float GradientPos : POSITION0;
[[vk::location(2)]] float2 CenterPos : POSITION1;
[[vk::location(3)]] float PointSize : TEXCOORD0;
};
struct PushConsts
{
float2 screendim;
};
[[vk::push_constant]] PushConsts pushConstants;
VSOutput main (VSInput input)
{
VSOutput output = (VSOutput)0;
output.PSize = output.PointSize = 8.0;
output.Color = float4(0.035, 0.035, 0.035, 0.035);
output.GradientPos = input.GradientPos.x;
output.Pos = float4(input.Pos.xy, 1.0, 1.0);
output.CenterPos = ((output.Pos.xy / output.Pos.w) + 1.0) * 0.5 * pushConstants.screendim;
return output;
}

Binary file not shown.

View file

@ -0,0 +1,272 @@
// Copyright 2020 Google LLC
// Shader is looseley based on the ray tracing coding session by Inigo Quilez (www.iquilezles.org)
RWTexture2D<float4> resultImage : register(u0);
#define EPSILON 0.0001
#define MAXLEN 1000.0
#define SHADOW 0.5
#define RAYBOUNCES 2
#define REFLECTIONS true
#define REFLECTIONSTRENGTH 0.4
#define REFLECTIONFALLOFF 0.5
struct Camera
{
float3 pos;
float3 lookat;
float fov;
};
struct UBO
{
float3 lightPos;
float aspectRatio;
float4 fogColor;
Camera camera;
float4x4 rotMat;
};
cbuffer ubo : register(b1) { UBO ubo; }
struct Sphere
{
float3 pos;
float radius;
float3 diffuse;
float specular;
int id;
};
struct Plane
{
float3 normal;
float distance;
float3 diffuse;
float specular;
int id;
};
StructuredBuffer<Sphere> spheres : register(t2);
StructuredBuffer<Plane> planes : register(t3);
void reflectRay(inout float3 rayD, in float3 mormal)
{
rayD = rayD + 2.0 * -dot(mormal, rayD) * mormal;
}
// Lighting =========================================================
float lightDiffuse(float3 normal, float3 lightDir)
{
return clamp(dot(normal, lightDir), 0.1, 1.0);
}
float lightSpecular(float3 normal, float3 lightDir, float specularFactor)
{
float3 viewVec = normalize(ubo.camera.pos);
float3 halfVec = normalize(lightDir + viewVec);
return pow(clamp(dot(normal, halfVec), 0.0, 1.0), specularFactor);
}
// Sphere ===========================================================
float sphereIntersect(in float3 rayO, in float3 rayD, in Sphere sphere)
{
float3 oc = rayO - sphere.pos;
float b = 2.0 * dot(oc, rayD);
float c = dot(oc, oc) - sphere.radius*sphere.radius;
float h = b*b - 4.0*c;
if (h < 0.0)
{
return -1.0;
}
float t = (-b - sqrt(h)) / 2.0;
return t;
}
float3 sphereNormal(in float3 pos, in Sphere sphere)
{
return (pos - sphere.pos) / sphere.radius;
}
// Plane ===========================================================
float planeIntersect(float3 rayO, float3 rayD, Plane plane)
{
float d = dot(rayD, plane.normal);
if (d == 0.0)
return 0.0;
float t = -(plane.distance + dot(rayO, plane.normal)) / d;
if (t < 0.0)
return 0.0;
return t;
}
int intersect(in float3 rayO, in float3 rayD, inout float resT)
{
int id = -1;
uint spheresLength;
uint spheresStride;
spheres.GetDimensions(spheresLength, spheresStride);
int i;
for (i = 0; i < spheresLength; i++)
{
float tSphere = sphereIntersect(rayO, rayD, spheres[i]);
if ((tSphere > EPSILON) && (tSphere < resT))
{
id = spheres[i].id;
resT = tSphere;
}
}
uint planesLength;
uint planesStride;
planes.GetDimensions(planesLength, planesStride);
for (i = 0; i < planesLength; i++)
{
float tplane = planeIntersect(rayO, rayD, planes[i]);
if ((tplane > EPSILON) && (tplane < resT))
{
id = planes[i].id;
resT = tplane;
}
}
return id;
}
float calcShadow(in float3 rayO, in float3 rayD, in int objectId, inout float t)
{
uint spheresLength;
uint spheresStride;
spheres.GetDimensions(spheresLength, spheresStride);
for (int i = 0; i < spheresLength; i++)
{
if (spheres[i].id == objectId)
continue;
float tSphere = sphereIntersect(rayO, rayD, spheres[i]);
if ((tSphere > EPSILON) && (tSphere < t))
{
t = tSphere;
return SHADOW;
}
}
return 1.0;
}
float3 fog(in float t, in float3 color)
{
return lerp(color, ubo.fogColor.rgb, clamp(sqrt(t*t)/20.0, 0.0, 1.0));
}
float3 renderScene(inout float3 rayO, inout float3 rayD, inout int id)
{
float3 color = float3(0, 0, 0);
float t = MAXLEN;
// Get intersected object ID
int objectID = intersect(rayO, rayD, t);
if (objectID == -1)
{
return color;
}
float3 pos = rayO + t * rayD;
float3 lightVec = normalize(ubo.lightPos - pos);
float3 normal;
// Planes
// Spheres
uint planesLength;
uint planesStride;
planes.GetDimensions(planesLength, planesStride);
int i;
for (i = 0; i < planesLength; i++)
{
if (objectID == planes[i].id)
{
normal = planes[i].normal;
float diffuse = lightDiffuse(normal, lightVec);
float specular = lightSpecular(normal, lightVec, planes[i].specular);
color = diffuse * planes[i].diffuse + specular;
}
}
uint spheresLength;
uint spheresStride;
spheres.GetDimensions(spheresLength, spheresStride);
for (i = 0; i < spheresLength; i++)
{
if (objectID == spheres[i].id)
{
normal = sphereNormal(pos, spheres[i]);
float diffuse = lightDiffuse(normal, lightVec);
float specular = lightSpecular(normal, lightVec, spheres[i].specular);
color = diffuse * spheres[i].diffuse + specular;
}
}
if (id == -1)
return color;
id = objectID;
// Shadows
t = length(ubo.lightPos - pos);
color *= calcShadow(pos, lightVec, id, t);
// Fog
color = fog(t, color);
// Reflect ray for next render pass
reflectRay(rayD, normal);
rayO = pos;
return color;
}
[numthreads(16, 16, 1)]
void main(uint3 GlobalInvocationID : SV_DispatchThreadID)
{
int2 dim;
resultImage.GetDimensions(dim.x, dim.y);
float2 uv = float2(GlobalInvocationID.xy) / dim;
float3 rayO = ubo.camera.pos;
float3 rayD = normalize(float3((-1.0 + 2.0 * uv) * float2(ubo.aspectRatio, 1.0), -1.0));
// Basic color path
int id = 0;
float3 finalColor = renderScene(rayO, rayD, id);
// Reflection
if (REFLECTIONS)
{
float reflectionStrength = REFLECTIONSTRENGTH;
for (int i = 0; i < RAYBOUNCES; i++)
{
float3 reflectionColor = renderScene(rayO, rayD, id);
finalColor = (1.0 - reflectionStrength) * finalColor + reflectionStrength * lerp(reflectionColor, finalColor, 1.0 - reflectionStrength);
reflectionStrength *= REFLECTIONFALLOFF;
}
}
resultImage[int2(GlobalInvocationID.xy)] = float4(finalColor, 0.0);
}

Binary file not shown.

View file

@ -0,0 +1,9 @@
// Copyright 2020 Google LLC
Texture2D textureColor : register(t0);
SamplerState samplerColor : register(s0);
float4 main([[vk::location(0)]] float2 inUV : TEXCOORD0) : SV_TARGET
{
return textureColor.Sample(samplerColor, float2(inUV.x, 1.0 - inUV.y));
}

Binary file not shown.

View file

@ -0,0 +1,15 @@
// Copyright 2020 Google LLC
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float2 UV : TEXCOORD0;
};
VSOutput main(uint VertexIndex : SV_VertexID)
{
VSOutput output = (VSOutput)0;
output.UV = float2((VertexIndex << 1) & 2, VertexIndex & 2);
output.Pos = float4(output.UV * 2.0f + -1.0f, 0.0f, 1.0f);
return output;
}

Binary file not shown.

View file

@ -0,0 +1,40 @@
// Copyright 2020 Google LLC
Texture2D inputImage : register(t0);
RWTexture2D<float4> resultImage : register(u1);
float conv(float kernel[9], in float data[9], in float denom, in float offset)
{
float res = 0.0;
for (int i=0; i<9; ++i)
{
res += kernel[i] * data[i];
}
return saturate(res/denom + offset);
}
[numthreads(16, 16, 1)]
void main(uint3 GlobalInvocationID : SV_DispatchThreadID)
{
float imageData[9];
// Fetch neighbouring texels
int n = -1;
for (int i=-1; i<2; ++i)
{
for(int j=-1; j<2; ++j)
{
n++;
float3 rgb = inputImage[uint2(GlobalInvocationID.x + i, GlobalInvocationID.y + j)].rgb;
imageData[n] = (rgb.r + rgb.g + rgb.b) / 3.0;
}
}
float kernel[9];
kernel[0] = -1.0/8.0; kernel[1] = -1.0/8.0; kernel[2] = -1.0/8.0;
kernel[3] = -1.0/8.0; kernel[4] = 1.0; kernel[5] = -1.0/8.0;
kernel[6] = -1.0/8.0; kernel[7] = -1.0/8.0; kernel[8] = -1.0/8.0;
float4 res = float4(conv(kernel, imageData, 0.1, 0.0).xxx, 1.0);
resultImage[int2(GlobalInvocationID.xy)] = res;
}

Binary file not shown.

View file

@ -0,0 +1,40 @@
// Copyright 2020 Google LLC
Texture2D inputImage : register(t0);
RWTexture2D<float4> resultImage : register(u1);
float conv(in float kernel[9], in float data[9], in float denom, in float offset)
{
float res = 0.0;
for (int i=0; i<9; ++i)
{
res += kernel[i] * data[i];
}
return saturate(res/denom + offset);
}
[numthreads(16, 16, 1)]
void main(uint3 GlobalInvocationID : SV_DispatchThreadID)
{
float imageData[9];
// Fetch neighbouring texels
int n = -1;
for (int i=-1; i<2; ++i)
{
for(int j=-1; j<2; ++j)
{
n++;
float3 rgb = inputImage[uint2(GlobalInvocationID.x + i, GlobalInvocationID.y + j)].rgb;
imageData[n] = (rgb.r + rgb.g + rgb.b) / 3.0;
}
}
float kernel[9];
kernel[0] = -1.0; kernel[1] = 0.0; kernel[2] = 0.0;
kernel[3] = 0.0; kernel[4] = -1.0; kernel[5] = 0.0;
kernel[6] = 0.0; kernel[7] = 0.0; kernel[8] = 2.0;
float4 res = float4(conv(kernel, imageData, 1.0, 0.50).xxx, 1.0);
resultImage[int2(GlobalInvocationID.xy)] = res;
}

Binary file not shown.

View file

@ -0,0 +1,49 @@
// Copyright 2020 Google LLC
Texture2D inputImage : register(t0);
RWTexture2D<float4> resultImage : register(u1);
float conv(in float kernel[9], in float data[9], in float denom, in float offset)
{
float res = 0.0;
for (int i=0; i<9; ++i)
{
res += kernel[i] * data[i];
}
return saturate(res/denom + offset);
}
[numthreads(16, 16, 1)]
void main(uint3 GlobalInvocationID : SV_DispatchThreadID)
{
float r[9];
float g[9];
float b[9];
// Fetch neighbouring texels
int n = -1;
for (int i=-1; i<2; ++i)
{
for(int j=-1; j<2; ++j)
{
n++;
float3 rgb = inputImage[uint2(GlobalInvocationID.x + i, GlobalInvocationID.y + j)].rgb;
r[n] = rgb.r;
g[n] = rgb.g;
b[n] = rgb.b;
}
}
float kernel[9];
kernel[0] = -1.0; kernel[1] = -1.0; kernel[2] = -1.0;
kernel[3] = -1.0; kernel[4] = 9.0; kernel[5] = -1.0;
kernel[6] = -1.0; kernel[7] = -1.0; kernel[8] = -1.0;
float4 res = float4(
conv(kernel, r, 1.0, 0.0),
conv(kernel, g, 1.0, 0.0),
conv(kernel, b, 1.0, 0.0),
1.0);
resultImage[int2(GlobalInvocationID.xy)] = res;
}

Binary file not shown.

View file

@ -0,0 +1,9 @@
// Copyright 2020 Google LLC
Texture2D textureColor : register(t1);
SamplerState samplerColor : register(s1);
float4 main([[vk::location(0)]] float2 inUV : TEXCOORD0) : SV_TARGET
{
return textureColor.Sample(samplerColor, inUV);
}

Binary file not shown.

View file

@ -0,0 +1,29 @@
// Copyright 2020 Google LLC
struct VSInput
{
[[vk::location(0)]] float3 Pos : POSITION0;
[[vk::location(1)]] float2 UV : TEXCOORD0;
};
struct UBO
{
float4x4 projection;
float4x4 model;
};
cbuffer ubo : register(b0) { UBO ubo; }
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float2 UV : TEXCOORD0;
};
VSOutput main(VSInput input)
{
VSOutput output = (VSOutput)0;
output.UV = input.UV;
output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos.xyz, 1.0)));
return output;
}

Binary file not shown.

View file

@ -0,0 +1,21 @@
// Copyright 2020 Google LLC
struct VSOutput
{
[[vk::location(0)]] float3 Normal : NORMAL0;
[[vk::location(1)]] float3 Color : COLOR0;
[[vk::location(2)]] float3 ViewVec : TEXCOORD1;
[[vk::location(3)]] float3 LightVec : TEXCOORD2;
};
float4 main(VSOutput input) : SV_TARGET
{
float3 N = normalize(input.Normal);
float3 L = normalize(input.LightVec);
float3 V = normalize(input.ViewVec);
float3 R = reflect(-L, N);
float3 ambient = float3(0.1, 0.1, 0.1);
float3 diffuse = max(dot(N, L), 0.0) * float3(1.0, 1.0, 1.0);
float3 specular = pow(max(dot(R, V), 0.0), 16.0) * float3(0.75, 0.75, 0.75);
return float4((ambient + diffuse) * input.Color.rgb + specular, 1.0);
}

Binary file not shown.

View file

@ -0,0 +1,57 @@
// Copyright 2020 Google LLC
struct VSInput
{
[[vk::location(0)]] float3 Pos : POSITION0;
[[vk::location(1)]] float3 Normal : NORMAL0;
[[vk::location(2)]] float3 Color : COLOR0;
};
struct UBO
{
float4x4 projection;
float4x4 view;
float4x4 model;
};
cbuffer ubo : register(b0) { UBO ubo; }
struct Node
{
float4x4 transform;
};
cbuffer NodeBuf : register(b0, space1) { Node node; }
struct PushConstant
{
float4 baseColorFactor;
};
[[vk::push_constant]] PushConstant material;
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float3 Normal : NORMAL0;
[[vk::location(1)]] float3 Color : COLOR0;
[[vk::location(2)]] float3 ViewVec : TEXCOORD1;
[[vk::location(3)]] float3 LightVec : TEXCOORD2;
};
VSOutput main(VSInput input)
{
VSOutput output = (VSOutput)0;
output.Normal = input.Normal;
output.Color = material.baseColorFactor.rgb;
float4 pos = float4(input.Pos, 1.0);
output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, mul(node.transform, pos))));
output.Normal = mul((float4x3)mul(ubo.view, mul(ubo.model, node.transform)), input.Normal).xyz;
float4 localpos = mul(ubo.view, mul(ubo.model, mul(node.transform, pos)));
float3 lightPos = float3(10.0f, -10.0f, 10.0f);
output.LightVec = lightPos.xyz - localpos.xyz;
output.ViewVec = -localpos.xyz;
return output;
}

Binary file not shown.

View file

@ -0,0 +1,9 @@
// Copyright 2020 Google LLC
Texture2D textureColor : register(t1);
SamplerState samplerColor : register(s1);
float4 main([[vk::location(0)]] float2 inUV : TEXCOORD0) : SV_TARGET
{
return textureColor.Sample(samplerColor, inUV);
}

Binary file not shown.

View file

@ -0,0 +1,15 @@
// Copyright 2020 Google LLC
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float2 UV : TEXCOORD0;
};
VSOutput main(uint VertexIndex : SV_VertexID)
{
VSOutput output = (VSOutput)0;
output.UV = float2((VertexIndex << 1) & 2, VertexIndex & 2);
output.Pos = float4(output.UV * 2.0f - 1.0f, 0.0f, 1.0f);
return output;
}

Binary file not shown.

View file

@ -0,0 +1,6 @@
// Copyright 2020 Google LLC
float4 main([[vk::location(0)]] float3 Color : COLOR0) : SV_TARGET
{
return float4(Color, 1);
}

Binary file not shown.

View file

@ -0,0 +1,29 @@
// Copyright 2020 Google LLC
struct VSInput
{
[[vk::location(0)]] float3 Pos : POSITION0;
[[vk::location(1)]] float3 Color : COLOR0;
};
struct UBO
{
float4x4 projection;
float4x4 model;
};
cbuffer ubo : register(b0) { UBO ubo; }
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float3 Color : COLOR0;
};
VSOutput main(VSInput input)
{
VSOutput output = (VSOutput)0;
output.Color = input.Color;
output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos, 1.0)));
return output;
}

Binary file not shown.

View file

@ -0,0 +1,6 @@
// Copyright 2020 Google LLC
float4 main() : SV_TARGET
{
return float4(1.0, 1.0, 1.0, 1.0);
}

View file

@ -0,0 +1,6 @@
// Copyright 2020 Google LLC
float4 main([[vk::location(0)]] float3 Color : COLOR0) : SV_TARGET
{
return float4(Color, 1);
}

Binary file not shown.

View file

@ -0,0 +1,29 @@
// Copyright 2020 Google LLC
struct VSInput
{
[[vk::location(0)]] float4 Pos : POSITION0;
[[vk::location(3)]] float3 Color : COLOR0;
};
struct UBO
{
float4x4 projection;
float4x4 model;
};
cbuffer ubo : register(b0) { UBO ubo; }
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float3 Color : COLOR0;
};
VSOutput main(VSInput input)
{
VSOutput output = (VSOutput)0;
output.Color = input.Color;
output.Pos = mul(ubo.projection, mul(ubo.model, input.Pos));
return output;
}

Binary file not shown.

View file

@ -0,0 +1,36 @@
// Copyright 2020 Google LLC
Texture2D textureColor : register(t1);
SamplerState samplerColor : register(s1);
float4 main([[vk::location(0)]] float2 inUV : TEXCOORD0) : SV_TARGET
{
// Single pass gauss blur
const float2 texOffset = float2(0.01, 0.01);
float2 tc0 = inUV + float2(-texOffset.x, -texOffset.y);
float2 tc1 = inUV + float2( 0.0, -texOffset.y);
float2 tc2 = inUV + float2(+texOffset.x, -texOffset.y);
float2 tc3 = inUV + float2(-texOffset.x, 0.0);
float2 tc4 = inUV + float2( 0.0, 0.0);
float2 tc5 = inUV + float2(+texOffset.x, 0.0);
float2 tc6 = inUV + float2(-texOffset.x, +texOffset.y);
float2 tc7 = inUV + float2( 0.0, +texOffset.y);
float2 tc8 = inUV + float2(+texOffset.x, +texOffset.y);
float4 col0 = textureColor.Sample(samplerColor, tc0);
float4 col1 = textureColor.Sample(samplerColor, tc1);
float4 col2 = textureColor.Sample(samplerColor, tc2);
float4 col3 = textureColor.Sample(samplerColor, tc3);
float4 col4 = textureColor.Sample(samplerColor, tc4);
float4 col5 = textureColor.Sample(samplerColor, tc5);
float4 col6 = textureColor.Sample(samplerColor, tc6);
float4 col7 = textureColor.Sample(samplerColor, tc7);
float4 col8 = textureColor.Sample(samplerColor, tc8);
float4 sum = (1.0 * col0 + 2.0 * col1 + 1.0 * col2 +
2.0 * col3 + 4.0 * col4 + 2.0 * col5 +
1.0 * col6 + 2.0 * col7 + 1.0 * col8) / 16.0;
return float4(sum.rgb, 1.0);
}

Binary file not shown.

View file

@ -0,0 +1,15 @@
// Copyright 2020 Google LLC
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float2 UV : TEXCOORD0;
};
VSOutput main(uint VertexIndex : SV_VertexID)
{
VSOutput output = (VSOutput)0;
output.UV = float2((VertexIndex << 1) & 2, VertexIndex & 2);
output.Pos = float4(output.UV * float2(2.0f, 2.0f) + float2(-1.0f, -1.0f), 0.0f, 1.0f);
return output;
}

Binary file not shown.

View file

@ -0,0 +1,37 @@
// Copyright 2020 Google LLC
Texture2D textureColorMap : register(t1);
SamplerState samplerColorMap : register(s1);
struct VSOutput
{
[[vk::location(0)]] float3 Normal : NORMAL0;
[[vk::location(1)]] float3 Color : COLOR0;
[[vk::location(2)]] float2 UV : TEXCOORD0;
[[vk::location(3)]] float3 ViewVec : TEXCOORD1;
[[vk::location(4)]] float3 LightVec : TEXCOORD2;
};
float4 main(VSOutput input) : SV_TARGET
{
// Desaturate color
float3 color = float3(lerp(input.Color, dot(float3(0.2126,0.7152,0.0722), input.Color).xxx, 0.65));
// High ambient colors because mesh materials are pretty dark
float3 ambient = color * float3(1.0, 1.0, 1.0);
float3 N = normalize(input.Normal);
float3 L = normalize(input.LightVec);
float3 V = normalize(input.ViewVec);
float3 R = reflect(-L, N);
float3 diffuse = max(dot(N, L), 0.0) * color;
float3 specular = pow(max(dot(R, V), 0.0), 16.0) * float3(0.75, 0.75, 0.75);
float intensity = dot(N,L);
float shade = 1.0;
shade = intensity < 0.5 ? 0.75 : shade;
shade = intensity < 0.35 ? 0.6 : shade;
shade = intensity < 0.25 ? 0.5 : shade;
shade = intensity < 0.1 ? 0.25 : shade;
return float4(input.Color * 3.0 * shade, 1);
}

Binary file not shown.

View file

@ -0,0 +1,44 @@
// Copyright 2020 Google LLC
struct VSInput
{
[[vk::location(0)]] float3 Pos : POSITION0;
[[vk::location(1)]] float3 Normal : NORMAL0;
[[vk::location(2)]] float2 UV : TEXCOORD0;
[[vk::location(3)]] float3 Color : COLOR0;
};
struct UBO
{
float4x4 projection;
float4x4 model;
float4 lightPos;
};
cbuffer ubo : register(b0) { UBO ubo; }
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float3 Normal : NORMAL0;
[[vk::location(1)]] float3 Color : COLOR0;
[[vk::location(2)]] float2 UV : TEXCOORD0;
[[vk::location(3)]] float3 ViewVec : TEXCOORD1;
[[vk::location(4)]] float3 LightVec : TEXCOORD2;
};
VSOutput main(VSInput input)
{
VSOutput output = (VSOutput)0;
output.Normal = input.Normal;
output.Color = input.Color;
output.UV = input.UV;
output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos.xyz, 1.0)));
float4 pos = mul(ubo.model, float4(input.Pos, 1.0));
output.Normal = mul((float4x3)ubo.model, input.Normal).xyz;
float3 lPos = mul((float4x3)ubo.model, ubo.lightPos.xyz).xyz;
output.LightVec = lPos - pos.xyz;
output.ViewVec = -pos.xyz;
return output;
}

Binary file not shown.

Some files were not shown because too many files have changed in this diff Show more