Skip to content

Commit

Permalink
[WIP] Add compute pipeline example
Browse files Browse the repository at this point in the history
  • Loading branch information
deccer committed Nov 27, 2022
1 parent 744a65e commit 997fdc1
Show file tree
Hide file tree
Showing 22 changed files with 591 additions and 0 deletions.
17 changes: 17 additions & 0 deletions EngineKit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Swapchain.Assets", "example
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Swapchain", "examples\Swapchain\Swapchain\Swapchain.csproj", "{429972C0-ECDF-4E78-BA31-567732C86AFA}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ComputeConvolution", "ComputeConvolution", "{7F49DA89-CC45-4D03-AC70-6D84A79AC134}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeConvolution.Assets", "examples\ComputeConvolution\ComputeConvolution.Assets\ComputeConvolution.Assets.csproj", "{B2CDF93A-7131-4D21-BDD6-2D9440857B2B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeConvolution", "examples\ComputeConvolution\ComputeConvolution\ComputeConvolution.csproj", "{EF7B69DA-38E5-4E0D-AE29-5CF5AE21CCBA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -49,6 +55,9 @@ Global
{29F56725-43B2-43C5-8675-E4FF7DC9FD65} = {1E1BD9CA-189A-45FD-8A36-A57C836C6BB6}
{C8063C91-B683-4189-A221-DF9835F7049C} = {29F56725-43B2-43C5-8675-E4FF7DC9FD65}
{429972C0-ECDF-4E78-BA31-567732C86AFA} = {29F56725-43B2-43C5-8675-E4FF7DC9FD65}
{7F49DA89-CC45-4D03-AC70-6D84A79AC134} = {1E1BD9CA-189A-45FD-8A36-A57C836C6BB6}
{B2CDF93A-7131-4D21-BDD6-2D9440857B2B} = {7F49DA89-CC45-4D03-AC70-6D84A79AC134}
{EF7B69DA-38E5-4E0D-AE29-5CF5AE21CCBA} = {7F49DA89-CC45-4D03-AC70-6D84A79AC134}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8E305337-228C-4930-B355-91F1E82C64C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -83,5 +92,13 @@ Global
{429972C0-ECDF-4E78-BA31-567732C86AFA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{429972C0-ECDF-4E78-BA31-567732C86AFA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{429972C0-ECDF-4E78-BA31-567732C86AFA}.Release|Any CPU.Build.0 = Release|Any CPU
{B2CDF93A-7131-4D21-BDD6-2D9440857B2B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B2CDF93A-7131-4D21-BDD6-2D9440857B2B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B2CDF93A-7131-4D21-BDD6-2D9440857B2B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B2CDF93A-7131-4D21-BDD6-2D9440857B2B}.Release|Any CPU.Build.0 = Release|Any CPU
{EF7B69DA-38E5-4E0D-AE29-5CF5AE21CCBA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EF7B69DA-38E5-4E0D-AE29-5CF5AE21CCBA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF7B69DA-38E5-4E0D-AE29-5CF5AE21CCBA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF7B69DA-38E5-4E0D-AE29-5CF5AE21CCBA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<None Update="**\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#version 460 core

layout(binding = 0, rgba8) readonly uniform imageCube u_environment;
layout(binding = 1, rgba16f) writeonly uniform imageCube u_irradiance;

const float PI = 3.14159265359;

vec3 cubeCoordToWorld(ivec3 cubeCoord, vec2 cubemapSize)
{
vec2 texCoord = vec2(cubeCoord.xy) / cubemapSize;
texCoord = texCoord * 2.0 - 1.0; // -1..1
switch(cubeCoord.z)
{
case 0: return vec3(1.0, -texCoord.yx); // posx
case 1: return vec3(-1.0, -texCoord.y, texCoord.x); //negx
case 2: return vec3(texCoord.x, 1.0, texCoord.y); // posy
case 3: return vec3(texCoord.x, -1.0, -texCoord.y); //negy
case 4: return vec3(texCoord.x, -texCoord.y, 1.0); // posz
case 5: return vec3(-texCoord.xy, -1.0); // negz
}
return vec3(0.0);
}

float max3(vec3 v)
{
return max(max(v.x, v.y), v.z);
}

ivec3 texCoordToCube(vec3 texCoord, vec2 cubemapSize)
{
vec3 abst = abs(texCoord);
texCoord /= max3(abst);

float cubeFace;
vec2 uvCoord;
if (abst.x > abst.y && abst.x > abst.z)
{
// x major
float negx = step(texCoord.x, 0.0);
uvCoord = mix(-texCoord.zy, vec2(texCoord.z, -texCoord.y), negx);
cubeFace = negx;
}
else if (abst.y > abst.z)
{
// y major
float negy = step(texCoord.y, 0.0);
uvCoord = mix(texCoord.xz, vec2(texCoord.x, -texCoord.z), negy);
cubeFace = 2.0 + negy;
}
else
{
// z major
float negz = step(texCoord.z, 0.0);
uvCoord = mix(vec2(texCoord.x, -texCoord.y), -texCoord.xy, negz);
cubeFace = 4.0 + negz;
}
uvCoord = (uvCoord + 1.0) * 0.5; // 0..1
uvCoord = uvCoord * cubemapSize;
uvCoord = clamp(uvCoord, vec2(0.0), cubemapSize - vec2(1.0));

return ivec3(ivec2(uvCoord), int(cubeFace));
}

layout (local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
void main()
{
vec2 cubeSize = imageSize(u_irradiance);
ivec3 cubeCoord = ivec3(gl_GlobalInvocationID);
if (any(lessThan(cubeCoord, ivec3(0))) || any(greaterThanEqual(cubeCoord.xy, cubeSize)))
{
return;
}

vec3 worldPos = cubeCoordToWorld(cubeCoord, cubeSize);
// tangent space from origin point
vec3 normal = normalize(worldPos);
vec3 up = vec3(0.0, 1.0, 0.0);
vec3 right = normalize(cross(up, normal));
up = cross(normal, right);

vec3 irradiance = vec3(0.0);

//float sampleDelta = 0.025;
float sampleDelta = 1.0;
float nrSamples = 0.0;

for (float phi = 0.0; phi < 2.0 * PI; phi += sampleDelta)
{
for (float theta = 0.0; theta < 0.5 * PI; theta += sampleDelta)
{
vec3 tangentSample = vec3(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta));
vec3 sampleVec = tangentSample.x * right + tangentSample.y * up + tangentSample.z * normal;
ivec3 sampleCoord = texCoordToCube(sampleVec, cubeSize);
irradiance += imageLoad(u_environment, sampleCoord).rgb * cos(theta) * sin(theta);
nrSamples++;
}
}

irradiance = PI * irradiance * (1.0 / float(nrSamples));
imageStore(u_irradiance, cubeCoord, vec4(irradiance, 1.0));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#version 460 core

layout (location = 0) out gl_PerVertex
{
vec4 gl_Position;
};
layout(location = 1) out vec2 fs_uv;

void main()
{
vec2 pos = vec2(gl_VertexID == 0, gl_VertexID == 2);
fs_uv = pos.xy * 2.0;
gl_Position = vec4(pos * 4.0 - 1.0, 0.0, 1.0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#version 460 core

layout(location = 0) in vec2 v_uv;

layout(location = 0) uniform sampler2D s_texture;

layout(location = 0) out vec4 o_color;

void main()
{
o_color = texture(s_texture, v_uv);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\EngineKit\EngineKit.csproj" />
<ProjectReference Include="..\ComputeConvolution.Assets\ComputeConvolution.Assets.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<Reference Include="Serilog">
<HintPath>..\..\..\..\..\..\..\..\.nuget\packages\serilog\2.12.0\lib\net6.0\Serilog.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
Loading

0 comments on commit 997fdc1

Please sign in to comment.