Skip to content

Commit

Permalink
Merge pull request #1954 from iaomw/bcx
Browse files Browse the repository at this point in the history
Fix texture reload for BCx
  • Loading branch information
iaomw authored Jul 4, 2024
2 parents 4a431bc + 60b9a0e commit 89b96da
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 92 deletions.
33 changes: 16 additions & 17 deletions zenovis/src/optx/RenderEngineOptx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ struct GraphicsManager {
if (!path.empty()) {
if (OptixUtil::sky_tex.has_value() && OptixUtil::sky_tex.value() != path
&& OptixUtil::sky_tex.value() != OptixUtil::default_sky_tex ) {
OptixUtil::removeTexture(OptixUtil::sky_tex.value());
OptixUtil::removeTexture( {OptixUtil::sky_tex.value(), false} );
}

OptixUtil::sky_tex = path;
Expand Down Expand Up @@ -1227,7 +1227,7 @@ struct RenderEngineOptx : RenderEngine, zeno::disable_copy {

// Auto unload unused texure
{
std::map<std::string, bool> realNeedTexPaths;
std::set<OptixUtil::TexKey> realNeedTexPaths;
for(auto const &[matkey, mtldet] : matMap) {
if (mtldet->parameters.find("vol") != std::string::npos
|| cachedMeshesMaterials.count(mtldet->mtlidkey) > 0
Expand All @@ -1248,24 +1248,26 @@ struct RenderEngineOptx : RenderEngine, zeno::disable_copy {
realNeedTexPaths.insert( {ld.textureKey, false});
}
}
std::vector<std::string> needToRemoveTexPaths;
for(auto const &[tex, _]: OptixUtil::g_tex) {
if (realNeedTexPaths.count(tex) > 0) {
std::vector<OptixUtil::TexKey> needToRemoveTexPaths;
for(auto const &[key, _]: OptixUtil::tex_lut) {

if (realNeedTexPaths.count(key) > 0) {
continue;
}
if (OptixUtil::sky_tex.has_value() && tex == OptixUtil::sky_tex.value()) {
if (OptixUtil::sky_tex.has_value() && key.path == OptixUtil::sky_tex.value()) {
continue;
}
if (tex == OptixUtil::default_sky_tex) {
if (key.path == OptixUtil::default_sky_tex) {
continue;
}
needToRemoveTexPaths.emplace_back(tex);
needToRemoveTexPaths.emplace_back(key);
}
for (const auto& need_remove_tex: needToRemoveTexPaths) {
OptixUtil::removeTexture(need_remove_tex);
}
for (const auto& realNeedTexPath: realNeedTexPaths) {
OptixUtil::addTexture(realNeedTexPath.first, realNeedTexPath.second);
for (const auto& realNeedTexKey: realNeedTexPaths) {

OptixUtil::addTexture(realNeedTexKey.path, realNeedTexKey.blockCompression);
}
}
for(auto const &[matkey, mtldet] : matMap)
Expand Down Expand Up @@ -1323,20 +1325,17 @@ struct RenderEngineOptx : RenderEngine, zeno::disable_copy {
callable.append(shadtpl2.second);
//std::cout<<callable<<std::endl;

std::vector<std::string> shaderTex;
for(auto tex:mtldet->tex2Ds)
{
shaderTex.emplace_back(tex->path);
}

ShaderPrepared shaderP;

shaderP.callable = callable;
shaderP.filename = selected_source.name;
shaderP.parameters = mtldet->parameters;

shaderP.matid = mtldet->mtlidkey;
shaderP.tex_names = shaderTex;
for(auto tex:mtldet->tex2Ds)
{
shaderP.tex_keys.push_back( {tex->path, tex->blockCompression} );
}

if (isVol) {

Expand Down
50 changes: 31 additions & 19 deletions zenovis/xinxinoptix/BCX.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <cstring>
#include <stb_dxt.h>
#include <tbb/task.h>
#include <tbb/task_group.h>
Expand All @@ -9,26 +10,24 @@
#include <vector_types.h>

template <char N>
inline void compress(std::vector<unsigned char> &packed, std::vector<unsigned char> &block) {
inline void compress(unsigned char* packed, unsigned char* block) {

if constexpr (N == 1)
return stb_compress_bc4_block(packed.data(), (unsigned char*)block.data());
stb_compress_bc4_block(packed, block);
if constexpr (N == 2)
return stb_compress_bc5_block(packed.data(), (unsigned char*)block.data());
stb_compress_bc5_block(packed, block);
if constexpr (N == 3)
stb_compress_dxt_block(packed, block, 0, STB_DXT_HIGHQUAL);
if constexpr (N == 4)
return stb_compress_dxt_block(packed.data(), (unsigned char*)block.data(), 1, STB_DXT_HIGHQUAL);
stb_compress_dxt_block(packed, block, 1, STB_DXT_HIGHQUAL);
}

template <char N>
template <char channel, char byte_per_source_pixel=channel>
inline std::vector<unsigned char> compressBCx(unsigned char* img, uint32_t nx, uint32_t ny) {

static std::map<char, uint32_t> sizes {
{ 1, 8 },
{ 2, 16},
{ 4, 16}
};
static const char sizes[] = { 0, 8, 16, 8, 16 };

const auto size_per_packed = sizes[N];
const auto size_per_packed = sizes[channel];

auto count = size_per_packed * (nx/4) * (ny/4);
std::vector<unsigned char> result(count);
Expand All @@ -39,8 +38,7 @@ inline std::vector<unsigned char> compressBCx(unsigned char* img, uint32_t nx, u

bc_group.run([&, i]{

std::vector<unsigned char> block(16 * N, 0);
std::vector<unsigned char> packed(size_per_packed, 0);
std::vector<unsigned char> block(16 * byte_per_source_pixel, 0);

for (size_t j=0; j<nx; j+=4) { // col

Expand All @@ -49,15 +47,15 @@ inline std::vector<unsigned char> compressBCx(unsigned char* img, uint32_t nx, u
//auto offset_j = k % 4;

auto index = nx * (i+offset_i) + (j);
//raw_block[k] = img[index];
auto dst_ptr = block.data() + k*N;
auto src_ptr = img + index*N ;
memcpy(dst_ptr, src_ptr, N * 4);

auto dst_ptr = block.data() + k*byte_per_source_pixel;
auto src_ptr = img + index*byte_per_source_pixel;
memcpy(dst_ptr, src_ptr, byte_per_source_pixel * 4);
}
compress<N>(packed, block);

auto offset = size_per_packed * ((nx/4) * i/4 + j/4);
memcpy(result.data()+offset, packed.data(), packed.size());
auto packed = result.data()+offset;
compress<channel>(packed, block.data());
}

}); // run
Expand All @@ -75,6 +73,20 @@ inline std::vector<unsigned char> compressBC5(unsigned char* two_byte_per_pixel,
return compressBCx<2>(two_byte_per_pixel, nx, ny);
}

inline std::vector<unsigned char> compressBC1(unsigned char* three_byte_per_pixel, uint32_t nx, uint32_t ny) {

auto raw = three_byte_per_pixel;

auto count = nx * ny;
std::vector<uchar4> alt(count);

for (size_t i=0; i<count; ++i) {
alt[i] = { raw[i*3 + 0], raw[i*3 + 1], raw[i*3 + 2], 255u };
}

return compressBCx<3, 4>((unsigned char*)alt.data(), nx, ny);
}

inline std::vector<unsigned char> compressBC3(unsigned char* four_byte_per_pixel, uint32_t nx, uint32_t ny) {
return compressBCx<4>(four_byte_per_pixel, nx, ny);
}
Loading

0 comments on commit 89b96da

Please sign in to comment.