mirror of
https://github.com/vortexgpgpu/vortex.git
synced 2025-04-24 22:07:41 -04:00
cocogfx fixes and refactoring
This commit is contained in:
parent
a671e1a05d
commit
b995843a5b
44 changed files with 339 additions and 3921 deletions
|
@ -1,10 +1,12 @@
|
|||
#include "utils.h"
|
||||
#include <assert.h>
|
||||
#include <cstring>
|
||||
#include "blitter.h"
|
||||
#include "format.h"
|
||||
#include "tga.h"
|
||||
#include "lupng.h"
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <tga.h>
|
||||
#include <png.h>
|
||||
|
||||
using namespace cocogfx;
|
||||
|
||||
std::string getFileExt(const std::string& str) {
|
||||
auto i = str.rfind('.');
|
||||
|
@ -41,22 +43,9 @@ int LoadImage(const char *filename,
|
|||
return ret;
|
||||
} else
|
||||
if (iequals(ext, "png")) {
|
||||
auto image = luPngReadFile(filename, NULL);
|
||||
if (image == NULL)
|
||||
return -1;
|
||||
if (image->depth != 8
|
||||
|| (image->channels != 3
|
||||
&& image->channels != 4)) {
|
||||
luImageRelease(image, NULL);
|
||||
std::cerr << "invalid png file format!" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
pixels.resize(image->channels * image->width * image->height);
|
||||
memcpy(pixels.data(), image->data, pixels.size());
|
||||
img_width = image->width;
|
||||
img_height = image->height;
|
||||
img_bpp = image->channels;
|
||||
luImageRelease(image, NULL);
|
||||
int ret = LoadPNG(filename, pixels, &img_width, &img_height, &img_bpp);
|
||||
if (ret)
|
||||
return ret;
|
||||
} else {
|
||||
std::cerr << "invalid file extension: " << ext << "!" << std::endl;
|
||||
return -1;
|
||||
|
@ -83,7 +72,7 @@ int LoadImage(const char *filename,
|
|||
if (img_format != format) {
|
||||
// format conversion to RGBA
|
||||
std::vector<uint8_t> staging;
|
||||
int ret = ConvertImage(staging, pixels, img_width, img_height, img_format, format);
|
||||
int ret = ConvertImage(staging, format, pixels, img_format, img_width, img_height, img_width * img_bpp);
|
||||
if (ret)
|
||||
return ret;
|
||||
pixels.swap(staging);
|
||||
|
@ -100,19 +89,13 @@ int SaveImage(const char *filename,
|
|||
const std::vector<uint8_t> &pixels,
|
||||
uint32_t width,
|
||||
uint32_t height) {
|
||||
uint32_t bpp = Format::GetInfo(format).BytePerPixel;
|
||||
uint32_t bpp = GetInfo(format).BytePerPixel;
|
||||
auto ext = getFileExt(filename);
|
||||
if (iequals(ext, "tga")) {
|
||||
return SaveTGA(filename, pixels, width, height, bpp);
|
||||
} else
|
||||
if (iequals(ext, "png")) {
|
||||
LuImage image;
|
||||
image.width = width;
|
||||
image.height = height;
|
||||
image.depth = 8;
|
||||
image.channels = bpp;
|
||||
image.data = (uint8_t*)pixels.data();
|
||||
return luPngWriteFile(filename, &image);
|
||||
return SavePNG(filename, pixels, width, height, bpp);
|
||||
} else {
|
||||
std::cerr << "invalid file extension: " << ext << "!" << std::endl;
|
||||
return -1;
|
||||
|
@ -132,171 +115,8 @@ void dump_image(const std::vector<uint8_t>& pixels, uint32_t width, uint32_t hei
|
|||
pixel32 |= pixel8 << (b * 8);
|
||||
}
|
||||
if (x) std::cout << ", ";
|
||||
std::cout << std::hex << pixel32;
|
||||
std::cout << std::hex << std::setw(bpp * 2) << std::setfill('0') << pixel32;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
int CopyBuffers(SurfaceDesc &dstDesc,
|
||||
int32_t dstOffsetX,
|
||||
int32_t dstOffsetY,
|
||||
uint32_t copyWidth,
|
||||
uint32_t copyHeight,
|
||||
const SurfaceDesc &srcDesc,
|
||||
int32_t srcOffsetX,
|
||||
int32_t srcOffsetY) {
|
||||
|
||||
static const BlitTable s_blitTable;
|
||||
|
||||
if ((srcOffsetX >= (int32_t)srcDesc.Width) || (srcOffsetY >= (int32_t)srcDesc.Height) ||
|
||||
(dstOffsetX >= (int32_t)dstDesc.Width) || (dstOffsetY >= (int32_t)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;
|
||||
}
|
||||
|
||||
return s_blitTable.get(srcDesc.Format, dstDesc.Format)(
|
||||
dstDesc, dstOffsetX, dstOffsetY, copyWidth, copyHeight, srcDesc,
|
||||
srcOffsetX, srcOffsetY);
|
||||
}
|
||||
|
||||
int ConvertImage(std::vector<uint8_t>& dst_pixels,
|
||||
const std::vector<uint8_t>& src_pixels,
|
||||
uint32_t width,
|
||||
uint32_t height,
|
||||
ePixelFormat src_format,
|
||||
ePixelFormat dst_format) {
|
||||
|
||||
uint32_t src_pitch = Format::GetInfo(src_format).BytePerPixel * width;
|
||||
uint32_t dst_pitch = Format::GetInfo(dst_format).BytePerPixel * width;
|
||||
|
||||
dst_pixels.resize(dst_pitch * height);
|
||||
|
||||
SurfaceDesc srcDesc{src_format, (uint8_t*)src_pixels.data(), width, height, src_pitch};
|
||||
SurfaceDesc dstDesc{dst_format, dst_pixels.data(), width, height, dst_pitch};
|
||||
|
||||
return CopyBuffers(dstDesc, 0, 0, width, height, srcDesc, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int GenerateMipmaps(std::vector<uint8_t>& dst_pixels,
|
||||
std::vector<uint32_t>& mip_offsets,
|
||||
const std::vector<uint8_t>& src_pixels,
|
||||
ePixelFormat format,
|
||||
uint32_t src_width,
|
||||
uint32_t src_height) {
|
||||
std::vector<uint8_t> src_staging, dst_staging;
|
||||
const std::vector<uint8_t> *pSrcPixels;
|
||||
std::vector<uint8_t> *pDstPixels;
|
||||
|
||||
// convert source image if needed
|
||||
bool need_conversion = (format != FORMAT_A8R8G8B8);
|
||||
if (need_conversion) {
|
||||
ConvertImage(src_staging, src_pixels, src_width, src_height, format, FORMAT_A8R8G8B8);
|
||||
pSrcPixels = &src_staging;
|
||||
pDstPixels = &dst_staging;
|
||||
} else {
|
||||
pSrcPixels = &src_pixels;
|
||||
pDstPixels = &dst_pixels;
|
||||
}
|
||||
|
||||
uint32_t src_logwidth = log2ceil(src_width);
|
||||
uint32_t src_logheight = log2ceil(src_height);
|
||||
uint32_t max_lod = std::max(src_logwidth, src_logheight) + 1;
|
||||
|
||||
mip_offsets.resize(max_lod);
|
||||
|
||||
// Calculate mipmaps buffer size
|
||||
uint32_t dst_height = 1;
|
||||
uint32_t dst_width = 0;
|
||||
for (uint32_t lod = 0, w = src_width, h = src_height; lod < max_lod; ++lod) {
|
||||
assert((w > 0) || (w > 0));
|
||||
uint32_t pw = std::max<int>(w, 1);
|
||||
uint32_t ph = std::max<int>(h, 1);
|
||||
mip_offsets.at(lod) = dst_width;
|
||||
dst_width += pw * ph;
|
||||
w >>= 1;
|
||||
h >>= 1;
|
||||
}
|
||||
|
||||
// allocate mipmap
|
||||
pDstPixels->resize(dst_width * 4);
|
||||
|
||||
// generate mipmaps
|
||||
{
|
||||
auto pSrc = reinterpret_cast<const uint32_t*>(pSrcPixels->data());
|
||||
auto pDst = reinterpret_cast<uint32_t*>(pDstPixels->data());
|
||||
|
||||
// copy level 0
|
||||
memcpy(pDst, pSrc, pSrcPixels->size());
|
||||
assert(pSrcPixels->size() == 4 * src_width * src_height);
|
||||
pSrc = pDst;
|
||||
pDst += src_width * src_height;
|
||||
|
||||
// copy lower levels
|
||||
for (uint32_t lod = 1, w = (src_width/2), h = (src_height/2); lod < max_lod;) {
|
||||
assert((w > 0) || (w > 0));
|
||||
uint32_t pw = std::max<int>(w, 1);
|
||||
uint32_t ph = std::max<int>(h, 1);
|
||||
for (uint32_t y = 0; y < pw; ++y) {
|
||||
auto v0 = 2 * y;
|
||||
auto v1 = 2 * y + ((ph > 1) ? 1 : 0);
|
||||
auto pSrc0 = pSrc + v0 * (2 * pw);
|
||||
auto pSrc1 = pSrc + v1 * (2 * pw);
|
||||
|
||||
for (uint32_t x = 0; x <pw; ++x) {
|
||||
auto u0 = 2 * x;
|
||||
auto u1 = 2 * x + ((pw > 1) ? 1 : 0);
|
||||
|
||||
auto c00 = Format::ConvertFrom<FORMAT_A8R8G8B8, false>(pSrc0 + u0);
|
||||
auto c01 = Format::ConvertFrom<FORMAT_A8R8G8B8, false>(pSrc0 + u1);
|
||||
auto c10 = Format::ConvertFrom<FORMAT_A8R8G8B8, false>(pSrc1 + u0);
|
||||
auto c11 = Format::ConvertFrom<FORMAT_A8R8G8B8, false>(pSrc1 + u1);
|
||||
|
||||
const ColorARGB color((c00.a + c01.a + c10.a + c11.a+2) >> 2,
|
||||
(c00.r + c01.r + c10.r + c11.r+2) >> 2,
|
||||
(c00.g + c01.g + c10.g + c11.g+2) >> 2,
|
||||
(c00.b + c01.b + c10.b + c11.b+2) >> 2);
|
||||
|
||||
uint32_t ncolor;
|
||||
Format::ConvertTo<FORMAT_A8R8G8B8>(&ncolor, color);
|
||||
pDst[x + y * pw] = ncolor;
|
||||
}
|
||||
}
|
||||
++lod;
|
||||
pSrc = pDst;
|
||||
pDst += pw * ph;
|
||||
w >>= 1;
|
||||
h >>= 1;
|
||||
}
|
||||
assert((pDst - reinterpret_cast<uint32_t*>(pDstPixels->data())) == dst_width);
|
||||
}
|
||||
|
||||
// convert destination image if needed
|
||||
if (need_conversion) {
|
||||
ConvertImage(dst_staging, dst_staging, dst_width, dst_height, FORMAT_A8R8G8B8, format);
|
||||
}
|
||||
|
||||
uint32_t bpp = Format::GetInfo(format).BytePerPixel;
|
||||
for (auto& offset : mip_offsets) {
|
||||
offset *= bpp;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue