Skip to content

Commit

Permalink
- make RenderCommands able to use Shape2D vertex buffers past the Sha…
Browse files Browse the repository at this point in the history
…pe2D's lifetime without crashing
  • Loading branch information
Gutawer authored and coelckers committed Aug 11, 2021
1 parent ccf4628 commit 67e7d1a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 37 deletions.
42 changes: 21 additions & 21 deletions src/common/2d/v_2ddrawer.cpp
Expand Up @@ -124,7 +124,7 @@ static void Shape2D_Clear(DShape2D* self, int which)
if (which & C_Verts) self->mVertices.Clear();
if (which & C_Coords) self->mCoords.Clear();
if (which & C_Indices) self->mIndices.Clear();
self->needsVertexUpload = true;
self->bufferInfo->needsVertexUpload = true;
}

DEFINE_ACTION_FUNCTION_NATIVE(DShape2D, Clear, Shape2D_Clear)
Expand All @@ -138,7 +138,7 @@ DEFINE_ACTION_FUNCTION_NATIVE(DShape2D, Clear, Shape2D_Clear)
static void Shape2D_PushVertex(DShape2D* self, double x, double y)
{
self->mVertices.Push(DVector2(x, y));
self->needsVertexUpload = true;
self->bufferInfo->needsVertexUpload = true;
}

DEFINE_ACTION_FUNCTION_NATIVE(DShape2D, PushVertex, Shape2D_PushVertex)
Expand All @@ -153,7 +153,7 @@ DEFINE_ACTION_FUNCTION_NATIVE(DShape2D, PushVertex, Shape2D_PushVertex)
static void Shape2D_PushCoord(DShape2D* self, double u, double v)
{
self->mCoords.Push(DVector2(u, v));
self->needsVertexUpload = true;
self->bufferInfo->needsVertexUpload = true;
}

DEFINE_ACTION_FUNCTION_NATIVE(DShape2D, PushCoord, Shape2D_PushCoord)
Expand All @@ -170,7 +170,7 @@ static void Shape2D_PushTriangle(DShape2D* self, int a, int b, int c)
self->mIndices.Push(a);
self->mIndices.Push(b);
self->mIndices.Push(c);
self->needsVertexUpload = true;
self->bufferInfo->needsVertexUpload = true;
}

DEFINE_ACTION_FUNCTION_NATIVE(DShape2D, PushTriangle, Shape2D_PushTriangle)
Expand Down Expand Up @@ -534,7 +534,7 @@ void DShape2D::OnDestroy() {
mIndices.Reset();
mVertices.Reset();
mCoords.Reset();
buffers.Reset();
bufferInfo.reset();
}

//==========================================================================
Expand Down Expand Up @@ -567,11 +567,11 @@ void F2DDrawer::AddShape(FGameTexture* img, DShape2D* shape, DrawParms& parms)
shape->lastParms = new DrawParms(parms);
}
else if (shape->lastParms->vertexColorChange(parms)) {
shape->needsVertexUpload = true;
if (!shape->uploadedOnce) {
shape->bufIndex = -1;
shape->buffers.Clear();
shape->lastCommand = -1;
shape->bufferInfo->needsVertexUpload = true;
if (!shape->bufferInfo->uploadedOnce) {
shape->bufferInfo->bufIndex = -1;
shape->bufferInfo->buffers.Clear();
shape->bufferInfo->lastCommand = -1;
}
delete shape->lastParms;
shape->lastParms = new DrawParms(parms);
Expand All @@ -583,7 +583,7 @@ void F2DDrawer::AddShape(FGameTexture* img, DShape2D* shape, DrawParms& parms)
auto osave = offset;
if (parms.nooffset) offset = { 0,0 };

if (shape->needsVertexUpload)
if (shape->bufferInfo->needsVertexUpload)
{
shape->minx = 16383;
shape->miny = 16383;
Expand Down Expand Up @@ -622,15 +622,15 @@ void F2DDrawer::AddShape(FGameTexture* img, DShape2D* shape, DrawParms& parms)
dg.transform = shape->transform;
dg.transform.Cells[0][2] += offset.X;
dg.transform.Cells[1][2] += offset.Y;
dg.shape2D = shape;
dg.shape2DBufInfo = shape->bufferInfo;
dg.shape2DIndexCount = shape->mIndices.Size();
if (shape->needsVertexUpload)
if (shape->bufferInfo->needsVertexUpload)
{
shape->bufIndex += 1;
shape->bufferInfo->bufIndex += 1;

shape->buffers.Reserve(1);
shape->bufferInfo->buffers.Reserve(1);

auto buf = &shape->buffers[shape->bufIndex];
auto buf = &shape->bufferInfo->buffers[shape->bufferInfo->bufIndex];

auto verts = TArray<TwoDVertex>(dg.mVertCount, true);
for ( int i=0; i<dg.mVertCount; i++ )
Expand All @@ -649,12 +649,12 @@ void F2DDrawer::AddShape(FGameTexture* img, DShape2D* shape, DrawParms& parms)
}

buf->UploadData(&verts[0], dg.mVertCount, &shape->mIndices[0], shape->mIndices.Size());
shape->needsVertexUpload = false;
shape->uploadedOnce = true;
shape->bufferInfo->needsVertexUpload = false;
shape->bufferInfo->uploadedOnce = true;
}
dg.shape2DBufIndex = shape->bufIndex;
shape->lastCommand += 1;
dg.shape2DCommandCounter = shape->lastCommand;
dg.shape2DBufIndex = shape->bufferInfo->bufIndex;
shape->bufferInfo->lastCommand += 1;
dg.shape2DCommandCounter = shape->bufferInfo->lastCommand;
AddCommand(&dg);
offset = osave;
}
Expand Down
24 changes: 17 additions & 7 deletions src/common/2d/v_2ddrawer.h
Expand Up @@ -7,6 +7,7 @@
#include "textures.h"
#include "renderstyle.h"
#include "dobject.h"
#include <memory>

struct DrawParms;
struct FColormap;
Expand Down Expand Up @@ -49,6 +50,7 @@ struct F2DPolygons
};

class DShape2D;
class DShape2DBufferInfo;

class F2DDrawer
{
Expand Down Expand Up @@ -123,20 +125,21 @@ class F2DDrawer
bool useTransform;
DMatrix3x3 transform;

DShape2D* shape2D;
std::shared_ptr<DShape2DBufferInfo> shape2DBufInfo;
int shape2DBufIndex;
int shape2DIndexCount;
int shape2DCommandCounter;

RenderCommand()
{
memset(this, 0, sizeof(*this));
shape2DBufInfo.reset();
}

// If these fields match, two draw commands can be batched.
bool isCompatible(const RenderCommand &other) const
{
if (shape2D != nullptr || other.shape2D != nullptr) return false;
if (shape2DBufInfo != nullptr || other.shape2DBufInfo != nullptr) return false;
return mTexture == other.mTexture &&
mType == other.mType &&
mTranslationId == other.mTranslationId &&
Expand Down Expand Up @@ -240,13 +243,24 @@ class F2DDrawer
bool mIsFirstPass = true;
};

class DShape2DBufferInfo
{
public:
TArray<F2DVertexBuffer> buffers;
bool needsVertexUpload = true;
int bufIndex = -1;
int lastCommand = -1;
bool uploadedOnce = false;
};

class DShape2D : public DObject
{

DECLARE_CLASS(DShape2D,DObject)
public:
DShape2D()
{
bufferInfo = std::make_shared<DShape2DBufferInfo>();
transform.Identity();
}

Expand All @@ -261,12 +275,8 @@ class DShape2D : public DObject

DMatrix3x3 transform;

TArray<F2DVertexBuffer> buffers;
bool needsVertexUpload = true;
int bufIndex = -1;
int lastCommand = -1;
std::shared_ptr<DShape2DBufferInfo> bufferInfo;

bool uploadedOnce = false;
DrawParms* lastParms;

void OnDestroy() override;
Expand Down
18 changes: 9 additions & 9 deletions src/common/rendering/hwrenderer/hw_draw2d.cpp
Expand Up @@ -178,22 +178,22 @@ void Draw2D(F2DDrawer *drawer, FRenderState &state)
state.EnableTexture(false);
}

if (cmd.shape2D != nullptr)
if (cmd.shape2DBufInfo != nullptr)
{
state.SetVertexBuffer(&cmd.shape2D->buffers[cmd.shape2DBufIndex]);
state.SetVertexBuffer(&cmd.shape2DBufInfo->buffers[cmd.shape2DBufIndex]);
state.DrawIndexed(DT_Triangles, 0, cmd.shape2DIndexCount);
state.SetVertexBuffer(&vb);
if (cmd.shape2DCommandCounter == cmd.shape2D->lastCommand)
if (cmd.shape2DCommandCounter == cmd.shape2DBufInfo->lastCommand)
{
cmd.shape2D->lastCommand = -1;
if (cmd.shape2D->bufIndex > 0)
cmd.shape2DBufInfo->lastCommand = -1;
if (cmd.shape2DBufInfo->bufIndex > 0)
{
cmd.shape2D->needsVertexUpload = true;
cmd.shape2D->buffers.Clear();
cmd.shape2D->bufIndex = -1;
cmd.shape2DBufInfo->needsVertexUpload = true;
cmd.shape2DBufInfo->buffers.Clear();
cmd.shape2DBufInfo->bufIndex = -1;
}
}
cmd.shape2D->uploadedOnce = false;
cmd.shape2DBufInfo->uploadedOnce = false;
}
else
{
Expand Down

0 comments on commit 67e7d1a

Please sign in to comment.