Skip to content

Commit

Permalink
Add canvastexture definition to animdefs.
Browse files Browse the repository at this point in the history
Split canvas texture rendering from camera texture rendering.
  • Loading branch information
dpjudas authored and coelckers committed Jul 25, 2022
1 parent aa08360 commit bb50395
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 54 deletions.
7 changes: 2 additions & 5 deletions src/common/2d/v_2ddrawer.h
Expand Up @@ -279,11 +279,8 @@ class FCanvas : public DObject
{
DECLARE_CLASS(FCanvas, DObject)
public:
FCanvas()
{
Drawer->SetSize(256, 256);
}
std::shared_ptr<F2DDrawer> Drawer = std::make_shared<F2DDrawer>();
F2DDrawer Drawer;
FCanvasTexture* Tex = nullptr;
};

struct DShape2DBufferInfo : RefCountedBase
Expand Down
61 changes: 39 additions & 22 deletions src/common/2d/v_draw.cpp
Expand Up @@ -273,7 +273,8 @@ DEFINE_ACTION_FUNCTION(FCanvas, DrawTexture)

auto tex = TexMan.GameByIndex(texid, animate);
VMVa_List args = { param + 5, 0, numparam - 6, va_reginfo + 5 };
DoDrawTexture(self->Drawer.get(), tex, x, y, args);
DoDrawTexture(&self->Drawer, tex, x, y, args);
self->Tex->NeedUpdate();
return 0;
}

Expand Down Expand Up @@ -348,7 +349,8 @@ DEFINE_ACTION_FUNCTION(FCanvas, DrawShape)
auto tex = TexMan.GameByIndex(texid, animate);
VMVa_List args = { param + 4, 0, numparam - 5, va_reginfo + 4 };

DrawShape(self->Drawer.get(), tex, shape, args);
DrawShape(&self->Drawer, tex, shape, args);
self->Tex->NeedUpdate();
return 0;
}

Expand Down Expand Up @@ -384,7 +386,8 @@ DEFINE_ACTION_FUNCTION(FCanvas, DrawShapeFill)

color.a = int(amount * 255.0f);

DrawShapeFill(self->Drawer.get(), color, shape, args);
DrawShapeFill(&self->Drawer, color, shape, args);
self->Tex->NeedUpdate();
return 0;
}

Expand Down Expand Up @@ -420,7 +423,8 @@ DEFINE_ACTION_FUNCTION(FCanvas, SetClipRect)
PARAM_INT(y);
PARAM_INT(w);
PARAM_INT(h);
self->Drawer->SetClipRect(x, y, w, h);
self->Drawer.SetClipRect(x, y, w, h);
self->Tex->NeedUpdate();
return 0;
}

Expand All @@ -434,7 +438,8 @@ DEFINE_ACTION_FUNCTION(_Screen, ClearClipRect)
DEFINE_ACTION_FUNCTION(FCanvas, ClearClipRect)
{
PARAM_SELF_PROLOGUE(FCanvas);
self->Drawer->ClearClipRect();
self->Drawer.ClearClipRect();
self->Tex->NeedUpdate();
return 0;
}

Expand All @@ -448,7 +453,8 @@ DEFINE_ACTION_FUNCTION(_Screen, ClearScreen)
DEFINE_ACTION_FUNCTION(FCanvas, ClearScreen)
{
PARAM_SELF_PROLOGUE(FCanvas);
self->Drawer->ClearScreen();
self->Drawer.ClearScreen();
self->Tex->NeedUpdate();
return 0;
}

Expand All @@ -464,7 +470,8 @@ DEFINE_ACTION_FUNCTION(FCanvas, SetScreenFade)
{
PARAM_SELF_PROLOGUE(FCanvas);
PARAM_FLOAT(x);
self->Drawer->SetScreenFade(float(x));
self->Drawer.SetScreenFade(float(x));
self->Tex->NeedUpdate();
return 0;
}

