Merge branch 'master' into emscripten

This commit is contained in:
Christian Semmler 2025-06-03 21:00:19 -07:00
commit f973aa77b3
No known key found for this signature in database
GPG Key ID: 086DAA1360BEEE5C
9 changed files with 104 additions and 46 deletions

View File

@ -92,11 +92,6 @@ OpenGL15Renderer::~OpenGL15Renderer()
} }
} }
void OpenGL15Renderer::SetBackbuffer(SDL_Surface* surface)
{
m_backbuffer = surface;
}
void OpenGL15Renderer::PushVertices(const PositionColorVertex* verts, size_t count) void OpenGL15Renderer::PushVertices(const PositionColorVertex* verts, size_t count)
{ {
m_vertices.assign(verts, verts + count); m_vertices.assign(verts, verts + count);
@ -148,7 +143,7 @@ const char* OpenGL15Renderer::GetName()
HRESULT OpenGL15Renderer::Render() HRESULT OpenGL15Renderer::Render()
{ {
if (!m_backbuffer) { if (!DDBackBuffer) {
return DDERR_GENERIC; return DDERR_GENERIC;
} }
SDL_GL_MakeCurrent(DDWindow, m_context); SDL_GL_MakeCurrent(DDWindow, m_context);
@ -251,7 +246,7 @@ HRESULT OpenGL15Renderer::Render()
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Composite onto SDL backbuffer // Composite onto SDL backbuffer
SDL_BlitSurface(m_renderedImage, nullptr, m_backbuffer, nullptr); SDL_BlitSurface(m_renderedImage, nullptr, DDBackBuffer, nullptr);
return DD_OK; return DD_OK;
} }

View File

@ -200,11 +200,6 @@ Direct3DRMSDL3GPURenderer::~Direct3DRMSDL3GPURenderer()
SDL_DestroyGPUDevice(m_device); SDL_DestroyGPUDevice(m_device);
} }
void Direct3DRMSDL3GPURenderer::SetBackbuffer(SDL_Surface* buf)
{
m_backbuffer = buf;
}
void Direct3DRMSDL3GPURenderer::PushLights(const SceneLight* vertices, size_t count) void Direct3DRMSDL3GPURenderer::PushLights(const SceneLight* vertices, size_t count)
{ {
if (count > 3) { if (count > 3) {
@ -388,7 +383,7 @@ HRESULT Direct3DRMSDL3GPURenderer::Render()
SDL_DestroySurface(m_renderedImage); SDL_DestroySurface(m_renderedImage);
SDL_UnmapGPUTransferBuffer(m_device, m_downloadTransferBuffer); SDL_UnmapGPUTransferBuffer(m_device, m_downloadTransferBuffer);
m_renderedImage = convertedRender; m_renderedImage = convertedRender;
SDL_BlitSurface(m_renderedImage, nullptr, m_backbuffer, nullptr); SDL_BlitSurface(m_renderedImage, nullptr, DDBackBuffer, nullptr);
return DD_OK; return DD_OK;
} }

View File

@ -14,11 +14,6 @@ Direct3DRMSoftwareRenderer::Direct3DRMSoftwareRenderer(DWORD width, DWORD height
m_zBuffer.resize(m_width * m_height); m_zBuffer.resize(m_width * m_height);
} }
void Direct3DRMSoftwareRenderer::SetBackbuffer(SDL_Surface* buf)
{
m_backbuffer = buf;
}
void Direct3DRMSoftwareRenderer::PushLights(const SceneLight* lights, size_t count) void Direct3DRMSoftwareRenderer::PushLights(const SceneLight* lights, size_t count)
{ {
m_lights.assign(lights, lights + count); m_lights.assign(lights, lights + count);
@ -292,8 +287,8 @@ void Direct3DRMSoftwareRenderer::DrawTriangleProjected(
} }
} }
Uint8* pixels = (Uint8*) m_backbuffer->pixels; Uint8* pixels = (Uint8*) DDBackBuffer->pixels;
int pitch = m_backbuffer->pitch; int pitch = DDBackBuffer->pitch;
for (int y = minY; y <= maxY; ++y) { for (int y = minY; y <= maxY; ++y) {
for (int x = minX; x <= maxX; ++x) { for (int x = minX; x <= maxX; ++x) {
@ -409,7 +404,7 @@ Uint32 Direct3DRMSoftwareRenderer::GetTextureId(IDirect3DRMTexture* iTexture)
if (texRef.version != texture->m_version) { if (texRef.version != texture->m_version) {
// Update animated textures // Update animated textures
SDL_DestroySurface(texRef.cached); SDL_DestroySurface(texRef.cached);
texRef.cached = SDL_ConvertSurface(surface->m_surface, m_backbuffer->format); texRef.cached = SDL_ConvertSurface(surface->m_surface, DDBackBuffer->format);
SDL_LockSurface(texRef.cached); SDL_LockSurface(texRef.cached);
texRef.version = texture->m_version; texRef.version = texture->m_version;
} }
@ -417,7 +412,7 @@ Uint32 Direct3DRMSoftwareRenderer::GetTextureId(IDirect3DRMTexture* iTexture)
} }
} }
SDL_Surface* convertedRender = SDL_ConvertSurface(surface->m_surface, m_backbuffer->format); SDL_Surface* convertedRender = SDL_ConvertSurface(surface->m_surface, DDBackBuffer->format);
SDL_LockSurface(convertedRender); SDL_LockSurface(convertedRender);
// Reuse freed slot // Reuse freed slot
@ -468,17 +463,17 @@ const char* Direct3DRMSoftwareRenderer::GetName()
HRESULT Direct3DRMSoftwareRenderer::Render() HRESULT Direct3DRMSoftwareRenderer::Render()
{ {
if (!m_backbuffer || m_vertexBuffer.size() % 3 != 0 || !SDL_LockSurface(m_backbuffer)) { if (!DDBackBuffer || m_vertexBuffer.size() % 3 != 0 || !SDL_LockSurface(DDBackBuffer)) {
return DDERR_GENERIC; return DDERR_GENERIC;
} }
ClearZBuffer(); ClearZBuffer();
m_format = SDL_GetPixelFormatDetails(m_backbuffer->format); m_format = SDL_GetPixelFormatDetails(DDBackBuffer->format);
m_palette = SDL_GetSurfacePalette(m_backbuffer); m_palette = SDL_GetSurfacePalette(DDBackBuffer);
m_bytesPerPixel = m_format->bits_per_pixel / 8; m_bytesPerPixel = m_format->bits_per_pixel / 8;
for (size_t i = 0; i + 2 < m_vertexBuffer.size(); i += 3) { for (size_t i = 0; i + 2 < m_vertexBuffer.size(); i += 3) {
DrawTriangleClipped(m_vertexBuffer[i], m_vertexBuffer[i + 1], m_vertexBuffer[i + 2]); DrawTriangleClipped(m_vertexBuffer[i], m_vertexBuffer[i + 1], m_vertexBuffer[i + 2]);
} }
SDL_UnlockSurface(m_backbuffer); SDL_UnlockSurface(DDBackBuffer);
m_vertexBuffer.clear(); m_vertexBuffer.clear();

View File

@ -122,9 +122,9 @@ HRESULT Direct3DRMViewportImpl::CollectSceneData()
std::vector<PositionColorVertex> verts; std::vector<PositionColorVertex> verts;
// Compute camera matrix // Compute camera matrix
D3DRMMATRIX4D cameraWorld, viewMatrix; D3DRMMATRIX4D cameraWorld;
ComputeFrameWorldMatrix(m_camera, cameraWorld); ComputeFrameWorldMatrix(m_camera, cameraWorld);
D3DRMMatrixInvertOrthogonal(viewMatrix, cameraWorld); D3DRMMatrixInvertOrthogonal(m_viewMatrix, cameraWorld);
std::function<void(IDirect3DRMFrame*, D3DRMMATRIX4D)> recurseFrame; std::function<void(IDirect3DRMFrame*, D3DRMMATRIX4D)> recurseFrame;
std::function<void(IDirect3DRMFrame*, D3DRMMATRIX4D)> recurseChildren; std::function<void(IDirect3DRMFrame*, D3DRMMATRIX4D)> recurseChildren;
@ -270,12 +270,12 @@ HRESULT Direct3DRMViewportImpl::CollectSceneData()
// View transform // View transform
D3DVECTOR viewPos; D3DVECTOR viewPos;
viewPos.x = worldPos.x * viewMatrix[0][0] + worldPos.y * viewMatrix[1][0] + viewPos.x = worldPos.x * m_viewMatrix[0][0] + worldPos.y * m_viewMatrix[1][0] +
worldPos.z * viewMatrix[2][0] + viewMatrix[3][0]; worldPos.z * m_viewMatrix[2][0] + m_viewMatrix[3][0];
viewPos.y = worldPos.x * viewMatrix[0][1] + worldPos.y * viewMatrix[1][1] + viewPos.y = worldPos.x * m_viewMatrix[0][1] + worldPos.y * m_viewMatrix[1][1] +
worldPos.z * viewMatrix[2][1] + viewMatrix[3][1]; worldPos.z * m_viewMatrix[2][1] + m_viewMatrix[3][1];
viewPos.z = worldPos.x * viewMatrix[0][2] + worldPos.y * viewMatrix[1][2] + viewPos.z = worldPos.x * m_viewMatrix[0][2] + worldPos.y * m_viewMatrix[1][2] +
worldPos.z * viewMatrix[2][2] + viewMatrix[3][2]; worldPos.z * m_viewMatrix[2][2] + m_viewMatrix[3][2];
// View transform // View transform
D3DVECTOR viewNorm; D3DVECTOR viewNorm;
@ -334,7 +334,6 @@ HRESULT Direct3DRMViewportImpl::CollectSceneData()
m_renderer->PushLights(lights.data(), lights.size()); m_renderer->PushLights(lights.data(), lights.size());
m_renderer->PushVertices(verts.data(), verts.size()); m_renderer->PushVertices(verts.data(), verts.size());
m_renderer->SetBackbuffer(DDBackBuffer);
return D3DRM_OK; return D3DRM_OK;
} }
@ -441,14 +440,23 @@ void Direct3DRMViewportImpl::UpdateProjectionMatrix()
float f = m_front / m_field; float f = m_front / m_field;
float depth = m_back - m_front; float depth = m_back - m_front;
D3DRMMATRIX4D perspective = { D3DRMMATRIX4D projection = {
{f, 0, 0, 0}, {f, 0, 0, 0},
{0, f * aspect, 0, 0}, {0, f * aspect, 0, 0},
{0, 0, m_back / depth, 1}, {0, 0, m_back / depth, 1},
{0, 0, (-m_front * m_back) / depth, 0}, {0, 0, (-m_front * m_back) / depth, 0},
}; };
memcpy(m_projectionMatrix, projection, sizeof(D3DRMMATRIX4D));
m_renderer->SetProjection(perspective, m_front, m_back); m_renderer->SetProjection(projection, m_front, m_back);
D3DRMMATRIX4D inverseProjectionMatrix = {
{1.0f / f, 0, 0, 0},
{0, 1.0f / (f * aspect), 0, 0},
{0, 0, 0, depth / (-m_front * m_back)},
{0, 0, 1, -(m_back / depth) * depth / (-m_front * m_back)},
};
memcpy(m_inverseProjectionMatrix, inverseProjectionMatrix, sizeof(D3DRMMATRIX4D));
} }
D3DVALUE Direct3DRMViewportImpl::GetField() D3DVALUE Direct3DRMViewportImpl::GetField()
@ -468,13 +476,82 @@ DWORD Direct3DRMViewportImpl::GetHeight()
HRESULT Direct3DRMViewportImpl::Transform(D3DRMVECTOR4D* screen, D3DVECTOR* world) HRESULT Direct3DRMViewportImpl::Transform(D3DRMVECTOR4D* screen, D3DVECTOR* world)
{ {
MINIWIN_NOT_IMPLEMENTED(); D3DRMVECTOR4D worldVec = {world->x, world->y, world->z, 1.0f};
D3DRMVECTOR4D viewVec;
viewVec.x = m_viewMatrix[0][0] * worldVec.x + m_viewMatrix[1][0] * worldVec.y + m_viewMatrix[2][0] * worldVec.z +
m_viewMatrix[3][0] * worldVec.w;
viewVec.y = m_viewMatrix[0][1] * worldVec.x + m_viewMatrix[1][1] * worldVec.y + m_viewMatrix[2][1] * worldVec.z +
m_viewMatrix[3][1] * worldVec.w;
viewVec.z = m_viewMatrix[0][2] * worldVec.x + m_viewMatrix[1][2] * worldVec.y + m_viewMatrix[2][2] * worldVec.z +
m_viewMatrix[3][2] * worldVec.w;
viewVec.w = m_viewMatrix[0][3] * worldVec.x + m_viewMatrix[1][3] * worldVec.y + m_viewMatrix[2][3] * worldVec.z +
m_viewMatrix[3][3] * worldVec.w;
screen->x = viewVec.x * m_projectionMatrix[0][0] + viewVec.y * m_projectionMatrix[1][0] +
viewVec.z * m_projectionMatrix[2][0] + viewVec.w * m_projectionMatrix[3][0];
screen->y = viewVec.x * m_projectionMatrix[0][1] + viewVec.y * m_projectionMatrix[1][1] +
viewVec.z * m_projectionMatrix[2][1] + viewVec.w * m_projectionMatrix[3][1];
screen->z = viewVec.x * m_projectionMatrix[0][2] + viewVec.y * m_projectionMatrix[1][2] +
viewVec.z * m_projectionMatrix[2][2] + viewVec.w * m_projectionMatrix[3][2];
screen->w = viewVec.x * m_projectionMatrix[0][3] + viewVec.y * m_projectionMatrix[1][3] +
viewVec.z * m_projectionMatrix[2][3] + viewVec.w * m_projectionMatrix[3][3];
float invW = 1.0f / screen->w;
float ndcX = screen->x * invW;
float ndcY = screen->y * invW;
screen->x = (ndcX * 0.5f + 0.5f) * m_width;
screen->y = (1.0f - (ndcY * 0.5f + 0.5f)) * m_height;
// Undo perspective divide
screen->x *= screen->z;
screen->y *= screen->w;
return DD_OK; return DD_OK;
} }
HRESULT Direct3DRMViewportImpl::InverseTransform(D3DVECTOR* world, D3DRMVECTOR4D* screen) HRESULT Direct3DRMViewportImpl::InverseTransform(D3DVECTOR* world, D3DRMVECTOR4D* screen)
{ {
MINIWIN_NOT_IMPLEMENTED(); // Convert to screen coordinates
float screenX = screen->x / screen->z;
float screenY = screen->y / screen->w;
// Convert screen coordinates to NDC
float ndcX = screenX / m_width * 2.0f - 1.0f;
float ndcY = 1.0f - (screenY / m_height) * 2.0f;
float clipVec[4] = {ndcX * screen->w, ndcY * screen->w, screen->z, screen->w};
float viewVec[4] = {0.0f, 0.0f, 0.0f, 0.0f};
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
viewVec[j] += m_inverseProjectionMatrix[i][j] * clipVec[i];
}
}
float invViewMatrix[4][4];
D3DRMMatrixInvertOrthogonal(invViewMatrix, m_viewMatrix);
float worldVec[4] = {0.0f, 0.0f, 0.0f, 0.0f};
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
worldVec[j] += invViewMatrix[i][j] * viewVec[i];
}
}
// Perspective divide
if (worldVec[3] != 0.0f) {
world->x = worldVec[0] / worldVec[3];
world->y = worldVec[1] / worldVec[3];
world->z = worldVec[2] / worldVec[3];
}
else {
world->x = worldVec[0];
world->y = worldVec[1];
world->z = worldVec[2];
}
return DD_OK; return DD_OK;
} }

