From ce68be2a59c823eda49e993305368a5c3ad73db7 Mon Sep 17 00:00:00 2001 From: samcake Date: Wed, 26 Oct 2016 23:09:41 -0700 Subject: [PATCH] a few step further --- libraries/gpu/src/gpu/Image.cpp | 31 +++++-- libraries/gpu/src/gpu/Image.h | 110 ++++++++++++++++------- libraries/model/src/model/TextureMap.cpp | 2 +- 3 files changed, 103 insertions(+), 40 deletions(-) diff --git a/libraries/gpu/src/gpu/Image.cpp b/libraries/gpu/src/gpu/Image.cpp index e8d1899f11..b3fcbe11eb 100644 --- a/libraries/gpu/src/gpu/Image.cpp +++ b/libraries/gpu/src/gpu/Image.cpp @@ -1,28 +1,45 @@ #include "Image.h" -int image::Pixel::cpp { 0 }; +int image::BC::cpp { 0 }; namespace image { - template <> void compress(const PB_RGB32& src, CB_8& dst) { + namespace pixel { + template <> const RGB16_565 mix(const RGB16_565 p0, const RGB16_565 p1, const Byte alpha) { + return RGB16_565( + mix5_4(p0.r, p1.r, alpha), + mix6_4(p0.g, p1.g, alpha), + mix5_4(p0.b, p1.b, alpha)); + } + } + +template <> void compress(const PB_RGB32& src, CB_BC1& dst) { for (auto& b : dst.bytes) { b = 12; } } -template <> void uncompress(const CB_8& src, PB_RGB32& dst) { - for (auto& b : dst.bytes) { - b = 12; +template <> void uncompress(const CB_BC1& src, PB_RGB32& dst) { + auto bc1 = src.bc; + auto c0 = bc1.color0; + auto c1 = bc1.color1; + + for (auto& p : dst.pixels) { + auto r = pixel::mix( + c0, + c1, + (pixel::Byte)bc1.table); + ///p.val = } } -template <> void compress(const PB_RGBA32& src, CB_8& dst) { +template <> void compress(const PB_RGBA32& src, CB_BC4& dst) { } -template <> void uncompress(const CB_8& src, PB_RGBA32& dst) { +template <> void uncompress(const CB_BC4& src, PB_RGBA32& dst) { } diff --git a/libraries/gpu/src/gpu/Image.h b/libraries/gpu/src/gpu/Image.h index 37f5f2788a..ff106f65b6 100644 --- a/libraries/gpu/src/gpu/Image.h +++ b/libraries/gpu/src/gpu/Image.h @@ -7,8 +7,8 @@ namespace image { - class Pixel { - public: + namespace pixel { + using Byte = uint8_t; using Byte2 = uint16_t; using Byte4 = uint32_t; @@ -16,19 +16,22 @@ namespace image { static const Byte BLACK8 { 0 }; static const Byte WHITE8 { 255 }; - + template int bitVal() { return 1 << N; } + template int bitProduct() { return bitVal() * bitVal(); } + template T mix(const T x, const T y, const A a) { return T(((bitVal() - a) * x + a * y) / bitProduct()); } + struct RGB32 { - Byte r { BLACK }; - Byte g { BLACK }; - Byte b { BLACK }; - Byte x { WHITE }; + Byte r { BLACK8 }; + Byte g { BLACK8 }; + Byte b { BLACK8 }; + Byte x { WHITE8 }; }; struct RGBA32 { - Byte r { BLACK }; - Byte g { BLACK }; - Byte b { BLACK }; - Byte a { WHITE }; + Byte r { BLACK8 }; + Byte g { BLACK8 }; + Byte b { BLACK8 }; + Byte a { WHITE8 }; }; @@ -36,17 +39,48 @@ namespace image { Byte2 b : 5; Byte2 g : 6; Byte2 r : 5; + + RGB16_565() : b(BLACK8), g(BLACK8), r(BLACK8) {} + RGB16_565(Byte pR, Byte pG, Byte pB) : b(pB), g(pG), r(pR) {} }; + + + Byte mix5_4(const Byte x, const Byte y, const Byte a) { return mix<5, 4>(x, y, a); } + Byte mix6_4(const Byte x, const Byte y, const Byte a) { return mix<6, 4>(x, y, a); } + + + template const P mix(const P p0, const P p1, const S alpha) { return p0; } + + template <> const RGB16_565 mix(const RGB16_565 p0, const RGB16_565 p1, const Byte alpha); + - static int cpp; }; + template class Pixel { + public: + using Format = F; + using Storage = S; + + union { + Format val; + Storage raw; + }; + + Pixel() {}; + Pixel(Format v) : val(v) {} + Pixel(Storage s) : raw(s) {} + }; + + using PixRGB565 = Pixel; + using PixRGB32 = Pixel; + using PixRGBA32 = Pixel; + template class PixelBlock { public: static const uint32_t WIDTH { 4 }; static const uint32_t HEIGHT { WIDTH }; static const uint32_t SIZE { WIDTH * HEIGHT }; - uint32_t getByteSize() const { return SIZE * sizeof(P); } + uint32_t getSize() const { return SIZE * sizeof(P); } P pixels[SIZE]; @@ -58,38 +92,50 @@ namespace image { } void setPixels(const P* srcPixels) { - memcpy(pixels, srcPixels, getByteSize()); + memcpy(pixels, srcPixels, getSize()); } }; - template class CompressedBlock { - public: - uint8_t bytes[Size]; - }; - class BC { public: + static int cpp; + struct BC1 { - Pixel::RGB16_565 color_0; - Pixel::RGB16_565 color_1; - Pixel::Byte4 table; + PixRGB565 color0; + PixRGB565 color1; + pixel::Byte4 table; + }; + struct BC4 { + PixRGB565 color0; + PixRGB565 color1; + pixel::Byte4 table; }; }; - - + + template class CompressedBlock { + public: + static const uint32_t SIZE { sizeof(T) }; + union { + pixel::Byte bytes[SIZE]; + T bc; + }; + + CompressedBlock() {} + }; + template void compress(const PB& srcBlock, CB& dstBlock) { } template void uncompress(const CB& srcBlock, PB& dstBlock) { } - using PB_RGB32 = PixelBlock; - using PB_RGBA32 = PixelBlock; + using PB_RGB32 = PixelBlock; + using PB_RGBA32 = PixelBlock; - using CB_8 = CompressedBlock<8>; - using CB_16 = CompressedBlock<16>; + using CB_BC1 = CompressedBlock; + using CB_BC4 = CompressedBlock; - template <> void compress(const PB_RGB32& src, CB_8& dst); - template <> void compress(const PB_RGBA32& src, CB_8& dst); + template <> void compress(const PB_RGB32& src, CB_BC1& dst); + template <> void compress(const PB_RGBA32& src, CB_BC4& dst); - template <> void uncompress(const CB_8& src, PB_RGB32& dst); - template <> void uncompress(const CB_8& src, PB_RGBA32& dst); + template <> void uncompress(const CB_BC1& src, PB_RGB32& dst); + template <> void uncompress(const CB_BC4& src, PB_RGBA32& dst); } \ No newline at end of file diff --git a/libraries/model/src/model/TextureMap.cpp b/libraries/model/src/model/TextureMap.cpp index e80a8b2502..b0efbf2f30 100755 --- a/libraries/model/src/model/TextureMap.cpp +++ b/libraries/model/src/model/TextureMap.cpp @@ -202,7 +202,7 @@ gpu::Texture* TextureUsage::process2DTextureColorFromImage(const QImage& srcImag theTexture->assignStoredMip(0, formatMip, image.byteCount(), image.constBits()); image::PB_RGB32 pb; - image::CB_8 cb; + image::CB_BC1 cb; image::compress(pb, cb);