Expand Down Expand Up @@ -493,7 +500,7 @@ DEFINE_ACTION_FUNCTION(FCanvas, GetClipRect)
{
PARAM_SELF_PROLOGUE(FCanvas);
int x, y, w, h;
self->Drawer->GetClipRect(&x, &y, &w, &h);
self->Drawer.GetClipRect(&x, &y, &w, &h);
if (numret > 0) ret[0].SetInt(x);
if (numret > 1) ret[1].SetInt(y);
if (numret > 2) ret[2].SetInt(w);
Expand Down Expand Up @@ -613,8 +620,8 @@ DEFINE_ACTION_FUNCTION(FCanvas, GetFullscreenRect)

DrawParms parms;
DoubleRect rect;
parms.viewport.width = self->Drawer->GetWidth();
parms.viewport.height = self->Drawer->GetHeight();
parms.viewport.width = self->Drawer.GetWidth();
parms.viewport.height = self->Drawer.GetHeight();
CalcFullscreenScale(&parms, virtw, virth, fsmode, rect);
if (numret >= 1) ret[0].SetFloat(rect.left);
if (numret >= 2) ret[1].SetFloat(rect.top);
Expand Down Expand Up @@ -1526,7 +1533,7 @@ DEFINE_ACTION_FUNCTION(FCanvas, VirtualToRealCoords)
PARAM_FLOAT(vh);
PARAM_BOOL(vbottom);
PARAM_BOOL(handleaspect);
VirtualToRealCoords(self->Drawer.get(), x, y, w, h, vw, vh, vbottom, handleaspect);
VirtualToRealCoords(&self->Drawer, x, y, w, h, vw, vh, vbottom, handleaspect);
if (numret >= 1) ret[0].SetVector2(DVector2(x, y));
if (numret >= 2) ret[1].SetVector2(DVector2(w, h));
return min(numret, 2);
Expand Down Expand Up @@ -1582,7 +1589,8 @@ DEFINE_ACTION_FUNCTION(FCanvas, DrawLine)
PARAM_INT(y1);
PARAM_INT(color);
PARAM_INT(alpha);
self->Drawer->AddLine((float)x0, (float)y0, (float)x1, (float)y1, -1, -1, INT_MAX, INT_MAX, color | MAKEARGB(255, 0, 0, 0), alpha);
self->Drawer.AddLine((float)x0, (float)y0, (float)x1, (float)y1, -1, -1, INT_MAX, INT_MAX, color | MAKEARGB(255, 0, 0, 0), alpha);
self->Tex->NeedUpdate();
return 0;
}

Expand Down Expand Up @@ -1616,7 +1624,8 @@ DEFINE_ACTION_FUNCTION(FCanvas, DrawThickLine)
PARAM_FLOAT(thickness);
PARAM_INT(color);
PARAM_INT(alpha);
self->Drawer->AddThickLine(x0, y0, x1, y1, thickness, color, alpha);
self->Drawer.AddThickLine(x0, y0, x1, y1, thickness, color, alpha);
self->Tex->NeedUpdate();
return 0;
}

Expand Down Expand Up @@ -1688,7 +1697,8 @@ DEFINE_ACTION_FUNCTION(FCanvas, Clear)
PARAM_INT(y2);
PARAM_INT(color);
PARAM_INT(palcol);
ClearRect(self->Drawer.get(), x1, y1, x2, y2, palcol, color);
ClearRect(&self->Drawer, x1, y1, x2, y2, palcol, color);
self->Tex->NeedUpdate();
return 0;
}

Expand Down Expand Up @@ -1766,7 +1776,8 @@ DEFINE_ACTION_FUNCTION(FCanvas, Dim)
PARAM_INT(w);
PARAM_INT(h);
PARAM_INT(style);
Dim(self->Drawer.get(), color, float(amount), x1, y1, w, h, &LegacyRenderStyles[style]);
Dim(&self->Drawer, color, float(amount), x1, y1, w, h, &LegacyRenderStyles[style]);
self->Tex->NeedUpdate();
return 0;
}

Expand Down Expand Up @@ -1835,7 +1846,8 @@ DEFINE_ACTION_FUNCTION(FCanvas, DrawLineFrame)
PARAM_INT(width);
PARAM_INT(height);
PARAM_INT(thickness);
DrawFrame(self->Drawer.get(), color, left, top, width, height, thickness);
DrawFrame(&self->Drawer, color, left, top, width, height, thickness);
self->Tex->NeedUpdate();
return 0;
}

Expand All @@ -1859,7 +1871,7 @@ DEFINE_ACTION_FUNCTION(FCanvas, SetOffset)
PARAM_SELF_PROLOGUE(FCanvas);
PARAM_FLOAT(x);
PARAM_FLOAT(y);
ACTION_RETURN_VEC2(self->Drawer->SetOffset(DVector2(x, y)));
ACTION_RETURN_VEC2(self->Drawer.SetOffset(DVector2(x, y)));
}

DEFINE_ACTION_FUNCTION(_Screen, EnableStencil)
Expand All @@ -1878,7 +1890,8 @@ DEFINE_ACTION_FUNCTION(FCanvas, EnableStencil)
PARAM_SELF_PROLOGUE(FCanvas);
PARAM_BOOL(on);