View File

@ -35,7 +35,6 @@ static_assert(sizeof(SceneLight) == 48);
class Direct3DRMRenderer : public IDirect3DDevice2 { class Direct3DRMRenderer : public IDirect3DDevice2 {
public: public:
virtual void SetBackbuffer(SDL_Surface* backbuffer) = 0;
virtual void PushVertices(const PositionColorVertex* vertices, size_t count) = 0; virtual void PushVertices(const PositionColorVertex* vertices, size_t count) = 0;
virtual void PushLights(const SceneLight* vertices, size_t count) = 0; virtual void PushLights(const SceneLight* vertices, size_t count) = 0;
virtual void SetProjection(D3DRMMATRIX4D perspective, D3DVALUE front, D3DVALUE back) = 0; virtual void SetProjection(D3DRMMATRIX4D perspective, D3DVALUE front, D3DVALUE back) = 0;

View File

@ -15,7 +15,6 @@ class OpenGL15Renderer : public Direct3DRMRenderer {
static Direct3DRMRenderer* Create(DWORD width, DWORD height); static Direct3DRMRenderer* Create(DWORD width, DWORD height);
OpenGL15Renderer(int width, int height, SDL_GLContext context, GLuint fbo, GLuint colorTex, GLuint depthRb); OpenGL15Renderer(int width, int height, SDL_GLContext context, GLuint fbo, GLuint colorTex, GLuint depthRb);
~OpenGL15Renderer() override; ~OpenGL15Renderer() override;
void SetBackbuffer(SDL_Surface* surface) override;
void PushVertices(const PositionColorVertex* verts, size_t count) override; void PushVertices(const PositionColorVertex* verts, size_t count) override;
void PushLights(const SceneLight* lightsArray, size_t count) override; void PushLights(const SceneLight* lightsArray, size_t count) override;
void SetProjection(D3DRMMATRIX4D perspective, D3DVALUE front, D3DVALUE back) override; void SetProjection(D3DRMMATRIX4D perspective, D3DVALUE front, D3DVALUE back) override;
@ -29,7 +28,6 @@ class OpenGL15Renderer : public Direct3DRMRenderer {
private: private:
SDL_GLContext m_context; SDL_GLContext m_context;
D3DRMMATRIX4D m_projection; D3DRMMATRIX4D m_projection;
SDL_Surface* m_backbuffer = nullptr;
SDL_Surface* m_renderedImage; SDL_Surface* m_renderedImage;
int m_width, m_height; int m_width, m_height;
std::vector<PositionColorVertex> m_vertices; std::vector<PositionColorVertex> m_vertices;

View File

@ -20,7 +20,6 @@ class Direct3DRMSDL3GPURenderer : public Direct3DRMRenderer {
public: public:
static Direct3DRMRenderer* Create(DWORD width, DWORD height); static Direct3DRMRenderer* Create(DWORD width, DWORD height);
~Direct3DRMSDL3GPURenderer() override; ~Direct3DRMSDL3GPURenderer() override;
void SetBackbuffer(SDL_Surface* backbuffer) override;
void PushVertices(const PositionColorVertex* vertices, size_t count) override; void PushVertices(const PositionColorVertex* vertices, size_t count) override;
void PushLights(const SceneLight* vertices, size_t count) override; void PushLights(const SceneLight* vertices, size_t count) override;
Uint32 GetTextureId(IDirect3DRMTexture* texture) override; Uint32 GetTextureId(IDirect3DRMTexture* texture) override;
@ -52,7 +51,6 @@ class Direct3DRMSDL3GPURenderer : public Direct3DRMRenderer {
ViewportUniforms m_uniforms; ViewportUniforms m_uniforms;
SceneLights m_lights; SceneLights m_lights;
D3DDEVICEDESC m_desc; D3DDEVICEDESC m_desc;
SDL_Surface* m_backbuffer = nullptr;
SDL_GPUDevice* m_device; SDL_GPUDevice* m_device;
SDL_GPUGraphicsPipeline* m_pipeline; SDL_GPUGraphicsPipeline* m_pipeline;
SDL_GPUTexture* m_transferTexture; SDL_GPUTexture* m_transferTexture;

View File

@ -19,7 +19,6 @@ struct TextureCache {
class Direct3DRMSoftwareRenderer : public Direct3DRMRenderer { class Direct3DRMSoftwareRenderer : public Direct3DRMRenderer {
public: public:
Direct3DRMSoftwareRenderer(DWORD width, DWORD height); Direct3DRMSoftwareRenderer(DWORD width, DWORD height);
void SetBackbuffer(SDL_Surface* backbuffer) override;
void PushVertices(const PositionColorVertex* vertices, size_t count) override; void PushVertices(const PositionColorVertex* vertices, size_t count) override;
void PushLights(const SceneLight* vertices, size_t count) override; void PushLights(const SceneLight* vertices, size_t count) override;
Uint32 GetTextureId(IDirect3DRMTexture* texture) override; Uint32 GetTextureId(IDirect3DRMTexture* texture) override;
@ -45,7 +44,6 @@ class Direct3DRMSoftwareRenderer : public Direct3DRMRenderer {
DWORD m_width; DWORD m_width;
DWORD m_height; DWORD m_height;
SDL_Surface* m_backbuffer = nullptr;
SDL_Palette* m_palette; SDL_Palette* m_palette;
const SDL_PixelFormatDetails* m_format; const SDL_PixelFormatDetails* m_format;
int m_bytesPerPixel; int m_bytesPerPixel;

View File

@ -41,6 +41,9 @@ struct Direct3DRMViewportImpl : public Direct3DRMObjectBaseImpl<IDirect3DRMViewp
D3DCOLOR m_backgroundColor = 0xFF000000; D3DCOLOR m_backgroundColor = 0xFF000000;
DWORD m_width; DWORD m_width;
DWORD m_height; DWORD m_height;
D3DRMMATRIX4D m_viewMatrix;
D3DRMMATRIX4D m_projectionMatrix;
D3DRMMATRIX4D m_inverseProjectionMatrix;
IDirect3DRMFrame* m_rootFrame = nullptr; IDirect3DRMFrame* m_rootFrame = nullptr;
IDirect3DRMFrame* m_camera = nullptr; IDirect3DRMFrame* m_camera = nullptr;
D3DVALUE m_front = 1.f; D3DVALUE m_front = 1.f;