Skip to content

Commit

Permalink
only pad images in create texture (#2008)
Browse files Browse the repository at this point in the history
  • Loading branch information
fundies committed Aug 7, 2020
1 parent 2e697aa commit 2d1220f
Show file tree
Hide file tree
Showing 41 changed files with 598 additions and 553 deletions.
19 changes: 19 additions & 0 deletions CI/install_emake_deps.sh
Expand Up @@ -25,6 +25,9 @@ if [ "$TRAVIS_OS_NAME" != "osx" ]; then
else
# new lcov
sudo add-apt-repository -y ppa:cheeseboy16/travis-backports
# new clang
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo apt-add-repository "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-10 main"
fi

install_yaml_cpp
Expand All @@ -44,6 +47,22 @@ if [ "$TRAVIS_OS_NAME" != "osx" ]; then
--slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-9;

g++ --version

if [ "$COMPILER" != "Android" ]; then
sudo apt-get -y install clang-10 lldb-10 lld-10 libc++abi-10-dev libc++-10-dev
# Remove clang symlinks from the travis base install.
# The update-alternatives changes will not change the used clang version without this.
# /usr/local is searched for clang binaries before the /usr/bin direcory.
sudo rm -rf /usr/local/clang-*

sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-10 20
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-10 20
sudo update-alternatives --install /usr/bin/llvm-config llvm-config /usr/bin/llvm-config-10 20
sudo update-alternatives --install /usr/bin/llvm-link llvm-link /usr/bin/llvm-link-10 20
sudo update-alternatives --install /usr/bin/llvm-dis llvm-dis /usr/bin/llvm-dis-10 20
fi

clang++ --version
fi

if [ "$COMPILER" == "Android" ]; then
Expand Down
11 changes: 11 additions & 0 deletions CommandLine/testing/Tests/image_load_save_test.cpp
@@ -0,0 +1,11 @@
#include "CommonTest.hpp"

TEST(Regression, image_load_save_test) {
if (!TestHarness::windowing_supported()) return;
// Iterate only platforms and graphics systems
for (TestConfig tc : GetValidConfigs(true, true, false, false, false, false)) {
tc.extensions = "Paths,libpng";
auto test_harness = LAUNCH_HARNESS_FOR_SOG(tc);
test_common(test_harness.get(), "image_load_save_test" + tc.stringify());
}
}
49 changes: 49 additions & 0 deletions CommandLine/testing/Tests/image_load_save_test.sog/create.edl
@@ -0,0 +1,49 @@
// Sprites

// Non power of 2
npow2_spr_gif = sprite_add("CommandLine/testing/data/npow2.gif", -1, false, false, 0, 0);
// Save frames as bmp/png
sprite_save(npow2_spr_gif, 0, "/tmp/img.bmp");
sprite_save(npow2_spr_gif, 1, "/tmp/img.png");
npow2_spr_bmp = sprite_add("/tmp/img.bmp", 1, false, false, 0, 0);
npow2_spr_png = sprite_add("/tmp/img.png", 1, false, false, 0, 0);

file_delete("/tmp/img.bmp")
file_delete("/tmp/img.png")

// Power of 2
pow2_spr_gif = sprite_add("CommandLine/testing/data/pow2.gif", -1, true, false, 0, 0);
// Save frames as bmp/png
sprite_save(pow2_spr_gif, 0, "/tmp/img.bmp");
sprite_save(pow2_spr_gif, 1, "/tmp/img.png");
pow2_spr_bmp = sprite_add("/tmp/img.bmp", 1, false, false, 0, 0);
pow2_spr_png = sprite_add("/tmp/img.png", 1, false, false, 0, 0);

// Strip
spr_strip = sprite_add("CommandLine/testing/data/sprite.png", 4, true, false, 0, 0);

file_delete("/tmp/img.bmp")
file_delete("/tmp/img.png")

// Backgrounds

// Non power of 2
npow2_bkg_gif = background_add("CommandLine/testing/data/npow2.gif", false, false);
// Save frames as bmp/png
background_save(npow2_bkg_gif, "/tmp/img.bmp");
background_save(npow2_bkg_gif, "/tmp/img.png");
npow2_bkg_bmp = background_add("/tmp/img.bmp", 1, false, false);
npow2_bkg_png = background_add("/tmp/img.png", 1, false, false);

file_delete("/tmp/img.bmp")
file_delete("/tmp/img.png")

// Power of 2
pow2_bkg_gif = background_add("CommandLine/testing/data/pow2.gif", true, false);
background_save(pow2_bkg_gif, "/tmp/img.bmp");
background_save(pow2_bkg_gif, "/tmp/img.png");
pow2_bkg_bmp = background_add("/tmp/img.bmp", 1, false, false);
pow2_bkg_png = background_add("/tmp/img.png", 1, false, false);

file_delete("/tmp/img.bmp")
file_delete("/tmp/img.png")
24 changes: 24 additions & 0 deletions CommandLine/testing/Tests/image_load_save_test.sog/draw.edl
@@ -0,0 +1,24 @@
// Draw sprites
draw_sprite(npow2_spr_gif, 0, 0, 0);
draw_sprite(npow2_spr_gif, 1, 70, 0);

draw_sprite(npow2_spr_bmp, 0, 0, 35);
draw_sprite(npow2_spr_png, 1, 70, 35);

draw_sprite(pow2_spr_gif, 0, 0, 70);
draw_sprite(pow2_spr_gif, 1, 64, 70);

draw_sprite(pow2_spr_bmp, 0, 0, 134);
draw_sprite(pow2_spr_png, 1, 64, 134);

for (int i = 0 i < 4; ++i)
draw_sprite(spr_strip, i, i*64, 198);

// Draw backgrounds
draw_background(npow2_bkg_gif, 0, 262);
draw_background(npow2_bkg_bmp, 70, 262);
draw_background(npow2_bkg_png, 140, 262);

draw_background(pow2_bkg_gif, 0, 297);
draw_background(pow2_bkg_bmp, 64, 297);
draw_background(pow2_bkg_png, 128, 297);
Binary file added CommandLine/testing/data/npow2.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CommandLine/testing/data/pow2.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion Compilers/Linux/gcc.ey
Expand Up @@ -14,7 +14,7 @@ Make-Vars:
cppflags:
cxxflags: -fdiagnostics-color=always
cflags:
ldflags:
ldflags: -lstdc++fs
links:

Parser-Vars:
Expand Down
2 changes: 1 addition & 1 deletion ENIGMAsystem/SHELL/Graphics_Systems/Direct3D11/DX11d3d.cpp
Expand Up @@ -46,7 +46,7 @@ ID3D11ShaderResourceView *getDefaultWhiteTexture() {
static int texid = -1;
if (texid == -1) {
unsigned data[1] = {0xFFFFFFFF};
texid = enigma::graphics_create_texture(1, 1, 1, 1, (void*)data, false);
texid = enigma::graphics_create_texture(enigma::RawImage((unsigned char*)data, 1, 1) , false);
}
return static_cast<enigma::DX11Texture*>(enigma::textures[texid].get())->view;
}
Expand Down
35 changes: 24 additions & 11 deletions ENIGMAsystem/SHELL/Graphics_Systems/Direct3D11/DX11textures.cpp
Expand Up @@ -21,25 +21,38 @@
#include "Graphics_Systems/General/GStextures.h"
#include "Graphics_Systems/General/GStextures_impl.h"
#include "Graphics_Systems/General/GSprimitives.h"
#include "Universal_System/image_formats.h"
#include "Universal_System/nlpo2.h"

using namespace enigma::dx11;

namespace enigma {

int graphics_create_texture(unsigned width, unsigned height, unsigned fullwidth, unsigned fullheight, void* pxdata, bool mipmap)
int graphics_create_texture(const RawImage& img, bool mipmap, unsigned* fullwidth, unsigned* fullheight)
{
ID3D11Texture2D *tex;
D3D11_TEXTURE2D_DESC tdesc;
D3D11_SUBRESOURCE_DATA tbsd;

tbsd.pSysMem = pxdata;
tbsd.SysMemPitch = fullwidth*4;

unsigned fw, fh;
if (fullwidth == nullptr) fullwidth = &fw;
if (fullheight == nullptr) fullheight = &fh;

*fullwidth = nlpo2dc(img.w)+1;
*fullheight = nlpo2dc(img.h)+1;

if (img.pxdata != nullptr && (img.w != *fullwidth || img.h != *fullheight)) {
RawImage padded = image_pad(img, *fullwidth, *fullheight);
tbsd.pSysMem = padded.pxdata;
} else tbsd.pSysMem = img.pxdata;

tbsd.SysMemPitch = *fullwidth*4;
// not needed since this is a 2d texture,
// but we can pass size info for debugging
tbsd.SysMemSlicePitch = fullwidth*fullheight*4;
tbsd.SysMemSlicePitch = *fullwidth*(*fullheight)*4;

tdesc.Width = fullwidth;
tdesc.Height = fullheight;
tdesc.Width = *fullwidth;
tdesc.Height = *fullheight;
tdesc.MipLevels = 1;
tdesc.ArraySize = 1;

Expand Down Expand Up @@ -67,10 +80,10 @@ int graphics_create_texture(unsigned width, unsigned height, unsigned fullwidth,
const int id = textures.size();
textures.push_back(std::make_unique<DX11Texture>(tex, view));
auto& textureStruct = textures.back();
textureStruct->width = width;
textureStruct->height = height;
textureStruct->fullwidth = fullwidth;
textureStruct->fullheight = fullheight;
textureStruct->width = img.w;
textureStruct->height = img.h;
textureStruct->fullwidth = *fullwidth;
textureStruct->fullheight = *fullheight;
return id;
}

Expand Down
31 changes: 23 additions & 8 deletions ENIGMAsystem/SHELL/Graphics_Systems/Direct3D9/DX9textures.cpp
Expand Up @@ -21,6 +21,8 @@
#include "Graphics_Systems/General/GStextures.h"
#include "Graphics_Systems/General/GStextures_impl.h"
#include "Graphics_Systems/General/GSprimitives.h"
#include "Universal_System/image_formats.h"
#include "Universal_System/nlpo2.h"

#include <string.h> // for memcpy
using namespace enigma::dx9;
Expand Down Expand Up @@ -133,23 +135,36 @@ void graphics_push_texture_pixels(int texture, int x, int y, int width, int heig
}
}

int graphics_create_texture(unsigned width, unsigned height, unsigned fullwidth, unsigned fullheight, void* pxdata, bool mipmap)
int graphics_create_texture(const RawImage& img, bool mipmap, unsigned* fullwidth, unsigned* fullheight)
{
unsigned fw, fh;
if (fullwidth == nullptr) fullwidth = &fw;
if (fullheight == nullptr) fullheight = &fh;

*fullwidth = nlpo2dc(img.w)+1;
*fullheight = nlpo2dc(img.h)+1;

LPDIRECT3DTEXTURE9 texture = NULL;

DWORD usage = mipmap?D3DUSAGE_AUTOGENMIPMAP:0;
d3ddev->CreateTexture(fullwidth, fullheight, 1, usage, D3DFMT_A8R8G8B8, Direct3D9Managed ? D3DPOOL_MANAGED : D3DPOOL_DEFAULT, &texture, 0);
d3ddev->CreateTexture(*fullwidth, *fullheight, 1, usage, D3DFMT_A8R8G8B8, Direct3D9Managed ? D3DPOOL_MANAGED : D3DPOOL_DEFAULT, &texture, 0);

const int id = textures.size();
if (img.pxdata != nullptr) {
if (img.w != *fullwidth || img.h != *fullheight) {
RawImage padded = image_pad(img, *fullwidth, *fullheight);
graphics_push_texture_pixels(id, 0, 0, img.w, img.h, *fullwidth, *fullheight, padded.pxdata);
} else graphics_push_texture_pixels(id, 0, 0, img.w, img.h, *fullwidth, *fullheight, img.pxdata);
}

if (mipmap) texture->GenerateMipSubLevels();

const int id = textures.size();
textures.push_back(std::make_unique<DX9Texture>(texture));
auto& textureStruct = textures.back();
textureStruct->width = width;
textureStruct->height = height;
textureStruct->fullwidth = fullwidth;
textureStruct->fullheight = fullheight;
if (pxdata != nullptr) graphics_push_texture_pixels(id, 0, 0, width, height, fullwidth, fullheight, (unsigned char*)pxdata);
textureStruct->width = img.w;
textureStruct->height = img.h;
textureStruct->fullwidth = *fullwidth;
textureStruct->fullheight = *fullheight;
return id;
}

Expand Down
26 changes: 9 additions & 17 deletions ENIGMAsystem/SHELL/Graphics_Systems/General/GSscreen.cpp
Expand Up @@ -67,28 +67,18 @@ RawImage create_from_screen_helper(int x, int y, int w, int h, bool removeback,

bool flipped = false;

unsigned char* rgba = enigma::graphics_copy_screen_pixels(x, y, w, h, &flipped);
RawImage i(enigma::graphics_copy_screen_pixels(x, y, w, h, &flipped), w, h);

// FIXME: This logic is duplicated here, backgrounds.cpp, sprites_internal.cpp in this file somewhat and likely 12 other places
if (flipped)
rgba = enigma::image_flip(rgba, w, h, 4);
enigma::image_flip(i);

if (removeback) {
Color c = enigma::image_get_pixel_color(rgba, w, h, 0, h - 1);
enigma::image_swap_color(rgba, w, h, c, Color {0, 0, 0, 0});
Color c = enigma::image_get_pixel_color(i, 0, h - 1);
enigma::image_swap_color(i, c, Color {0, 0, 0, 0});
}

if (pad) {
RawImage i = image_pad(rgba, w, h, nlpo2dc(w)+1, nlpo2dc(h)+1);
delete[] rgba;
return i;
} else {
RawImage i;
i.pxdata = rgba;
i.w = w;
i.h = h;
return i;
}
return i;
}

//These are used to reset the screen viewport for surfaces
Expand Down Expand Up @@ -466,7 +456,9 @@ int screen_save_part(string filename,unsigned x,unsigned y,unsigned w,unsigned h

int background_create_from_screen(int x, int y, int w, int h, bool removeback, bool smooth, bool preload) {
RawImage img = create_from_screen_helper(x, y, w, h, removeback, true);
Background bkg(w, h, graphics_create_texture(w, h, img.w, img.h, img.pxdata, false));
unsigned fullwidth, fullheight;
int texID = graphics_create_texture(img, false, &fullwidth, &fullheight);
Background bkg(w, h, fullwidth, fullheight, texID);
return backgrounds.add(std::move(bkg));
}

Expand All @@ -485,7 +477,7 @@ int sprite_create_from_screen(int x, int y, int w, int h, bool removeback, bool
void sprite_add_from_screen(int id, int x, int y, int w, int h, bool removeback, bool smooth) {
RawImage img = create_from_screen_helper(x, y, w, h, removeback, false);
Sprite& spr = sprites.get(id);
spr.AddSubimage(img.pxdata, w, h, enigma::ct_precise, img.pxdata); //TODO: Support toggling of precise.
spr.AddSubimage(img, enigma::ct_precise, img.pxdata); //TODO: Support toggling of precise.
}

int draw_getpixel(int x,int y)
Expand Down
6 changes: 5 additions & 1 deletion ENIGMAsystem/SHELL/Graphics_Systems/General/GSsprite.cpp
Expand Up @@ -38,7 +38,11 @@ using enigma::Sprite;
using enigma::sprites;
using enigma::TexRect;

inline int get_usi(const Sprite& spr2d, int subimg) { return subimg >= 0 ? (subimg % spr2d.SubimageCount()) : int(((enigma::object_graphics*)enigma::instance_event_iterator->inst)->image_index) % spr2d.SubimageCount(); }
inline int get_usi(const Sprite& spr2d, int subimg) {
if (spr2d.SubimageCount() == 0) return 0;
return subimg >= 0 ? (subimg % spr2d.SubimageCount()) : int(((enigma::object_graphics*)enigma::instance_event_iterator->inst)->image_index) % spr2d.SubimageCount();
}

// VD: see https://yal.cc/2d-pivot-points/ for math
inline gs_scalar rotx(gs_scalar qx, gs_scalar qy, gs_scalar rx, gs_scalar ry) { return ((qx) * rx - (qy) * ry); }
inline gs_scalar roty(gs_scalar qx, gs_scalar qy, gs_scalar rx, gs_scalar ry) { return ((qx) * ry + (qy) * rx); }
Expand Down
25 changes: 10 additions & 15 deletions ENIGMAsystem/SHELL/Graphics_Systems/General/GSsurface.cpp
Expand Up @@ -376,17 +376,16 @@ int background_create_from_surface(int id, int x, int y, int w, int h, bool remo
get_surfacev(surf,id,-1);
const enigma::BaseSurface& base = ((enigma::BaseSurface&)surf);

unsigned char *surfbuf = enigma::graphics_copy_texture_pixels(base.texture,x,y,w,h);
enigma::RawImage s(enigma::graphics_copy_texture_pixels(base.texture,x,y,w,h), w, h);

if (removeback) {
enigma::Color c = enigma::image_get_pixel_color(surfbuf, w, h, 0, h - 1);
enigma::image_swap_color(surfbuf, w, h, c, enigma::Color {0, 0, 0, 0});
enigma::Color c = enigma::image_get_pixel_color(s, 0, h - 1);
enigma::image_swap_color(s, c, enigma::Color {0, 0, 0, 0});
}

enigma::RawImage img = enigma::image_pad(surfbuf, w, h, enigma::nlpo2dc(w)+1, enigma::nlpo2dc(h)+1);
enigma::Background bkg(w, h, enigma::graphics_create_texture(w, h, img.w, img.h, img.pxdata, false));

delete[] surfbuf;
unsigned fullwidth, fullheight;
int texID = enigma::graphics_create_texture(s, false, &fullwidth, &fullheight);
enigma::Background bkg(w, h, fullwidth, fullheight, texID);

return enigma::backgrounds.add(std::move(bkg));
}
Expand All @@ -398,12 +397,10 @@ int sprite_create_from_surface(int id, int x, int y, int w, int h, bool removeba
get_surfacev(surf,id,-1);
const enigma::BaseSurface& base = ((enigma::BaseSurface&)surf);

unsigned char *surfbuf=enigma::graphics_copy_texture_pixels(base.texture,x,y,w,h);
enigma::RawImage surfImg(enigma::graphics_copy_texture_pixels(base.texture,x,y,w,h), w, h);

Sprite spr(w, h, xorig, yorig);
spr.AddSubimage(surfbuf, w, h, enigma::ct_precise, surfbuf); //TODO: Support toggling of precise.

delete[] surfbuf;
spr.AddSubimage(surfImg, enigma::ct_precise, surfImg.pxdata); //TODO: Support toggling of precise.

return sprites.add(std::move(spr));
}
Expand All @@ -420,12 +417,10 @@ void sprite_add_from_surface(int ind, int id, int x, int y, int w, int h, bool r
get_surface(surf,id);
const enigma::BaseSurface& base = ((enigma::BaseSurface&)surf);

unsigned char *surfbuf=enigma::graphics_copy_texture_pixels(base.texture,x,y,w,h);
enigma::RawImage surfImg(enigma::graphics_copy_texture_pixels(base.texture,x,y,w,h), w, h);

Sprite& spr = sprites.get(ind);
spr.AddSubimage(surfbuf, w, h, enigma::ct_precise, surfbuf); //TODO: Support toggling of precise.

delete[] surfbuf;
spr.AddSubimage(surfImg, enigma::ct_precise, surfImg.pxdata); //TODO: Support toggling of precise.
}

void surface_copy_part(int destination, gs_scalar x, gs_scalar y, int source, int xs, int ys, int ws, int hs)
Expand Down

0 comments on commit 2d1220f

Please sign in to comment.