Skip to content

Commit

Permalink
Fix Surfaces and Power of Two Typo (#2159)
Browse files Browse the repository at this point in the history
Don't resize surface textures to a power of two. Also corrects an old off-by-one in a5bcd1b for the next largest power of two function that was permeating the engine.
  • Loading branch information
RobertBColton committed Nov 16, 2020
1 parent 148a56c commit 63f99db
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 20 deletions.
Expand Up @@ -33,12 +33,14 @@ int graphics_create_texture(const RawImage& img, bool mipmap, unsigned* fullwidt
D3D11_TEXTURE2D_DESC tdesc;
D3D11_SUBRESOURCE_DATA tbsd;

unsigned fw, fh;
unsigned fw = img.w, fh = img.h;
if (fullwidth == nullptr) fullwidth = &fw;
if (fullheight == nullptr) fullheight = &fh;

*fullwidth = nlpo2dc(img.w) + 1;
*fullheight = nlpo2dc(img.h) + 1;
if (img.pxdata != nullptr) {
*fullwidth = nlpo2(img.w);
*fullheight = nlpo2(img.h);
}

tbsd.SysMemPitch = (*fullwidth) * 4;
// not needed since this is a 2d texture,
Expand Down
8 changes: 5 additions & 3 deletions ENIGMAsystem/SHELL/Graphics_Systems/Direct3D9/DX9textures.cpp
Expand Up @@ -137,12 +137,14 @@ void graphics_push_texture_pixels(int texture, int x, int y, int width, int heig

int graphics_create_texture(const RawImage& img, bool mipmap, unsigned* fullwidth, unsigned* fullheight)
{
unsigned fw, fh;
unsigned fw = img.w, fh = img.h;
if (fullwidth == nullptr) fullwidth = &fw;
if (fullheight == nullptr) fullheight = &fh;

*fullwidth = nlpo2dc(img.w)+1;
*fullheight = nlpo2dc(img.h)+1;
if (img.pxdata != nullptr) {
*fullwidth = nlpo2(img.w);
*fullheight = nlpo2(img.h);
}

LPDIRECT3DTEXTURE9 texture = NULL;

Expand Down
Expand Up @@ -230,7 +230,7 @@ namespace enigma_user {
enigma::texture_atlas_array.emplace(id,enigma::texture_atlas());

if (w != -1 && h != -1){ //If we set the size manually
unsigned int fullwidth = enigma::nlpo2dc(w)+1, fullheight = enigma::nlpo2dc(h)+1; //We only take power of two
unsigned int fullwidth = enigma::nlpo2(w), fullheight = enigma::nlpo2(h); //We only take power of two
enigma::texture_atlas_array[id].width = fullwidth;
enigma::texture_atlas_array[id].height = fullheight;
enigma::texture_atlas_array[id].texture = enigma::graphics_create_texture(enigma::RawImage(nullptr, fullwidth, fullheight), false);
Expand Down
Expand Up @@ -38,12 +38,14 @@ GLuint get_texture_peer(int texid) {
//This allows GL3 surfaces to bind and hold many different types of data
int graphics_create_texture_custom(const RawImage& img, bool mipmap, unsigned* fullwidth, unsigned* fullheight, GLint internalFormat, GLenum format, GLenum type)
{
unsigned fw, fh;
unsigned fw = img.w, fh = img.h;
if (fullwidth == nullptr) fullwidth = &fw;
if (fullheight == nullptr) fullheight = &fh;

*fullwidth = nlpo2dc(img.w)+1;
*fullheight = nlpo2dc(img.h)+1;
if (img.pxdata != nullptr) {
*fullwidth = nlpo2(img.w);
*fullheight = nlpo2(img.h);
}

bool pad = img.pxdata != nullptr && (img.w != *fullwidth || img.h != *fullheight);

Expand Down
4 changes: 2 additions & 2 deletions ENIGMAsystem/SHELL/Universal_System/Resources/backgrounds.cpp
Expand Up @@ -27,7 +27,7 @@

using enigma::Background;
using enigma::backgrounds;
using enigma::nlpo2dc;
using enigma::nlpo2;
using enigma::TexRect;
using enigma::Color;
using enigma::RawImage;
Expand Down Expand Up @@ -74,7 +74,7 @@ int background_add(std::string filename, bool transparent, bool smooth, bool pre
}

int background_create_color(unsigned w, unsigned h, int col, bool preload) {
unsigned int fullwidth = nlpo2dc(w) + 1, fullheight = nlpo2dc(h) + 1;
unsigned int fullwidth = nlpo2(w), fullheight = nlpo2(h);
RawImage img(new unsigned char[fullwidth * fullheight * 4], fullwidth, fullheight);
std::fill((unsigned*)(img.pxdata), (unsigned*)(img.pxdata) + fullwidth * fullheight, (COL_GET_R(col) | (COL_GET_G(col) << 8) | (COL_GET_B(col) << 16) | 255 << 24));
int textureID = graphics_create_texture(img, false);
Expand Down
2 changes: 0 additions & 2 deletions ENIGMAsystem/SHELL/Universal_System/Resources/sprites.cpp
Expand Up @@ -19,7 +19,6 @@

#include "sprites_internal.h"
#include "Universal_System/image_formats.h"
#include "Universal_System/nlpo2.h"
#include "Graphics_Systems/graphics_mandatory.h"
#include "Graphics_Systems/General/GStextures.h"
#include "Graphics_Systems/General/GScolor_macros.h"
Expand All @@ -29,7 +28,6 @@

using enigma::Sprite;
using enigma::sprites;
using enigma::nlpo2dc;
using enigma::TexRect;
using enigma::Color;
using enigma::RawImage;
Expand Down
2 changes: 1 addition & 1 deletion ENIGMAsystem/SHELL/Universal_System/image_formats.cpp
Expand Up @@ -186,7 +186,7 @@ RawImage image_crop(const RawImage& in, unsigned newWidth, unsigned newHeight) {
}

unsigned long *bgra_to_argb(unsigned char *bgra_data, unsigned pngwidth, unsigned pngheight, bool prepend_size) {
unsigned widfull = nlpo2dc(pngwidth) + 1, hgtfull = nlpo2dc(pngheight) + 1, ih, iw;
unsigned widfull = nlpo2(pngwidth), hgtfull = nlpo2(pngheight), ih, iw;
const int bitmap_size = widfull * hgtfull * 4;
unsigned char *bitmap = new unsigned char[bitmap_size]();

Expand Down
12 changes: 7 additions & 5 deletions ENIGMAsystem/SHELL/Universal_System/nlpo2.h
Expand Up @@ -26,23 +26,25 @@
\********************************************************************************/

// This was forged in hell.
#ifndef ENIGMA_NLPO2DC_H
#define ENIGMA_NLPO2DC_H
#ifndef ENIGMA_NLPO2_H
#define ENIGMA_NLPO2_H

#include <stdio.h>

namespace enigma {

inline unsigned int nlpo2dc(unsigned int x) // Taking x, returns n such that n = 2**k where k is an integer and n >= x.
inline unsigned int nlpo2(unsigned int x) // Taking x, returns n such that n = 2**k where k is an integer and n >= x.
{
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
return x | (x >> 16);
x |= x >> 16;
++x;
return x;
}

} //namespace enigma

#endif //ENIGMA_NLPO2DC_H
#endif //ENIGMA_NLPO2_H

0 comments on commit 63f99db

Please sign in to comment.