diff --git a/driver/tests/tex_demo/.depend b/driver/tests/tex_demo/.depend index 755676e6..eaa75ee4 100644 --- a/driver/tests/tex_demo/.depend +++ b/driver/tests/tex_demo/.depend @@ -1,2 +1,3 @@ -main.o: main.cpp ../../include/vortex.h common.h utils.h -utils.o: utils.cpp utils.h +main.o: main.cpp ../../include/vortex.h common.h utils.h blitter.h \ + format.h int24.h color.h +utils.o: utils.cpp utils.h blitter.h format.h int24.h color.h diff --git a/driver/tests/tex_demo/color.h b/driver/tests/tex_demo/color.h index d0460526..319ccd9b 100644 --- a/driver/tests/tex_demo/color.h +++ b/driver/tests/tex_demo/color.h @@ -61,4 +61,11 @@ struct ColorARGB { void operator=(const ColorARGB &rhs) { this->value = rhs.value; } + + uint32_t toRGBA() const { + // swap B/R channels + return (this->value & 0xff00ff00) + | ((this->value >> 16) & 0xff) + | ((this->value & 0xff) << 16); + } }; \ No newline at end of file diff --git a/driver/tests/tex_demo/demo b/driver/tests/tex_demo/demo index 54fe58d0..dfc9944f 100755 Binary files a/driver/tests/tex_demo/demo and b/driver/tests/tex_demo/demo differ diff --git a/driver/tests/tex_demo/main.cpp b/driver/tests/tex_demo/main.cpp index c61367e4..60b74867 100644 --- a/driver/tests/tex_demo/main.cpp +++ b/driver/tests/tex_demo/main.cpp @@ -20,7 +20,7 @@ /////////////////////////////////////////////////////////////////////////////// const char* kernel_file = "kernel.bin"; -const char* input_file = "palette16.tga"; +const char* input_file = "palette64.tga"; const char* output_file = "output.tga"; int wrap = 0; int filter = 0; diff --git a/driver/tests/tex_demo/palete4.tga b/driver/tests/tex_demo/palete4.tga deleted file mode 100644 index c342c47f..00000000 Binary files a/driver/tests/tex_demo/palete4.tga and /dev/null differ diff --git a/driver/tests/tex_demo/utils.cpp b/driver/tests/tex_demo/utils.cpp index dde621d6..5bdfb2b7 100644 --- a/driver/tests/tex_demo/utils.cpp +++ b/driver/tests/tex_demo/utils.cpp @@ -47,41 +47,52 @@ int LoadTGA(const char *filename, } switch (header.bitsperpixel) { + case 16: case 24: case 32: { auto stride = header.bitsperpixel / 8; - auto pitch = header.width * stride; - pixels.resize(header.height * pitch); + std::vector staging(stride * header.width * header.height); - // we are going to load the pixel data line by line - for (int y = 0; y < header.height; ++y) { - // Read current line of pixels - auto line = pixels.data() + y * pitch; - ifs.read(reinterpret_cast(line), pitch); - if (ifs.fail()) { - std::cerr << "invalid TGA file!" << std::endl; - return -1; - } + // Read pixels data + ifs.read((char*)staging.data(), staging.size()); + if (ifs.fail()) { + std::cerr << "invalid TGA file!" << std::endl; + return -1; + } - // TGA uses BGR instead of RGB, we must swap RG components - if (stride >= 3) { - for (int i = 0; i < pitch; i += stride) { - auto tmp = line[i]; - line[i] = line[i + 2]; - line[i + 2] = tmp; - } + // format conversion to RGBA + pixels.resize(4 * header.width * header.height); + const uint8_t* src_bytes = staging.data(); + uint32_t* dst_bytes = (uint32_t*)pixels.data(); + for (const uint8_t* const src_end = src_bytes + staging.size(); + src_bytes != src_end; + src_bytes += stride) { + ColorARGB color; + switch (stride) { + case 2: + color = Format::ConvertFrom(src_bytes); + break; + case 3: + color = Format::ConvertFrom(src_bytes); + break; + case 4: + color = Format::ConvertFrom(src_bytes); + break; + default: + std::abort(); } + *dst_bytes++ = color.toRGBA(); } break; } default: std::cerr << "unsupported TGA bitsperpixel!" << std::endl; return -1; - } + } *width = header.width; *height = header.height; - *bpp = header.bitsperpixel / 8; + *bpp = 4; return 0; } @@ -111,14 +122,16 @@ int SaveTGA(const char *filename, header.bitsperpixel = bpp * 8; header.imagedescriptor = 0; + // write header ofs.write(reinterpret_cast(&header), sizeof(tga_header_t)); + // write pixel data uint32_t pitch = bpp * width; const uint8_t* pixel_bytes = pixels.data() + (height - 1) * pitch; for (uint32_t y = 0; y < height; ++y) { const uint8_t* pixel_row = pixel_bytes; for (uint32_t x = 0; x < width; ++x) { - // TGA uses BGR instead of RGB, we must swap RG components + // swap R/B color channels if (bpp == 4) { ofs.write((const char*)pixel_row + 2, 1); ofs.write((const char*)pixel_row + 1, 1); @@ -129,7 +142,8 @@ int SaveTGA(const char *filename, ofs.write((const char*)pixel_row + 1, 1); ofs.write((const char*)pixel_row + 0, 1); } else{ - ofs.write((const char*)pixel_row, bpp); + std::cerr << "unsupported TGA bitsperpixel!" << std::endl; + return -1; } pixel_row += bpp; } @@ -154,4 +168,43 @@ void dump_image(const std::vector& pixels, uint32_t width, uint32_t hei } std::cout << std::endl; } +} + +int CopyBuffers(const GLSurfaceDesc &dstDesc, + int32_t dstOffsetX, + int32_t dstOffsetY, + int32_t copyWidth, + int32_t copyHeight, + const GLSurfaceDesc &srcDesc, + int32_t srcOffsetX, + int32_t srcOffsetY) { + + static const BlitTable s_blitTable; + + if ((srcOffsetX >= srcDesc.Width) || (srcOffsetY >= srcDesc.Height) || + (dstOffsetX >= dstDesc.Width) || (dstOffsetY >= dstDesc.Height)) { + return -1; + } + + if (copyWidth > dstDesc.Width) { + copyWidth = dstDesc.Width; + } + + if (copyWidth > srcDesc.Width) { + copyWidth = srcDesc.Width; + } + + if (copyHeight > dstDesc.Height) { + copyHeight = dstDesc.Height; + } + + if (copyHeight > srcDesc.Height) { + copyHeight = srcDesc.Height; + } + + s_blitTable.get(srcDesc.Format, dstDesc.Format)( + dstDesc, dstOffsetX, dstOffsetY, copyWidth, copyHeight, srcDesc, + srcOffsetX, srcOffsetY); + + return 0; } \ No newline at end of file diff --git a/driver/tests/tex_demo/utils.h b/driver/tests/tex_demo/utils.h index 24794a8f..65a7f412 100644 --- a/driver/tests/tex_demo/utils.h +++ b/driver/tests/tex_demo/utils.h @@ -1,6 +1,7 @@ #include #include #include +#include "blitter.h" int LoadTGA(const char *filename, std::vector &pixels, @@ -14,4 +15,13 @@ int SaveTGA(const char *filename, uint32_t height, uint32_t bpp); +int CopyBuffers(const GLSurfaceDesc &dstDesc, + int32_t dstOffsetX, + int32_t dstOffsetY, + int32_t copyWidth, + int32_t copyHeight, + const GLSurfaceDesc &srcDesc, + int32_t srcOffsetX, + int32_t srcOffsetY); + void dump_image(const std::vector& pixels, uint32_t width, uint32_t height, uint32_t bpp); \ No newline at end of file