1
0
Fork 0
Univerxel/src/client/render/vk/texture.cpp

75 lines
2.1 KiB
C++

#include "texture.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cassert>
#include <cmath>
#include <iostream>
#include <array>
#define FOURCC_DXT1 0x31545844 // Equivalent to "DXT1" in ASCII
#define FOURCC_DXT3 0x33545844 // Equivalent to "DXT3" in ASCII
#define FOURCC_DXT5 0x35545844 // Equivalent to "DXT5" in ASCII
VkResult render::vk::dds::readDDS(const std::string& imagepath, std::vector<unsigned char>& data, header_info& info) {
unsigned char header[124];
FILE *fp;
/* try to open the file */
fp = fopen(imagepath.c_str(), "rb");
if (fp == NULL){
printf("%s could not be opened.\n", imagepath.c_str()); getchar();
return VK_ERROR_INVALID_DEVICE_ADDRESS_EXT;
}
/* verify the type of file */
char filecode[4];
fread(filecode, 1, 4, fp);
if (strncmp(filecode, "DDS ", 4) != 0) {
fclose(fp);
return VK_ERROR_INVALID_DEVICE_ADDRESS_EXT;
}
/* get the surface desc */
fread(&header, 124, 1, fp);
info.height = *(unsigned int*)&(header[8 ]);
info.width = *(unsigned int*)&(header[12]);
unsigned int linearSize = *(unsigned int*)&(header[16]);
info.mipMapCount = *(unsigned int*)&(header[24]);
unsigned int fourCC = *(unsigned int*)&(header[80]);
/* how big is it going to be including all mipmaps? */
unsigned int bufsize = info.mipMapCount > 1 ? linearSize * 2 : linearSize;
data.resize(bufsize);
fread(data.data(), 1, bufsize, fp);
/* close the file pointer */
fclose(fp);
switch(fourCC)
{
case FOURCC_DXT1:
info.format = VK_FORMAT_BC1_RGBA_SRGB_BLOCK;
break;
case FOURCC_DXT3:
info.format = VK_FORMAT_BC2_SRGB_BLOCK;
break;
case FOURCC_DXT5:
info.format = VK_FORMAT_BC3_SRGB_BLOCK;
break;
//MAYBE: VK_FORMAT_BC6H_SFLOAT_BLOCK
default:
return VK_ERROR_FORMAT_NOT_SUPPORTED;
}
#if FALSE
glTextureParameteri(textureID, GL_TEXTURE_MAG_FILTER, linear ? GL_LINEAR : GL_NEAREST);
glTextureParameteri(textureID, GL_TEXTURE_MIN_FILTER, linear ? GL_LINEAR_MIPMAP_LINEAR : GL_NEAREST);
TODO: glGenerateTextureMipmap(textureID);
#endif
return VK_SUCCESS;
}