Skip to content

Commit

Permalink
Remove code to resize textures to powers of 2. WebGL support non-powe…
Browse files Browse the repository at this point in the history
…r-of-2 textures.
  • Loading branch information
hulkholden committed Oct 25, 2023
1 parent 5e83540 commit e9e13c6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 68 deletions.
10 changes: 4 additions & 6 deletions src/hle/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Vector2 } from "../graphics/Vector2.js";
import { Vector4 } from "../graphics/Vector4.js";
import * as gbi from './gbi.js';
import * as shaders from './shaders.js';
import { Texture, clampTexture } from './textures.js';
import { Texture } from './textures.js';
import { VertexArray } from "./vertex_array.js";

const kBlendModeUnknown = 0;
Expand Down Expand Up @@ -503,12 +503,10 @@ export class Renderer {
`${cacheID}: ${gbi.ImageFormat.nameOf(tile.format)}, ${gbi.ImageSize.nameOf(tile.size)},${tile.width}x${tile.height}, <br>`);

const ctx = texture.$canvas[0].getContext('2d');
const imgData = ctx.createImageData(texture.nativeWidth, texture.nativeHeight);
const imgData = ctx.createImageData(texture.width, texture.height);

const handled = this.state.tmem.convertTexels(tile, tlutFormat, imgData);
if (handled) {
clampTexture(imgData, tile.width, tile.height);

ctx.putImageData(imgData, 0, 0);

this.$textureOutput.append(texture.$canvas);
Expand Down Expand Up @@ -542,8 +540,8 @@ export class Renderer {

let uvOffsetU = tile.left;
let uvOffsetV = tile.top;
let uvScaleU = 1.0 / texture.nativeWidth;
let uvScaleV = 1.0 / texture.nativeHeight;
let uvScaleU = 1.0 / texture.width;
let uvScaleV = 1.0 / texture.height;

// Horrible hack for wetrix. For some reason uvs come out 2x what they should be.
if (texture.width === 56 && texture.height === 29) {
Expand Down
64 changes: 2 additions & 62 deletions src/hle/textures.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,9 @@ export class Texture {
this.width = width;
this.height = height;

// TODO: with WebGL 2 non-power of 2 textures are supported.
var nativeWidth = nextPow2(width);
var nativeHeight = nextPow2(height);

this.nativeWidth = nativeWidth;
this.nativeHeight = nativeHeight;

// Create a canvas element to poke data into.
this.$canvas = $('<canvas width="' + nativeWidth +
'" height="' + nativeHeight + '" />',
{ 'width': nativeWidth, 'height': nativeHeight });
this.$canvas = $(`<canvas width="${width}" height="${height}" />`,
{ 'width': width, 'height': height });
this.texture = gl.createTexture();
}

Expand Down Expand Up @@ -60,55 +52,3 @@ export class Texture {
return $canvas;
}
}

export function clampTexture(imgData, width, height) {
let dst = imgData.data;
// Might not be the same as width, due to power of 2.
let dstRowStride = imgData.width * 4;

let y = 0;
if (width < imgData.width) {
let dstRowOffset = 0;
for (; y < height; ++y) {
let dstOffset = dstRowOffset + ((width - 1) * 4);

let r = dst[dstOffset + 0];
let g = dst[dstOffset + 1];
let b = dst[dstOffset + 2];
let a = dst[dstOffset + 3];

dstOffset += 4;

for (let x = width; x < imgData.width; ++x) {
dst[dstOffset + 0] = r;
dst[dstOffset + 1] = g;
dst[dstOffset + 2] = b;
dst[dstOffset + 3] = a;
dstOffset += 4;
}
dstRowOffset += dstRowStride;
}
}

if (height < imgData.height) {
// Repeat the final line
let dstRowOffset = dstRowStride * height;
let lastRowOffset = dstRowOffset - dstRowStride;

for (; y < imgData.height; ++y) {
for (let i = 0; i < dstRowStride; ++i) {
dst[dstRowOffset + i] = dst[lastRowOffset + i];
}
dstRowOffset += dstRowStride;
}
}
}

function nextPow2(x) {
var y = 1;
while (y < x) {
y *= 2;
}

return y;
}

0 comments on commit e9e13c6

Please sign in to comment.