self->Drawer->AddEnableStencil(on);
self->Drawer.AddEnableStencil(on);
self->Tex->NeedUpdate();
return 0;
}

Expand All @@ -1902,7 +1915,8 @@ DEFINE_ACTION_FUNCTION(FCanvas, SetStencil)
PARAM_INT(op);
PARAM_INT(flags);

self->Drawer->AddSetStencil(offs, op, flags);
self->Drawer.AddSetStencil(offs, op, flags);
self->Tex->NeedUpdate();
return 0;
}

Expand All @@ -1920,7 +1934,8 @@ DEFINE_ACTION_FUNCTION(FCanvas, ClearStencil)
{
PARAM_SELF_PROLOGUE(FCanvas);

self->Drawer->AddClearStencil();
self->Drawer.AddClearStencil();
self->Tex->NeedUpdate();
return 0;
}

Expand All @@ -1941,7 +1956,8 @@ DEFINE_ACTION_FUNCTION(FCanvas, SetTransform)
PARAM_SELF_PROLOGUE(FCanvas);
PARAM_OBJECT_NOT_NULL(transform, DShape2DTransform);

self->Drawer->SetTransform(*transform);
self->Drawer.SetTransform(*transform);
self->Tex->NeedUpdate();

return 0;
}
Expand All @@ -1960,7 +1976,8 @@ DEFINE_ACTION_FUNCTION(FCanvas, ClearTransform)
{
PARAM_SELF_PROLOGUE(FCanvas);

self->Drawer->ClearTransform();
self->Drawer.ClearTransform();
self->Tex->NeedUpdate();
return 0;
}

6 changes: 4 additions & 2 deletions src/common/2d/v_drawtext.cpp
Expand Up @@ -247,7 +247,8 @@ DEFINE_ACTION_FUNCTION(FCanvas, DrawChar)
PARAM_VA_POINTER(va_reginfo) // Get the hidden type information array

VMVa_List args = { param + 6, 0, numparam - 7, va_reginfo + 6 };
DrawChar(self->Drawer.get(), font, cr, x, y, chr, args);
DrawChar(&self->Drawer, font, cr, x, y, chr, args);
self->Tex->NeedUpdate();
return 0;
}

Expand Down Expand Up @@ -450,7 +451,8 @@ DEFINE_ACTION_FUNCTION(FCanvas, DrawText)

VMVa_List args = { param + 6, 0, numparam - 7, va_reginfo + 6 };
const char *txt = chr[0] == '$' ? GStrings(&chr[1]) : chr.GetChars();
DrawText(self->Drawer.get(), font, cr, x, y, txt, args);
DrawText(&self->Drawer, font, cr, x, y, txt, args);
self->Tex->NeedUpdate();
return 0;
}

