Skip to content

Commit 7dd79e8

Browse files
authored
Merge pull request #204 from DimitrisKalyvas/material
[material]: tweaked texture loading assert
2 parents 3ae9e45 + ff1d5ec commit 7dd79e8

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

source/runtime/Rendering/Material.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -267,22 +267,25 @@ namespace spartan
267267
}
268268
}
269269

270-
void resize_texture(const vector<byte>& src_data, uint32_t src_width, uint32_t src_height, vector<byte>& dst_data, uint32_t dst_width, uint32_t dst_height)
270+
void resize_texture(const vector<byte>& src_data, uint32_t src_width, uint32_t src_height, uint32_t src_channels, vector<byte>& dst_data, uint32_t dst_width, uint32_t dst_height)
271271
{
272-
SP_ASSERT_MSG(src_data.size() == src_width * src_height * 4, "Invalid source data size");
272+
SP_ASSERT_MSG(src_channels >= 1 && src_channels <= 4, "Invalid channel count");
273+
SP_ASSERT_MSG(src_data.size() == src_width * src_height * src_channels, "Invalid source data size");
273274
dst_data.resize(dst_width * dst_height * 4);
274275

275276
auto get_pixel = [&](uint32_t x, uint32_t y) -> Vector4
276277
{
277278
x = clamp(x, 0u, src_width - 1);
278279
y = clamp(y, 0u, src_height - 1);
279-
uint32_t index = (y * src_width + x) * 4;
280-
return Vector4(
281-
static_cast<float>(src_data[index + 0]) / 255.0f,
282-
static_cast<float>(src_data[index + 1]) / 255.0f,
283-
static_cast<float>(src_data[index + 2]) / 255.0f,
284-
static_cast<float>(src_data[index + 3]) / 255.0f
285-
);
280+
uint32_t index = (y * src_width + x) * src_channels;
281+
282+
// handle different channel counts - expand to RGBA
283+
float r = static_cast<float>(src_data[index + 0]) / 255.0f;
284+
float g = (src_channels >= 2) ? static_cast<float>(src_data[index + 1]) / 255.0f : r;
285+
float b = (src_channels >= 3) ? static_cast<float>(src_data[index + 2]) / 255.0f : r;
286+
float a = (src_channels >= 4) ? static_cast<float>(src_data[index + 3]) / 255.0f : 1.0f;
287+
288+
return Vector4(r, g, b, a);
286289
};
287290

288291
for (uint32_t y = 0; y < dst_height; ++y)
@@ -437,6 +440,7 @@ namespace spartan
437440
texture_alpha_mask->GetMip(0, 0).bytes,
438441
texture_alpha_mask->GetWidth(),
439442
texture_alpha_mask->GetHeight(),
443+
texture_alpha_mask->GetChannelCount(),
440444
resized_mask,
441445
texture_color->GetWidth(),
442446
texture_color->GetHeight()
@@ -491,19 +495,19 @@ namespace spartan
491495
// resize if necessary
492496
if (texture_occlusion && (texture_occlusion->GetWidth() != max_width || texture_occlusion->GetHeight() != max_height))
493497
{
494-
texture_processing::resize_texture(texture_occlusion->GetMip(0, 0).bytes, texture_occlusion->GetWidth(), texture_occlusion->GetHeight(), occlusion_data, max_width, max_height);
498+
texture_processing::resize_texture(texture_occlusion->GetMip(0, 0).bytes, texture_occlusion->GetWidth(), texture_occlusion->GetHeight(), texture_occlusion->GetChannelCount(), occlusion_data, max_width, max_height);
495499
}
496500
if (texture_roughness && (texture_roughness->GetWidth() != max_width || texture_roughness->GetHeight() != max_height))
497501
{
498-
texture_processing::resize_texture(texture_roughness->GetMip(0, 0).bytes, texture_roughness->GetWidth(), texture_roughness->GetHeight(), roughness_data, max_width, max_height);
502+
texture_processing::resize_texture(texture_roughness->GetMip(0, 0).bytes, texture_roughness->GetWidth(), texture_roughness->GetHeight(), texture_roughness->GetChannelCount(), roughness_data, max_width, max_height);
499503
}
500504
if (texture_metalness && (texture_metalness->GetWidth() != max_width || texture_metalness->GetHeight() != max_height))
501505
{
502-
texture_processing::resize_texture(texture_metalness->GetMip(0, 0).bytes, texture_metalness->GetWidth(), texture_metalness->GetHeight(), metalness_data, max_width, max_height);
506+
texture_processing::resize_texture(texture_metalness->GetMip(0, 0).bytes, texture_metalness->GetWidth(), texture_metalness->GetHeight(), texture_metalness->GetChannelCount(), metalness_data, max_width, max_height);
503507
}
504508
if (texture_height && (texture_height->GetWidth() != max_width || texture_height->GetHeight() != max_height))
505509
{
506-
texture_processing::resize_texture(texture_height->GetMip(0, 0).bytes, texture_height->GetWidth(), texture_height->GetHeight(), height_data, max_width, max_height);
510+
texture_processing::resize_texture(texture_height->GetMip(0, 0).bytes, texture_height->GetWidth(), texture_height->GetHeight(), texture_height->GetChannelCount(), height_data, max_width, max_height);
507511
}
508512

509513
texture_processing::pack_occlusion_roughness_metalness_height(

0 commit comments

Comments
 (0)