- adjust PolyTriangleDrawer to closer match what PolyRenderState receives as input

This commit is contained in:
Magnus Norddahl 2019-05-24 22:30:14 +02:00
commit b453e15929
10 changed files with 249 additions and 121 deletions

View file

@ -18,6 +18,10 @@ PolyBuffer::~PolyBuffer()
if (Next) Next->Prev = Prev;
if (Prev) Prev->Next = Next;
else First = Next;
auto fb = GetPolyFrameBuffer();
if (fb && !mData.empty())
fb->FrameDeleteList.Buffers.push_back(std::move(mData));
}
void PolyBuffer::ResetAll()
@ -79,43 +83,29 @@ void PolyVertexBuffer::SetFormat(int numBindingPoints, int numAttributes, size_t
mStride = stride;
}
void PolyVertexBuffer::CopyVertices(TriVertex *dst, int count, int index)
ShadedTriVertex PolyVertexBuffer::Shade(PolyTriangleThreadData *thread, const PolyDrawArgs &drawargs, const void *vertices, int index)
{
size_t stride = mStride;
size_t offsetVertex = mOffsets[VATTR_VERTEX];
size_t offsetTexcoord = mOffsets[VATTR_TEXCOORD];
uint8_t *vertex = static_cast<uint8_t*>(map) + stride * index;
const uint8_t *vertex = static_cast<const uint8_t*>(vertices) + mStride * index;
const float *attrVertex = reinterpret_cast<const float*>(vertex + mOffsets[VATTR_VERTEX]);
const float *attrTexcoord = reinterpret_cast<const float*>(vertex + mOffsets[VATTR_TEXCOORD]);
for (int i = 0; i < count; i++)
{
dst[i].x = *reinterpret_cast<float*>(vertex + offsetVertex);
dst[i].y = *reinterpret_cast<float*>(vertex + offsetVertex + 4);
dst[i].z = *reinterpret_cast<float*>(vertex + offsetVertex + 8);
dst[i].w = 1.0f;
dst[i].v = *reinterpret_cast<float*>(vertex + offsetTexcoord);
dst[i].u = *reinterpret_cast<float*>(vertex + offsetTexcoord + 4);
vertex += stride;
}
}
Vec4f objpos = Vec4f(attrVertex[0], attrVertex[1], attrVertex[2], 1.0f);
Vec4f clippos = (*thread->objectToClip) * objpos;
void PolyVertexBuffer::CopyIndexed(TriVertex *dst, uint32_t *elements, int count, int index)
{
size_t stride = mStride;
size_t offsetVertex = mOffsets[VATTR_VERTEX];
size_t offsetTexcoord = mOffsets[VATTR_TEXCOORD];
uint8_t *vertices = static_cast<uint8_t*>(map);
elements += index;
for (int i = 0; i < count; i++)
{
uint8_t *vertex = vertices + stride * elements[i];
dst[i].x = *reinterpret_cast<float*>(vertex + offsetVertex);
dst[i].y = *reinterpret_cast<float*>(vertex + offsetVertex + 4);
dst[i].z = *reinterpret_cast<float*>(vertex + offsetVertex + 8);
dst[i].w = 1.0f;
dst[i].v = *reinterpret_cast<float*>(vertex + offsetTexcoord);
dst[i].u = *reinterpret_cast<float*>(vertex + offsetTexcoord + 4);
}
ShadedTriVertex sv;
sv.u = attrTexcoord[1];
sv.v = attrTexcoord[0];
sv.x = clippos.X;
sv.y = clippos.Y;
sv.z = clippos.Z;
sv.w = clippos.W;
sv.worldX = objpos.X;
sv.worldY = objpos.Y;
sv.worldZ = objpos.Z;
sv.clipDistance[0] = 1.0f;
sv.clipDistance[1] = 1.0f;
sv.clipDistance[2] = 1.0f;
return sv;
}
/////////////////////////////////////////////////////////////////////////////

View file

