Skip to content

Commit

Permalink
feat: resolve texture loading problem DX12 (#85)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Pollind <mpollind@gmail.com>
  • Loading branch information
pollend authored Sep 25, 2023
1 parent 1bd4831 commit 519daa7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 23 deletions.
2 changes: 0 additions & 2 deletions HPL2/resource/translucency.frag.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ PsOut PS_MAIN(PsIn In)
float4 finalColor = float4(0.0, 0.0 ,0.0, 1.0);
if(HasDiffuse(textureConfig)) {
finalColor = SampleTex2D(Get(diffuseMap), Get(materialSampler), In.uv) * In.color;
} else {
finalColor.a = SampleTex2D(Get(diffuseMap), Get(materialSampler), In.uv).a * In.color.a;
}

float finalAlpha = Get(sceneAlpha);
Expand Down
2 changes: 0 additions & 2 deletions HPL2/resource/translucency_particle.frag.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ float4 PS_MAIN(PsIn In)

if(HasDiffuse(textureConfig)) {
finalColor = SampleTex2D(Get(diffuseMap), Get(materialSampler), In.uv) * In.color;
} else {
finalColor.a = SampleTex2D(Get(diffuseMap), Get(materialSampler), In.uv).a * In.color.a;
}

float finalAlpha = Get(sceneAlpha);
Expand Down
49 changes: 35 additions & 14 deletions HPL2/sources/graphics/ForgeHandles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ namespace hpl {
}


#define MIP_REDUCE(s, mip) (max(1u, (uint32_t)((s) >> (mip))))


SharedTexture SharedTexture::LoadFromHPLBitmap(cBitmap& bitmap, const BitmapLoadOptions& options) {
SharedTexture handle;
SyncToken token = {};
Expand Down Expand Up @@ -238,32 +241,50 @@ namespace hpl {
});

auto sourceImageFormat = FromHPLPixelFormat(bitmap.GetPixelFormat());
uint32_t sourceRowStride;
if(!util_get_surface_info(desc.mWidth, desc.mHeight, sourceImageFormat, nullptr, &sourceRowStride, nullptr)) {
ASSERT(false && "Failed to get surface info");
}
uint32_t srcElementStride = sourceRowStride / desc.mWidth;

auto isCompressed = TinyImageFormat_IsCompressed(desc.mFormat);
for(uint32_t arrIndex = 0; arrIndex < desc.mArraySize; arrIndex++) {
for(uint32_t mipLevel = 0; mipLevel < desc.mMipLevels; mipLevel++) {
TextureUpdateDesc update = {handle.m_handle, mipLevel, arrIndex};
const auto& input = bitmap.GetData(arrIndex, mipLevel);
auto data = std::span<unsigned char>(input->mpData, static_cast<size_t>(input->mlSize));
//auto data = std::span<uint8_t>(input->mpData, static_cast<size_t>(input->mlSize));
beginUpdateResource(&update);
uint32_t dstElementStride = update.mDstRowStride / desc.mWidth;

uint32_t sourceRowStride;
uint32_t destRowStride;
if (!util_get_surface_info(
MIP_REDUCE(desc.mWidth, mipLevel),
MIP_REDUCE(desc.mHeight, mipLevel),
sourceImageFormat,
nullptr,
&sourceRowStride,
nullptr)) {
ASSERT(false && "Failed to get surface info");
}
uint32_t srcElementStride = sourceRowStride / desc.mWidth;

if (!util_get_surface_info(
MIP_REDUCE(desc.mWidth, mipLevel),
MIP_REDUCE(desc.mHeight, mipLevel),
desc.mFormat,
nullptr,
&destRowStride,
nullptr)) {
ASSERT(false && "Failed to get surface info");
}

uint32_t dstElementStride = destRowStride / desc.mWidth;

for (uint32_t z = 0; z < desc.mDepth; ++z)
for (size_t z = 0; z < desc.mDepth; ++z)
{
uint8_t* dstData = update.pMappedData + update.mDstSliceStride * z;
auto srcData = data.begin() + update.mSrcSliceStride * z;
uint8_t* dstData = update.pMappedData + (update.mDstSliceStride * z);
auto srcData = input->mpData + update.mSrcSliceStride * z;
for (uint32_t row = 0; row < update.mRowCount; ++row) {
if(isCompressed) {
std::memcpy(dstData + row * update.mDstRowStride, &srcData[row * update.mSrcRowStride], update.mSrcRowStride);
std::memcpy(dstData + (row * update.mDstRowStride), srcData + (row * update.mSrcRowStride), update.mSrcRowStride);
} else {
for(uint32_t column = 0; column < desc.mWidth; column++) {
std::memset(dstData + row * update.mDstRowStride + column * dstElementStride, 0xff, dstElementStride);
std::memcpy(dstData + row * update.mDstRowStride + column * dstElementStride, &srcData[row * sourceRowStride + column * srcElementStride], std::min(dstElementStride, srcElementStride));
std::memset(dstData + (row * update.mDstRowStride + column * dstElementStride), 0xff, dstElementStride);
std::memcpy(dstData + (row * update.mDstRowStride + column * dstElementStride), srcData + (row * sourceRowStride + column * srcElementStride), std::min(dstElementStride, srcElementStride));
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions HPL2/sources/graphics/RendererDeferred.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2510,11 +2510,11 @@ namespace hpl {


// for DirectX12 these frame descriptors are not used so its ommitied for the draw
#if defined(VULKAN)
if (frame.m_renderer->GetApi() == RENDERER_API_VULKAN) {
//#if defined(VULKAN)
// if (frame.m_renderer->GetApi() == RENDERER_API_VULKAN) {
cmdBindDescriptorSet(cmd, frameDescriptorIndex, m_materialSet.m_frameSet[frame.m_frameIndex].m_handle);
}
#endif
// }
//#endif
for (auto& illuminationItem : m_rendererList.GetRenderableItems(eRenderListType_Illumination)) {
cMaterial* pMaterial = illuminationItem->GetMaterial();
iVertexBuffer* vertexBuffer = illuminationItem->GetVertexBuffer();
Expand Down
1 change: 0 additions & 1 deletion HPL2/sources/resources/TextureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ namespace hpl {
asName,
[&abUseMipMaps](const tString& asName, const tWString& path, cBitmap* pBmp) -> Image*
{

auto* resource = new Image(asName, path);
SharedTexture::BitmapLoadOptions opts = {0};
opts.m_useMipmaps = abUseMipMaps;
Expand Down

0 comments on commit 519daa7

Please sign in to comment.