7 changes: 0 additions & 7 deletions src/common/rendering/gl/gl_framebuffer.cpp
Expand Up @@ -229,13 +229,6 @@ void OpenGLFrameBuffer::RenderTextureView(FCanvasTexture* tex, std::function<voi
bounds.height = FHardwareTexture::GetTexDimension(tex->GetHeight());

renderFunc(bounds);

if (tex->Drawer)
{
::Draw2D(tex->Drawer.get(), gl_RenderState);
tex->Drawer->Clear();
}

GLRenderer->EndOffscreen();

tex->SetUpdated(true);
Expand Down
6 changes: 0 additions & 6 deletions src/common/rendering/vulkan/system/vk_framebuffer.cpp
Expand Up @@ -215,12 +215,6 @@ void VulkanFrameBuffer::RenderTextureView(FCanvasTexture* tex, std::function<voi

renderFunc(bounds);

if (tex->Drawer)
{
::Draw2D(tex->Drawer.get(), *mRenderState);
tex->Drawer->Clear();
}

mRenderState->EndRenderPass();

VkImageTransition()
Expand Down
5 changes: 3 additions & 2 deletions src/common/textures/textures.h
Expand Up @@ -303,7 +303,7 @@ class FTexture : public RefCountedBase
friend class FTextureManager;
};

class F2DDrawer;
class FCanvas;

// A texture that can be drawn to.

Expand All @@ -321,10 +321,11 @@ class FCanvasTexture : public FTexture

void NeedUpdate() { bNeedsUpdate = true; }
void SetUpdated(bool rendertype) { bNeedsUpdate = false; bFirstUpdate = false; bLastUpdateType = rendertype; }
bool CheckNeedsUpdate() const { return bNeedsUpdate; }

void SetAspectRatio(double aspectScale, bool useTextureRatio) { aspectRatio = (float)aspectScale * (useTextureRatio? ((float)Width / Height) : 1); }

std::shared_ptr<F2DDrawer> Drawer;
FCanvas* Canvas = nullptr;

protected:

Expand Down
3 changes: 3 additions & 0 deletions src/d_main.cpp
Expand Up @@ -2926,13 +2926,16 @@ static void Doom_CastSpriteIDToString(FString* a, unsigned int b)


extern DThinker* NextToThink;
extern TArray<FCanvas*> AllCanvases;

static void GC_MarkGameRoots()
{
GC::Mark(staticEventManager.FirstEventHandler);
GC::Mark(staticEventManager.LastEventHandler);
for (auto Level : AllLevels())
Level->Mark();
for (auto canvas : AllCanvases)
GC::Mark(canvas);

// Mark players.
for (int i = 0; i < MAXPLAYERS; i++)
Expand Down
19 changes: 19 additions & 0 deletions src/gamedata/textures/animations.cpp
Expand Up @@ -325,6 +325,10 @@ void FTextureAnimator::InitAnimDefs ()
{
ParseWarp(sc);
}
else if (sc.Compare("canvastexture"))
{
ParseCanvasTexture(sc);
}
else if (sc.Compare ("cameratexture"))
{
ParseCameraTexture(sc);
Expand Down Expand Up @@ -683,6 +687,21 @@ void FTextureAnimator::ParseWarp(FScanner &sc)
}
}


//==========================================================================
//
// ParseCameraTexture
//
// Parses a canvas texture definition
//
//==========================================================================

void FTextureAnimator::ParseCanvasTexture(FScanner& sc)
{
// This is currently identical to camera textures.
ParseCameraTexture(sc);
}

//==========================================================================
//
// ParseCameraTexture
Expand Down
1 change: 1 addition & 0 deletions src/gamedata/textures/animations.h
Expand Up @@ -69,6 +69,7 @@ class FTextureAnimator
FAnimDef* ParseRangeAnim(FScanner& sc, FTextureID picnum, ETextureType usetype, bool missing);
void ParsePicAnim(FScanner& sc, FTextureID picnum, ETextureType usetype, bool missing, TArray<FAnimDef::FAnimFrame>& frames);
void ParseWarp(FScanner& sc);
void ParseCanvasTexture(FScanner& sc);
void ParseCameraTexture(FScanner& sc);
FTextureID ParseFramenum(FScanner& sc, FTextureID basepicnum, ETextureType usetype, bool allowMissing);
void ParseTime(FScanner& sc, uint32_t& min, uint32_t& max);
Expand Down
16 changes: 13 additions & 3 deletions src/r_data/r_canvastexture.cpp
Expand Up @@ -110,19 +110,29 @@ void SetCameraToTexture(AActor *viewpoint, const FString &texturename, double fo
}
}

void SetCanvasToTexture(FCanvas* canvas, const FString& texturename)
TArray<FCanvas*> AllCanvases;

FCanvas* GetTextureCanvas(const FString& texturename)
{
FTextureID textureid = TexMan.CheckForTexture(texturename, ETextureType::Wall, FTextureManager::TEXMAN_Overridable);
if (textureid.isValid())
{
// Only proceed if the texture actually has a canvas.
// Only proceed if the texture is a canvas texture.
auto tex = TexMan.GetGameTexture(textureid);
if (tex && tex->GetTexture()->isCanvas())
{
FCanvasTexture* canvasTex = static_cast<FCanvasTexture*>(tex->GetTexture());
canvasTex->Drawer = canvas->Drawer;
if (!canvasTex->Canvas)
{
canvasTex->Canvas = Create<FCanvas>();
canvasTex->Canvas->Tex = canvasTex;
canvasTex->Canvas->Drawer.SetSize(tex->GetTexelWidth(), tex->GetTexelHeight());
AllCanvases.Push(canvasTex->Canvas);
}
return canvasTex->Canvas;
}
}
return nullptr;
}

//==========================================================================
Expand Down
4 changes: 4 additions & 0 deletions src/r_data/r_canvastexture.h
@@ -1,6 +1,8 @@
#pragma once

class FCanvas;
class FCanvasTexture;

// This list keeps track of the cameras that draw into canvas textures.
struct FCanvasTextureEntry
{
Expand All @@ -22,3 +24,5 @@ struct FCanvasTextureInfo
void Mark();

};

extern TArray<FCanvas*> AllCanvases;

0 comments on commit bb50395

Please sign in to comment.