@@ -419,59 +419,69 @@ parameter_t SContext::getTexture(const CElementTexture* const rootTex, hlsl::flo
419419 SAssetBundle viewBundle = interm_getImageViewInHierarchy (tex->bitmap .filename ,/* ICPUScene::MATERIAL_IMAGES_HIERARCHY_LEVELS_BELOW*/ 1 );
420420 if (auto contents=viewBundle.getContents (); !contents.empty ())
421421 {
422- retval.view = IAsset::castDown<const ICPUImageView>(*contents.begin ());
422+ auto view = IAsset::castDown<const ICPUImageView>(*contents.begin ());
423+ const auto format = view->getCreationParameters ().format ;
423424 if (bitmap.channel !=CElementTexture::Bitmap::CHANNEL::INVALID)
424425 retval.viewChannel = bitmap.channel -CElementTexture::Bitmap::CHANNEL::R;
425- // get sampler parameters
426- using tex_clamp_e = asset::ISampler::E_TEXTURE_CLAMP;
427- auto getWrapMode = [](CElementTexture::Bitmap::WRAP_MODE mode)
426+ if (retval.viewChannel <asset::getFormatChannelCount (format))
428427 {
429- switch (mode)
428+ retval.view = std::move (view);
429+ // get sampler parameters
430+ using tex_clamp_e = asset::ISampler::E_TEXTURE_CLAMP;
431+ auto getWrapMode = [](CElementTexture::Bitmap::WRAP_MODE mode)
430432 {
431- case CElementTexture::Bitmap::WRAP_MODE::CLAMP:
432- return tex_clamp_e::ETC_CLAMP_TO_EDGE;
433- break ;
434- case CElementTexture::Bitmap::WRAP_MODE::MIRROR:
435- return tex_clamp_e::ETC_MIRROR;
436- break ;
437- case CElementTexture::Bitmap::WRAP_MODE::ONE:
438- assert (false ); // TODO : replace whole texture?
439- break ;
440- case CElementTexture::Bitmap::WRAP_MODE::ZERO:
441- assert (false ); // TODO : replace whole texture?
433+ switch (mode)
434+ {
435+ case CElementTexture::Bitmap::WRAP_MODE::CLAMP:
436+ return tex_clamp_e::ETC_CLAMP_TO_EDGE;
437+ break ;
438+ case CElementTexture::Bitmap::WRAP_MODE::MIRROR:
439+ return tex_clamp_e::ETC_MIRROR;
440+ break ;
441+ case CElementTexture::Bitmap::WRAP_MODE::ONE:
442+ assert (false ); // TODO : replace whole texture?
443+ break ;
444+ case CElementTexture::Bitmap::WRAP_MODE::ZERO:
445+ assert (false ); // TODO : replace whole texture?
446+ break ;
447+ default :
448+ break ;
449+ }
450+ return tex_clamp_e::ETC_REPEAT;
451+ };
452+ auto & params = retval.sampler ;
453+ params.TextureWrapU = getWrapMode (bitmap.wrapModeU );
454+ params.TextureWrapV = getWrapMode (bitmap.wrapModeV );
455+ switch (bitmap.filterType )
456+ {
457+ case CElementTexture::Bitmap::FILTER_TYPE::EWA:
458+ [[fallthrough]]; // we dont support this fancy stuff
459+ case CElementTexture::Bitmap::FILTER_TYPE::TRILINEAR:
460+ params.MinFilter = ISampler::ETF_LINEAR;
461+ params.MaxFilter = ISampler::ETF_LINEAR;
462+ params.MipmapMode = ISampler::ESMM_LINEAR;
442463 break ;
443464 default :
465+ params.MinFilter = ISampler::ETF_NEAREST;
466+ params.MaxFilter = ISampler::ETF_NEAREST;
467+ params.MipmapMode = ISampler::ESMM_NEAREST;
444468 break ;
445469 }
446- return tex_clamp_e::ETC_REPEAT;
447- };
448- auto & params = retval.sampler ;
449- params.TextureWrapU = getWrapMode (bitmap.wrapModeU );
450- params.TextureWrapV = getWrapMode (bitmap.wrapModeV );
451- switch (bitmap.filterType )
452- {
453- case CElementTexture::Bitmap::FILTER_TYPE::EWA:
454- [[fallthrough]]; // we dont support this fancy stuff
455- case CElementTexture::Bitmap::FILTER_TYPE::TRILINEAR:
456- params.MinFilter = ISampler::ETF_LINEAR;
457- params.MaxFilter = ISampler::ETF_LINEAR;
458- params.MipmapMode = ISampler::ESMM_LINEAR;
459- break ;
460- default :
461- params.MinFilter = ISampler::ETF_NEAREST;
462- params.MaxFilter = ISampler::ETF_NEAREST;
463- params.MipmapMode = ISampler::ESMM_NEAREST;
464- break ;
470+ params.AnisotropicFilter = core::max (hlsl::findMSB<uint32_t >(bitmap.maxAnisotropy ),1u );
471+ // TODO: embed the gamma in the material compiler Frontend
472+ // or adjust gamma on pixels (painful and long process)
473+ // assert(std::isnan(bitmap.gamma));
474+ auto & transform = *outUvTransform;
475+ transform[0 ][0 ] = bitmap.uscale ;
476+ transform[0 ][2 ] = bitmap.uoffset ;
477+ transform[1 ][1 ] = bitmap.vscale ;
478+ transform[1 ][2 ] = bitmap.voffset ;
465479 }
466- params.AnisotropicFilter = core::max (hlsl::findMSB<uint32_t >(bitmap.maxAnisotropy ),1u );
467- // TODO: embed the gamma in the material compiler Frontend
468- // or adjust gamma on pixels (painful and long process)
469- // assert(std::isnan(bitmap.gamma));
470- auto & transform = *outUvTransform;
471- transform[0 ][0 ] = bitmap.uscale ;
472- transform[0 ][2 ] = bitmap.uoffset ;
473- transform[1 ][1 ] = bitmap.vscale ;
474- transform[1 ][2 ] = bitmap.voffset ;
480+ else
481+ inner.params .logger .log (
482+ " Failed to parse CElementTexture bitmap for %p with id %s from path \" %s\" , channel swizzle %s requested out of range of format %s" ,
483+ LoggerError,tex,tex ? tex->id .c_str ():" " ,tex->bitmap .filename ,system::to_string (bitmap.channel ),system::to_string (format)
484+ );
475485 }
476486 else
477487 inner.params .logger .log (" Failed to load bitmap texture for %p with id %s from path \" %s\" " ,LoggerError,tex,tex ? tex->id .c_str ():" " ,tex->bitmap .filename );
@@ -501,10 +511,19 @@ hlsl::float32_t2x3 SContext::getParameters(const std::span<parameter_t,3> out, c
501511 if (src.texture )
502512 {
503513 const auto param = getTexture (src.texture ,&retval);
514+ const auto formatChannelCount = getFormatChannelCount (param.view ? param.view ->getCreationParameters ().format :E_FORMAT::EF_UNKNOWN);
515+ if (src.texture ->bitmap .channel ==CElementTexture::Bitmap::CHANNEL::INVALID && formatChannelCount>1 && out.size ()>formatChannelCount)
516+ {
517+ for (auto c=0 ; c<out.size (); c++)
518+ out[c].scale = std::numeric_limits<decltype (out[c].scale )>::signaling_NaN ();
519+ return hlsl::math::linalg::diagonal<hlsl::float32_t2x3>(0 .f );;
520+ }
504521 for (auto c=0 ; c<out.size (); c++)
505522 {
506523 out[c].scale = param.scale ;
507- out[c].viewChannel = param.viewChannel +c;
524+ out[c].viewChannel = param.viewChannel ;
525+ if (formatChannelCount>1 && src.texture ->bitmap .channel ==CElementTexture::Bitmap::CHANNEL::INVALID)
526+ out[c].viewChannel += c;
508527 out[c].view = param.view ;
509528 out[c].sampler = param.sampler ;
510529 }
@@ -535,7 +554,8 @@ hlsl::float32_t2x3 SContext::getParameters(const std::span<parameter_t> out, con
535554 for (auto c=0 ; c<out.size (); c++)
536555 {
537556 out[c].scale = param.scale ;
538- out[c].viewChannel = param.viewChannel +c;
557+ // assign same channel to everything because the `src` is monochrome
558+ out[c].viewChannel = param.viewChannel ;
539559 out[c].view = param.view ;
540560 out[c].sampler = param.sampler ;
541561 }
0 commit comments