542 lines
19 KiB
C++
542 lines
19 KiB
C++
// SPDX-License-Identifier: GPL-2.0-only
|
|||
|
|
// ReMEdy J2ME (MIDP 2.0 / CLDC 1.1) Emulator for Bananarchy Linux
|
||
|
|
// Part 4B: JSR-184 (M3G) 3D Hardware Acceleration via EGL + OpenGL ES 2.0 on Adreno 304 (FD304)
|
||
|
|
|
||
|
|
#ifndef BANANARCHY_REMEDY_M3G_GL_HPP
|
||
|
|
#define BANANARCHY_REMEDY_M3G_GL_HPP
|
||
|
|
|
||
|
|
#include "remedy_lcdui.hpp"
|
||
|
|
|
||
|
|
#include <EGL/egl.h>
|
||
|
|
#include <GLES2/gl2.h>
|
||
|
|
#include <array>
|
||
|
|
#include <cmath>
|
||
|
|
|
||
|
|
namespace remedy {
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// 4x4 Column-Major Matrix Helper for M3G Transform & Camera
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
struct Mat4 {
|
||
|
|
std::array<float, 16> m = {
|
||
|
|
1, 0, 0, 0,
|
||
|
|
0, 1, 0, 0,
|
||
|
|
0, 0, 1, 0,
|
||
|
|
0, 0, 0, 1
|
||
|
|
};
|
||
|
|
|
||
|
|
static Mat4 Identity() { return Mat4{}; }
|
||
|
|
|
||
|
|
static Mat4 Multiply(const Mat4& a, const Mat4& b) {
|
||
|
|
Mat4 r;
|
||
|
|
for (int col = 0; col < 4; ++col) {
|
||
|
|
for (int row = 0; row < 4; ++row) {
|
||
|
|
float sum = 0.0f;
|
||
|
|
for (int k = 0; k < 4; ++k) {
|
||
|
|
sum += a.m[k * 4 + row] * b.m[col * 4 + k];
|
||
|
|
}
|
||
|
|
r.m[col * 4 + row] = sum;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return r;
|
||
|
|
}
|
||
|
|
|
||
|
|
static Mat4 Perspective(float fovyDeg, float aspect, float zNear, float zFar) {
|
||
|
|
Mat4 r;
|
||
|
|
r.m.fill(0.0f);
|
||
|
|
float rad = fovyDeg * 0.0174532925f;
|
||
|
|
float f = 1.0f / std::tan(rad * 0.5f);
|
||
|
|
if (aspect <= 0.0001f) aspect = 240.0f / 320.0f;
|
||
|
|
float nf = 1.0f / (zNear - zFar);
|
||
|
|
r.m[0] = f / aspect;
|
||
|
|
r.m[5] = f;
|
||
|
|
r.m[10] = (zFar + zNear) * nf;
|
||
|
|
r.m[11] = -1.0f;
|
||
|
|
r.m[14] = (2.0f * zFar * zNear) * nf;
|
||
|
|
return r;
|
||
|
|
}
|
||
|
|
|
||
|
|
void postTranslate(float tx, float ty, float tz) {
|
||
|
|
Mat4 t = Identity();
|
||
|
|
t.m[12] = tx;
|
||
|
|
t.m[13] = ty;
|
||
|
|
t.m[14] = tz;
|
||
|
|
*this = Multiply(*this, t);
|
||
|
|
}
|
||
|
|
|
||
|
|
void postScale(float sx, float sy, float sz) {
|
||
|
|
Mat4 s = Identity();
|
||
|
|
s.m[0] = sx;
|
||
|
|
s.m[5] = sy;
|
||
|
|
s.m[10] = sz;
|
||
|
|
*this = Multiply(*this, s);
|
||
|
|
}
|
||
|
|
|
||
|
|
void postRotate(float angleDeg, float ax, float ay, float az) {
|
||
|
|
float len = std::sqrt(ax * ax + ay * ay + az * az);
|
||
|
|
if (len < 1e-6f) return;
|
||
|
|
ax /= len;
|
||
|
|
ay /= len;
|
||
|
|
az /= len;
|
||
|
|
float rad = angleDeg * 0.0174532925f;
|
||
|
|
float c = std::cos(rad);
|
||
|
|
float s = std::sin(rad);
|
||
|
|
float ic = 1.0f - c;
|
||
|
|
|
||
|
|
Mat4 r = Identity();
|
||
|
|
r.m[0] = ax * ax * ic + c;
|
||
|
|
r.m[1] = ay * ax * ic + az * s;
|
||
|
|
r.m[2] = az * ax * ic - ay * s;
|
||
|
|
|
||
|
|
r.m[4] = ax * ay * ic - az * s;
|
||
|
|
r.m[5] = ay * ay * ic + c;
|
||
|
|
r.m[6] = az * ay * ic + ax * s;
|
||
|
|
|
||
|
|
r.m[8] = ax * az * ic + ay * s;
|
||
|
|
r.m[9] = ay * az * ic - ax * s;
|
||
|
|
r.m[10] = az * az * ic + c;
|
||
|
|
*this = Multiply(*this, r);
|
||
|
|
}
|
||
|
|
|
||
|
|
void transformPoint(float x, float y, float z,
|
||
|
|
float* ox, float* oy, float* oz, float* ow) const {
|
||
|
|
*ox = m[0] * x + m[4] * y + m[8] * z + m[12];
|
||
|
|
*oy = m[1] * x + m[5] * y + m[9] * z + m[13];
|
||
|
|
*oz = m[2] * x + m[6] * y + m[10] * z + m[14];
|
||
|
|
*ow = m[3] * x + m[7] * y + m[11] * z + m[15];
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// M3G Native Data Structures
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
struct M3gVertexArrayData {
|
||
|
|
int numVertices = 0;
|
||
|
|
int numComponents = 3;
|
||
|
|
int componentSize = 2; // 1=byte, 2=short
|
||
|
|
std::vector<float> values;
|
||
|
|
};
|
||
|
|
|
||
|
|
struct M3gVertexBufferData {
|
||
|
|
uint32_t positionsRef = 0;
|
||
|
|
float posScale = 1.0f;
|
||
|
|
float posBias[3] = {0.0f, 0.0f, 0.0f};
|
||
|
|
uint32_t normalsRef = 0;
|
||
|
|
uint32_t colorsRef = 0;
|
||
|
|
uint32_t texCoordsRef = 0;
|
||
|
|
float texScale = 1.0f;
|
||
|
|
float texBias[3] = {0.0f, 0.0f, 0.0f};
|
||
|
|
uint32_t defaultColor = 0xFFFFFFFFu;
|
||
|
|
};
|
||
|
|
|
||
|
|
struct M3gIndexBufferData {
|
||
|
|
std::vector<uint16_t> triangles; // Expanded triangle list (3 indices per tri)
|
||
|
|
};
|
||
|
|
|
||
|
|
struct M3gTransformData {
|
||
|
|
Mat4 mat = Mat4::Identity();
|
||
|
|
};
|
||
|
|
|
||
|
|
struct M3gCameraData {
|
||
|
|
Mat4 proj = Mat4::Perspective(50.0f, 240.0f / 320.0f, 0.1f, 100.0f);
|
||
|
|
};
|
||
|
|
|
||
|
|
struct M3gBackgroundData {
|
||
|
|
uint32_t color = 0xFF0A101Cu;
|
||
|
|
bool colorClearEnabled = true;
|
||
|
|
bool depthClearEnabled = true;
|
||
|
|
};
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Adreno 304 (FD304) EGL + GLES2 Offscreen 3D Renderer (with SW Fallback)
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
class M3gGlEngine {
|
||
|
|
public:
|
||
|
|
bool useHardwareGl = true;
|
||
|
|
bool hwInitialized = false;
|
||
|
|
std::string glRendererName = "FD304";
|
||
|
|
|
||
|
|
int vpX = 0;
|
||
|
|
int vpY = 0;
|
||
|
|
int vpW = 240;
|
||
|
|
int vpH = 320;
|
||
|
|
uint32_t boundGraphicsRef = 0;
|
||
|
|
uint32_t boundTargetRef = 0;
|
||
|
|
Mat4 projMat = Mat4::Perspective(50.0f, 240.0f / 320.0f, 0.1f, 100.0f);
|
||
|
|
Mat4 viewMat = Mat4::Identity();
|
||
|
|
uint32_t lightColor = 0xFFFFFFFFu;
|
||
|
|
float lightIntensity = 1.0f;
|
||
|
|
|
||
|
|
~M3gGlEngine() { shutdown(); }
|
||
|
|
|
||
|
|
bool init(Display* x11Dpy) {
|
||
|
|
if (hwInitialized) return true;
|
||
|
|
eglDpy_ = eglGetDisplay(reinterpret_cast<EGLNativeDisplayType>(x11Dpy));
|
||
|
|
if (eglDpy_ == EGL_NO_DISPLAY) {
|
||
|
|
eglDpy_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
||
|
|
}
|
||
|
|
if (eglDpy_ == EGL_NO_DISPLAY) return false;
|
||
|
|
EGLint maj = 0, min = 0;
|
||
|
|
if (!eglInitialize(eglDpy_, &maj, &min)) return false;
|
||
|
|
eglBindAPI(EGL_OPENGL_ES_API);
|
||
|
|
|
||
|
|
const EGLint cfgAttribs[] = {
|
||
|
|
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
|
||
|
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
|
||
|
|
EGL_RED_SIZE, 8,
|
||
|
|
EGL_GREEN_SIZE, 8,
|
||
|
|
EGL_BLUE_SIZE, 8,
|
||
|
|
EGL_ALPHA_SIZE, 8,
|
||
|
|
EGL_DEPTH_SIZE, 16,
|
||
|
|
EGL_NONE
|
||
|
|
};
|
||
|
|
EGLConfig cfg = nullptr;
|
||
|
|
EGLint numCfg = 0;
|
||
|
|
if (!eglChooseConfig(eglDpy_, cfgAttribs, &cfg, 1, &numCfg) || numCfg < 1) {
|
||
|
|
// Fallback: config without pbuffer bit (we use an FBO anyway)
|
||
|
|
const EGLint cfgAttribs2[] = {
|
||
|
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
|
||
|
|
EGL_RED_SIZE, 5,
|
||
|
|
EGL_GREEN_SIZE, 6,
|
||
|
|
EGL_BLUE_SIZE, 5,
|
||
|
|
EGL_DEPTH_SIZE, 16,
|
||
|
|
EGL_NONE
|
||
|
|
};
|
||
|
|
if (!eglChooseConfig(eglDpy_, cfgAttribs2, &cfg, 1, &numCfg) ||
|
||
|
|
numCfg < 1) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const EGLint ctxAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
|
||
|
|
eglCtx_ = eglCreateContext(eglDpy_, cfg, EGL_NO_CONTEXT, ctxAttribs);
|
||
|
|
if (eglCtx_ == EGL_NO_CONTEXT) return false;
|
||
|
|
|
||
|
|
const EGLint pbufAttribs[] = {EGL_WIDTH, 240, EGL_HEIGHT, 320, EGL_NONE};
|
||
|
|
eglSurf_ = eglCreatePbufferSurface(eglDpy_, cfg, pbufAttribs);
|
||
|
|
if (!eglMakeCurrent(eglDpy_, eglSurf_, eglSurf_, eglCtx_)) {
|
||
|
|
if (!eglMakeCurrent(eglDpy_, EGL_NO_SURFACE, EGL_NO_SURFACE, eglCtx_)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const char* rend = reinterpret_cast<const char*>(glGetString(GL_RENDERER));
|
||
|
|
if (rend && rend[0]) glRendererName = rend;
|
||
|
|
|
||
|
|
// Create 240x320 FBO + Depth Renderbuffer on FD304
|
||
|
|
glGenFramebuffers(1, &fbo_);
|
||
|
|
glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
|
||
|
|
|
||
|
|
glGenTextures(1, &colorTex_);
|
||
|
|
glBindTexture(GL_TEXTURE_2D, colorTex_);
|
||
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 240, 320, 0, GL_RGBA,
|
||
|
|
GL_UNSIGNED_BYTE, nullptr);
|
||
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
|
||
|
|
GL_TEXTURE_2D, colorTex_, 0);
|
||
|
|
|
||
|
|
glGenRenderbuffers(1, &depthRbo_);
|
||
|
|
glBindRenderbuffer(GL_RENDERBUFFER, depthRbo_);
|
||
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, 240, 320);
|
||
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
|
||
|
|
GL_RENDERBUFFER, depthRbo_);
|
||
|
|
|
||
|
|
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Compile M3G GLSL ES 2.0 shader program
|
||
|
|
const char* vsSrc =
|
||
|
|
"attribute vec3 aPos;\n"
|
||
|
|
"attribute vec3 aNorm;\n"
|
||
|
|
"attribute vec4 aCol;\n"
|
||
|
|
"uniform mat4 uMVP;\n"
|
||
|
|
"varying vec4 vCol;\n"
|
||
|
|
"void main() {\n"
|
||
|
|
" gl_Position = uMVP * vec4(aPos, 1.0);\n"
|
||
|
|
" float lit = max(0.35, abs(aNorm.z) * 0.65 + 0.35);\n"
|
||
|
|
" vCol = vec4(aCol.rgb * lit, aCol.a);\n"
|
||
|
|
"}\n";
|
||
|
|
const char* fsSrc =
|
||
|
|
"precision mediump float;\n"
|
||
|
|
"varying vec4 vCol;\n"
|
||
|
|
"void main() {\n"
|
||
|
|
" gl_FragColor = vCol;\n"
|
||
|
|
"}\n";
|
||
|
|
|
||
|
|
auto compileShader = [](GLenum type, const char* src) -> GLuint {
|
||
|
|
GLuint s = glCreateShader(type);
|
||
|
|
glShaderSource(s, 1, &src, nullptr);
|
||
|
|
glCompileShader(s);
|
||
|
|
return s;
|
||
|
|
};
|
||
|
|
GLuint vs = compileShader(GL_VERTEX_SHADER, vsSrc);
|
||
|
|
GLuint fs = compileShader(GL_FRAGMENT_SHADER, fsSrc);
|
||
|
|
prog_ = glCreateProgram();
|
||
|
|
glAttachShader(prog_, vs);
|
||
|
|
glAttachShader(prog_, fs);
|
||
|
|
glBindAttribLocation(prog_, 0, "aPos");
|
||
|
|
glBindAttribLocation(prog_, 1, "aNorm");
|
||
|
|
glBindAttribLocation(prog_, 2, "aCol");
|
||
|
|
glLinkProgram(prog_);
|
||
|
|
uMvpLoc_ = glGetUniformLocation(prog_, "uMVP");
|
||
|
|
glDeleteShader(vs);
|
||
|
|
glDeleteShader(fs);
|
||
|
|
|
||
|
|
glEnable(GL_DEPTH_TEST);
|
||
|
|
glDepthFunc(GL_LEQUAL);
|
||
|
|
hwInitialized = true;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void shutdown() {
|
||
|
|
if (hwInitialized && eglDpy_ != EGL_NO_DISPLAY) {
|
||
|
|
if (prog_) glDeleteProgram(prog_);
|
||
|
|
if (depthRbo_) glDeleteRenderbuffers(1, &depthRbo_);
|
||
|
|
if (colorTex_) glDeleteTextures(1, &colorTex_);
|
||
|
|
if (fbo_) glDeleteFramebuffers(1, &fbo_);
|
||
|
|
eglMakeCurrent(eglDpy_, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
||
|
|
if (eglSurf_ != EGL_NO_SURFACE) eglDestroySurface(eglDpy_, eglSurf_);
|
||
|
|
if (eglCtx_ != EGL_NO_CONTEXT) eglDestroyContext(eglDpy_, eglCtx_);
|
||
|
|
eglTerminate(eglDpy_);
|
||
|
|
}
|
||
|
|
hwInitialized = false;
|
||
|
|
eglDpy_ = EGL_NO_DISPLAY;
|
||
|
|
eglCtx_ = EGL_NO_CONTEXT;
|
||
|
|
eglSurf_ = EGL_NO_SURFACE;
|
||
|
|
}
|
||
|
|
|
||
|
|
void clearTarget(NativeImageSurface* surf, uint32_t bgColorArgb,
|
||
|
|
bool clearColor, bool clearDepth) {
|
||
|
|
int w = surf ? surf->width : 240;
|
||
|
|
int h = surf ? surf->height : 320;
|
||
|
|
if (clearDepth) {
|
||
|
|
swZbuf_.assign(static_cast<size_t>(w) * h, 1.0e9f);
|
||
|
|
}
|
||
|
|
if (clearColor && surf) {
|
||
|
|
uint32_t c = bgColorArgb | 0xFF000000u;
|
||
|
|
for (int y = std::max(0, vpY); y < std::min(h, vpY + vpH); ++y) {
|
||
|
|
uint32_t* row = surf->pixels.data() + static_cast<size_t>(y) * w;
|
||
|
|
for (int x = std::max(0, vpX); x < std::min(w, vpX + vpW); ++x) {
|
||
|
|
row[x] = c;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (useHardwareGl && hwInitialized) {
|
||
|
|
glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
|
||
|
|
glViewport(vpX, 320 - (vpY + vpH), vpW, vpH);
|
||
|
|
GLbitfield mask = 0;
|
||
|
|
if (clearColor) {
|
||
|
|
float r = ((bgColorArgb >> 16) & 0xFFu) / 255.0f;
|
||
|
|
float g = ((bgColorArgb >> 8) & 0xFFu) / 255.0f;
|
||
|
|
float b = (bgColorArgb & 0xFFu) / 255.0f;
|
||
|
|
glClearColor(r, g, b, 1.0f);
|
||
|
|
mask |= GL_COLOR_BUFFER_BIT;
|
||
|
|
}
|
||
|
|
if (clearDepth) mask |= GL_DEPTH_BUFFER_BIT;
|
||
|
|
if (mask) glClear(mask);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void renderVertexBuffer(Jvm& vm, NativeImageSurface* surf,
|
||
|
|
uint32_t vbRef, uint32_t ibRef,
|
||
|
|
uint32_t /*appRef*/, const Mat4& modelMat) {
|
||
|
|
JObject* vbObj = vm.getObj(vbRef);
|
||
|
|
JObject* ibObj = vm.getObj(ibRef);
|
||
|
|
if (!vbObj || !vbObj->nativeData || !ibObj || !ibObj->nativeData) return;
|
||
|
|
auto* vb = static_cast<M3gVertexBufferData*>(vbObj->nativeData.get());
|
||
|
|
auto* ib = static_cast<M3gIndexBufferData*>(ibObj->nativeData.get());
|
||
|
|
|
||
|
|
JObject* posObj = vm.getObj(vb->positionsRef);
|
||
|
|
if (!posObj || !posObj->nativeData) return;
|
||
|
|
auto* posArr = static_cast<M3gVertexArrayData*>(posObj->nativeData.get());
|
||
|
|
if (posArr->numVertices <= 0 || ib->triangles.empty()) return;
|
||
|
|
|
||
|
|
M3gVertexArrayData* colArr = nullptr;
|
||
|
|
if (vb->colorsRef != 0) {
|
||
|
|
JObject* co = vm.getObj(vb->colorsRef);
|
||
|
|
if (co && co->nativeData) {
|
||
|
|
colArr = static_cast<M3gVertexArrayData*>(co->nativeData.get());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
M3gVertexArrayData* normArr = nullptr;
|
||
|
|
if (vb->normalsRef != 0) {
|
||
|
|
JObject* no = vm.getObj(vb->normalsRef);
|
||
|
|
if (no && no->nativeData) {
|
||
|
|
normArr = static_cast<M3gVertexArrayData*>(no->nativeData.get());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
int nVerts = posArr->numVertices;
|
||
|
|
std::vector<float> vPos(nVerts * 3);
|
||
|
|
std::vector<float> vNorm(nVerts * 3, 0.0f);
|
||
|
|
std::vector<float> vCol(nVerts * 4, 1.0f);
|
||
|
|
|
||
|
|
float defR = ((vb->defaultColor >> 16) & 0xFFu) / 255.0f;
|
||
|
|
float defG = ((vb->defaultColor >> 8) & 0xFFu) / 255.0f;
|
||
|
|
float defB = (vb->defaultColor & 0xFFu) / 255.0f;
|
||
|
|
float defA = ((vb->defaultColor >> 24) & 0xFFu) / 255.0f;
|
||
|
|
if (defA == 0.0f) defA = 1.0f;
|
||
|
|
|
||
|
|
for (int i = 0; i < nVerts; ++i) {
|
||
|
|
for (int c = 0; c < 3; ++c) {
|
||
|
|
float raw = (c < posArr->numComponents &&
|
||
|
|
static_cast<size_t>(i * posArr->numComponents + c) <
|
||
|
|
posArr->values.size())
|
||
|
|
? posArr->values[i * posArr->numComponents + c]
|
||
|
|
: 0.0f;
|
||
|
|
vPos[i * 3 + c] = raw * vb->posScale + vb->posBias[c];
|
||
|
|
}
|
||
|
|
if (normArr && static_cast<size_t>((i + 1) * normArr->numComponents) <=
|
||
|
|
normArr->values.size()) {
|
||
|
|
for (int c = 0; c < std::min(3, normArr->numComponents); ++c) {
|
||
|
|
vNorm[i * 3 + c] = normArr->values[i * normArr->numComponents + c] / 127.0f;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
vNorm[i * 3 + 2] = 1.0f;
|
||
|
|
}
|
||
|
|
if (colArr && static_cast<size_t>((i + 1) * colArr->numComponents) <=
|
||
|
|
colArr->values.size()) {
|
||
|
|
int nc = colArr->numComponents;
|
||
|
|
auto toU8 = [](float v) -> float {
|
||
|
|
int iv = static_cast<int>(v);
|
||
|
|
if (iv < 0) iv += 256;
|
||
|
|
return std::clamp(iv, 0, 255) / 255.0f;
|
||
|
|
};
|
||
|
|
vCol[i * 4 + 0] = toU8(colArr->values[i * nc + 0]);
|
||
|
|
vCol[i * 4 + 1] = (nc >= 2) ? toU8(colArr->values[i * nc + 1]) : 1.0f;
|
||
|
|
vCol[i * 4 + 2] = (nc >= 3) ? toU8(colArr->values[i * nc + 2]) : 1.0f;
|
||
|
|
vCol[i * 4 + 3] = (nc >= 4) ? toU8(colArr->values[i * nc + 3]) : 1.0f;
|
||
|
|
} else {
|
||
|
|
vCol[i * 4 + 0] = defR;
|
||
|
|
vCol[i * 4 + 1] = defG;
|
||
|
|
vCol[i * 4 + 2] = defB;
|
||
|
|
vCol[i * 4 + 3] = defA;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Mat4 mv = Mat4::Multiply(viewMat, modelMat);
|
||
|
|
Mat4 mvp = Mat4::Multiply(projMat, mv);
|
||
|
|
|
||
|
|
if (useHardwareGl && hwInitialized) {
|
||
|
|
glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
|
||
|
|
glViewport(vpX, 320 - (vpY + vpH), vpW, vpH);
|
||
|
|
glUseProgram(prog_);
|
||
|
|
glUniformMatrix4fv(uMvpLoc_, 1, GL_FALSE, mvp.m.data());
|
||
|
|
glEnableVertexAttribArray(0);
|
||
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vPos.data());
|
||
|
|
glEnableVertexAttribArray(1);
|
||
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, vNorm.data());
|
||
|
|
glEnableVertexAttribArray(2);
|
||
|
|
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, vCol.data());
|
||
|
|
glDrawElements(GL_TRIANGLES,
|
||
|
|
static_cast<GLsizei>(ib->triangles.size()),
|
||
|
|
GL_UNSIGNED_SHORT, ib->triangles.data());
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Software depth-buffered 3D rasterizer fallback
|
||
|
|
if (!surf) return;
|
||
|
|
if (swZbuf_.size() != static_cast<size_t>(surf->width) * surf->height) {
|
||
|
|
swZbuf_.assign(static_cast<size_t>(surf->width) * surf->height, 1.0e9f);
|
||
|
|
}
|
||
|
|
struct ProjV {
|
||
|
|
float sx, sy, sz;
|
||
|
|
uint32_t col;
|
||
|
|
bool valid;
|
||
|
|
};
|
||
|
|
std::vector<ProjV> pv(nVerts);
|
||
|
|
for (int i = 0; i < nVerts; ++i) {
|
||
|
|
float cx, cy, cz, cw;
|
||
|
|
mvp.transformPoint(vPos[i * 3 + 0], vPos[i * 3 + 1], vPos[i * 3 + 2],
|
||
|
|
&cx, &cy, &cz, &cw);
|
||
|
|
if (cw <= 0.05f) {
|
||
|
|
pv[i].valid = false;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
float invW = 1.0f / cw;
|
||
|
|
pv[i].sx = vpX + (cx * invW * 0.5f + 0.5f) * vpW;
|
||
|
|
pv[i].sy = vpY + (0.5f - cy * invW * 0.5f) * vpH;
|
||
|
|
pv[i].sz = cz * invW;
|
||
|
|
uint32_t r = static_cast<uint32_t>(std::clamp(vCol[i * 4 + 0], 0.0f, 1.0f) * 255.0f);
|
||
|
|
uint32_t g = static_cast<uint32_t>(std::clamp(vCol[i * 4 + 1], 0.0f, 1.0f) * 255.0f);
|
||
|
|
uint32_t b = static_cast<uint32_t>(std::clamp(vCol[i * 4 + 2], 0.0f, 1.0f) * 255.0f);
|
||
|
|
pv[i].col = 0xFF000000u | (r << 16) | (g << 8) | b;
|
||
|
|
pv[i].valid = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
for (size_t t = 0; t + 2 < ib->triangles.size(); t += 3) {
|
||
|
|
uint16_t i0 = ib->triangles[t + 0];
|
||
|
|
uint16_t i1 = ib->triangles[t + 1];
|
||
|
|
uint16_t i2 = ib->triangles[t + 2];
|
||
|
|
if (i0 >= nVerts || i1 >= nVerts || i2 >= nVerts) continue;
|
||
|
|
const ProjV &v0 = pv[i0], &v1 = pv[i1], &v2 = pv[i2];
|
||
|
|
if (!v0.valid || !v1.valid || !v2.valid) continue;
|
||
|
|
|
||
|
|
int minX = std::max(0, static_cast<int>(std::min({v0.sx, v1.sx, v2.sx})));
|
||
|
|
int maxX = std::min(surf->width - 1,
|
||
|
|
static_cast<int>(std::max({v0.sx, v1.sx, v2.sx})));
|
||
|
|
int minY = std::max(0, static_cast<int>(std::min({v0.sy, v1.sy, v2.sy})));
|
||
|
|
int maxY = std::min(surf->height - 1,
|
||
|
|
static_cast<int>(std::max({v0.sy, v1.sy, v2.sy})));
|
||
|
|
auto edge = [](float ax, float ay, float bx, float by, float px, float py) {
|
||
|
|
return (px - ax) * (by - ay) - (py - ay) * (bx - ax);
|
||
|
|
};
|
||
|
|
float area = edge(v0.sx, v0.sy, v1.sx, v1.sy, v2.sx, v2.sy);
|
||
|
|
if (std::fabs(area) < 0.5f) continue;
|
||
|
|
float invArea = 1.0f / area;
|
||
|
|
for (int py = minY; py <= maxY; ++py) {
|
||
|
|
for (int px = minX; px <= maxX; ++px) {
|
||
|
|
float w0 = edge(v1.sx, v1.sy, v2.sx, v2.sy, px + 0.5f, py + 0.5f) * invArea;
|
||
|
|
float w1 = edge(v2.sx, v2.sy, v0.sx, v0.sy, px + 0.5f, py + 0.5f) * invArea;
|
||
|
|
float w2 = 1.0f - w0 - w1;
|
||
|
|
if (w0 >= 0.0f && w1 >= 0.0f && w2 >= 0.0f) {
|
||
|
|
float z = w0 * v0.sz + w1 * v1.sz + w2 * v2.sz;
|
||
|
|
size_t idx = static_cast<size_t>(py) * surf->width + px;
|
||
|
|
if (z < swZbuf_[idx]) {
|
||
|
|
swZbuf_[idx] = z;
|
||
|
|
surf->pixels[idx] = v0.col;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void finishTarget(NativeImageSurface* surf) {
|
||
|
|
if (!surf || !useHardwareGl || !hwInitialized) return;
|
||
|
|
glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
|
||
|
|
glFinish();
|
||
|
|
std::vector<uint8_t> rgba(240 * 320 * 4);
|
||
|
|
glReadPixels(0, 0, 240, 320, GL_RGBA, GL_UNSIGNED_BYTE, rgba.data());
|
||
|
|
int copyW = std::min(240, surf->width);
|
||
|
|
int copyH = std::min(320, surf->height);
|
||
|
|
for (int y = std::max(0, vpY); y < std::min(copyH, vpY + vpH); ++y) {
|
||
|
|
int glY = 319 - y;
|
||
|
|
const uint8_t* srcRow = rgba.data() + static_cast<size_t>(glY) * 240 * 4;
|
||
|
|
uint32_t* dstRow = surf->pixels.data() + static_cast<size_t>(y) * surf->width;
|
||
|
|
for (int x = std::max(0, vpX); x < std::min(copyW, vpX + vpW); ++x) {
|
||
|
|
uint32_t r = srcRow[x * 4 + 0];
|
||
|
|
uint32_t g = srcRow[x * 4 + 1];
|
||
|
|
uint32_t b = srcRow[x * 4 + 2];
|
||
|
|
dstRow[x] = 0xFF000000u | (r << 16) | (g << 8) | b;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private:
|
||
|
|
EGLDisplay eglDpy_ = EGL_NO_DISPLAY;
|
||
|
|
EGLContext eglCtx_ = EGL_NO_CONTEXT;
|
||
|
|
EGLSurface eglSurf_ = EGL_NO_SURFACE;
|
||
|
|
GLuint fbo_ = 0;
|
||
|
|
GLuint colorTex_ = 0;
|
||
|
|
GLuint depthRbo_ = 0;
|
||
|
|
GLuint prog_ = 0;
|
||
|
|
GLint uMvpLoc_ = -1;
|
||
|
|
std::vector<float> swZbuf_;
|
||
|
|
};
|
||
|
|
|
||
|
|
} // namespace remedy
|
||
|
|
|
||
|
|
#endif // BANANARCHY_REMEDY_M3G_GL_HPP
|