Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions atlas/application/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,9 @@ bool Window::stepFrame() {
}

if (!editorControlsRenderedInScenePass) {
if (this->usePathTracing) {
renderEditorGrid(commandBuffer);
}
renderEditorOverlays(commandBuffer);
}

Expand Down
110 changes: 81 additions & 29 deletions include/atlas/core/default_shaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -6398,7 +6398,8 @@ struct Material {

float transmittance;
float ior;
float _pad2[2];
float reflectivity;
float _pad2;
};

struct MeshData {
Expand Down Expand Up @@ -6475,7 +6476,12 @@ struct SceneData {
float indirectStrength;
float ambientIntensity;
uint materialTextureCount;
uint _pad0;
uint atmosphereEnabled;
float atmosphereSunSize;
float3 atmosphereSunDirection;
float atmosphereSunIntensity;
float3 atmosphereSunColor;
float _pad0;
};

float pow5(float x) {
Expand Down Expand Up @@ -6514,7 +6520,8 @@ uint seedBase(uint2 gid, uint w, uint frame, uint sampleIndex) {
constexpr sampler skyboxSampler(coord::normalized, address::clamp_to_edge,
filter::linear, mip_filter::linear);

float3 skyColor(float3 dir, float intensity, texturecube<float> skybox) {
float3 skyColor(float3 dir, float intensity, texturecube<float> skybox,
constant SceneData &sceneData) {
float3 sampleDir = dir;
float len2 = dot(sampleDir, sampleDir);
if (len2 > 1e-10) {
Expand All @@ -6523,6 +6530,33 @@ float3 skyColor(float3 dir, float intensity, texturecube<float> skybox) {
sampleDir = float3(0.0, 1.0, 0.0);
}
float3 sky = skybox.sample(skyboxSampler, sampleDir).xyz;
if (sceneData.atmosphereEnabled != 0 &&
sceneData.atmosphereSunDirection.y > -0.15) {
float3 sunDirection = sceneData.atmosphereSunDirection;
float sunDirectionLength = dot(sunDirection, sunDirection);
sunDirection = sunDirectionLength > 1e-10
? sunDirection * rsqrt(sunDirectionLength)
: float3(0.0, 1.0, 0.0);
float sunDot = dot(sampleDir, sunDirection);
float sizeAdjust =
1.0 - (sceneData.atmosphereSunSize - 1.0) * 0.001;
float sunSize = 0.9995 * sizeAdjust;
float sunGlowSize =
0.998 * (1.0 - (sceneData.atmosphereSunSize - 1.0) * 0.003);
float sunHaloSize =
0.99 * (1.0 - (sceneData.atmosphereSunSize - 1.0) * 0.015);
float sunDisk = smoothstep(sunSize - 0.0002, sunSize, sunDot);
float sunGlow =
smoothstep(sunGlowSize, sunSize, sunDot) * (1.0 - sunDisk);
float sunHalo = smoothstep(sunHaloSize, sunSize, sunDot) *
(1.0 - smoothstep(sunSize, sunGlowSize, sunDot));
float horizonFade = smoothstep(
-0.15, 0.05, sceneData.atmosphereSunDirection.y);
float sunIntensity = max(sceneData.atmosphereSunIntensity, 0.05);
sky += sceneData.atmosphereSunColor *
(sunDisk * 5.0 + sunGlow * 0.5 + sunHalo) * horizonFade *
sunIntensity;
}
float scale = intensity > 0.0 ? intensity : 1.0;
return sky * scale;
}
Expand Down Expand Up @@ -6587,7 +6621,8 @@ constexpr sampler materialTexSampler(coord::normalized, address::repeat,
texture2d<float> materialTexture32, \
texture2d<float> materialTexture33, \
texture2d<float> materialTexture34, \
texture2d<float> materialTexture35, \
texture2d<float> materialTexture35, )",
R"( \
texture2d<float> materialTexture36, \
texture2d<float> materialTexture37, \
texture2d<float> materialTexture38, \
Expand All @@ -6609,8 +6644,7 @@ constexpr sampler materialTexSampler(coord::normalized, address::repeat,
materialTexture16, materialTexture17, materialTexture18, \
materialTexture19, materialTexture20, materialTexture21, \
materialTexture22, materialTexture23, materialTexture24, \
materialTexture25, materi)",
R"(alTexture26, materialTexture27, \
materialTexture25, materialTexture26, materialTexture27, \
materialTexture28, materialTexture29, materialTexture30, \
materialTexture31, materialTexture32, materialTexture33, \
materialTexture34, materialTexture35, materialTexture36, \
Expand Down Expand Up @@ -6720,7 +6754,8 @@ float4 sampleMaterialTexture(int textureIndex, float2 uv,
return materialTexture22.sample(materialTexSampler, uv);
case 23:
return materialTexture23.sample(materialTexSampler, uv);
case 24:
case 24:)",
R"(
return materialTexture24.sample(materialTexSampler, uv);
case 25:
return materialTexture25.sample(materialTexSampler, uv);
Expand Down Expand Up @@ -6763,8 +6798,7 @@ float4 sampleMaterialTexture(int textureIndex, float2 uv,
case 44:
return materialTexture44.sample(materialTexSampler, uv);
case 45:
return m)",
R"(aterialTexture45.sample(materialTexSampler, uv);
return materialTexture45.sample(materialTexSampler, uv);
case 46:
return materialTexture46.sample(materialTexSampler, uv);
case 47:
Expand Down Expand Up @@ -6917,7 +6951,8 @@ bool isOccludedPointLight(PointLight light, float3 P, float3 N,
float3(r * cos(phi), r * sin(phi), z) * lightRadius;
float3 sampledLightPos = light.position + sphereOffset;

float3 toLight = sampledLightPos - P;
float3 toL)",
R"(ight = sampledLightPos - P;
float dist2 = max(dot(toLight, toLight), 0.001);
float dist = sqrt(dist2);
float3 L = toLight / dist;
Expand Down Expand Up @@ -6957,8 +6992,7 @@ bool isOccludedAreaLight(AreaLight light, float3 P, float3 N,
float dist2 = max(dot(toLight, toLight), 1e-4);
float dist = sqrt(dist2);
float3 L = toLight / dist;
return i)",
R"(sOccluded(isect, sceneAS, P, N, L, dist - 0.001);
return isOccluded(isect, sceneAS, P, N, L, dist - 0.001);
}

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -7102,7 +7136,8 @@ float3 evalDirectLightingPBR(intersector<triangle_data, instancing> isect,
float dist = max(length(toLight), 1e-4);
float3 L = toLight / dist;
float lightRange = max(pointLights[i].range, 1e-4);
float minDist = max(lightRange * 0.08, 0.15);
float minDist = max)",
R"((lightRange * 0.08, 0.15);
float distSq = dist * dist + minDist * minDist;
float rangeFade = 1.0 - smoothstep(lightRange * 0.75, lightRange, dist);
float atten = rangeFade / max(distSq, 1e-4);
Expand Down Expand Up @@ -7134,8 +7169,7 @@ float3 evalDirectLightingPBR(intersector<triangle_data, instancing> isect,
float lightRange = max(spotLights[i].range, 1e-4);
float minDist = max(lightRange * 0.08, 0.15);
float distSq = dist * dist + minDist * minDist;
float rangeFade = 1.0 - smoothstep(lightRang)",
R"(e * 0.75, lightRange, dist);
float rangeFade = 1.0 - smoothstep(lightRange * 0.75, lightRange, dist);
float atten = rangeFade / max(distSq, 1e-4);
float intensity = max(spotLights[i].intensity, 0.0) * atten * spot;
float3 c = evalPBR(albedo, metallic, roughness, N, V, L,
Expand Down Expand Up @@ -7218,7 +7252,7 @@ float3 sampleRadiance(uint2 gid, uint sampleIndex, uint w,

for (uint step = 0; step < 8; ++step) {
if (hit.type == intersection_type::none) {
return skyColor(surfaceRay.direction, 0.0, skybox);
return skyColor(surfaceRay.direction, 0.0, skybox, sceneData);
}

uint instanceIndex = hit.instance_id;
Expand Down Expand Up @@ -7274,10 +7308,11 @@ float3 sampleRadiance(uint2 gid, uint sampleIndex, uint w,
}

if (!foundOpaqueSurface) {
return skyColor(surfaceRay.direction, 0.0, skybox);
return skyColor(surfaceRay.direction, 0.0, skybox, sceneData);
}

float3 N = resolveShadingNormal(mat, texUV, localN, localT, localB, inst,
float3 N = resolveShadingNormal(mat, texUV, localN, localT, localB, inst)",
R"(,
sceneData.materialTextureCount,
PT_MATERIAL_TEXTURE_ARGS);
float3 P = surfaceRay.origin + surfaceRay.direction * hit.distance;
Expand All @@ -7296,6 +7331,7 @@ float3 sampleRadiance(uint2 gid, uint sampleIndex, uint w,
resolveMaterialParameters(mat, texUV, sceneData.materialTextureCount,
PT_MATERIAL_TEXTURE_ARGS, albedo, metallic,
roughness, ao, emissive, ior, transmittance);
float reflectivity = clamp(mat.reflectivity, 0.0, 1.0);
float sssStrength = clamp(1.0 - mat.albedo.w, 0.0, 1.0) * (1.0 - metallic);
float sssThickness = mix(0.25, 1.75, ao);

Expand All @@ -7307,7 +7343,9 @@ float3 sampleRadiance(uint2 gid, uint sampleIndex, uint w,
float3 indirect = float3(0.0);

if (sceneData.maxBounces > 0) {
float3 F0 = mix(float3(0.04), albedo, metallic);
float3 baseF0 = mix(float3(0.04), albedo, metallic);
float3 reflectedColor = mix(float3(1.0), albedo, metallic);
float3 F0 = mix(baseF0, reflectedColor, reflectivity);
float3 F_approx = F_Schlick(max(dot(N, V), 0.0), F0);

float dielectricF0 = pow((ior - 1.0) / (ior + 1.0), 2.0);
Expand All @@ -7317,8 +7355,10 @@ float3 sampleRadiance(uint2 gid, uint sampleIndex, uint w,
(1.0 - metallic) * dielectricSpec;
float transmitProb =
transmittance * (1.0 - metallic) * (1.0 - dielectricSpec);
float diffuseProb = (1.0 - metallic) * (1.0 - transmittance)",
R"();
float diffuseProb = (1.0 - metallic) * (1.0 - transmittance);
specProb = mix(specProb, 1.0, reflectivity);
transmitProb *= 1.0 - reflectivity;
diffuseProb *= 1.0 - reflectivity;
float probSum = max(specProb + transmitProb + diffuseProb, 1e-4);
specProb /= probSum;
transmitProb /= probSum;
Expand All @@ -7333,8 +7373,10 @@ R"();
float3 brdfWeight;
float chooseSplit = rand(rng);
bool choseTransmission = false;
bool choseSpecular = false;

if (specProb > 1e-4 && chooseSplit < specProb) {
choseSpecular = true;
float2 u = float2(rand(rng), rand(rng));
float3 localH = sampleGGX(u, max(roughness, 0.001));
float3 H_world = normalize(basis * localH);
Expand Down Expand Up @@ -7459,7 +7501,8 @@ R"();
bool enteringShell = dot(tN, tV) > 0.0;
float etaShell = enteringShell ? (1.0 / tIor) : tIor;
float3 faceNShell = enteringShell ? tN : -tN;
float3 refractShell = refract(-tV, faceNShell, etaShell);
float)",
R"(3 refractShell = refract(-tV, faceNShell, etaShell);
if (length(refractShell) < 1e-5) {
refractShell = reflect(-tV, faceNShell);
}
Expand All @@ -7474,9 +7517,14 @@ R"();
}

if (resolvedBounceHit.type == intersection_type::none) {
float bounceStrength = choseSpecular
? mix(sceneData.indirectStrength, 1.0,
max(metallic, reflectivity))
: sceneData.indirectStrength;
indirect = brdfWeight *
skyColor(resolvedBounceRay.direction, 0.0, skybox) *
sceneData.indirectStrength;
skyColor(resolvedBounceRay.direction, 0.0, skybox,
sceneData) *
bounceStrength;
} else {
uint bi = resolvedBounceHit.instance_id;
uint bp = resolvedBounceHit.primitive_id;
Expand Down Expand Up @@ -7505,8 +7553,7 @@ R"();
float3 bLocalT =
normalizeOr(float3(vertices[bj0].tangent) * bb0 +
float3(vertices[bj1].tangent) * bb1 +
)",
R"( float3(vertices[bj2].tangent) * bb2,
float3(vertices[bj2].tangent) * bb2,
float3(1.0, 0.0, 0.0));
float3 bLocalB =
normalizeOr(float3(vertices[bj0].bitangent) * bb0 +
Expand Down Expand Up @@ -7546,8 +7593,12 @@ R"( float3(vertices[bj2].tangent) * bb2,

float3 bAmbient = bAlbedo * max(sceneData.ambientIntensity, 0.0) *
(1.0 - bMetallic) * bAo;
float bounceStrength = choseSpecular
? mix(sceneData.indirectStrength, 1.0,
max(metallic, reflectivity))
: sceneData.indirectStrength;
indirect = brdfWeight * (bAmbient + bounceDirect + bEmissive) *
sceneData.indirectStrength;
bounceStrength;
}

indirect = clampLuminance(indirect, 16.0);
Expand Down Expand Up @@ -7646,7 +7697,8 @@ kernel void main0(texture2d<float, access::write> outTex [[texture(0)]],

soft = soft * soft / max(bloomKnee * 4.0, 0.00001);

float contribution =
float c)",
R"(ontribution =
max(brightness - bloomThreshold, soft) /
max(brightness, 0.00001);

Expand All @@ -7657,7 +7709,7 @@ kernel void main0(texture2d<float, access::write> outTex [[texture(0)]],
}
)",
};
static const AtlasPackedShaderSource PATH = {PATH_PARTS, 7};
static const AtlasPackedShaderSource PATH = {PATH_PARTS, 8};

static const char* const POINT_DEPTH_FRAG_PARTS[] = {
R"(#include <metal_stdlib>
Expand Down
31 changes: 17 additions & 14 deletions opal/command_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,8 +865,8 @@ void bindComputeTextures(const std::shared_ptr<Pipeline> &pipeline,
}

auto &pipelineState = metal::pipelineState(pipeline.get());
std::array<MTL::Texture *, 32> desiredTextures{};
std::array<MTL::SamplerState *, 32> desiredSamplers{};
std::array<MTL::Texture *, 64> desiredTextures{};
std::array<MTL::SamplerState *, 16> desiredSamplers{};
desiredTextures.fill(nullptr);
desiredSamplers.fill(nullptr);

Expand All @@ -881,11 +881,13 @@ void bindComputeTextures(const std::shared_ptr<Pipeline> &pipeline,
if (textureState.texture == nullptr) {
continue;
}
if (textureState.sampler == nullptr) {
metal::rebuildTextureSampler(texture.get(), device);
}
desiredTextures[static_cast<size_t>(unit)] = textureState.texture;
desiredSamplers[static_cast<size_t>(unit)] = textureState.sampler;
if (unit < static_cast<int>(desiredSamplers.size())) {
if (textureState.sampler == nullptr) {
metal::rebuildTextureSampler(texture.get(), device);
}
desiredSamplers[static_cast<size_t>(unit)] = textureState.sampler;
}
}

if (pipeline->shaderProgram != nullptr) {
Expand All @@ -907,20 +909,21 @@ void bindComputeTextures(const std::shared_ptr<Pipeline> &pipeline,
if (fallbackState.texture == nullptr) {
continue;
}
if (fallbackState.sampler == nullptr) {
metal::rebuildTextureSampler(fallback.get(), device);
}
desiredTextures[unitIndex] = fallbackState.texture;
desiredSamplers[unitIndex] = fallbackState.sampler;
if (unitIndex < desiredSamplers.size()) {
if (fallbackState.sampler == nullptr) {
metal::rebuildTextureSampler(fallback.get(), device);
}
desiredSamplers[unitIndex] = fallbackState.sampler;
}
}
}

constexpr size_t kMaxComputeTextureUnits = 16;
const size_t computeUnitCount =
std::min(desiredTextures.size(), kMaxComputeTextureUnits);
for (size_t unit = 0; unit < computeUnitCount; ++unit) {
for (size_t unit = 0; unit < desiredTextures.size(); ++unit) {
encoder->setTexture(desiredTextures[unit],
static_cast<NS::UInteger>(unit));
}
for (size_t unit = 0; unit < desiredSamplers.size(); ++unit) {
encoder->setSamplerState(desiredSamplers[unit],
static_cast<NS::UInteger>(unit));
}
Expand Down
Loading
Loading