From ee372fbc8ff742cbb47e126d31a5cf6e410ebc02 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Sun, 8 Jun 2025 23:06:38 +0200 Subject: [PATCH] Replace GeometryVertex with D3DRMVERTEX (#259) --- miniwin/include/miniwin/d3drm.h | 11 ++++- .../src/d3drm/backends/opengl15/renderer.cpp | 6 +-- .../src/d3drm/backends/sdl3gpu/renderer.cpp | 20 +++++----- .../src/d3drm/backends/software/renderer.cpp | 40 +++++++++---------- miniwin/src/d3drm/d3drmviewport.cpp | 4 +- miniwin/src/internal/d3drmrenderer.h | 13 +----- miniwin/src/internal/d3drmrenderer_opengl15.h | 2 +- miniwin/src/internal/d3drmrenderer_sdl3gpu.h | 2 +- miniwin/src/internal/d3drmrenderer_software.h | 14 +++---- miniwin/src/internal/d3drmviewport_impl.h | 2 +- 10 files changed, 57 insertions(+), 57 deletions(-) diff --git a/miniwin/include/miniwin/d3drm.h b/miniwin/include/miniwin/d3drm.h index 72d8637e..3bcd1ffc 100644 --- a/miniwin/include/miniwin/d3drm.h +++ b/miniwin/include/miniwin/d3drm.h @@ -136,10 +136,19 @@ struct D3DRMBOX { D3DVECTOR max; }; +struct TexCoord { + float u, v; +}; + struct D3DRMVERTEX { D3DVECTOR position; D3DVECTOR normal; - D3DVALUE tu, tv; + union { + struct { + D3DVALUE tu, tv; + }; + TexCoord texCoord; + }; }; struct IDirect3DRMObject : public IUnknown { diff --git a/miniwin/src/d3drm/backends/opengl15/renderer.cpp b/miniwin/src/d3drm/backends/opengl15/renderer.cpp index f59cb898..8e12debe 100644 --- a/miniwin/src/d3drm/backends/opengl15/renderer.cpp +++ b/miniwin/src/d3drm/backends/opengl15/renderer.cpp @@ -304,7 +304,7 @@ HRESULT OpenGL15Renderer::BeginFrame(const D3DRMMATRIX4D& viewMatrix) } void OpenGL15Renderer::SubmitDraw( - const GeometryVertex* vertices, + const D3DRMVERTEX* vertices, const size_t count, const D3DRMMATRIX4D& worldMatrix, const Matrix3x3& normalMatrix, @@ -350,8 +350,8 @@ void OpenGL15Renderer::SubmitDraw( glBegin(GL_TRIANGLES); for (size_t i = 0; i < count; i++) { - const GeometryVertex& v = vertices[i]; - glNormal3f(v.normals.x, v.normals.y, v.normals.z); + const D3DRMVERTEX& v = vertices[i]; + glNormal3f(v.normal.x, v.normal.y, v.normal.z); glTexCoord2f(v.texCoord.u, v.texCoord.v); glVertex3f(v.position.x, v.position.y, v.position.z); } diff --git a/miniwin/src/d3drm/backends/sdl3gpu/renderer.cpp b/miniwin/src/d3drm/backends/sdl3gpu/renderer.cpp index 329f98a4..56c508a0 100644 --- a/miniwin/src/d3drm/backends/sdl3gpu/renderer.cpp +++ b/miniwin/src/d3drm/backends/sdl3gpu/renderer.cpp @@ -32,7 +32,7 @@ static SDL_GPUGraphicsPipeline* InitializeGraphicsPipeline(SDL_GPUDevice* device SDL_GPUVertexBufferDescription vertexBufferDescs[1] = {}; vertexBufferDescs[0].slot = 0; - vertexBufferDescs[0].pitch = sizeof(GeometryVertex); + vertexBufferDescs[0].pitch = sizeof(D3DRMVERTEX); vertexBufferDescs[0].input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX; vertexBufferDescs[0].instance_step_rate = 0; @@ -40,17 +40,17 @@ static SDL_GPUGraphicsPipeline* InitializeGraphicsPipeline(SDL_GPUDevice* device vertexAttrs[0].location = 0; vertexAttrs[0].buffer_slot = 0; vertexAttrs[0].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3; - vertexAttrs[0].offset = offsetof(GeometryVertex, position); + vertexAttrs[0].offset = offsetof(D3DRMVERTEX, position); vertexAttrs[1].location = 1; vertexAttrs[1].buffer_slot = 0; vertexAttrs[1].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3; - vertexAttrs[1].offset = offsetof(GeometryVertex, normals); + vertexAttrs[1].offset = offsetof(D3DRMVERTEX, normal); vertexAttrs[2].location = 2; vertexAttrs[2].buffer_slot = 0; vertexAttrs[2].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2; - vertexAttrs[2].offset = offsetof(GeometryVertex, texCoord); + vertexAttrs[2].offset = offsetof(D3DRMVERTEX, tv); SDL_GPUVertexInputState vertexInputState = {}; vertexInputState.vertex_buffer_descriptions = vertexBufferDescs; @@ -298,7 +298,7 @@ HRESULT Direct3DRMSDL3GPURenderer::BeginFrame(const D3DRMMATRIX4D& viewMatrix) } void Direct3DRMSDL3GPURenderer::SubmitDraw( - const GeometryVertex* vertices, + const D3DRMVERTEX* vertices, const size_t count, const D3DRMMATRIX4D& worldMatrix, const Matrix3x3& normalMatrix, @@ -317,7 +317,7 @@ void Direct3DRMSDL3GPURenderer::SubmitDraw( } SDL_GPUBufferCreateInfo bufferCreateInfo = {}; bufferCreateInfo.usage = SDL_GPU_BUFFERUSAGE_VERTEX; - bufferCreateInfo.size = static_cast(sizeof(GeometryVertex) * count); + bufferCreateInfo.size = static_cast(sizeof(D3DRMVERTEX) * count); m_vertexBuffer = SDL_CreateGPUBuffer(m_device, &bufferCreateInfo); if (!m_vertexBuffer) { SDL_LogError(LOG_CATEGORY_MINIWIN, "SDL_CreateGPUBuffer returned NULL buffer (%s)", SDL_GetError()); @@ -332,7 +332,7 @@ void Direct3DRMSDL3GPURenderer::SubmitDraw( SDL_GPUTransferBufferCreateInfo transferCreateInfo = {}; transferCreateInfo.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD; - transferCreateInfo.size = static_cast(sizeof(GeometryVertex) * m_vertexCount); + transferCreateInfo.size = static_cast(sizeof(D3DRMVERTEX) * m_vertexCount); SDL_GPUTransferBuffer* transferBuffer = SDL_CreateGPUTransferBuffer(m_device, &transferCreateInfo); if (!transferBuffer) { SDL_LogError( @@ -342,12 +342,12 @@ void Direct3DRMSDL3GPURenderer::SubmitDraw( ); } - GeometryVertex* transferData = (GeometryVertex*) SDL_MapGPUTransferBuffer(m_device, transferBuffer, false); + D3DRMVERTEX* transferData = (D3DRMVERTEX*) SDL_MapGPUTransferBuffer(m_device, transferBuffer, false); if (!transferData) { SDL_LogError(LOG_CATEGORY_MINIWIN, "SDL_MapGPUTransferBuffer returned NULL buffer (%s)", SDL_GetError()); } - memcpy(transferData, vertices, m_vertexCount * sizeof(GeometryVertex)); + memcpy(transferData, vertices, m_vertexCount * sizeof(D3DRMVERTEX)); SDL_UnmapGPUTransferBuffer(m_device, transferBuffer); @@ -366,7 +366,7 @@ void Direct3DRMSDL3GPURenderer::SubmitDraw( SDL_GPUBufferRegion bufferRegion = {}; bufferRegion.buffer = m_vertexBuffer; bufferRegion.offset = 0; - bufferRegion.size = static_cast(sizeof(GeometryVertex) * m_vertexCount); + bufferRegion.size = static_cast(sizeof(D3DRMVERTEX) * m_vertexCount); SDL_UploadToGPUBuffer(copyPass, &transferLocation, &bufferRegion, false); diff --git a/miniwin/src/d3drm/backends/software/renderer.cpp b/miniwin/src/d3drm/backends/software/renderer.cpp index 6748aa72..bfa6db0d 100644 --- a/miniwin/src/d3drm/backends/software/renderer.cpp +++ b/miniwin/src/d3drm/backends/software/renderer.cpp @@ -32,7 +32,7 @@ void Direct3DRMSoftwareRenderer::ClearZBuffer() std::fill(m_zBuffer.begin(), m_zBuffer.end(), std::numeric_limits::infinity()); } -void Direct3DRMSoftwareRenderer::ProjectVertex(const GeometryVertex& v, D3DRMVECTOR4D& p) const +void Direct3DRMSoftwareRenderer::ProjectVertex(const D3DRMVERTEX& v, D3DRMVECTOR4D& p) const { float px = m_projection[0][0] * v.position.x + m_projection[1][0] * v.position.y + m_projection[2][0] * v.position.z + m_projection[3][0]; @@ -58,7 +58,7 @@ void Direct3DRMSoftwareRenderer::ProjectVertex(const GeometryVertex& v, D3DRMVEC p.z = pz; } -GeometryVertex SplitEdge(GeometryVertex a, const GeometryVertex& b, float plane) +D3DRMVERTEX SplitEdge(D3DRMVERTEX a, const D3DRMVERTEX& b, float plane) { float t = (plane - a.position.z) / (b.position.z - a.position.z); a.position.x = a.position.x + t * (b.position.x - a.position.x); @@ -68,21 +68,21 @@ GeometryVertex SplitEdge(GeometryVertex a, const GeometryVertex& b, float plane) a.texCoord.u = a.texCoord.u + t * (b.texCoord.u - a.texCoord.u); a.texCoord.v = a.texCoord.v + t * (b.texCoord.v - a.texCoord.v); - a.normals.x = a.normals.x + t * (b.normals.x - a.normals.x); - a.normals.y = a.normals.y + t * (b.normals.y - a.normals.y); - a.normals.z = a.normals.z + t * (b.normals.z - a.normals.z); + a.normal.x = a.normal.x + t * (b.normal.x - a.normal.x); + a.normal.y = a.normal.y + t * (b.normal.y - a.normal.y); + a.normal.z = a.normal.z + t * (b.normal.z - a.normal.z); - float len = std::sqrt(a.normals.x * a.normals.x + a.normals.y * a.normals.y + a.normals.z * a.normals.z); + float len = std::sqrt(a.normal.x * a.normal.x + a.normal.y * a.normal.y + a.normal.z * a.normal.z); if (len > 0.0001f) { - a.normals.x /= len; - a.normals.y /= len; - a.normals.z /= len; + a.normal.x /= len; + a.normal.y /= len; + a.normal.z /= len; } return a; } -void Direct3DRMSoftwareRenderer::DrawTriangleClipped(const GeometryVertex (&v)[3], const Appearance& appearance) +void Direct3DRMSoftwareRenderer::DrawTriangleClipped(const D3DRMVERTEX (&v)[3], const Appearance& appearance) { bool in0 = v[0].position.z >= m_front; bool in1 = v[1].position.z >= m_front; @@ -98,7 +98,7 @@ void Direct3DRMSoftwareRenderer::DrawTriangleClipped(const GeometryVertex (&v)[3 DrawTriangleProjected(v[0], v[1], v[2], appearance); } else if (insideCount == 2) { - GeometryVertex split; + D3DRMVERTEX split; if (!in0) { split = SplitEdge(v[2], v[0], m_front); DrawTriangleProjected(v[1], v[2], split, appearance); @@ -149,14 +149,14 @@ void Direct3DRMSoftwareRenderer::BlendPixel(Uint8* pixelAddr, Uint8 r, Uint8 g, memcpy(pixelAddr, &blended, m_bytesPerPixel); } -SDL_Color Direct3DRMSoftwareRenderer::ApplyLighting(const GeometryVertex& vertex, const Appearance& appearance) +SDL_Color Direct3DRMSoftwareRenderer::ApplyLighting(const D3DRMVERTEX& vertex, const Appearance& appearance) { FColor specular = {0, 0, 0, 0}; FColor diffuse = {0, 0, 0, 0}; // Position and normal D3DVECTOR position = vertex.position; - D3DVECTOR normal = vertex.normals; + D3DVECTOR normal = vertex.normal; float normLen = std::sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z); if (normLen == 0.0f) { return appearance.color; @@ -220,9 +220,9 @@ SDL_Color Direct3DRMSoftwareRenderer::ApplyLighting(const GeometryVertex& vertex } void Direct3DRMSoftwareRenderer::DrawTriangleProjected( - const GeometryVertex& v0, - const GeometryVertex& v1, - const GeometryVertex& v2, + const D3DRMVERTEX& v0, + const D3DRMVERTEX& v1, + const D3DRMVERTEX& v2, const Appearance& appearance ) { @@ -470,7 +470,7 @@ HRESULT Direct3DRMSoftwareRenderer::BeginFrame(const D3DRMMATRIX4D& viewMatrix) } void Direct3DRMSoftwareRenderer::SubmitDraw( - const GeometryVertex* vertices, + const D3DRMVERTEX* vertices, const size_t count, const D3DRMMATRIX4D& worldMatrix, const Matrix3x3& normalMatrix, @@ -481,11 +481,11 @@ void Direct3DRMSoftwareRenderer::SubmitDraw( MultiplyMatrix(mvMatrix, worldMatrix, m_viewMatrix); for (size_t i = 0; i + 2 < count; i += 3) { - GeometryVertex vrts[3]; + D3DRMVERTEX vrts[3]; for (size_t j = 0; j < 3; ++j) { - const GeometryVertex& src = vertices[i + j]; + const D3DRMVERTEX& src = vertices[i + j]; vrts[j].position = TransformPoint(src.position, mvMatrix); - vrts[j].normals = Normalize(TransformNormal(src.normals, normalMatrix)); + vrts[j].normal = Normalize(TransformNormal(src.normal, normalMatrix)); vrts[j].texCoord = src.texCoord; } DrawTriangleClipped(vrts, appearance); diff --git a/miniwin/src/d3drm/d3drmviewport.cpp b/miniwin/src/d3drm/d3drmviewport.cpp index 3048395c..0aedea2c 100644 --- a/miniwin/src/d3drm/d3drmviewport.cpp +++ b/miniwin/src/d3drm/d3drmviewport.cpp @@ -215,7 +215,7 @@ bool IsBoxInFrustum(const D3DVECTOR corners[8], const Plane planes[6]) void Direct3DRMViewportImpl::CollectMeshesFromFrame( IDirect3DRMFrame* frame, D3DRMMATRIX4D parentMatrix, - std::vector& verts, + std::vector& verts, std::vector& d3dVerts, std::vector& faces ) @@ -365,7 +365,7 @@ HRESULT Direct3DRMViewportImpl::RenderScene() return status; } - std::vector verts; + std::vector verts; std::vector d3dVerts; std::vector faces; ExtractFrustumPlanes(viewProj); diff --git a/miniwin/src/internal/d3drmrenderer.h b/miniwin/src/internal/d3drmrenderer.h index 2f02a1b4..2cbef824 100644 --- a/miniwin/src/internal/d3drmrenderer.h +++ b/miniwin/src/internal/d3drmrenderer.h @@ -7,16 +7,7 @@ #define NO_TEXTURE_ID 0xffffffff -struct TexCoord { - float u, v; -}; - -struct GeometryVertex { - D3DVECTOR position; - D3DVECTOR normals; - TexCoord texCoord; -}; -static_assert(sizeof(GeometryVertex) == 32); +static_assert(sizeof(D3DRMVERTEX) == 32); struct Appearance { SDL_Color color; @@ -49,7 +40,7 @@ public: virtual const char* GetName() = 0; virtual HRESULT BeginFrame(const D3DRMMATRIX4D& viewMatrix) = 0; virtual void SubmitDraw( - const GeometryVertex* vertices, + const D3DRMVERTEX* vertices, const size_t count, const D3DRMMATRIX4D& worldMatrix, const Matrix3x3& normalMatrix, diff --git a/miniwin/src/internal/d3drmrenderer_opengl15.h b/miniwin/src/internal/d3drmrenderer_opengl15.h index a6c69613..ad59f0e1 100644 --- a/miniwin/src/internal/d3drmrenderer_opengl15.h +++ b/miniwin/src/internal/d3drmrenderer_opengl15.h @@ -30,7 +30,7 @@ public: const char* GetName() override; HRESULT BeginFrame(const D3DRMMATRIX4D& viewMatrix) override; void SubmitDraw( - const GeometryVertex* vertices, + const D3DRMVERTEX* vertices, const size_t count, const D3DRMMATRIX4D& worldMatrix, const Matrix3x3& normalMatrix, diff --git a/miniwin/src/internal/d3drmrenderer_sdl3gpu.h b/miniwin/src/internal/d3drmrenderer_sdl3gpu.h index cbbe2b53..8d04424d 100644 --- a/miniwin/src/internal/d3drmrenderer_sdl3gpu.h +++ b/miniwin/src/internal/d3drmrenderer_sdl3gpu.h @@ -37,7 +37,7 @@ public: const char* GetName() override; HRESULT BeginFrame(const D3DRMMATRIX4D& viewMatrix) override; void SubmitDraw( - const GeometryVertex* vertices, + const D3DRMVERTEX* vertices, const size_t count, const D3DRMMATRIX4D& worldMatrix, const Matrix3x3& normalMatrix, diff --git a/miniwin/src/internal/d3drmrenderer_software.h b/miniwin/src/internal/d3drmrenderer_software.h index 800c7ca2..93f5ae68 100644 --- a/miniwin/src/internal/d3drmrenderer_software.h +++ b/miniwin/src/internal/d3drmrenderer_software.h @@ -28,7 +28,7 @@ public: const char* GetName() override; HRESULT BeginFrame(const D3DRMMATRIX4D& viewMatrix) override; void SubmitDraw( - const GeometryVertex* vertices, + const D3DRMVERTEX* vertices, const size_t count, const D3DRMMATRIX4D& worldMatrix, const Matrix3x3& normalMatrix, @@ -39,15 +39,15 @@ public: private: void ClearZBuffer(); void DrawTriangleProjected( - const GeometryVertex& v0, - const GeometryVertex& v1, - const GeometryVertex& v2, + const D3DRMVERTEX& v0, + const D3DRMVERTEX& v1, + const D3DRMVERTEX& v2, const Appearance& appearance ); - void DrawTriangleClipped(const GeometryVertex (&v)[3], const Appearance& appearance); - void ProjectVertex(const GeometryVertex& v, D3DRMVECTOR4D& p) const; + void DrawTriangleClipped(const D3DRMVERTEX (&v)[3], const Appearance& appearance); + void ProjectVertex(const D3DRMVERTEX& v, D3DRMVECTOR4D& p) const; void BlendPixel(Uint8* pixelAddr, Uint8 r, Uint8 g, Uint8 b, Uint8 a); - SDL_Color ApplyLighting(const GeometryVertex& vertex, const Appearance& appearance); + SDL_Color ApplyLighting(const D3DRMVERTEX& vertex, const Appearance& appearance); void AddTextureDestroyCallback(Uint32 id, IDirect3DRMTexture* texture); DWORD m_width; diff --git a/miniwin/src/internal/d3drmviewport_impl.h b/miniwin/src/internal/d3drmviewport_impl.h index 3a5a244b..6d4cce8c 100644 --- a/miniwin/src/internal/d3drmviewport_impl.h +++ b/miniwin/src/internal/d3drmviewport_impl.h @@ -40,7 +40,7 @@ private: void CollectMeshesFromFrame( IDirect3DRMFrame* frame, D3DRMMATRIX4D parentMatrix, - std::vector& verts, + std::vector& verts, std::vector& d3dVerts, std::vector& faces );