From 4aad9a97865e365446877444275e6c00e53dc8b8 Mon Sep 17 00:00:00 2001 From: olebeck <31539311+olebeck@users.noreply.github.com> Date: Fri, 27 Jun 2025 17:20:25 +0200 Subject: [PATCH] make 3d work, broken lights --- .../lego/legoomni/include/legoinputmanager.h | 1 + .../legoomni/src/input/legoinputmanager.cpp | 7 +- miniwin/src/d3drm/backends/gxm/renderer.cpp | 129 +++++++++--------- .../d3drm/backends/gxm/shaders/main.vert.cg | 4 +- .../d3drm/backends/gxm/shaders/main.vert.gxp | Bin 728 -> 768 bytes miniwin/src/d3drm/backends/gxm/utils.h | 2 +- miniwin/src/internal/d3drmrenderer_gxm.h | 1 + 7 files changed, 76 insertions(+), 68 deletions(-) diff --git a/LEGO1/lego/legoomni/include/legoinputmanager.h b/LEGO1/lego/legoomni/include/legoinputmanager.h index 7ccad404..bb7fe4c3 100644 --- a/LEGO1/lego/legoomni/include/legoinputmanager.h +++ b/LEGO1/lego/legoomni/include/legoinputmanager.h @@ -166,6 +166,7 @@ class LegoInputManager : public MxPresenter { const bool* m_keyboardState; MxBool m_unk0x195; // 0x195 SDL_JoystickID* m_joyids; + MxS32 m_numJoysticks; SDL_Joystick* m_joystick; MxS32 m_joystickIndex; // 0x19c MxBool m_useJoystick; // 0x334 diff --git a/LEGO1/lego/legoomni/src/input/legoinputmanager.cpp b/LEGO1/lego/legoomni/src/input/legoinputmanager.cpp index 3e9622c2..ff15b122 100644 --- a/LEGO1/lego/legoomni/src/input/legoinputmanager.cpp +++ b/LEGO1/lego/legoomni/src/input/legoinputmanager.cpp @@ -152,12 +152,11 @@ MxResult LegoInputManager::GetJoystick() return SUCCESS; } - MxS32 numJoysticks = 0; if (m_joyids == NULL) { - m_joyids = SDL_GetJoysticks(&numJoysticks); + m_joyids = SDL_GetJoysticks(&m_numJoysticks); } - if (m_useJoystick != FALSE && numJoysticks != 0) { + if (m_useJoystick != FALSE && m_numJoysticks != 0) { MxS32 joyid = m_joystickIndex; if (joyid >= 0) { m_joystick = SDL_OpenJoystick(m_joyids[joyid]); @@ -166,7 +165,7 @@ MxResult LegoInputManager::GetJoystick() } } - for (joyid = 0; joyid < numJoysticks; joyid++) { + for (joyid = 0; joyid < m_numJoysticks; joyid++) { m_joystick = SDL_OpenJoystick(m_joyids[joyid]); if (m_joystick != NULL) { return SUCCESS; diff --git a/miniwin/src/d3drm/backends/gxm/renderer.cpp b/miniwin/src/d3drm/backends/gxm/renderer.cpp index c3d8cf98..9f45096c 100644 --- a/miniwin/src/d3drm/backends/gxm/renderer.cpp +++ b/miniwin/src/d3drm/backends/gxm/renderer.cpp @@ -482,28 +482,28 @@ static void CreateOrthoMatrix(float left, float right, float bottom, float top, { float near = -1.0f; float far = 1.0f; - float width = right - left; - float height = top - bottom; - float depth = far - near; + float rl = right - left; + float tb = top - bottom; + float fn = far - near; - outMatrix[0][0] = 2.0f / width; + outMatrix[0][0] = 2.0f / rl; outMatrix[0][1] = 0.0f; outMatrix[0][2] = 0.0f; - outMatrix[0][3] = -(right + left) / width; // translation x + outMatrix[0][3] = 0.0f; outMatrix[1][0] = 0.0f; - outMatrix[1][1] = 2.0f / height; + outMatrix[1][1] = 2.0f / tb; outMatrix[1][2] = 0.0f; - outMatrix[1][3] = -(top + bottom) / height; // translation y + outMatrix[1][3] = 0.0f; outMatrix[2][0] = 0.0f; outMatrix[2][1] = 0.0f; - outMatrix[2][2] = -2.0f / depth; - outMatrix[2][3] = -(far + near) / depth; // translation z + outMatrix[2][2] = -2.0f / fn; + outMatrix[2][3] = 0.0f; - outMatrix[3][0] = 0.0f; - outMatrix[3][1] = 0.0f; - outMatrix[3][2] = 0.0f; + outMatrix[3][0] = -(right + left) / rl; + outMatrix[3][1] = -(top + bottom) / tb; + outMatrix[3][2] = -(far + near) / fn; outMatrix[3][3] = 1.0f; } @@ -567,7 +567,6 @@ void GXMRenderer::SetFrustumPlanes(const Plane* frustumPlanes) void GXMRenderer::SetProjection(const D3DRMMATRIX4D& projection, D3DVALUE front, D3DVALUE back) { memcpy(&m_projection, projection, sizeof(D3DRMMATRIX4D)); - m_projection[1][1] *= -1.0f; // GXM is upside down } struct TextureDestroyContextGXM { @@ -590,22 +589,24 @@ void GXMRenderer::AddTextureDestroyCallback(Uint32 id, IDirect3DRMTexture* textu ); } -static void convertTextureMetadata(SDL_Surface* surface, bool* supportedFormat, SceGxmTextureFormat* textureFormat, size_t* textureSize, size_t* textureAlignment) { +static void convertTextureMetadata(SDL_Surface* surface, bool* supportedFormat, SceGxmTextureFormat* textureFormat, size_t* textureSize, size_t* textureAlignment, size_t* textureStride) { *supportedFormat = true; *textureAlignment = SCE_GXM_TEXTURE_ALIGNMENT; switch(surface->format) { case SDL_PIXELFORMAT_ABGR8888: { *textureFormat = SCE_GXM_TEXTURE_FORMAT_U8U8U8U8_ABGR; *textureSize = surface->h * surface->pitch; + *textureStride = surface->pitch; break; } - /* crashes when opening the guestbook for some reason + /* case SDL_PIXELFORMAT_INDEX8: { *textureFormat = SCE_GXM_TEXTURE_FORMAT_P8_ABGR; int pixelsSize = surface->h * surface->pitch; int alignBytes = ALIGNMENT(pixelsSize, SCE_GXM_PALETTE_ALIGNMENT); *textureSize = pixelsSize + alignBytes + 0xff; *textureAlignment = SCE_GXM_PALETTE_ALIGNMENT; + *textureStride = surface->pitch; break; } */ @@ -615,6 +616,12 @@ static void convertTextureMetadata(SDL_Surface* surface, bool* supportedFormat, } } +void copySurfaceTo(SDL_Surface* src, void* dstData, size_t textureStride) { + SDL_Surface* dst = SDL_CreateSurfaceFrom(src->w, src->h, SDL_PIXELFORMAT_ABGR8888, dstData, textureStride); + SDL_BlitSurface(src, nullptr, dst, nullptr); + SDL_DestroySurface(dst); +} + Uint32 GXMRenderer::GetTextureId(IDirect3DRMTexture* iTexture) { auto texture = static_cast(iTexture); @@ -624,9 +631,19 @@ Uint32 GXMRenderer::GetTextureId(IDirect3DRMTexture* iTexture) bool supportedFormat; size_t textureSize; size_t textureAlignment; + size_t textureStride; SceGxmTextureFormat textureFormat; + int textureWidth = surface->m_surface->w; + int textureHeight = surface->m_surface->h; - convertTextureMetadata(surface->m_surface, &supportedFormat, &textureFormat, &textureSize, &textureAlignment); + convertTextureMetadata(surface->m_surface, &supportedFormat, &textureFormat, &textureSize, &textureAlignment, &textureStride); + + if(!supportedFormat) { + textureAlignment = SCE_GXM_TEXTURE_ALIGNMENT; + textureStride = textureWidth * 4; + textureSize = textureHeight * textureStride; + textureFormat = SCE_GXM_TEXTURE_FORMAT_U8U8U8U8_ABGR; + } for (Uint32 i = 0; i < m_textures.size(); ++i) { auto& tex = m_textures[i]; @@ -634,13 +651,7 @@ Uint32 GXMRenderer::GetTextureId(IDirect3DRMTexture* iTexture) if (tex.version != texture->m_version) { void* textureData = sceGxmTextureGetData(&tex.gxmTexture); if(!supportedFormat) { - SDL_ConvertPixels( - surface->m_surface->w, surface->m_surface->h, - surface->m_surface->format, - surface->m_surface->pixels, - surface->m_surface->pitch, - SDL_PIXELFORMAT_ABGR8888, textureData, surface->m_surface->pitch - ); + copySurfaceTo(surface->m_surface, textureData, textureStride); } else { memcpy(textureData, surface->m_surface->pixels, textureSize); } @@ -650,32 +661,16 @@ Uint32 GXMRenderer::GetTextureId(IDirect3DRMTexture* iTexture) } } - int textureWidth = surface->m_surface->w; - int textureHeight = surface->m_surface->h; - int textureStride = surface->m_surface->pitch; - SDL_Log("Create Texture %s w=%d h=%d s=%d", SDL_GetPixelFormatName(surface->m_surface->format), textureWidth, textureHeight, textureStride); - if(!supportedFormat) { - textureAlignment = SCE_GXM_TEXTURE_ALIGNMENT; - textureSize = textureHeight * textureStride * 4; - textureFormat = SCE_GXM_TEXTURE_FORMAT_U8U8U8U8_ABGR; - } - // allocate gpu memory void* textureData = sceClibMspaceMemalign(this->m_data.cdramPool, textureAlignment, textureSize); uint8_t* paletteData = nullptr; if(!supportedFormat) { SDL_Log("unsupported SDL texture format %s, falling back on SDL_PIXELFORMAT_ABGR8888", SDL_GetPixelFormatName(surface->m_surface->format)); - SDL_ConvertPixels( - surface->m_surface->w, surface->m_surface->h, - surface->m_surface->format, - surface->m_surface->pixels, - surface->m_surface->pitch, - SDL_PIXELFORMAT_ABGR8888, textureData, surface->m_surface->pitch - ); + copySurfaceTo(surface->m_surface, textureData, textureStride); } else if(surface->m_surface->format == SDL_PIXELFORMAT_INDEX8) { @@ -685,6 +680,7 @@ Uint32 GXMRenderer::GetTextureId(IDirect3DRMTexture* iTexture) int pixelsSize = surface->m_surface->w * surface->m_surface->h; int alignBytes = ALIGNMENT(pixelsSize, SCE_GXM_PALETTE_ALIGNMENT); + SDL_Log("copying indexed texture data from=%p to=%p", surface->m_surface->pixels, textureData); memcpy(textureData, surface->m_surface->pixels, pixelsSize); paletteData = (uint8_t*)textureData + pixelsSize + alignBytes; @@ -692,6 +688,7 @@ Uint32 GXMRenderer::GetTextureId(IDirect3DRMTexture* iTexture) } else { + SDL_Log("copying texture data from=%p to=%p", surface->m_surface->pixels, textureData); memcpy(textureData, surface->m_surface->pixels, textureSize); } @@ -709,13 +706,14 @@ Uint32 GXMRenderer::GetTextureId(IDirect3DRMTexture* iTexture) tex.texture = texture; tex.version = texture->m_version; tex.gxmTexture = gxmTexture; + tex.textureSize = textureSize; AddTextureDestroyCallback(i, texture); return i; } } - Uint32 textureId = (Uint32) (m_textures.size() - 1); m_textures.push_back({texture, texture->m_version, gxmTexture}); + Uint32 textureId = (Uint32) (m_textures.size() - 1); AddTextureDestroyCallback(textureId, texture); return textureId; } @@ -873,10 +871,12 @@ int frames = 0; HRESULT GXMRenderer::BeginFrame() { frames++; - if(with_razor && !razor_triggered && frames == 10) { - SDL_Log("trigger razor in 10 frames"); - sceRazorGpuCaptureSetTriggerNextFrame("ux0:/data/capture.sgx"); - razor_triggered = true; + if(with_razor) { + if(!razor_triggered && frames == 10) { + SDL_Log("trigger razor"); + sceRazorGpuCaptureSetTriggerNextFrame("ux0:/data/capture.sgx"); + razor_triggered = true; + } } this->transparencyEnabled = false; @@ -916,12 +916,20 @@ void GXMRenderer::EnableTransparency() { this->transparencyEnabled = true; } -void transpose4x4(const float src[4][4], float dst[4][4]) { +static void transpose4x4(const float src[4][4], float dst[4][4]) { for (int i = 0; i < 4; ++i) for (int j = 0; j < 4; ++j) dst[j][i] = src[i][j]; } +static const D3DRMMATRIX4D identity4x4 = { + {1.0, 0.0, 0.0, 0.0}, + {0.0, 1.0, 0.0, 0.0}, + {0.0, 0.0, 1.0, 0.0}, + {0.0, 0.0, 0.0, 1.0}, +}; + + void GXMRenderer::SubmitDraw( DWORD meshId, const D3DRMMATRIX4D& modelViewMatrix, @@ -948,13 +956,15 @@ void GXMRenderer::SubmitDraw( sceGxmReserveVertexDefaultUniformBuffer(this->m_data.context, &vertUniforms); sceGxmReserveFragmentDefaultUniformBuffer(this->m_data.context, &fragUniforms); + /* D3DRMMATRIX4D modelViewMatrixTrans; D3DRMMATRIX4D projectionTrans; transpose4x4(modelViewMatrix, modelViewMatrixTrans); transpose4x4(m_projection, projectionTrans); + */ - SET_UNIFORM(vertUniforms, this->m_data.uModelViewMatrix, modelViewMatrixTrans); - SET_UNIFORM(vertUniforms, this->m_data.uProjectionMatrix, projectionTrans); + SET_UNIFORM(vertUniforms, this->m_data.uModelViewMatrix, modelViewMatrix); + SET_UNIFORM(vertUniforms, this->m_data.uProjectionMatrix, m_projection); SET_UNIFORM(vertUniforms, this->m_data.uNormalMatrix, normalMatrix); float color[4] = { @@ -1019,16 +1029,9 @@ void GXMRenderer::Clear(float r, float g, float b) { D3DRMMATRIX4D projection; CreateOrthoMatrix(0.0, 1.0, 1.0, 0.0, projection); - D3DRMMATRIX4D identity; - memset(identity, 0, sizeof(identity)); - identity[0][0] = 1.0f; - identity[1][1] = 1.0f; - identity[2][2] = 1.0f; - identity[3][3] = 1.0f; - Matrix3x3 normal = {{1.f, 0.f, 0.f}, {0.f, 1.f, 0.f}, {0.f, 0.f, 1.f}}; - SET_UNIFORM(vertUniforms, this->m_data.uModelViewMatrix, identity); // float4x4 + SET_UNIFORM(vertUniforms, this->m_data.uModelViewMatrix, identity4x4); // float4x4 SET_UNIFORM(vertUniforms, this->m_data.uNormalMatrix, normal); // float3x3 SET_UNIFORM(vertUniforms, this->m_data.uProjectionMatrix, projection); // float4x4 @@ -1041,10 +1044,10 @@ void GXMRenderer::Clear(float r, float g, float b) { float y2 = y1 + 1.0; Vertex* quadVertices = this->GetQuadVertices(); - quadVertices[0] = Vertex{ .position = {x1, y1, 0}, .normal = {0,0,0}, .texCoord = {0,0}}; - quadVertices[1] = Vertex{ .position = {x2, y1, 0}, .normal = {0,0,0}, .texCoord = {0,0}}; - quadVertices[2] = Vertex{ .position = {x1, y2, 0}, .normal = {0,0,0}, .texCoord = {0,0}}; - quadVertices[3] = Vertex{ .position = {x2, y2, 0}, .normal = {0,0,0}, .texCoord = {0,0}}; + quadVertices[0] = Vertex{ .position = {x1, y1, -1.0}, .normal = {0,0,0}, .texCoord = {0,0}}; + quadVertices[1] = Vertex{ .position = {x2, y1, -1.0}, .normal = {0,0,0}, .texCoord = {0,0}}; + quadVertices[2] = Vertex{ .position = {x1, y2, -1.0}, .normal = {0,0,0}, .texCoord = {0,0}}; + quadVertices[3] = Vertex{ .position = {x2, y2, -1.0}, .normal = {0,0,0}, .texCoord = {0,0}}; sceGxmSetVertexStream(this->m_data.context, 0, quadVertices); sceGxmDraw( @@ -1153,12 +1156,16 @@ void GXMRenderer::Draw2DImage(Uint32 textureId, const SDL_Rect& srcRect, const S quadVertices[3] = Vertex{ .position = {x2, y2, 0}, .normal = {0,0,0}, .texCoord = {u2, v2}}; sceGxmSetVertexStream(this->m_data.context, 0, quadVertices); + + sceGxmSetFrontDepthWriteEnable(this->m_data.context, SCE_GXM_DEPTH_WRITE_DISABLED); sceGxmDraw( this->m_data.context, SCE_GXM_PRIMITIVE_TRIANGLE_STRIP, SCE_GXM_INDEX_FORMAT_U16, this->m_data.quadIndices, 4 ); + sceGxmSetFrontDepthWriteEnable(this->m_data.context, SCE_GXM_DEPTH_WRITE_ENABLED); + sceGxmPopUserMarker(this->m_data.context); } @@ -1173,7 +1180,7 @@ void GXMRenderer::Download(SDL_Surface* target) { SDL_Surface* src = SDL_CreateSurfaceFrom( this->m_width, this->m_height, SDL_PIXELFORMAT_RGBA32, - this->m_data.displayBuffers[this->backBufferIndex], VITA_GXM_SCREEN_STRIDE + this->m_data.displayBuffers[this->frontBufferIndex], VITA_GXM_SCREEN_STRIDE ); SDL_BlitSurfaceScaled(src, &srcRect, target, nullptr, SDL_SCALEMODE_NEAREST); SDL_DestroySurface(src); diff --git a/miniwin/src/d3drm/backends/gxm/shaders/main.vert.cg b/miniwin/src/d3drm/backends/gxm/shaders/main.vert.cg index b45779bd..e9726498 100644 --- a/miniwin/src/d3drm/backends/gxm/shaders/main.vert.cg +++ b/miniwin/src/d3drm/backends/gxm/shaders/main.vert.cg @@ -3,9 +3,9 @@ void main( float3 aNormal : NORMAL, float2 aTexCoord : TEXCOORD0, - uniform float4x4 uModelViewMatrix, + column_major uniform float4x4 uModelViewMatrix, uniform float3x3 uNormalMatrix, - uniform float4x4 uProjectionMatrix, + column_major uniform float4x4 uProjectionMatrix, out float4 vPosition : POSITION, out float3 vViewPos : TEXCOORD1, diff --git a/miniwin/src/d3drm/backends/gxm/shaders/main.vert.gxp b/miniwin/src/d3drm/backends/gxm/shaders/main.vert.gxp index 238e1b43a92394edea657ec43c50fbcea773a9a0..c593d2b3ea712dcd151122e223a3e6e3e47715b6 100644 GIT binary patch delta 465 zcmcb?+Q8=L9udI6$Qr=>mx+Nv=Yrzl3x5|BGB8Lo0D%CA00A~2)&R;E0BIfu3y=^H zFoG3yFfuSG0(m7sT){XoP}l^>&;jBHKnVvsW+01!;R=wAOdo;r_W)@SbIZi32K6m7 z*YDvy;9$e#(%-_rgo8`awZEZ%qXdtjCz$3F>}_jm+$h0+@myO|=SB&>ww+-0dzoAM zTkMx`@F;f92dU>$Yz5OCij31h>Ny`eO#`WCYq|+mZ-+2{J=Af9*dec&CK=78$= zfa`;p!wKbkGE7_qQtx@-9hl~U+Xpc(!Ut*rFH{3x8^i!#2Ej=njlKu+z%(CJqaRc~ z%z^w+zCXjniA{|gCHxP(1JYY1CLdu`vEboh1bS5D7gIyy`Z++~aV@ev&^TkF1QQb@ eBQsE(ZBZ?l&#-9kfkur95{y6qQa@RN=>Y%;+k7ej diff --git a/miniwin/src/d3drm/backends/gxm/utils.h b/miniwin/src/d3drm/backends/gxm/utils.h index d0662492..b19cce7d 100644 --- a/miniwin/src/d3drm/backends/gxm/utils.h +++ b/miniwin/src/d3drm/backends/gxm/utils.h @@ -32,7 +32,7 @@ return ret; \ } -static void printMatrix4x4(float mat[4][4]) { +static void printMatrix4x4(const float mat[4][4]) { sceClibPrintf("mat4{\n"); for(int i = 0; i < 4; i++) { sceClibPrintf("%f %f %f %f\n", mat[i][0], mat[i][1], mat[i][2], mat[i][3]); diff --git a/miniwin/src/internal/d3drmrenderer_gxm.h b/miniwin/src/internal/d3drmrenderer_gxm.h index 77e12647..edada145 100644 --- a/miniwin/src/internal/d3drmrenderer_gxm.h +++ b/miniwin/src/internal/d3drmrenderer_gxm.h @@ -20,6 +20,7 @@ struct GXMTextureCacheEntry { IDirect3DRMTexture* texture; Uint32 version; SceGxmTexture gxmTexture; + size_t textureSize; }; struct GXMMeshCacheEntry {