Skip to content

Commit

Permalink
noodling with coverage, better error-handling in gltf-loader
Browse files Browse the repository at this point in the history
  • Loading branch information
crocdialer committed Aug 12, 2023
1 parent f8714c8 commit 0fceca1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
10 changes: 5 additions & 5 deletions shaders/ray/raygen.rgen
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ trace_result_t trace(vec2 coord, vec2 size)

// sum radiance and coverage for all samples.
trace_result.radiance = vec3(0);
trace_result.coverage = 0;
trace_result.coverage = 0.0;

float sample_offset = 2 * rnd(rng_state) - 1;

Expand Down Expand Up @@ -141,10 +141,11 @@ trace_result_t trace(vec2 coord, vec2 size)
}

trace_result.radiance += path_radiance;
trace_result.coverage += payload.depth > 0 ? 1.0 : 0.0;
// trace_result.coverage += payload.depth > 0 ? 1.0 : 0.0;
trace_result.coverage = max(trace_result.coverage, payload.depth > 0 ? 1.0 : 0.0);
}
trace_result.radiance /= float(push_constants.num_samples);
trace_result.coverage /= float(push_constants.num_samples);
// trace_result.coverage /= float(push_constants.num_samples);

// error on the side of caution!?
// TODO: check if that helps with artifacts on older nvidia hardware, i.e. 1060
Expand All @@ -155,8 +156,7 @@ trace_result_t trace(vec2 coord, vec2 size)
void main()
{
trace_result_t trace_result = trace(gl_LaunchIDEXT.xy, gl_LaunchSizeEXT.xy);
if(!push_constants.draw_skybox){ trace_result.radiance *= trace_result.coverage * trace_result.coverage; }
vec4 accumulated_radiance = vec4(trace_result.radiance, trace_result.coverage);
vec4 accumulated_radiance = vec4(trace_result.radiance, smoothstep(0, 1, trace_result.coverage));

if (push_constants.batch_index != 0)
{
Expand Down
39 changes: 24 additions & 15 deletions src/gltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,24 +771,26 @@ struct load_image_context_t
crocore::ThreadPool *pool = nullptr;
};

bool LoadImageDataFunction(tinygltf::Image */*tiny_image*/, const int image_idx, std::string *err, std::string * /*warn*/,
int /*req_width*/, int /*req_height*/, const unsigned char *bytes, int size, void *user_data)
bool LoadImageDataFunction(tinygltf::Image * /*tiny_image*/, const int image_idx, std::string * /*err*/,
std::string * /*warn*/, int /*req_width*/, int /*req_height*/, const unsigned char *bytes,
int size, void *user_data)
{
assert(user_data && bytes && size);
auto &img_context = *reinterpret_cast<load_image_context_t *>(user_data);
assert(img_context.pool);

try
{
// create and cache image
std::vector<unsigned char> data(bytes, bytes + size);
img_context.image_cache[image_idx] =
img_context.pool->post([data = std::move(data)] { return crocore::create_image_from_data(data, 4); });
} catch(std::exception &e)
{
if(err) { *err = e.what(); }
return false;
}
// create and cache image
std::vector<unsigned char> data(bytes, bytes + size);
img_context.image_cache[image_idx] = img_context.pool->post([data = std::move(data)]() -> crocore::ImagePtr {
try
{
return crocore::create_image_from_data(data, 4);
} catch(std::exception &e)
{
spdlog::error(e.what());
return nullptr;
}
});
return true;
}

Expand Down Expand Up @@ -826,8 +828,15 @@ mesh_assets_t gltf(const std::filesystem::path &path, crocore::ThreadPool *const

if(pool)
{
// wait for image-futures
for(auto &[idx, img_future]: img_context.image_cache) { image_cache[idx] = img_future.get(); }
// wait for image-futures, check for nullptr
for(auto &[idx, img_future]: img_context.image_cache)
{
auto img = img_future.get();

// fail to load image, bail out
if(!img) { return {}; }
image_cache[idx] = std::move(img);
}
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion submodules/crocore

0 comments on commit 0fceca1

Please sign in to comment.