@ -1,6 +1,7 @@
#pragma once
#include "hwrenderer/data/buffers.h"
#include "polyrenderer/drawers/poly_triangle.h"
#include "utility/tarray.h"
#include <vector>
@ -38,14 +39,13 @@ private:
std::vector<uint32_t> mData;
};
class PolyVertexBuffer : public IVertexBuffer, public PolyBuffer
class PolyVertexBuffer : public IVertexBuffer, public PolyBuffer, public PolyVertexShader
{
public:
PolyVertexBuffer() { }
void SetFormat(int numBindingPoints, int numAttributes, size_t stride, const FVertexBufferAttribute *attrs) override;
void CopyVertices(TriVertex *dst, int count, int index);
void CopyIndexed(TriVertex *dst, uint32_t *elements, int count, int index);
ShadedTriVertex Shade(PolyTriangleThreadData *thread, const PolyDrawArgs &drawargs, const void *vertices, int index) override;
private:
size_t mOffsets[VATTR_MAX] = {};

View file

@ -141,6 +141,7 @@ void PolyFrameBuffer::Update()
FlushDrawCommands();
DrawerThreads::WaitForWorkers();
mFrameMemory.Clear();
FrameDeleteList.Buffers.clear();
if (mCanvas)
{

View file

@ -59,6 +59,11 @@ public:
void SetVSync(bool vsync) override;
void Draw2D() override;
struct DeleteList
{
std::vector<std::vector<uint32_t>> Buffers;
} FrameDeleteList;
private:
sector_t *RenderViewpoint(FRenderViewpoint &mainvp, AActor * camera, IntRect * bounds, float fov, float ratio, float fovratio, bool mainview, bool toscreen);
void RenderTextureView(FCanvasTexture *tex, AActor *Viewpoint, double FOV);

View file

@ -41,10 +41,7 @@ void PolyRenderState::Draw(int dt, int index, int count, bool apply)
if (apply)
Apply();
auto fb = GetPolyFrameBuffer();
TriVertex *vertices = fb->GetFrameMemory()->AllocMemory<TriVertex>(count);
static_cast<PolyVertexBuffer*>(mVertexBuffer)->CopyVertices(vertices, count, index);
PolyTriangleDrawer::DrawArray(fb->GetDrawCommands(), args, vertices, count, dtToDrawMode[dt]);
PolyTriangleDrawer::Draw(GetPolyFrameBuffer()->GetDrawCommands(), index, count, dtToDrawMode[dt]);
}
void PolyRenderState::DrawIndexed(int dt, int index, int count, bool apply)
@ -52,10 +49,7 @@ void PolyRenderState::DrawIndexed(int dt, int index, int count, bool apply)
if (apply)
Apply();
auto fb = GetPolyFrameBuffer();
TriVertex *vertices = fb->GetFrameMemory()->AllocMemory<TriVertex>(count);
static_cast<PolyVertexBuffer*>(mVertexBuffer)->CopyIndexed(vertices, (uint32_t *)mIndexBuffer->Memory(), count, index);
PolyTriangleDrawer::DrawArray(fb->GetDrawCommands(), args, vertices, count, dtToDrawMode[dt]);
PolyTriangleDrawer::DrawIndexed(GetPolyFrameBuffer()->GetDrawCommands(), index, count, dtToDrawMode[dt]);
}
bool PolyRenderState::SetDepthClamp(bool on)
@ -179,7 +173,11 @@ void PolyRenderState::Apply()
}
auto fb = GetPolyFrameBuffer();
if (mVertexBuffer) PolyTriangleDrawer::SetVertexBuffer(fb->GetDrawCommands(), mVertexBuffer->Memory());
if (mIndexBuffer) PolyTriangleDrawer::SetIndexBuffer(fb->GetDrawCommands(), mIndexBuffer->Memory());
PolyTriangleDrawer::SetVertexShader(fb->GetDrawCommands(), static_cast<PolyVertexBuffer*>(mVertexBuffer));
PolyTriangleDrawer::SetTwoSided(fb->GetDrawCommands(), true);
PolyTriangleDrawer::PushConstants(fb->GetDrawCommands(), args);
drawcalls.Unclock();
}