Replace GeometryVertex with D3DRMVERTEX (#259)

This commit is contained in:
Anders Jenbo 2025-06-08 23:06:38 +02:00 committed by GitHub
parent d32c30492f
commit ee372fbc8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 57 additions and 57 deletions

View File

@ -136,10 +136,19 @@ struct D3DRMBOX {
D3DVECTOR max; D3DVECTOR max;
}; };
struct TexCoord {
float u, v;
};
struct D3DRMVERTEX { struct D3DRMVERTEX {
D3DVECTOR position; D3DVECTOR position;
D3DVECTOR normal; D3DVECTOR normal;
D3DVALUE tu, tv; union {
struct {
D3DVALUE tu, tv;
};
TexCoord texCoord;
};
}; };
struct IDirect3DRMObject : public IUnknown { struct IDirect3DRMObject : public IUnknown {

View File

@ -304,7 +304,7 @@ HRESULT OpenGL15Renderer::BeginFrame(const D3DRMMATRIX4D& viewMatrix)
} }
void OpenGL15Renderer::SubmitDraw( void OpenGL15Renderer::SubmitDraw(
const GeometryVertex* vertices, const D3DRMVERTEX* vertices,
const size_t count, const size_t count,
const D3DRMMATRIX4D& worldMatrix, const D3DRMMATRIX4D& worldMatrix,
const Matrix3x3& normalMatrix, const Matrix3x3& normalMatrix,
@ -350,8 +350,8 @@ void OpenGL15Renderer::SubmitDraw(
glBegin(GL_TRIANGLES); glBegin(GL_TRIANGLES);
for (size_t i = 0; i < count; i++) { for (size_t i = 0; i < count; i++) {
const GeometryVertex& v = vertices[i]; const D3DRMVERTEX& v = vertices[i];
glNormal3f(v.normals.x, v.normals.y, v.normals.z); glNormal3f(v.normal.x, v.normal.y, v.normal.z);
glTexCoord2f(v.texCoord.u, v.texCoord.v); glTexCoord2f(v.texCoord.u, v.texCoord.v);
glVertex3f(v.position.x, v.position.y, v.position.z); glVertex3f(v.position.x, v.position.y, v.position.z);
} }

View File

@ -32,7 +32,7 @@ static SDL_GPUGraphicsPipeline* InitializeGraphicsPipeline(SDL_GPUDevice* device
SDL_GPUVertexBufferDescription vertexBufferDescs[1] = {}; SDL_GPUVertexBufferDescription vertexBufferDescs[1] = {};
vertexBufferDescs[0].slot = 0; vertexBufferDescs[0].slot = 0;
vertexBufferDescs[0].pitch = sizeof(GeometryVertex); vertexBufferDescs[0].pitch = sizeof(D3DRMVERTEX);
vertexBufferDescs[0].input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX; vertexBufferDescs[0].input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX;
vertexBufferDescs[0].instance_step_rate = 0; vertexBufferDescs[0].instance_step_rate = 0;
@ -40,17 +40,17 @@ static SDL_GPUGraphicsPipeline* InitializeGraphicsPipeline(SDL_GPUDevice* device
vertexAttrs[0].location = 0; vertexAttrs[0].location = 0;
vertexAttrs[0].buffer_slot = 0; vertexAttrs[0].buffer_slot = 0;
vertexAttrs[0].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3; 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].location = 1;
vertexAttrs[1].buffer_slot = 0; vertexAttrs[1].buffer_slot = 0;
vertexAttrs[1].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3; 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].location = 2;
vertexAttrs[2].buffer_slot = 0; vertexAttrs[2].buffer_slot = 0;
vertexAttrs[2].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2; vertexAttrs[2].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2;
vertexAttrs[2].offset = offsetof(GeometryVertex, texCoord); vertexAttrs[2].offset = offsetof(D3DRMVERTEX, tv);
SDL_GPUVertexInputState vertexInputState = {}; SDL_GPUVertexInputState vertexInputState = {};
vertexInputState.vertex_buffer_descriptions = vertexBufferDescs; vertexInputState.vertex_buffer_descriptions = vertexBufferDescs;
@ -298,7 +298,7 @@ HRESULT Direct3DRMSDL3GPURenderer::BeginFrame(const D3DRMMATRIX4D& viewMatrix)
} }
void Direct3DRMSDL3GPURenderer::SubmitDraw( void Direct3DRMSDL3GPURenderer::SubmitDraw(
const GeometryVertex* vertices, const D3DRMVERTEX* vertices,
const size_t count, const size_t count,
const D3DRMMATRIX4D& worldMatrix, const D3DRMMATRIX4D& worldMatrix,
const Matrix3x3& normalMatrix, const Matrix3x3& normalMatrix,
@ -317,7 +317,7 @@ void Direct3DRMSDL3GPURenderer::SubmitDraw(
} }
SDL_GPUBufferCreateInfo bufferCreateInfo = {}; SDL_GPUBufferCreateInfo bufferCreateInfo = {};
bufferCreateInfo.usage = SDL_GPU_BUFFERUSAGE_VERTEX; bufferCreateInfo.usage = SDL_GPU_BUFFERUSAGE_VERTEX;
bufferCreateInfo.size = static_cast<Uint32>(sizeof(GeometryVertex) * count); bufferCreateInfo.size = static_cast<Uint32>(sizeof(D3DRMVERTEX) * count);
m_vertexBuffer = SDL_CreateGPUBuffer(m_device, &bufferCreateInfo); m_vertexBuffer = SDL_CreateGPUBuffer(m_device, &bufferCreateInfo);
if (!m_vertexBuffer) { if (!m_vertexBuffer) {
SDL_LogError(LOG_CATEGORY_MINIWIN, "SDL_CreateGPUBuffer returned NULL buffer (%s)", SDL_GetError()); SDL_LogError(LOG_CATEGORY_MINIWIN, "SDL_CreateGPUBuffer returned NULL buffer (%s)", SDL_GetError());
@ -332,7 +332,7 @@ void Direct3DRMSDL3GPURenderer::SubmitDraw(
SDL_GPUTransferBufferCreateInfo transferCreateInfo = {}; SDL_GPUTransferBufferCreateInfo transferCreateInfo = {};
transferCreateInfo.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD; transferCreateInfo.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
transferCreateInfo.size = static_cast<Uint32>(sizeof(GeometryVertex) * m_vertexCount); transferCreateInfo.size = static_cast<Uint32>(sizeof(D3DRMVERTEX) * m_vertexCount);
SDL_GPUTransferBuffer* transferBuffer = SDL_CreateGPUTransferBuffer(m_device, &transferCreateInfo); SDL_GPUTransferBuffer* transferBuffer = SDL_CreateGPUTransferBuffer(m_device, &transferCreateInfo);
if (!transferBuffer) { if (!transferBuffer) {
SDL_LogError( 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) { if (!transferData) {
SDL_LogError(LOG_CATEGORY_MINIWIN, "SDL_MapGPUTransferBuffer returned NULL buffer (%s)", SDL_GetError()); 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); SDL_UnmapGPUTransferBuffer(m_device, transferBuffer);
@ -366,7 +366,7 @@ void Direct3DRMSDL3GPURenderer::SubmitDraw(
SDL_GPUBufferRegion bufferRegion = {}; SDL_GPUBufferRegion bufferRegion = {};
bufferRegion.buffer = m_vertexBuffer; bufferRegion.buffer = m_vertexBuffer;
bufferRegion.offset = 0; bufferRegion.offset = 0;
bufferRegion.size = static_cast<Uint32>(sizeof(GeometryVertex) * m_vertexCount); bufferRegion.size = static_cast<Uint32>(sizeof(D3DRMVERTEX) * m_vertexCount);
SDL_UploadToGPUBuffer(copyPass, &transferLocation, &bufferRegion, false); SDL_UploadToGPUBuffer(copyPass, &transferLocation, &bufferRegion, false);

View File

@ -32,7 +32,7 @@ void Direct3DRMSoftwareRenderer::ClearZBuffer()
std::fill(m_zBuffer.begin(), m_zBuffer.end(), std::numeric_limits<float>::infinity()); std::fill(m_zBuffer.begin(), m_zBuffer.end(), std::numeric_limits<float>::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 + 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]; 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; 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); 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); 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.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.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.normal.x = a.normal.x + t * (b.normal.x - a.normal.x);
a.normals.y = a.normals.y + t * (b.normals.y - a.normals.y); a.normal.y = a.normal.y + t * (b.normal.y - a.normal.y);
a.normals.z = a.normals.z + t * (b.normals.z - a.normals.z); 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) { if (len > 0.0001f) {
a.normals.x /= len; a.normal.x /= len;
a.normals.y /= len; a.normal.y /= len;
a.normals.z /= len; a.normal.z /= len;
} }
return a; 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 in0 = v[0].position.z >= m_front;
bool in1 = v[1].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); DrawTriangleProjected(v[0], v[1], v[2], appearance);
} }
else if (insideCount == 2) { else if (insideCount == 2) {
GeometryVertex split; D3DRMVERTEX split;
if (!in0) { if (!in0) {
split = SplitEdge(v[2], v[0], m_front); split = SplitEdge(v[2], v[0], m_front);
DrawTriangleProjected(v[1], v[2], split, appearance); 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); 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 specular = {0, 0, 0, 0};
FColor diffuse = {0, 0, 0, 0}; FColor diffuse = {0, 0, 0, 0};
// Position and normal // Position and normal
D3DVECTOR position = vertex.position; 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); float normLen = std::sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z);
if (normLen == 0.0f) { if (normLen == 0.0f) {
return appearance.color; return appearance.color;
@ -220,9 +220,9 @@ SDL_Color Direct3DRMSoftwareRenderer::ApplyLighting(const GeometryVertex& vertex
} }
void Direct3DRMSoftwareRenderer::DrawTriangleProjected( void Direct3DRMSoftwareRenderer::DrawTriangleProjected(
const GeometryVertex& v0, const D3DRMVERTEX& v0,
const GeometryVertex& v1, const D3DRMVERTEX& v1,
const GeometryVertex& v2, const D3DRMVERTEX& v2,
const Appearance& appearance const Appearance& appearance
) )
{ {
@ -470,7 +470,7 @@ HRESULT Direct3DRMSoftwareRenderer::BeginFrame(const D3DRMMATRIX4D& viewMatrix)
} }
void Direct3DRMSoftwareRenderer::SubmitDraw( void Direct3DRMSoftwareRenderer::SubmitDraw(
const GeometryVertex* vertices, const D3DRMVERTEX* vertices,
const size_t count, const size_t count,
const D3DRMMATRIX4D& worldMatrix, const D3DRMMATRIX4D& worldMatrix,
const Matrix3x3& normalMatrix, const Matrix3x3& normalMatrix,
@ -481,11 +481,11 @@ void Direct3DRMSoftwareRenderer::SubmitDraw(
MultiplyMatrix(mvMatrix, worldMatrix, m_viewMatrix); MultiplyMatrix(mvMatrix, worldMatrix, m_viewMatrix);
for (size_t i = 0; i + 2 < count; i += 3) { for (size_t i = 0; i + 2 < count; i += 3) {
GeometryVertex vrts[3]; D3DRMVERTEX vrts[3];
for (size_t j = 0; j < 3; ++j) { 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].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; vrts[j].texCoord = src.texCoord;
} }
DrawTriangleClipped(vrts, appearance); DrawTriangleClipped(vrts, appearance);

View File

@ -215,7 +215,7 @@ bool IsBoxInFrustum(const D3DVECTOR corners[8], const Plane planes[6])
void Direct3DRMViewportImpl::CollectMeshesFromFrame( void Direct3DRMViewportImpl::CollectMeshesFromFrame(
IDirect3DRMFrame* frame, IDirect3DRMFrame* frame,
D3DRMMATRIX4D parentMatrix, D3DRMMATRIX4D parentMatrix,
std::vector<GeometryVertex>& verts, std::vector<D3DRMVERTEX>& verts,
std::vector<D3DRMVERTEX>& d3dVerts, std::vector<D3DRMVERTEX>& d3dVerts,
std::vector<DWORD>& faces std::vector<DWORD>& faces
) )
@ -365,7 +365,7 @@ HRESULT Direct3DRMViewportImpl::RenderScene()
return status; return status;
} }
std::vector<GeometryVertex> verts; std::vector<D3DRMVERTEX> verts;
std::vector<D3DRMVERTEX> d3dVerts; std::vector<D3DRMVERTEX> d3dVerts;
std::vector<DWORD> faces; std::vector<DWORD> faces;
ExtractFrustumPlanes(viewProj); ExtractFrustumPlanes(viewProj);

View File

@ -7,16 +7,7 @@
#define NO_TEXTURE_ID 0xffffffff #define NO_TEXTURE_ID 0xffffffff
struct TexCoord { static_assert(sizeof(D3DRMVERTEX) == 32);
float u, v;
};
struct GeometryVertex {
D3DVECTOR position;
D3DVECTOR normals;
TexCoord texCoord;
};
static_assert(sizeof(GeometryVertex) == 32);
struct Appearance { struct Appearance {
SDL_Color color; SDL_Color color;
@ -49,7 +40,7 @@ public:
virtual const char* GetName() = 0; virtual const char* GetName() = 0;
virtual HRESULT BeginFrame(const D3DRMMATRIX4D& viewMatrix) = 0; virtual HRESULT BeginFrame(const D3DRMMATRIX4D& viewMatrix) = 0;
virtual void SubmitDraw( virtual void SubmitDraw(
const GeometryVertex* vertices, const D3DRMVERTEX* vertices,
const size_t count, const size_t count,
const D3DRMMATRIX4D& worldMatrix, const D3DRMMATRIX4D& worldMatrix,
const Matrix3x3& normalMatrix, const Matrix3x3& normalMatrix,

View File

@ -30,7 +30,7 @@ public:
const char* GetName() override; const char* GetName() override;
HRESULT BeginFrame(const D3DRMMATRIX4D& viewMatrix) override; HRESULT BeginFrame(const D3DRMMATRIX4D& viewMatrix) override;
void SubmitDraw( void SubmitDraw(
const GeometryVertex* vertices, const D3DRMVERTEX* vertices,
const size_t count, const size_t count,
const D3DRMMATRIX4D& worldMatrix, const D3DRMMATRIX4D& worldMatrix,
const Matrix3x3& normalMatrix, const Matrix3x3& normalMatrix,

View File

@ -37,7 +37,7 @@ public:
const char* GetName() override; const char* GetName() override;
HRESULT BeginFrame(const D3DRMMATRIX4D& viewMatrix) override; HRESULT BeginFrame(const D3DRMMATRIX4D& viewMatrix) override;
void SubmitDraw( void SubmitDraw(
const GeometryVertex* vertices, const D3DRMVERTEX* vertices,
const size_t count, const size_t count,
const D3DRMMATRIX4D& worldMatrix, const D3DRMMATRIX4D& worldMatrix,
const Matrix3x3& normalMatrix, const Matrix3x3& normalMatrix,

View File

@ -28,7 +28,7 @@ public:
const char* GetName() override; const char* GetName() override;
HRESULT BeginFrame(const D3DRMMATRIX4D& viewMatrix) override; HRESULT BeginFrame(const D3DRMMATRIX4D& viewMatrix) override;
void SubmitDraw( void SubmitDraw(
const GeometryVertex* vertices, const D3DRMVERTEX* vertices,
const size_t count, const size_t count,
const D3DRMMATRIX4D& worldMatrix, const D3DRMMATRIX4D& worldMatrix,
const Matrix3x3& normalMatrix, const Matrix3x3& normalMatrix,
@ -39,15 +39,15 @@ public:
private: private:
void ClearZBuffer(); void ClearZBuffer();
void DrawTriangleProjected( void DrawTriangleProjected(
const GeometryVertex& v0, const D3DRMVERTEX& v0,
const GeometryVertex& v1, const D3DRMVERTEX& v1,
const GeometryVertex& v2, const D3DRMVERTEX& v2,
const Appearance& appearance const Appearance& appearance
); );
void DrawTriangleClipped(const GeometryVertex (&v)[3], const Appearance& appearance); void DrawTriangleClipped(const D3DRMVERTEX (&v)[3], const Appearance& appearance);
void ProjectVertex(const GeometryVertex& v, D3DRMVECTOR4D& p) const; void ProjectVertex(const D3DRMVERTEX& v, D3DRMVECTOR4D& p) const;
void BlendPixel(Uint8* pixelAddr, Uint8 r, Uint8 g, Uint8 b, Uint8 a); 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); void AddTextureDestroyCallback(Uint32 id, IDirect3DRMTexture* texture);
DWORD m_width; DWORD m_width;

View File

@ -40,7 +40,7 @@ private:
void CollectMeshesFromFrame( void CollectMeshesFromFrame(
IDirect3DRMFrame* frame, IDirect3DRMFrame* frame,
D3DRMMATRIX4D parentMatrix, D3DRMMATRIX4D parentMatrix,
std::vector<GeometryVertex>& verts, std::vector<D3DRMVERTEX>& verts,
std::vector<D3DRMVERTEX>& d3dVerts, std::vector<D3DRMVERTEX>& d3dVerts,
std::vector<DWORD>& faces std::vector<DWORD>& faces
); );