2019-10-20 06:07:12 +00:00
|
|
|
/*
|
|
|
|
** gl_hwtexture.cpp
|
|
|
|
** GL texture abstraction
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 2019 Christoph Oelckers
|
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
**
|
|
|
|
*/
|
2019-09-17 17:03:42 +00:00
|
|
|
|
2019-10-16 21:09:02 +00:00
|
|
|
#include <algorithm>
|
2019-12-23 14:40:17 +00:00
|
|
|
#include "gl_load.h"
|
2019-09-17 17:03:42 +00:00
|
|
|
#include "glbackend.h"
|
2019-10-05 19:59:03 +00:00
|
|
|
#include "bitmap.h"
|
2019-11-09 22:58:26 +00:00
|
|
|
#include "c_dispatch.h"
|
|
|
|
#include "printf.h"
|
2019-09-17 17:03:42 +00:00
|
|
|
//#include "compat.h"
|
|
|
|
|
|
|
|
// Workaround to avoid including the dirty 'compat.h' header. This will hopefully not be needed anymore once the texture format uses something better.
|
|
|
|
# define B_LITTLE_ENDIAN 1
|
|
|
|
# define B_BIG_ENDIAN 0
|
|
|
|
|
2019-11-09 22:58:26 +00:00
|
|
|
uint64_t alltexturesize;
|
|
|
|
|
|
|
|
CCMD(alltexturesize)
|
|
|
|
{
|
|
|
|
Printf("All textures are %llu bytes\n", alltexturesize);
|
|
|
|
}
|
2019-09-17 17:03:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Allocates an empty texture
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2019-10-19 21:14:36 +00:00
|
|
|
unsigned int FHardwareTexture::CreateTexture(int w, int h, int type, bool mipmapped)
|
2019-09-17 17:03:42 +00:00
|
|
|
{
|
2019-10-19 21:14:36 +00:00
|
|
|
static int gltypes[] = { GL_R8, GL_RGBA8, GL_RGB5_A1, GL_RGBA2 };
|
2019-11-09 22:58:26 +00:00
|
|
|
static uint8_t bytes[] = { 1, 4, 2, 1 };
|
2020-01-02 22:15:16 +00:00
|
|
|
glGenTextures(1, &glTexID);
|
2019-09-17 17:03:42 +00:00
|
|
|
glActiveTexture(GL_TEXTURE15);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, glTexID);
|
|
|
|
int size = std::max(w, h);
|
|
|
|
int bits = 0;
|
|
|
|
while (size) bits++, size >>= 1;
|
2019-10-19 21:14:36 +00:00
|
|
|
internalType = type;
|
|
|
|
if (type == Indexed) mipmapped = false;
|
2019-09-17 17:03:42 +00:00
|
|
|
mWidth = w;
|
|
|
|
mHeight = h;
|
|
|
|
|
2019-11-09 22:58:26 +00:00
|
|
|
allocated = w * h;
|
|
|
|
if (mipmapped)
|
|
|
|
{
|
|
|
|
|
|
|
|
for (auto mip = allocated>>2; mip > 0; mip >>= 2)
|
|
|
|
{
|
|
|
|
allocated += mip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
allocated *= bytes[type];
|
|
|
|
alltexturesize += allocated;
|
|
|
|
|
2019-10-19 21:14:36 +00:00
|
|
|
glTexStorage2D(GL_TEXTURE_2D, mipmapped? bits : 1, gltypes[type], w, h);
|
2019-09-17 17:03:42 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
this->mipmapped = mipmapped;
|
|
|
|
return glTexID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Loads the texture image into the hardware
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2019-10-19 08:21:07 +00:00
|
|
|
unsigned int FHardwareTexture::LoadTexture(const unsigned char * buffer)
|
2019-10-07 20:11:09 +00:00
|
|
|
{
|
2019-10-19 08:21:07 +00:00
|
|
|
return LoadTexturePart(buffer, 0, 0, mWidth, mHeight);
|
2019-10-07 20:11:09 +00:00
|
|
|
}
|
|
|
|
|
2019-10-19 08:21:07 +00:00
|
|
|
unsigned int FHardwareTexture::LoadTexture(FBitmap& bmp)
|
2019-10-07 20:11:09 +00:00
|
|
|
{
|
2019-10-19 08:21:07 +00:00
|
|
|
return LoadTexture(bmp.GetPixels());
|
2019-10-07 20:11:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Loads the texture image into the hardware
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2019-10-19 08:21:07 +00:00
|
|
|
unsigned int FHardwareTexture::LoadTexturePart(const unsigned char* buffer, int x, int y, int w, int h)
|
2019-09-17 17:03:42 +00:00
|
|
|
{
|
|
|
|
if (glTexID == 0) return 0;
|
2019-09-18 18:44:21 +00:00
|
|
|
|
2019-10-19 21:14:36 +00:00
|
|
|
int srcformat = internalType == Indexed ? GL_RED : GL_BGRA;// TexFormat[gl_texture_format];
|
2019-09-17 17:03:42 +00:00
|
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE15);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, glTexID);
|
|
|
|
|
2019-10-19 21:14:36 +00:00
|
|
|
if (internalType == Indexed) glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
2019-10-07 20:11:09 +00:00
|
|
|
|
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, srcformat, GL_UNSIGNED_BYTE, buffer);
|
2019-09-17 17:03:42 +00:00
|
|
|
if (mipmapped) glGenerateMipmap(GL_TEXTURE_2D);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
2019-10-19 21:14:36 +00:00
|
|
|
if (internalType == Indexed) glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
2019-09-17 17:03:42 +00:00
|
|
|
return glTexID;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Destroys the texture
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
FHardwareTexture::~FHardwareTexture()
|
|
|
|
{
|
2019-11-09 22:58:26 +00:00
|
|
|
alltexturesize -= allocated;
|
2019-09-17 17:03:42 +00:00
|
|
|
if (glTexID != 0) glDeleteTextures(1, &glTexID);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int FHardwareTexture::GetTextureHandle()
|
|
|
|
{
|
|
|
|
return glTexID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|