From e78e76a593760b34dbba83822a94b1e367277d3e Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Wed, 1 Feb 2017 16:02:21 +0100 Subject: [PATCH] Move r_viewport variables into a class --- src/polyrenderer/drawers/poly_draw_args.cpp | 5 +- src/polyrenderer/math/tri_matrix.cpp | 5 +- src/polyrenderer/poly_renderer.cpp | 47 +++++++++++-------- src/polyrenderer/scene/poly_decal.cpp | 2 +- src/polyrenderer/scene/poly_particle.cpp | 2 +- src/polyrenderer/scene/poly_playersprite.cpp | 14 +++--- src/polyrenderer/scene/poly_sky.cpp | 2 +- src/polyrenderer/scene/poly_sprite.cpp | 2 +- src/swrenderer/drawers/r_draw.h | 2 - src/swrenderer/drawers/r_draw_pal.cpp | 18 ++++---- src/swrenderer/drawers/r_draw_rgba.cpp | 26 +++++------ src/swrenderer/drawers/r_drawerargs.cpp | 38 ++++++++++------ src/swrenderer/line/r_line.cpp | 5 +- src/swrenderer/line/r_renderdrawsegment.cpp | 9 ++-- src/swrenderer/line/r_walldraw.cpp | 22 +++++---- src/swrenderer/line/r_wallsetup.cpp | 22 +++++---- src/swrenderer/plane/r_flatplane.cpp | 36 ++++++++------- src/swrenderer/plane/r_skyplane.cpp | 10 ++-- src/swrenderer/plane/r_slopeplane.cpp | 14 +++--- src/swrenderer/r_swrenderer.cpp | 10 ++-- src/swrenderer/scene/r_light.cpp | 14 +++--- src/swrenderer/scene/r_light.h | 2 +- src/swrenderer/scene/r_opaque_pass.cpp | 9 ++-- src/swrenderer/scene/r_portal.cpp | 30 ++++++------ src/swrenderer/scene/r_scene.cpp | 48 +++++++++++--------- src/swrenderer/scene/r_viewport.cpp | 26 ----------- src/swrenderer/scene/r_viewport.h | 46 +++++++++++-------- src/swrenderer/things/r_decal.cpp | 6 ++- src/swrenderer/things/r_particle.cpp | 16 ++++--- src/swrenderer/things/r_playersprite.cpp | 32 +++++++------ src/swrenderer/things/r_sprite.cpp | 14 ++++-- src/swrenderer/things/r_visiblesprite.cpp | 14 +++--- src/swrenderer/things/r_voxel.cpp | 15 +++--- src/swrenderer/things/r_wallsprite.cpp | 6 ++- src/v_draw.cpp | 44 ++++++++++-------- 35 files changed, 336 insertions(+), 277 deletions(-) diff --git a/src/polyrenderer/drawers/poly_draw_args.cpp b/src/polyrenderer/drawers/poly_draw_args.cpp index 9e9686e92..81ca52a1b 100644 --- a/src/polyrenderer/drawers/poly_draw_args.cpp +++ b/src/polyrenderer/drawers/poly_draw_args.cpp @@ -48,7 +48,8 @@ void PolyDrawArgs::SetTexture(FTexture *texture) { textureWidth = texture->GetWidth(); textureHeight = texture->GetHeight(); - if (swrenderer::r_swtruecolor) + auto viewport = swrenderer::RenderViewport::Instance(); + if (viewport->r_swtruecolor) texturePixels = (const uint8_t *)texture->GetPixelsBgra(); else texturePixels = texture->GetPixels(); @@ -62,7 +63,7 @@ void PolyDrawArgs::SetTexture(FTexture *texture, uint32_t translationID, bool fo FRemapTable *table = TranslationToTable(translationID); if (table != nullptr && !table->Inactive) { - if (swrenderer::r_swtruecolor) + if (swrenderer::RenderViewport::Instance()->r_swtruecolor) translation = (uint8_t*)table->Palette; else translation = table->Remap; diff --git a/src/polyrenderer/math/tri_matrix.cpp b/src/polyrenderer/math/tri_matrix.cpp index 1f26ef2ef..cd6109539 100644 --- a/src/polyrenderer/math/tri_matrix.cpp +++ b/src/polyrenderer/math/tri_matrix.cpp @@ -146,11 +146,12 @@ TriMatrix TriMatrix::worldToView() TriMatrix TriMatrix::viewToClip() { + auto viewport = swrenderer::RenderViewport::Instance(); float near = 5.0f; float far = 65536.0f; float width = (float)(FocalTangent * near); - float top = (float)(swrenderer::CenterY / swrenderer::InvZtoScale * near); - float bottom = (float)(top - viewheight / swrenderer::InvZtoScale * near); + float top = (float)(viewport->CenterY / viewport->InvZtoScale * near); + float bottom = (float)(top - viewheight / viewport->InvZtoScale * near); return frustum(-width, width, bottom, top, near, far); } diff --git a/src/polyrenderer/poly_renderer.cpp b/src/polyrenderer/poly_renderer.cpp index be1fe0074..9d5475dcc 100644 --- a/src/polyrenderer/poly_renderer.cpp +++ b/src/polyrenderer/poly_renderer.cpp @@ -53,47 +53,51 @@ PolyRenderer *PolyRenderer::Instance() void PolyRenderer::RenderView(player_t *player) { using namespace swrenderer; + + auto viewport = RenderViewport::Instance(); - swrenderer::RenderTarget = screen; + viewport->RenderTarget = screen; - bool saved_swtruecolor = r_swtruecolor; - r_swtruecolor = screen->IsBgra(); + bool saved_swtruecolor = viewport->r_swtruecolor; + viewport->r_swtruecolor = screen->IsBgra(); int width = SCREENWIDTH; int height = SCREENHEIGHT; int stHeight = gST_Y; float trueratio; ActiveRatio(width, height, &trueratio); - RenderViewport::Instance()->SetViewport(width, height, trueratio); + viewport->SetViewport(width, height, trueratio); RenderActorView(player->mo, false); // Apply special colormap if the target cannot do it CameraLight *cameraLight = CameraLight::Instance(); - if (cameraLight->realfixedcolormap && r_swtruecolor && !(r_shadercolormaps && screen->Accel2D)) + if (cameraLight->realfixedcolormap && viewport->r_swtruecolor && !(r_shadercolormaps && screen->Accel2D)) { R_BeginDrawerCommands(); DrawerCommandQueue::QueueCommand(cameraLight->realfixedcolormap, screen); R_EndDrawerCommands(); } - r_swtruecolor = saved_swtruecolor; + viewport->r_swtruecolor = saved_swtruecolor; } void PolyRenderer::RenderViewToCanvas(AActor *actor, DCanvas *canvas, int x, int y, int width, int height, bool dontmaplines) { + auto viewport = swrenderer::RenderViewport::Instance(); + const bool savedviewactive = viewactive; - const bool savedoutputformat = swrenderer::r_swtruecolor; + const bool savedoutputformat = viewport->r_swtruecolor; viewwidth = width; - swrenderer::RenderTarget = canvas; - swrenderer::bRenderingToCanvas = true; + viewport->RenderTarget = canvas; + viewport->bRenderingToCanvas = true; R_SetWindow(12, width, height, height, true); - swrenderer::RenderViewport::Instance()->SetViewport(width, height, WidescreenRatio); + viewport->SetViewport(width, height, WidescreenRatio); viewwindowx = x; viewwindowy = y; viewactive = true; - swrenderer::r_swtruecolor = canvas->IsBgra(); + viewport->r_swtruecolor = canvas->IsBgra(); canvas->Lock(true); @@ -101,14 +105,14 @@ void PolyRenderer::RenderViewToCanvas(AActor *actor, DCanvas *canvas, int x, int canvas->Unlock(); - swrenderer::RenderTarget = screen; - swrenderer::bRenderingToCanvas = false; + viewport->RenderTarget = screen; + viewport->bRenderingToCanvas = false; R_ExecuteSetViewSize(); float trueratio; ActiveRatio(width, height, &trueratio); - swrenderer::RenderViewport::Instance()->SetViewport(width, height, WidescreenRatio); + viewport->SetViewport(width, height, WidescreenRatio); viewactive = savedviewactive; - swrenderer::r_swtruecolor = savedoutputformat; + viewport->r_swtruecolor = savedoutputformat; } void PolyRenderer::RenderActorView(AActor *actor, bool dontmaplines) @@ -157,8 +161,9 @@ void PolyRenderer::RenderRemainingPlayerSprites() void PolyRenderer::ClearBuffers() { PolyVertexBuffer::Clear(); - PolyStencilBuffer::Instance()->Clear(swrenderer::RenderTarget->GetWidth(), swrenderer::RenderTarget->GetHeight(), 0); - PolySubsectorGBuffer::Instance()->Resize(swrenderer::RenderTarget->GetPitch(), swrenderer::RenderTarget->GetHeight()); + auto viewport = swrenderer::RenderViewport::Instance(); + PolyStencilBuffer::Instance()->Clear(viewport->RenderTarget->GetWidth(), viewport->RenderTarget->GetHeight(), 0); + PolySubsectorGBuffer::Instance()->Resize(viewport->RenderTarget->GetPitch(), viewport->RenderTarget->GetHeight()); NextStencilValue = 0; SeenLinePortals.clear(); SeenMirrors.clear(); @@ -167,8 +172,10 @@ void PolyRenderer::ClearBuffers() void PolyRenderer::SetSceneViewport() { using namespace swrenderer; + + auto viewport = RenderViewport::Instance(); - if (RenderTarget == screen) // Rendering to screen + if (viewport->RenderTarget == screen) // Rendering to screen { int height; if (screenblocks >= 10) @@ -177,11 +184,11 @@ void PolyRenderer::SetSceneViewport() height = (screenblocks*SCREENHEIGHT / 10) & ~7; int bottom = SCREENHEIGHT - (height + viewwindowy - ((height - viewheight) / 2)); - PolyTriangleDrawer::set_viewport(viewwindowx, SCREENHEIGHT - bottom - height, viewwidth, height, RenderTarget); + PolyTriangleDrawer::set_viewport(viewwindowx, SCREENHEIGHT - bottom - height, viewwidth, height, viewport->RenderTarget); } else // Rendering to camera texture { - PolyTriangleDrawer::set_viewport(0, 0, RenderTarget->GetWidth(), RenderTarget->GetHeight(), RenderTarget); + PolyTriangleDrawer::set_viewport(0, 0, viewport->RenderTarget->GetWidth(), viewport->RenderTarget->GetHeight(), viewport->RenderTarget); } } diff --git a/src/polyrenderer/scene/poly_decal.cpp b/src/polyrenderer/scene/poly_decal.cpp index 78e43df9e..c658a76fa 100644 --- a/src/polyrenderer/scene/poly_decal.cpp +++ b/src/polyrenderer/scene/poly_decal.cpp @@ -151,7 +151,7 @@ void RenderPolyDecal::Render(const TriMatrix &worldToClip, const Vec4f &clipPlan } args.uniforms.subsectorDepth = subsectorDepth; - if (swrenderer::r_swtruecolor) + if (swrenderer::RenderViewport::Instance()->r_swtruecolor) { args.uniforms.color = 0xff000000 | decal->AlphaColor; } diff --git a/src/polyrenderer/scene/poly_particle.cpp b/src/polyrenderer/scene/poly_particle.cpp index bb21275f9..e9a80dc43 100644 --- a/src/polyrenderer/scene/poly_particle.cpp +++ b/src/polyrenderer/scene/poly_particle.cpp @@ -90,7 +90,7 @@ void RenderPolyParticle::Render(const TriMatrix &worldToClip, const Vec4f &clipP uint32_t alpha = (uint32_t)clamp(particle->alpha * 255.0f + 0.5f, 0.0f, 255.0f); - if (swrenderer::r_swtruecolor) + if (swrenderer::RenderViewport::Instance()->r_swtruecolor) { args.uniforms.color = (alpha << 24) | (particle->color & 0xffffff); } diff --git a/src/polyrenderer/scene/poly_playersprite.cpp b/src/polyrenderer/scene/poly_playersprite.cpp index dd8fc4850..0dee44f73 100644 --- a/src/polyrenderer/scene/poly_playersprite.cpp +++ b/src/polyrenderer/scene/poly_playersprite.cpp @@ -140,23 +140,25 @@ void RenderPolyPlayerSprites::RenderSprite(DPSprite *sprite, AActor *owner, floa sx += wx; sy += wy; } + + auto viewport = swrenderer::RenderViewport::Instance(); double pspritexscale = centerxwide / 160.0; - double pspriteyscale = pspritexscale * swrenderer::YaspectMul; + double pspriteyscale = pspritexscale * viewport->YaspectMul; double pspritexiscale = 1 / pspritexscale; // calculate edges of the shape double tx = sx - BaseXCenter; tx -= tex->GetScaledLeftOffset(); - int x1 = xs_RoundToInt(swrenderer::CenterX + tx * pspritexscale); + int x1 = xs_RoundToInt(viewport->CenterX + tx * pspritexscale); // off the right side if (x1 > viewwidth) return; tx += tex->GetScaledWidth(); - int x2 = xs_RoundToInt(swrenderer::CenterX + tx * pspritexscale); + int x2 = xs_RoundToInt(viewport->CenterX + tx * pspritexscale); // off the left side if (x2 <= 0) @@ -165,12 +167,12 @@ void RenderPolyPlayerSprites::RenderSprite(DPSprite *sprite, AActor *owner, floa double texturemid = (BaseYCenter - sy) * tex->Scale.Y + tex->TopOffset; // Adjust PSprite for fullscreen views - if (camera->player && (swrenderer::RenderTarget != screen || viewheight == swrenderer::RenderTarget->GetHeight() || (swrenderer::RenderTarget->GetWidth() > (BaseXCenter * 2) && !st_scale))) + if (camera->player && (viewport->RenderTarget != screen || viewheight == viewport->RenderTarget->GetHeight() || (viewport->RenderTarget->GetWidth() > (BaseXCenter * 2) && !st_scale))) { AWeapon *weapon = dyn_cast(sprite->GetCaller()); if (weapon != nullptr && weapon->YAdjust != 0) { - if (swrenderer::RenderTarget != screen || viewheight == swrenderer::RenderTarget->GetHeight()) + if (viewport->RenderTarget != screen || viewheight == viewport->RenderTarget->GetHeight()) { texturemid -= weapon->YAdjust; } @@ -343,7 +345,7 @@ void RenderPolyPlayerSprites::RenderSprite(DPSprite *sprite, AActor *owner, floa // Check for hardware-assisted 2D. If it's available, and this sprite is not // fuzzy, don't draw it until after the switch to 2D mode. - if (!noaccel && swrenderer::RenderTarget == screen && (DFrameBuffer *)screen->Accel2D) + if (!noaccel && swrenderer::RenderViewport::Instance()->RenderTarget == screen && (DFrameBuffer *)screen->Accel2D) { FRenderStyle style = RenderStyle; style.CheckFuzz(); diff --git a/src/polyrenderer/scene/poly_sky.cpp b/src/polyrenderer/scene/poly_sky.cpp index c4d0d7320..d4ebc5796 100644 --- a/src/polyrenderer/scene/poly_sky.cpp +++ b/src/polyrenderer/scene/poly_sky.cpp @@ -94,7 +94,7 @@ void PolySkyDome::RenderRow(PolyDrawArgs &args, int row, uint32_t capcolor) void PolySkyDome::RenderCapColorRow(PolyDrawArgs &args, FTexture *skytex, int row, bool bottomCap) { uint32_t solid = skytex->GetSkyCapColor(bottomCap); - if (!swrenderer::r_swtruecolor) + if (!swrenderer::RenderViewport::Instance()->r_swtruecolor) solid = RGB32k.RGB[(RPART(solid) >> 3)][(GPART(solid) >> 3)][(BPART(solid) >> 3)]; args.vinput = &mVertices[mPrimStart[row]]; diff --git a/src/polyrenderer/scene/poly_sprite.cpp b/src/polyrenderer/scene/poly_sprite.cpp index 0b9b7a986..43ec99d90 100644 --- a/src/polyrenderer/scene/poly_sprite.cpp +++ b/src/polyrenderer/scene/poly_sprite.cpp @@ -250,7 +250,7 @@ void RenderPolySprite::Render(const TriMatrix &worldToClip, const Vec4f &clipPla args.SetTexture(tex, thing->Translation, true); } - if (!swrenderer::r_swtruecolor) + if (!swrenderer::RenderViewport::Instance()->r_swtruecolor) { uint32_t r = (args.uniforms.color >> 16) & 0xff; uint32_t g = (args.uniforms.color >> 8) & 0xff; diff --git a/src/swrenderer/drawers/r_draw.h b/src/swrenderer/drawers/r_draw.h index 625e67ead..d6ec7a149 100644 --- a/src/swrenderer/drawers/r_draw.h +++ b/src/swrenderer/drawers/r_draw.h @@ -44,8 +44,6 @@ namespace swrenderer #define PARTICLE_TEXTURE_SIZE 64 extern uint32_t particle_texture[PARTICLE_TEXTURE_SIZE * PARTICLE_TEXTURE_SIZE]; - extern bool r_swtruecolor; - class SWPixelFormatDrawers { public: diff --git a/src/swrenderer/drawers/r_draw_pal.cpp b/src/swrenderer/drawers/r_draw_pal.cpp index 9051126ea..e7906d319 100644 --- a/src/swrenderer/drawers/r_draw_pal.cpp +++ b/src/swrenderer/drawers/r_draw_pal.cpp @@ -106,7 +106,7 @@ namespace swrenderer _dest = args.Dest(); _dest_y = args.DestY(); _fracbits = args.dc_wall_fracbits; - _pitch = dc_pitch; + _pitch = RenderViewport::Instance()->dc_pitch; _srcblend = args.dc_srcblend; _destblend = args.dc_destblend; _dynlights = args.dc_lights; @@ -564,7 +564,7 @@ namespace swrenderer _dest = args.Dest(); _dest_y = args.DestY(); _count = args.Count(); - _pitch = dc_pitch; + _pitch = RenderViewport::Instance()->dc_pitch; _source = args.FrontTexturePixels(); _source2 = args.BackTexturePixels(); _sourceheight[0] = args.FrontTextureHeight(); @@ -870,7 +870,7 @@ namespace swrenderer _count = args.dc_count; _dest = args.Dest(); _dest_y = args.DestY(); - _pitch = dc_pitch; + _pitch = RenderViewport::Instance()->dc_pitch; _iscale = args.dc_iscale; _texturefrac = args.dc_texturefrac; _colormap = args.Colormap(); @@ -1767,8 +1767,8 @@ namespace swrenderer _yl = args.dc_yl; _yh = args.dc_yh; _x = args.dc_x; - _destorg = dc_destorg; - _pitch = dc_pitch; + _destorg = RenderViewport::Instance()->dc_destorg; + _pitch = RenderViewport::Instance()->dc_pitch; _fuzzpos = fuzzpos; _fuzzviewheight = fuzzviewheight; } @@ -1858,7 +1858,7 @@ namespace swrenderer _y = args.DestY(); _x1 = args.DestX1(); _x2 = args.DestX2(); - _destorg = dc_destorg; + _destorg = RenderViewport::Instance()->dc_destorg; _xstep = args.TextureUStep(); _ystep = args.TextureVStep(); _xbits = args.TextureWidthBits(); @@ -2628,7 +2628,7 @@ namespace swrenderer : y(y), x1(x1), x2(x2), plane_sz(plane_sz), plane_su(plane_su), plane_sv(plane_sv), plane_shade(plane_shade), planeshade(planeshade), planelightfloat(planelightfloat), pviewx(pviewx), pviewy(pviewy) { _colormap = args.Colormap(); - _destorg = dc_destorg; + _destorg = RenderViewport::Instance()->dc_destorg; _ybits = args.TextureHeightBits(); _xbits = args.TextureWidthBits(); _source = args.TexturePixels(); @@ -2873,7 +2873,7 @@ namespace swrenderer DrawColoredSpanPalCommand::DrawColoredSpanPalCommand(const SpanDrawerArgs &args, int y, int x1, int x2) : PalSpanCommand(args), y(y), x1(x1), x2(x2) { color = args.SolidColor(); - destorg = dc_destorg; + destorg = RenderViewport::Instance()->dc_destorg; } void DrawColoredSpanPalCommand::Execute(DrawerThread *thread) @@ -2889,7 +2889,7 @@ namespace swrenderer DrawFogBoundaryLinePalCommand::DrawFogBoundaryLinePalCommand(const SpanDrawerArgs &args, int y, int x1, int x2) : PalSpanCommand(args), y(y), x1(x1), x2(x2) { _colormap = args.Colormap(); - _destorg = dc_destorg; + _destorg = RenderViewport::Instance()->dc_destorg; } void DrawFogBoundaryLinePalCommand::Execute(DrawerThread *thread) diff --git a/src/swrenderer/drawers/r_draw_rgba.cpp b/src/swrenderer/drawers/r_draw_rgba.cpp index 897990cdb..59521bb06 100644 --- a/src/swrenderer/drawers/r_draw_rgba.cpp +++ b/src/swrenderer/drawers/r_draw_rgba.cpp @@ -72,8 +72,8 @@ namespace swrenderer args.y = drawerargs.DestY(); args.xbits = drawerargs.TextureWidthBits(); args.ybits = drawerargs.TextureHeightBits(); - args.destorg = (uint32_t*)dc_destorg; - args.destpitch = dc_pitch; + args.destorg = (uint32_t*)RenderViewport::Instance()->dc_destorg; + args.destpitch = RenderViewport::Instance()->dc_pitch; args.source = (const uint32_t*)drawerargs.TexturePixels(); args.light = LightBgra::calc_light_multiplier(drawerargs.Light()); args.light_red = shade_constants.light_red; @@ -186,7 +186,7 @@ namespace swrenderer auto shade_constants = drawerargs.ColormapConstants(); args.dest = (uint32_t*)drawerargs.Dest(); args.dest_y = drawerargs.DestY(); - args.pitch = dc_pitch; + args.pitch = RenderViewport::Instance()->dc_pitch; args.count = drawerargs.dc_count; args.texturefrac[0] = drawerargs.dc_texturefrac; args.texturefracx[0] = drawerargs.dc_texturefracx; @@ -256,7 +256,7 @@ namespace swrenderer args.colormap = drawerargs.Colormap(); args.translation = drawerargs.TranslationMap(); args.basecolors = (const uint32_t *)GPalette.BaseColors; - args.pitch = dc_pitch; + args.pitch = RenderViewport::Instance()->dc_pitch; args.count = drawerargs.dc_count; args.dest_y = drawerargs.DestY(); args.iscale = drawerargs.dc_iscale; @@ -307,7 +307,7 @@ namespace swrenderer args.dest = (uint32_t*)drawerargs.Dest(); args.dest_y = drawerargs.DestY(); args.count = drawerargs.Count(); - args.pitch = dc_pitch; + args.pitch = RenderViewport::Instance()->dc_pitch; args.texturefrac[0] = drawerargs.TextureVPos(); args.iscale[0] = drawerargs.TextureVStep(); args.source0[0] = (const uint32_t *)drawerargs.FrontTexturePixels(); @@ -331,8 +331,8 @@ namespace swrenderer _x = drawerargs.dc_x; _yl = drawerargs.dc_yl; _yh = drawerargs.dc_yh; - _destorg = dc_destorg; - _pitch = dc_pitch; + _destorg = RenderViewport::Instance()->dc_destorg; + _pitch = RenderViewport::Instance()->dc_pitch; _fuzzpos = fuzzpos; _fuzzviewheight = fuzzviewheight; } @@ -439,7 +439,7 @@ namespace swrenderer _x1 = drawerargs.DestX1(); _x2 = drawerargs.DestX2(); _y = drawerargs.DestY(); - _destorg = dc_destorg; + _destorg = RenderViewport::Instance()->dc_destorg; _light = drawerargs.Light(); _color = drawerargs.SolidColor(); } @@ -470,7 +470,7 @@ namespace swrenderer _x = x; _x2 = x2; - _destorg = dc_destorg; + _destorg = RenderViewport::Instance()->dc_destorg; _light = drawerargs.Light(); _shade_constants = drawerargs.ColormapConstants(); } @@ -537,7 +537,7 @@ namespace swrenderer _x1 = x1; _x2 = x2; _y = y; - _destorg = dc_destorg; + _destorg = RenderViewport::Instance()->dc_destorg; _light = drawerargs.Light(); _shade_constants = drawerargs.ColormapConstants(); _plane_sz = plane_sz; @@ -672,7 +672,7 @@ namespace swrenderer _x1 = x1; _x2 = x2; - _destorg = dc_destorg; + _destorg = RenderViewport::Instance()->dc_destorg; _light = drawerargs.Light(); _color = drawerargs.SolidColor(); } @@ -709,8 +709,8 @@ namespace swrenderer _color = color; _a = a; - _destorg = dc_destorg; - _pitch = dc_pitch; + _destorg = RenderViewport::Instance()->dc_destorg; + _pitch = RenderViewport::Instance()->dc_pitch; } void FillTransColumnRGBACommand::Execute(DrawerThread *thread) diff --git a/src/swrenderer/drawers/r_drawerargs.cpp b/src/swrenderer/drawers/r_drawerargs.cpp index b78982952..8da9e3d84 100644 --- a/src/swrenderer/drawers/r_drawerargs.cpp +++ b/src/swrenderer/drawers/r_drawerargs.cpp @@ -41,7 +41,7 @@ namespace swrenderer { SWPixelFormatDrawers *DrawerArgs::Drawers() { - if (r_swtruecolor) + if (RenderViewport::Instance()->r_swtruecolor) { static SWTruecolorDrawers tc_drawers; return &tc_drawers; @@ -80,7 +80,7 @@ namespace swrenderer { if (mBaseColormap) { - if (r_swtruecolor) + if (RenderViewport::Instance()->r_swtruecolor) return mBaseColormap->Maps; else return mBaseColormap->Maps + (GETPALOOKUP(mLight, mShade) << COLORMAPSHIFT); @@ -137,14 +137,17 @@ namespace swrenderer ds_ybits--; } - ds_source = r_swtruecolor ? (const uint8_t*)tex->GetPixelsBgra() : tex->GetPixels(); + auto viewport = RenderViewport::Instance(); + ds_source = viewport->r_swtruecolor ? (const uint8_t*)tex->GetPixelsBgra() : tex->GetPixels(); ds_source_mipmapped = tex->Mipmapped() && tex->GetWidth() > 1 && tex->GetHeight() > 1; } void SpriteDrawerArgs::DrawMaskedColumn(int x, fixed_t iscale, FTexture *tex, fixed_t col, double spryscale, double sprtopscreen, bool sprflipvert, const short *mfloorclip, const short *mceilingclip, bool unmasked) { + auto viewport = RenderViewport::Instance(); + // Handle the linear filtered version in a different function to reduce chances of merge conflicts from zdoom. - if (r_swtruecolor && !drawer_needs_pal_input) // To do: add support to R_DrawColumnHoriz_rgba + if (viewport->r_swtruecolor && !drawer_needs_pal_input) // To do: add support to R_DrawColumnHoriz_rgba { DrawMaskedColumnBgra(x, iscale, tex, col, spryscale, sprtopscreen, sprflipvert, mfloorclip, mceilingclip, unmasked); return; @@ -156,7 +159,7 @@ namespace swrenderer const FTexture::Span *span; const BYTE *column; - if (r_swtruecolor && !drawer_needs_pal_input) + if (viewport->r_swtruecolor && !drawer_needs_pal_input) column = (const BYTE *)tex->GetColumnBgra(col >> FRACBITS, &span); else column = tex->GetColumn(col >> FRACBITS, &span); @@ -171,7 +174,7 @@ namespace swrenderer unmaskedSpan[1].Length = 0; } - int pixelsize = r_swtruecolor ? 4 : 1; + int pixelsize = viewport->r_swtruecolor ? 4 : 1; while (span->Length != 0) { @@ -500,6 +503,8 @@ namespace swrenderer { alpha = clamp(alpha, 0, OPAQUE); } + + auto viewport = RenderViewport::Instance(); if (translation != -1) { @@ -509,7 +514,7 @@ namespace swrenderer FRemapTable *table = TranslationToTable(translation); if (table != NULL && !table->Inactive) { - if (r_swtruecolor) + if (viewport->r_swtruecolor) SetTranslationMap((uint8_t*)table->Palette); else SetTranslationMap(table->Remap); @@ -585,8 +590,9 @@ namespace swrenderer void WallDrawerArgs::SetDest(int x, int y) { - int pixelsize = r_swtruecolor ? 4 : 1; - dc_dest = dc_destorg + (ylookup[y] + x) * pixelsize; + auto viewport = RenderViewport::Instance(); + int pixelsize = viewport->r_swtruecolor ? 4 : 1; + dc_dest = viewport->dc_destorg + (ylookup[y] + x) * pixelsize; dc_dest_y = y; } @@ -719,14 +725,15 @@ namespace swrenderer void SkyDrawerArgs::SetDest(int x, int y) { - int pixelsize = r_swtruecolor ? 4 : 1; - dc_dest = dc_destorg + (ylookup[y] + x) * pixelsize; + auto viewport = RenderViewport::Instance(); + int pixelsize = viewport->r_swtruecolor ? 4 : 1; + dc_dest = viewport->dc_destorg + (ylookup[y] + x) * pixelsize; dc_dest_y = y; } void SkyDrawerArgs::SetFrontTexture(FTexture *texture, uint32_t column) { - if (r_swtruecolor) + if (RenderViewport::Instance()->r_swtruecolor) { dc_source = (const uint8_t *)texture->GetColumnBgra(column, nullptr); dc_sourceheight = texture->GetHeight(); @@ -745,7 +752,7 @@ namespace swrenderer dc_source2 = nullptr; dc_sourceheight2 = 1; } - else if (r_swtruecolor) + else if (RenderViewport::Instance()->r_swtruecolor) { dc_source2 = (const uint8_t *)texture->GetColumnBgra(column, nullptr); dc_sourceheight2 = texture->GetHeight(); @@ -764,8 +771,9 @@ namespace swrenderer void SpriteDrawerArgs::SetDest(int x, int y) { - int pixelsize = r_swtruecolor ? 4 : 1; - dc_dest = dc_destorg + (ylookup[y] + x) * pixelsize; + auto viewport = RenderViewport::Instance(); + int pixelsize = viewport->r_swtruecolor ? 4 : 1; + dc_dest = viewport->dc_destorg + (ylookup[y] + x) * pixelsize; dc_dest_y = y; } } diff --git a/src/swrenderer/line/r_line.cpp b/src/swrenderer/line/r_line.cpp index 7c1a6b78a..dea8f62e7 100644 --- a/src/swrenderer/line/r_line.cpp +++ b/src/swrenderer/line/r_line.cpp @@ -1148,6 +1148,7 @@ namespace swrenderer tright.Y = float(pt2.X * ViewTanCos + pt2.Y * ViewTanSin); RenderPortal *renderportal = RenderPortal::Instance(); + auto viewport = RenderViewport::Instance(); if (renderportal->MirrorFlags & RF_XFLIP) { @@ -1161,7 +1162,7 @@ namespace swrenderer { if (tleft.X > tleft.Y) return true; // left edge is off the right side if (tleft.Y == 0) return true; - sx1 = xs_RoundToInt(CenterX + tleft.X * CenterX / tleft.Y); + sx1 = xs_RoundToInt(viewport->CenterX + tleft.X * viewport->CenterX / tleft.Y); sz1 = tleft.Y; } else @@ -1180,7 +1181,7 @@ namespace swrenderer { if (tright.X < -tright.Y) return true; // right edge is off the left side if (tright.Y == 0) return true; - sx2 = xs_RoundToInt(CenterX + tright.X * CenterX / tright.Y); + sx2 = xs_RoundToInt(viewport->CenterX + tright.X * viewport->CenterX / tright.Y); sz2 = tright.Y; } else diff --git a/src/swrenderer/line/r_renderdrawsegment.cpp b/src/swrenderer/line/r_renderdrawsegment.cpp index bdd587aa8..42f46da5e 100644 --- a/src/swrenderer/line/r_renderdrawsegment.cpp +++ b/src/swrenderer/line/r_renderdrawsegment.cpp @@ -48,6 +48,7 @@ namespace swrenderer { void RenderDrawSegment::Render(DrawSegment *ds, int x1, int x2) { + auto viewport = RenderViewport::Instance(); RenderFogBoundary renderfog; float *MaskedSWall = nullptr, MaskedScaleY = 0, rw_scalestep = 0; fixed_t *maskedtexturecol = nullptr; @@ -209,12 +210,12 @@ namespace swrenderer } // [RH] Don't bother drawing segs that are completely offscreen - if (globaldclip * ds->sz1 < -textop && globaldclip * ds->sz2 < -textop) + if (viewport->globaldclip * ds->sz1 < -textop && viewport->globaldclip * ds->sz2 < -textop) { // Texture top is below the bottom of the screen goto clearfog; } - if (globaluclip * ds->sz1 > texheight - textop && globaluclip * ds->sz2 > texheight - textop) + if (viewport->globaluclip * ds->sz1 > texheight - textop && viewport->globaluclip * ds->sz2 > texheight - textop) { // Texture bottom is above the top of the screen goto clearfog; } @@ -290,9 +291,9 @@ namespace swrenderer fixed_t iscale = xs_Fix<16>::ToFix(MaskedSWall[x] * MaskedScaleY); double sprtopscreen; if (sprflipvert) - sprtopscreen = CenterY + texturemid * spryscale; + sprtopscreen = viewport->CenterY + texturemid * spryscale; else - sprtopscreen = CenterY - texturemid * spryscale; + sprtopscreen = viewport->CenterY - texturemid * spryscale; columndrawerargs.DrawMaskedColumn(x, iscale, tex, maskedtexturecol[x], spryscale, sprtopscreen, sprflipvert, mfloorclip, mceilingclip); diff --git a/src/swrenderer/line/r_walldraw.cpp b/src/swrenderer/line/r_walldraw.cpp index bf18426ef..e8dedc6e1 100644 --- a/src/swrenderer/line/r_walldraw.cpp +++ b/src/swrenderer/line/r_walldraw.cpp @@ -45,9 +45,11 @@ namespace swrenderer { WallSampler::WallSampler(int y1, double texturemid, float swal, double yrepeat, fixed_t xoffset, double xmagnitude, FTexture *texture) { + auto viewport = RenderViewport::Instance(); + xoffset += FLOAT2FIXED(xmagnitude * 0.5); - if (!r_swtruecolor) + if (!viewport->r_swtruecolor) { height = texture->GetHeight(); @@ -59,7 +61,7 @@ namespace swrenderer // Find start uv in [0-base_height[ range. // Not using xs_ToFixed because it rounds the result and we need something that always rounds down to stay within the range. double uv_stepd = swal * yrepeat; - double v = (texturemid + uv_stepd * (y1 - CenterY + 0.5)) / height; + double v = (texturemid + uv_stepd * (y1 - viewport->CenterY + 0.5)) / height; v = v - floor(v); v *= height; v *= (1 << uv_fracbits); @@ -86,7 +88,7 @@ namespace swrenderer col = width + (col % width); } - if (r_swtruecolor) + if (viewport->r_swtruecolor) source = (const uint8_t *)texture->GetColumnBgra(col, nullptr); else source = texture->GetColumn(col, nullptr); @@ -98,7 +100,7 @@ namespace swrenderer { // Normalize to 0-1 range: double uv_stepd = swal * yrepeat; - double v = (texturemid + uv_stepd * (y1 - CenterY + 0.5)) / texture->GetHeight(); + double v = (texturemid + uv_stepd * (y1 - viewport->CenterY + 0.5)) / texture->GetHeight(); v = v - floor(v); double v_step = uv_stepd / texture->GetHeight(); @@ -172,16 +174,18 @@ namespace swrenderer { if (r_dynlights && light_list) { + auto viewport = RenderViewport::Instance(); + // Find column position in view space float w1 = 1.0f / WallC.sz1; float w2 = 1.0f / WallC.sz2; float t = (x - WallC.sx1 + 0.5f) / (WallC.sx2 - WallC.sx1); float wcol = w1 * (1.0f - t) + w2 * t; float zcol = 1.0f / wcol; - drawerargs.dc_viewpos.X = (float)((x + 0.5 - CenterX) / CenterX * zcol); + drawerargs.dc_viewpos.X = (float)((x + 0.5 - viewport->CenterX) / viewport->CenterX * zcol); drawerargs.dc_viewpos.Y = zcol; - drawerargs.dc_viewpos.Z = (float)((CenterY - y1 - 0.5) / InvZtoScale * zcol); - drawerargs.dc_viewpos_step.Z = (float)(-zcol / InvZtoScale); + drawerargs.dc_viewpos.Z = (float)((viewport->CenterY - y1 - 0.5) / viewport->InvZtoScale * zcol); + drawerargs.dc_viewpos_step.Z = (float)(-zcol / viewport->InvZtoScale); static TriLight lightbuffer[64 * 1024]; static int nextlightindex = 0; @@ -236,7 +240,7 @@ namespace swrenderer drawerargs.dc_num_lights = 0; } - if (r_swtruecolor) + if (RenderViewport::Instance()->r_swtruecolor) { int count = y2 - y1; @@ -320,7 +324,7 @@ namespace swrenderer texturemid = 0; } - drawerargs.dc_wall_fracbits = r_swtruecolor ? FRACBITS : fracbits; + drawerargs.dc_wall_fracbits = RenderViewport::Instance()->r_swtruecolor ? FRACBITS : fracbits; CameraLight *cameraLight = CameraLight::Instance(); bool fixed = (cameraLight->fixedcolormap != NULL || cameraLight->fixedlightlev >= 0); diff --git a/src/swrenderer/line/r_wallsetup.cpp b/src/swrenderer/line/r_wallsetup.cpp index 2c9c82442..33388586f 100644 --- a/src/swrenderer/line/r_wallsetup.cpp +++ b/src/swrenderer/line/r_wallsetup.cpp @@ -34,8 +34,10 @@ namespace swrenderer ProjectedWallCull ProjectedWallLine::Project(double z1, double z2, const FWallCoords *wallc) { - float y1 = (float)(CenterY - z1 * InvZtoScale / wallc->sz1); - float y2 = (float)(CenterY - z2 * InvZtoScale / wallc->sz2); + auto viewport = RenderViewport::Instance(); + + float y1 = (float)(viewport->CenterY - z1 * viewport->InvZtoScale / wallc->sz1); + float y2 = (float)(viewport->CenterY - z2 * viewport->InvZtoScale / wallc->sz2); if (y1 < 0 && y2 < 0) // entire line is above screen { @@ -151,13 +153,15 @@ namespace swrenderer void ProjectedWallTexcoords::Project(double walxrepeat, int x1, int x2, const FWallTmapVals &WallT) { - float uOverZ = WallT.UoverZorg + WallT.UoverZstep * (float)(x1 + 0.5 - CenterX); - float invZ = WallT.InvZorg + WallT.InvZstep * (float)(x1 + 0.5 - CenterX); + auto viewport = RenderViewport::Instance(); + + float uOverZ = WallT.UoverZorg + WallT.UoverZstep * (float)(x1 + 0.5 - viewport->CenterX); + float invZ = WallT.InvZorg + WallT.InvZstep * (float)(x1 + 0.5 - viewport->CenterX); float uGradient = WallT.UoverZstep; float zGradient = WallT.InvZstep; float xrepeat = (float)walxrepeat; - float depthScale = (float)(WallT.InvZstep * WallTMapScale2); - float depthOrg = (float)(-WallT.UoverZstep * WallTMapScale2); + float depthScale = (float)(WallT.InvZstep * viewport->WallTMapScale2); + float depthOrg = (float)(-WallT.UoverZstep * viewport->WallTMapScale2); if (xrepeat < 0.0f) { @@ -189,8 +193,10 @@ namespace swrenderer void ProjectedWallTexcoords::ProjectPos(double walxrepeat, int x1, int x2, const FWallTmapVals &WallT) { - float uOverZ = WallT.UoverZorg + WallT.UoverZstep * (float)(x1 + 0.5 - CenterX); - float invZ = WallT.InvZorg + WallT.InvZstep * (float)(x1 + 0.5 - CenterX); + auto viewport = RenderViewport::Instance(); + + float uOverZ = WallT.UoverZorg + WallT.UoverZstep * (float)(x1 + 0.5 - viewport->CenterX); + float invZ = WallT.InvZorg + WallT.InvZstep * (float)(x1 + 0.5 - viewport->CenterX); float uGradient = WallT.UoverZstep; float zGradient = WallT.InvZstep; float xrepeat = (float)walxrepeat; diff --git a/src/swrenderer/plane/r_flatplane.cpp b/src/swrenderer/plane/r_flatplane.cpp index 895734ea6..cccac6d9d 100644 --- a/src/swrenderer/plane/r_flatplane.cpp +++ b/src/swrenderer/plane/r_flatplane.cpp @@ -78,9 +78,11 @@ namespace swrenderer // left to right mapping planeang += (ViewAngle - 90).Radians(); + auto viewport = RenderViewport::Instance(); + // Scale will be unit scale at FocalLengthX (normally SCREENWIDTH/2) distance - xstep = cos(planeang) / FocalLengthX; - ystep = -sin(planeang) / FocalLengthX; + xstep = cos(planeang) / viewport->FocalLengthX; + ystep = -sin(planeang) / viewport->FocalLengthX; // [RH] flip for mirrors RenderPortal *renderportal = RenderPortal::Instance(); @@ -170,12 +172,14 @@ namespace swrenderer drawerargs.SetTextureVStep(0); drawerargs.SetTextureVPos(0); } + + auto viewport = RenderViewport::Instance(); - if (r_swtruecolor) + if (viewport->r_swtruecolor) { double distance2 = planeheight * yslope[(y + 1 < viewheight) ? y + 1 : y - 1]; - double xmagnitude = fabs(ystepscale * (distance2 - distance) * FocalLengthX); - double ymagnitude = fabs(xstepscale * (distance2 - distance) * FocalLengthX); + double xmagnitude = fabs(ystepscale * (distance2 - distance) * viewport->FocalLengthX); + double ymagnitude = fabs(xstepscale * (distance2 - distance) * viewport->FocalLengthX); double magnitude = MAX(ymagnitude, xmagnitude); double min_lod = -1000.0; drawerargs.SetTextureLOD(MAX(log2(magnitude) + r_lod_bias, min_lod)); @@ -184,17 +188,17 @@ namespace swrenderer if (plane_shade) { // Determine lighting based on the span's distance from the viewer. - drawerargs.SetColorMapLight(basecolormap, (float)(GlobVis * fabs(CenterY - y)), planeshade); + drawerargs.SetColorMapLight(basecolormap, (float)(GlobVis * fabs(viewport->CenterY - y)), planeshade); } if (r_dynlights) { // Find row position in view space - float zspan = (float)(planeheight / (fabs(y + 0.5 - CenterY) / InvZtoScale)); - drawerargs.dc_viewpos.X = (float)((x1 + 0.5 - CenterX) / CenterX * zspan); + float zspan = (float)(planeheight / (fabs(y + 0.5 - viewport->CenterY) / viewport->InvZtoScale)); + drawerargs.dc_viewpos.X = (float)((x1 + 0.5 - viewport->CenterX) / viewport->CenterX * zspan); drawerargs.dc_viewpos.Y = zspan; - drawerargs.dc_viewpos.Z = (float)((CenterY - y - 0.5) / InvZtoScale * zspan); - drawerargs.dc_viewpos_step.X = (float)(zspan / CenterX); + drawerargs.dc_viewpos.Z = (float)((viewport->CenterY - y - 0.5) / viewport->InvZtoScale * zspan); + drawerargs.dc_viewpos_step.X = (float)(zspan / viewport->CenterX); static TriLight lightbuffer[64 * 1024]; static int nextlightindex = 0; @@ -202,7 +206,7 @@ namespace swrenderer // Plane normal drawerargs.dc_normal.X = 0.0f; drawerargs.dc_normal.Y = 0.0f; - drawerargs.dc_normal.Z = (y >= CenterY) ? 1.0f : -1.0f; + drawerargs.dc_normal.Z = (y >= viewport->CenterY) ? 1.0f : -1.0f; // Setup lights for row drawerargs.dc_num_lights = 0; @@ -266,13 +270,13 @@ namespace swrenderer void RenderFlatPlane::SetupSlope() { - int e, i; + auto viewport = RenderViewport::Instance(); - i = 0; - e = viewheight; - float focus = float(FocalLengthY); + int i = 0; + int e = viewheight; + float focus = float(viewport->FocalLengthY); float den; - float cy = float(CenterY); + float cy = float(viewport->CenterY); if (i < centery) { den = cy - i - 0.5f; diff --git a/src/swrenderer/plane/r_skyplane.cpp b/src/swrenderer/plane/r_skyplane.cpp index e0e12c091..f7b4222f2 100644 --- a/src/swrenderer/plane/r_skyplane.cpp +++ b/src/swrenderer/plane/r_skyplane.cpp @@ -169,11 +169,12 @@ namespace swrenderer void RenderSkyPlane::DrawSkyColumnStripe(int start_x, int y1, int y2, double scale, double texturemid, double yrepeat) { RenderPortal *renderportal = RenderPortal::Instance(); + auto viewport = RenderViewport::Instance(); uint32_t height = frontskytex->GetHeight(); double uv_stepd = skyiscale * yrepeat; - double v = (texturemid + uv_stepd * (y1 - CenterY + 0.5)) / height; + double v = (texturemid + uv_stepd * (y1 - viewport->CenterY + 0.5)) / height; double v_step = uv_stepd / height; uint32_t uv_pos = (uint32_t)(v * 0x01000000); @@ -192,7 +193,7 @@ namespace swrenderer } else { - ang = (skyangle + xtoviewangle[x]) ^ skyflip; + ang = (skyangle + viewport->xtoviewangle[x]) ^ skyflip; } angle1 = (uint32_t)((UMulScale16(ang, frontcyl) + frontpos) >> FRACBITS); angle2 = (uint32_t)((UMulScale16(ang, backcyl) + backpos) >> FRACBITS); @@ -222,13 +223,14 @@ namespace swrenderer } else { + auto viewport = RenderViewport::Instance(); double yrepeat = frontskytex->Scale.Y; double scale = frontskytex->Scale.Y * skyscale; double iscale = 1 / scale; short drawheight = short(frontskytex->GetHeight() * scale); - double topfrac = fmod(skymid + iscale * (1 - CenterY), frontskytex->GetHeight()); + double topfrac = fmod(skymid + iscale * (1 - viewport->CenterY), frontskytex->GetHeight()); if (topfrac < 0) topfrac += frontskytex->GetHeight(); - double texturemid = topfrac - iscale * (1 - CenterY); + double texturemid = topfrac - iscale * (1 - viewport->CenterY); DrawSkyColumnStripe(start_x, y1, y2, scale, texturemid, yrepeat); } } diff --git a/src/swrenderer/plane/r_slopeplane.cpp b/src/swrenderer/plane/r_slopeplane.cpp index cdee76e0c..93e98f2d8 100644 --- a/src/swrenderer/plane/r_slopeplane.cpp +++ b/src/swrenderer/plane/r_slopeplane.cpp @@ -69,6 +69,8 @@ namespace swrenderer { return; } + + auto viewport = RenderViewport::Instance(); drawerargs.SetSolidColor(3); drawerargs.SetTexture(texture); @@ -124,13 +126,13 @@ namespace swrenderer plane_sv = p ^ n; plane_sz = m ^ n; - plane_su.Z *= FocalLengthX; - plane_sv.Z *= FocalLengthX; - plane_sz.Z *= FocalLengthX; + plane_su.Z *= viewport->FocalLengthX; + plane_sv.Z *= viewport->FocalLengthX; + plane_sz.Z *= viewport->FocalLengthX; - plane_su.Y *= IYaspectMul; - plane_sv.Y *= IYaspectMul; - plane_sz.Y *= IYaspectMul; + plane_su.Y *= viewport->IYaspectMul; + plane_sv.Y *= viewport->IYaspectMul; + plane_sz.Y *= viewport->IYaspectMul; // Premultiply the texture vectors with the scale factors plane_su *= 4294967296.f; diff --git a/src/swrenderer/r_swrenderer.cpp b/src/swrenderer/r_swrenderer.cpp index a558e04c3..ab7158471 100644 --- a/src/swrenderer/r_swrenderer.cpp +++ b/src/swrenderer/r_swrenderer.cpp @@ -89,7 +89,7 @@ void FSoftwareRenderer::Init() { gl_ParseDefs(); - r_swtruecolor = screen->IsBgra(); + RenderViewport::Instance()->r_swtruecolor = screen->IsBgra(); RenderScene::Instance()->Init(); } @@ -249,8 +249,10 @@ void FSoftwareRenderer::SetClearColor(int color) void FSoftwareRenderer::RenderTextureView (FCanvasTexture *tex, AActor *viewpoint, int fov) { - BYTE *Pixels = r_swtruecolor ? (BYTE*)tex->GetPixelsBgra() : (BYTE*)tex->GetPixels(); - DSimpleCanvas *Canvas = r_swtruecolor ? tex->GetCanvasBgra() : tex->GetCanvas(); + auto viewport = RenderViewport::Instance(); + + BYTE *Pixels = viewport->r_swtruecolor ? (BYTE*)tex->GetPixelsBgra() : (BYTE*)tex->GetPixels(); + DSimpleCanvas *Canvas = viewport->r_swtruecolor ? tex->GetCanvasBgra() : tex->GetCanvas(); // curse Doom's overuse of global variables in the renderer. // These get clobbered by rendering to a camera texture but they need to be preserved so the final rendering can be done with the correct palette. @@ -289,7 +291,7 @@ void FSoftwareRenderer::RenderTextureView (FCanvasTexture *tex, AActor *viewpoin } } - if (r_swtruecolor) + if (viewport->r_swtruecolor) { // True color render still sometimes uses palette textures (for sprites, mostly). // We need to make sure that both pixel buffers contain data: diff --git a/src/swrenderer/scene/r_light.cpp b/src/swrenderer/scene/r_light.cpp index 851fab750..2b0f86092 100644 --- a/src/swrenderer/scene/r_light.cpp +++ b/src/swrenderer/scene/r_light.cpp @@ -59,7 +59,8 @@ namespace swrenderer if (player->fixedcolormap >= 0 && player->fixedcolormap < (int)SpecialColormaps.Size()) { realfixedcolormap = &SpecialColormaps[player->fixedcolormap]; - if (RenderTarget == screen && (r_swtruecolor || ((DFrameBuffer *)screen->Accel2D && r_shadercolormaps))) + auto viewport = RenderViewport::Instance(); + if (viewport->RenderTarget == screen && (viewport->r_swtruecolor || ((DFrameBuffer *)screen->Accel2D && r_shadercolormaps))) { // Render everything fullbright. The copy to video memory will // apply the special colormap, so it won't be restricted to the @@ -105,7 +106,8 @@ namespace swrenderer CurrentVisibility = vis; - if (FocalTangent == 0 || FocalLengthY == 0) + auto viewport = RenderViewport::Instance(); + if (FocalTangent == 0 || viewport->FocalLengthY == 0) { // If r_visibility is called before the renderer is all set up, don't // divide by zero. This will be called again later, and the proper // values can be initialized then. @@ -114,9 +116,9 @@ namespace swrenderer BaseVisibility = vis; - MaxVisForWall = (InvZtoScale * (SCREENWIDTH*r_Yaspect) / (viewwidth*SCREENHEIGHT * FocalTangent)); + MaxVisForWall = (viewport->InvZtoScale * (SCREENWIDTH*r_Yaspect) / (viewwidth*SCREENHEIGHT * FocalTangent)); MaxVisForWall = 32767.0 / MaxVisForWall; - MaxVisForFloor = 32767.0 / (viewheight >> 2) * FocalLengthY / 160; + MaxVisForFloor = 32767.0 / (viewheight >> 2) * viewport->FocalLengthY / 160; // Prevent overflow on walls if (BaseVisibility < 0 && BaseVisibility < -MaxVisForWall) @@ -126,7 +128,7 @@ namespace swrenderer else WallVisibility = BaseVisibility; - WallVisibility = (InvZtoScale * SCREENWIDTH*AspectBaseHeight(WidescreenRatio) / + WallVisibility = (viewport->InvZtoScale * SCREENWIDTH*AspectBaseHeight(WidescreenRatio) / (viewwidth*SCREENHEIGHT * 3)) * (WallVisibility * FocalTangent); // Prevent overflow on floors/ceilings. Note that the calculation of @@ -140,7 +142,7 @@ namespace swrenderer else FloorVisibility = BaseVisibility; - FloorVisibility = 160.0 * FloorVisibility / FocalLengthY; + FloorVisibility = 160.0 * FloorVisibility / viewport->FocalLengthY; TiltVisibility = float(vis * FocalTangent * (16.f * 320.f) / viewwidth); } diff --git a/src/swrenderer/scene/r_light.h b/src/swrenderer/scene/r_light.h index cebb34e9e..099700980 100644 --- a/src/swrenderer/scene/r_light.h +++ b/src/swrenderer/scene/r_light.h @@ -84,7 +84,7 @@ namespace swrenderer double WallVis(double screenZ) const { return WallGlobVis() / screenZ; } double SpriteVis(double screenZ) const { return WallGlobVis() / screenZ; } double ParticleVis(double screenZ) const { return WallGlobVis() / screenZ; } - double FlatPlaneVis(int screenY, double planeZ) const { return FlatPlaneGlobVis() / fabs(planeZ - ViewPos.Z) * fabs(CenterY - screenY); } + double FlatPlaneVis(int screenY, double planeZ) const { return FlatPlaneGlobVis() / fabs(planeZ - ViewPos.Z) * fabs(RenderViewport::Instance()->CenterY - screenY); } private: double BaseVisibility = 0.0; diff --git a/src/swrenderer/scene/r_opaque_pass.cpp b/src/swrenderer/scene/r_opaque_pass.cpp index 2391d0542..a1c5caafc 100644 --- a/src/swrenderer/scene/r_opaque_pass.cpp +++ b/src/swrenderer/scene/r_opaque_pass.cpp @@ -348,12 +348,14 @@ namespace swrenderer rx2 = t; swapvalues(ry1, ry2); } + + auto viewport = RenderViewport::Instance(); if (rx1 >= -ry1) { if (rx1 > ry1) return false; // left edge is off the right side if (ry1 == 0) return false; - sx1 = xs_RoundToInt(CenterX + rx1 * CenterX / ry1); + sx1 = xs_RoundToInt(viewport->CenterX + rx1 * viewport->CenterX / ry1); } else { @@ -366,7 +368,7 @@ namespace swrenderer { if (rx2 < -ry2) return false; // right edge is off the left side if (ry2 == 0) return false; - sx2 = xs_RoundToInt(CenterX + rx2 * CenterX / ry2); + sx2 = xs_RoundToInt(viewport->CenterX + rx2 * viewport->CenterX / ry2); } else { @@ -810,9 +812,10 @@ namespace swrenderer void RenderOpaquePass::ClearClip() { + auto viewport = RenderViewport::Instance(); // clip ceiling to console bottom fillshort(floorclip, viewwidth, viewheight); - fillshort(ceilingclip, viewwidth, !screen->Accel2D && ConBottom > viewwindowy && !bRenderingToCanvas ? (ConBottom - viewwindowy) : 0); + fillshort(ceilingclip, viewwidth, !screen->Accel2D && ConBottom > viewwindowy && !viewport->bRenderingToCanvas ? (ConBottom - viewwindowy) : 0); } void RenderOpaquePass::AddSprites(sector_t *sec, int lightlevel, WaterFakeSide fakeside, bool foggy, FDynamicColormap *basecolormap) diff --git a/src/swrenderer/scene/r_portal.cpp b/src/swrenderer/scene/r_portal.cpp index be18ff59f..be45c1a01 100644 --- a/src/swrenderer/scene/r_portal.cpp +++ b/src/swrenderer/scene/r_portal.cpp @@ -289,22 +289,24 @@ namespace swrenderer void RenderPortal::RenderLinePortal(PortalDrawseg* pds, int depth) { + auto viewport = RenderViewport::Instance(); + // [ZZ] check depth. fill portal with black if it's exceeding the visual recursion limit, and continue like nothing happened. if (depth >= r_portal_recursions) { BYTE color = (BYTE)BestColor((DWORD *)GPalette.BaseColors, 0, 0, 0, 0, 255); - int spacing = RenderTarget->GetPitch(); + int spacing = viewport->RenderTarget->GetPitch(); for (int x = pds->x1; x < pds->x2; x++) { - if (x < 0 || x >= RenderTarget->GetWidth()) + if (x < 0 || x >= viewport->RenderTarget->GetWidth()) continue; int Ytop = pds->ceilingclip[x - pds->x1]; int Ybottom = pds->floorclip[x - pds->x1]; - if (r_swtruecolor) + if (viewport->r_swtruecolor) { - uint32_t *dest = (uint32_t*)RenderTarget->GetBuffer() + x + Ytop * spacing; + uint32_t *dest = (uint32_t*)viewport->RenderTarget->GetBuffer() + x + Ytop * spacing; uint32_t c = GPalette.BaseColors[color].d; for (int y = Ytop; y <= Ybottom; y++) @@ -315,7 +317,7 @@ namespace swrenderer } else { - BYTE *dest = RenderTarget->GetBuffer() + x + Ytop * spacing; + BYTE *dest = viewport->RenderTarget->GetBuffer() + x + Ytop * spacing; for (int y = Ytop; y <= Ybottom; y++) { @@ -483,16 +485,18 @@ namespace swrenderer // [ZZ] NO OVERFLOW CHECKS HERE // I believe it won't break. if it does, blame me. :( - if (r_swtruecolor) // Assuming this is just a debug function + auto viewport = RenderViewport::Instance(); + + if (viewport->r_swtruecolor) // Assuming this is just a debug function return; BYTE color = (BYTE)BestColor((DWORD *)GPalette.BaseColors, 255, 0, 0, 0, 255); - BYTE* pixels = RenderTarget->GetBuffer(); + BYTE* pixels = viewport->RenderTarget->GetBuffer(); // top edge for (int x = pds->x1; x < pds->x2; x++) { - if (x < 0 || x >= RenderTarget->GetWidth()) + if (x < 0 || x >= viewport->RenderTarget->GetWidth()) continue; int p = x - pds->x1; @@ -501,7 +505,7 @@ namespace swrenderer if (x == pds->x1 || x == pds->x2 - 1) { - RenderTarget->DrawLine(x, Ytop, x, Ybottom + 1, color, 0); + viewport->RenderTarget->DrawLine(x, Ytop, x, Ybottom + 1, color, 0); continue; } @@ -509,12 +513,12 @@ namespace swrenderer int YbottomPrev = pds->floorclip[p - 1]; if (abs(Ytop - YtopPrev) > 1) - RenderTarget->DrawLine(x, YtopPrev, x, Ytop, color, 0); - else *(pixels + Ytop * RenderTarget->GetPitch() + x) = color; + viewport->RenderTarget->DrawLine(x, YtopPrev, x, Ytop, color, 0); + else *(pixels + Ytop * viewport->RenderTarget->GetPitch() + x) = color; if (abs(Ybottom - YbottomPrev) > 1) - RenderTarget->DrawLine(x, YbottomPrev, x, Ybottom, color, 0); - else *(pixels + Ybottom * RenderTarget->GetPitch() + x) = color; + viewport->RenderTarget->DrawLine(x, YbottomPrev, x, Ybottom, color, 0); + else *(pixels + Ybottom * viewport->RenderTarget->GetPitch() + x) = color; } } diff --git a/src/swrenderer/scene/r_scene.cpp b/src/swrenderer/scene/r_scene.cpp index 24e04d5e8..63c314707 100644 --- a/src/swrenderer/scene/r_scene.cpp +++ b/src/swrenderer/scene/r_scene.cpp @@ -68,30 +68,31 @@ namespace swrenderer void RenderScene::RenderView(player_t *player) { - RenderTarget = screen; + auto viewport = RenderViewport::Instance(); + viewport->RenderTarget = screen; int width = SCREENWIDTH; int height = SCREENHEIGHT; float trueratio; ActiveRatio(width, height, &trueratio); - RenderViewport::Instance()->SetViewport(width, height, trueratio); + viewport->SetViewport(width, height, trueratio); - if (r_swtruecolor != screen->IsBgra()) + if (viewport->r_swtruecolor != screen->IsBgra()) { - r_swtruecolor = screen->IsBgra(); + viewport->r_swtruecolor = screen->IsBgra(); } if (r_clearbuffer != 0) { - if (!r_swtruecolor) + if (!viewport->r_swtruecolor) { - memset(RenderTarget->GetBuffer(), clearcolor, RenderTarget->GetPitch() * RenderTarget->GetHeight()); + memset(viewport->RenderTarget->GetBuffer(), clearcolor, viewport->RenderTarget->GetPitch() * viewport->RenderTarget->GetHeight()); } else { uint32_t bgracolor = GPalette.BaseColors[clearcolor].d; - int size = RenderTarget->GetPitch() * RenderTarget->GetHeight(); - uint32_t *dest = (uint32_t *)RenderTarget->GetBuffer(); + int size = viewport->RenderTarget->GetPitch() * viewport->RenderTarget->GetHeight(); + uint32_t *dest = (uint32_t *)viewport->RenderTarget->GetBuffer(); for (int i = 0; i < size; i++) dest[i] = bgracolor; } @@ -102,7 +103,7 @@ namespace swrenderer RenderActorView(player->mo); // Apply special colormap if the target cannot do it - if (CameraLight::Instance()->realfixedcolormap && r_swtruecolor && !(r_shadercolormaps && screen->Accel2D)) + if (CameraLight::Instance()->realfixedcolormap && viewport->r_swtruecolor && !(r_shadercolormaps && screen->Accel2D)) { DrawerCommandQueue::QueueCommand(CameraLight::Instance()->realfixedcolormap, screen); } @@ -187,7 +188,7 @@ namespace swrenderer // If we don't want shadered colormaps, NULL it now so that the // copy to the screen does not use a special colormap shader. - if (!r_shadercolormaps && !r_swtruecolor) + if (!r_shadercolormaps && !RenderViewport::Instance()->r_swtruecolor) { CameraLight::Instance()->realfixedcolormap = NULL; } @@ -195,53 +196,56 @@ namespace swrenderer void RenderScene::RenderViewToCanvas(AActor *actor, DCanvas *canvas, int x, int y, int width, int height, bool dontmaplines) { + auto viewport = RenderViewport::Instance(); + const bool savedviewactive = viewactive; - const bool savedoutputformat = r_swtruecolor; + const bool savedoutputformat = viewport->r_swtruecolor; - if (r_swtruecolor != canvas->IsBgra()) + if (viewport->r_swtruecolor != canvas->IsBgra()) { - r_swtruecolor = canvas->IsBgra(); + viewport->r_swtruecolor = canvas->IsBgra(); } R_BeginDrawerCommands(); viewwidth = width; - RenderTarget = canvas; - bRenderingToCanvas = true; + viewport->RenderTarget = canvas; + viewport->bRenderingToCanvas = true; R_SetWindow(12, width, height, height, true); viewwindowx = x; viewwindowy = y; viewactive = true; - RenderViewport::Instance()->SetViewport(width, height, WidescreenRatio); + viewport->SetViewport(width, height, WidescreenRatio); RenderActorView(actor, dontmaplines); R_EndDrawerCommands(); - RenderTarget = screen; - bRenderingToCanvas = false; + viewport->RenderTarget = screen; + viewport->bRenderingToCanvas = false; R_ExecuteSetViewSize(); float trueratio; ActiveRatio(width, height, &trueratio); screen->Lock(true); - RenderViewport::Instance()->SetViewport(width, height, trueratio); + viewport->SetViewport(width, height, trueratio); screen->Unlock(); viewactive = savedviewactive; - r_swtruecolor = savedoutputformat; + viewport->r_swtruecolor = savedoutputformat; } void RenderScene::ScreenResized() { - RenderTarget = screen; + auto viewport = RenderViewport::Instance(); + viewport->RenderTarget = screen; int width = SCREENWIDTH; int height = SCREENHEIGHT; float trueratio; ActiveRatio(width, height, &trueratio); screen->Lock(true); - RenderViewport::Instance()->SetViewport(SCREENWIDTH, SCREENHEIGHT, trueratio); + viewport->SetViewport(SCREENWIDTH, SCREENHEIGHT, trueratio); screen->Unlock(); } diff --git a/src/swrenderer/scene/r_viewport.cpp b/src/swrenderer/scene/r_viewport.cpp index dc4a246b8..c2d50839e 100644 --- a/src/swrenderer/scene/r_viewport.cpp +++ b/src/swrenderer/scene/r_viewport.cpp @@ -38,32 +38,6 @@ CVAR(String, r_viewsize, "", CVAR_NOSET) namespace swrenderer { - bool r_swtruecolor; - - fixed_t viewingrangerecip; - double FocalLengthX; - double FocalLengthY; - - DCanvas *RenderTarget; - bool bRenderingToCanvas; - double globaluclip, globaldclip; - double CenterX, CenterY; - double YaspectMul; - double BaseYaspectMul; // yaspectmul without a forced aspect ratio - double IYaspectMul; - double InvZtoScale; - - double WallTMapScale2; - - uint8_t *dc_destorg; - int dc_destheight; - int dc_pitch; - - // The xtoviewangleangle[] table maps a screen pixel - // to the lowest viewangle that maps back to x ranges - // from clipangle to -clipangle. - angle_t xtoviewangle[MAXWIDTH + 1]; - RenderViewport *RenderViewport::Instance() { static RenderViewport instance; diff --git a/src/swrenderer/scene/r_viewport.h b/src/swrenderer/scene/r_viewport.h index c036adb5a..f3d945bbb 100644 --- a/src/swrenderer/scene/r_viewport.h +++ b/src/swrenderer/scene/r_viewport.h @@ -18,26 +18,6 @@ namespace swrenderer { - extern DCanvas *RenderTarget; - extern bool bRenderingToCanvas; - extern fixed_t viewingrangerecip; - extern double FocalLengthX; - extern double FocalLengthY; - extern double InvZtoScale; - extern double WallTMapScale2; - extern double CenterX; - extern double CenterY; - extern double YaspectMul; - extern double IYaspectMul; - extern bool r_swtruecolor; - extern double globaluclip; - extern double globaldclip; - extern angle_t xtoviewangle[MAXWIDTH + 1]; - - extern uint8_t *dc_destorg; - extern int dc_destheight; - extern int dc_pitch; - class RenderViewport { public: @@ -45,9 +25,35 @@ namespace swrenderer void SetViewport(int width, int height, float trueratio); void SetupFreelook(); + + DCanvas *RenderTarget = nullptr; + bool bRenderingToCanvas = false; + fixed_t viewingrangerecip = 0; + double FocalLengthX = 0.0; + double FocalLengthY = 0.0; + double InvZtoScale = 0.0; + double WallTMapScale2 = 0.0; + double CenterX = 0.0; + double CenterY = 0.0; + double YaspectMul = 0.0; + double IYaspectMul = 0.0; + bool r_swtruecolor = false; + double globaluclip = 0.0; + double globaldclip = 0.0; + + // The xtoviewangleangle[] table maps a screen pixel + // to the lowest viewangle that maps back to x ranges + // from clipangle to -clipangle. + angle_t xtoviewangle[MAXWIDTH + 1]; + + uint8_t *dc_destorg = nullptr; + int dc_destheight = 0; + int dc_pitch = 0; private: void InitTextureMapping(); void SetupBuffer(); + + double BaseYaspectMul = 0.0; // yaspectmul without a forced aspect ratio }; } diff --git a/src/swrenderer/things/r_decal.cpp b/src/swrenderer/things/r_decal.cpp index 9653a91fa..522448b20 100644 --- a/src/swrenderer/things/r_decal.cpp +++ b/src/swrenderer/things/r_decal.cpp @@ -317,13 +317,15 @@ namespace swrenderer void RenderDecal::DrawColumn(SpriteDrawerArgs &drawerargs, int x, FTexture *WallSpriteTile, const ProjectedWallTexcoords &walltexcoords, double texturemid, float maskedScaleY, bool sprflipvert, const short *mfloorclip, const short *mceilingclip) { + auto viewport = RenderViewport::Instance(); + float iscale = walltexcoords.VStep[x] * maskedScaleY; double spryscale = 1 / iscale; double sprtopscreen; if (sprflipvert) - sprtopscreen = CenterY + texturemid * spryscale; + sprtopscreen = viewport->CenterY + texturemid * spryscale; else - sprtopscreen = CenterY - texturemid * spryscale; + sprtopscreen = viewport->CenterY - texturemid * spryscale; drawerargs.DrawMaskedColumn(x, FLOAT2FIXED(iscale), WallSpriteTile, walltexcoords.UPos[x], spryscale, sprtopscreen, sprflipvert, mfloorclip, mceilingclip); } diff --git a/src/swrenderer/things/r_particle.cpp b/src/swrenderer/things/r_particle.cpp index 461964314..72d3ce617 100644 --- a/src/swrenderer/things/r_particle.cpp +++ b/src/swrenderer/things/r_particle.cpp @@ -108,11 +108,13 @@ namespace swrenderer if (x1 >= x2) return; + + auto viewport = RenderViewport::Instance(); yscale = xscale; // YaspectMul is not needed for particles as they should always be square ty = particle->Pos.Z - ViewPos.Z; - y1 = xs_RoundToInt(CenterY - (ty + psize) * yscale); - y2 = xs_RoundToInt(CenterY - (ty - psize) * yscale); + y1 = xs_RoundToInt(viewport->CenterY - (ty + psize) * yscale); + y2 = xs_RoundToInt(viewport->CenterY - (ty - psize) * yscale); // Clip the particle now. Because it's a point and projected as its subsector is // entered, we don't need to clip it to drawsegs like a normal sprite. @@ -227,21 +229,23 @@ namespace swrenderer // vis->renderflags holds translucency level (0-255) fixed_t fglevel = ((vis->renderflags + 1) << 8) & ~0x3ff; uint32_t alpha = fglevel * 256 / FRACUNIT; + + auto viewport = RenderViewport::Instance(); - spacing = RenderTarget->GetPitch(); + spacing = viewport->RenderTarget->GetPitch(); uint32_t fracstepx = PARTICLE_TEXTURE_SIZE * FRACUNIT / countbase; uint32_t fracposx = fracstepx / 2; RenderTranslucentPass *translucentPass = RenderTranslucentPass::Instance(); - if (r_swtruecolor) + if (viewport->r_swtruecolor) { for (int x = x1; x < (x1 + countbase); x++, fracposx += fracstepx) { if (translucentPass->ClipSpriteColumnWithPortals(x, vis)) continue; - uint32_t *dest = ylookup[yl] + x + (uint32_t*)dc_destorg; + uint32_t *dest = ylookup[yl] + x + (uint32_t*)viewport->dc_destorg; DrawerCommandQueue::QueueCommand(dest, yl, spacing, ycount, fg, alpha, fracposx); } } @@ -251,7 +255,7 @@ namespace swrenderer { if (translucentPass->ClipSpriteColumnWithPortals(x, vis)) continue; - uint8_t *dest = ylookup[yl] + x + dc_destorg; + uint8_t *dest = ylookup[yl] + x + viewport->dc_destorg; DrawerCommandQueue::QueueCommand(dest, yl, spacing, ycount, fg, alpha, fracposx); } } diff --git a/src/swrenderer/things/r_playersprite.cpp b/src/swrenderer/things/r_playersprite.cpp index 938c45900..44a82eb22 100644 --- a/src/swrenderer/things/r_playersprite.cpp +++ b/src/swrenderer/things/r_playersprite.cpp @@ -136,11 +136,13 @@ namespace swrenderer if (camera->player != NULL) { - double centerhack = CenterY; + auto viewport = RenderViewport::Instance(); + + double centerhack = viewport->CenterY; double wx, wy; float bobx, boby; - CenterY = viewheight / 2; + viewport->CenterY = viewheight / 2; P_BobWeapon(camera->player, &bobx, &boby, r_TicFracF); @@ -181,7 +183,7 @@ namespace swrenderer psp = psp->GetNext(); } - CenterY = centerhack; + viewport->CenterY = centerhack; } } @@ -241,23 +243,25 @@ namespace swrenderer sx += wx; sy += wy; } + + auto viewport = RenderViewport::Instance(); double pspritexscale = centerxwide / 160.0; - double pspriteyscale = pspritexscale * YaspectMul; + double pspriteyscale = pspritexscale * viewport->YaspectMul; double pspritexiscale = 1 / pspritexscale; // calculate edges of the shape tx = sx - BASEXCENTER; tx -= tex->GetScaledLeftOffset(); - x1 = xs_RoundToInt(CenterX + tx * pspritexscale); + x1 = xs_RoundToInt(viewport->CenterX + tx * pspritexscale); // off the right side if (x1 > viewwidth) return; tx += tex->GetScaledWidth(); - x2 = xs_RoundToInt(CenterX + tx * pspritexscale); + x2 = xs_RoundToInt(viewport->CenterX + tx * pspritexscale); // off the left side if (x2 <= 0) @@ -270,14 +274,14 @@ namespace swrenderer vis.texturemid = (BASEYCENTER - sy) * tex->Scale.Y + tex->TopOffset; - if (camera->player && (RenderTarget != screen || - viewheight == RenderTarget->GetHeight() || - (RenderTarget->GetWidth() > (BASEXCENTER * 2) && !st_scale))) + if (camera->player && (viewport->RenderTarget != screen || + viewheight == viewport->RenderTarget->GetHeight() || + (viewport->RenderTarget->GetWidth() > (BASEXCENTER * 2) && !st_scale))) { // Adjust PSprite for fullscreen views AWeapon *weapon = dyn_cast(pspr->GetCaller()); if (weapon != nullptr && weapon->YAdjust != 0) { - if (RenderTarget != screen || viewheight == RenderTarget->GetHeight()) + if (viewport->RenderTarget != screen || viewheight == viewport->RenderTarget->GetHeight()) { vis.texturemid -= weapon->YAdjust; } @@ -499,7 +503,7 @@ namespace swrenderer // Check for hardware-assisted 2D. If it's available, and this sprite is not // fuzzy, don't draw it until after the switch to 2D mode. - if (!noaccel && RenderTarget == screen && (DFrameBuffer *)screen->Accel2D) + if (!noaccel && viewport->RenderTarget == screen && (DFrameBuffer *)screen->Accel2D) { FRenderStyle style = vis.RenderStyle; style.CheckFuzz(); @@ -599,6 +603,8 @@ namespace swrenderer double spryscale = yscale; bool sprflipvert = false; fixed_t iscale = FLOAT2FIXED(1 / yscale); + + auto viewport = RenderViewport::Instance(); double sprtopscreen; if (renderflags & RF_YFLIP) @@ -606,12 +612,12 @@ namespace swrenderer sprflipvert = true; spryscale = -spryscale; iscale = -iscale; - sprtopscreen = CenterY + (texturemid - pic->GetHeight()) * spryscale; + sprtopscreen = viewport->CenterY + (texturemid - pic->GetHeight()) * spryscale; } else { sprflipvert = false; - sprtopscreen = CenterY - texturemid * spryscale; + sprtopscreen = viewport->CenterY - texturemid * spryscale; } // clip to screen bounds diff --git a/src/swrenderer/things/r_sprite.cpp b/src/swrenderer/things/r_sprite.cpp index f3df8d2a4..fd4fefc9b 100644 --- a/src/swrenderer/things/r_sprite.cpp +++ b/src/swrenderer/things/r_sprite.cpp @@ -120,11 +120,13 @@ namespace swrenderer return; } } + + auto viewport = RenderViewport::Instance(); - double xscale = CenterX / tz; + double xscale = viewport->CenterX / tz; // [RH] Reject sprites that are off the top or bottom of the screen - if (globaluclip * tz > ViewPos.Z - gzb || globaldclip * tz < ViewPos.Z - gzt) + if (viewport->globaluclip * tz > ViewPos.Z - gzb || viewport->globaldclip * tz < ViewPos.Z - gzt) { return; } @@ -160,7 +162,7 @@ namespace swrenderer vis->CurrentPortalUniq = renderportal->CurrentPortalUniq; vis->xscale = FLOAT2FIXED(xscale); - vis->yscale = float(InvZtoScale * yscale / tz); + vis->yscale = float(viewport->InvZtoScale * yscale / tz); vis->idepth = float(1 / tz); vis->floorclip = thing->Floorclip / yscale; vis->texturemid = tex->TopOffset - (ViewPos.Z - pos.Z + thing->Floorclip) / yscale; @@ -262,18 +264,20 @@ namespace swrenderer xiscale = vis->xiscale; double texturemid = vis->texturemid; + auto viewport = RenderViewport::Instance(); + if (vis->renderflags & RF_YFLIP) { sprflipvert = true; spryscale = -spryscale; iscale = -iscale; texturemid -= vis->pic->GetHeight(); - sprtopscreen = CenterY + texturemid * spryscale; + sprtopscreen = viewport->CenterY + texturemid * spryscale; } else { sprflipvert = false; - sprtopscreen = CenterY - texturemid * spryscale; + sprtopscreen = viewport->CenterY - texturemid * spryscale; } int x = vis->x1; diff --git a/src/swrenderer/things/r_visiblesprite.cpp b/src/swrenderer/things/r_visiblesprite.cpp index b2c0b054a..1b5a3b447 100644 --- a/src/swrenderer/things/r_visiblesprite.cpp +++ b/src/swrenderer/things/r_visiblesprite.cpp @@ -149,8 +149,10 @@ namespace swrenderer // killough 3/27/98: // Clip the sprite against deep water and/or fake ceilings. // [RH] rewrote this to be based on which part of the sector is really visible + + auto viewport = RenderViewport::Instance(); - double scale = InvZtoScale * spr->idepth; + double scale = viewport->InvZtoScale * spr->idepth; double hzb = DBL_MIN, hzt = DBL_MAX; if (spr->IsVoxel() && spr->floorclip != 0) @@ -163,7 +165,7 @@ namespace swrenderer if (spr->FakeFlatStat != WaterFakeSide::AboveCeiling) { double hz = spr->heightsec->floorplane.ZatPoint(spr->gpos); - int h = xs_RoundToInt(CenterY - (hz - ViewPos.Z) * scale); + int h = xs_RoundToInt(viewport->CenterY - (hz - ViewPos.Z) * scale); if (spr->FakeFlatStat == WaterFakeSide::BelowFloor) { // seen below floor: clip top @@ -185,7 +187,7 @@ namespace swrenderer if (spr->FakeFlatStat != WaterFakeSide::BelowFloor && !(spr->heightsec->MoreFlags & SECF_FAKEFLOORONLY)) { double hz = spr->heightsec->ceilingplane.ZatPoint(spr->gpos); - int h = xs_RoundToInt(CenterY - (hz - ViewPos.Z) * scale); + int h = xs_RoundToInt(viewport->CenterY - (hz - ViewPos.Z) * scale); if (spr->FakeFlatStat == WaterFakeSide::AboveCeiling) { // seen above ceiling: clip bottom @@ -209,7 +211,7 @@ namespace swrenderer else if (!spr->IsVoxel() && spr->floorclip) { // [RH] Move floorclip stuff from R_DrawVisSprite to here //int clip = ((FLOAT2FIXED(CenterY) - FixedMul (spr->texturemid - (spr->pic->GetHeight() << FRACBITS) + spr->floorclip, spr->yscale)) >> FRACBITS); - int clip = xs_RoundToInt(CenterY - (spr->texturemid - spr->pic->GetHeight() + spr->floorclip) * spr->yscale); + int clip = xs_RoundToInt(viewport->CenterY - (spr->texturemid - spr->pic->GetHeight() + spr->floorclip) * spr->yscale); if (clip < botclip) { botclip = MAX(0, clip); @@ -229,7 +231,7 @@ namespace swrenderer hz = spr->fakefloor->bottom.plane->Zat0(); } } - int h = xs_RoundToInt(CenterY - (hz - ViewPos.Z) * scale); + int h = xs_RoundToInt(viewport->CenterY - (hz - ViewPos.Z) * scale); if (h < botclip) { botclip = MAX(0, h); @@ -250,7 +252,7 @@ namespace swrenderer hz = spr->fakeceiling->top.plane->Zat0(); } } - int h = xs_RoundToInt(CenterY - (hz - ViewPos.Z) * scale); + int h = xs_RoundToInt(viewport->CenterY - (hz - ViewPos.Z) * scale); if (h > topclip) { topclip = short(MIN(h, viewheight)); diff --git a/src/swrenderer/things/r_voxel.cpp b/src/swrenderer/things/r_voxel.cpp index 0d35a1768..f513b59ee 100644 --- a/src/swrenderer/things/r_voxel.cpp +++ b/src/swrenderer/things/r_voxel.cpp @@ -184,6 +184,7 @@ namespace swrenderer void RenderVoxel::Render(short *cliptop, short *clipbottom, int minZ, int maxZ) { auto sprite = this; + auto viewport = RenderViewport::Instance(); FDynamicColormap *basecolormap = static_cast(sprite->Light.BaseColormap); @@ -208,7 +209,7 @@ namespace swrenderer double viewCos = view_angle.Sin(); double logmip = fabs((view_origin.X - sprite_origin.X) * viewCos - (view_origin.Y - sprite_origin.Y) * viewSin); int miplevel = 0; - while (miplevel < voxel->NumMips - 1 && logmip >= FocalLengthX) + while (miplevel < voxel->NumMips - 1 && logmip >= viewport->FocalLengthX) { logmip *= 0.5; miplevel++; @@ -313,6 +314,8 @@ namespace swrenderer void RenderVoxel::FillBox(SpriteDrawerArgs &drawerargs, DVector3 origin, double extentX, double extentY, int color, short *cliptop, short *clipbottom, bool viewspace, bool pixelstretch) { + auto viewport = RenderViewport::Instance(); + double viewX, viewY, viewZ; if (viewspace) { @@ -333,17 +336,17 @@ namespace swrenderer if (viewZ < 0.01f) return; - double screenX = CenterX + viewX / viewZ * CenterX; - double screenY = CenterY - viewY / viewZ * InvZtoScale; - double screenExtentX = extentX / viewZ * CenterX; - double screenExtentY = pixelstretch ? screenExtentX * YaspectMul : screenExtentX; + double screenX = viewport->CenterX + viewX / viewZ * viewport->CenterX; + double screenY = viewport->CenterY - viewY / viewZ * viewport->InvZtoScale; + double screenExtentX = extentX / viewZ * viewport->CenterX; + double screenExtentY = pixelstretch ? screenExtentX * viewport->YaspectMul : screenExtentX; int x1 = MAX((int)(screenX - screenExtentX), 0); int x2 = MIN((int)(screenX + screenExtentX + 0.5f), viewwidth - 1); int y1 = MAX((int)(screenY - screenExtentY), 0); int y2 = MIN((int)(screenY + screenExtentY + 0.5f), viewheight - 1); - int pixelsize = r_swtruecolor ? 4 : 1; + int pixelsize = viewport->r_swtruecolor ? 4 : 1; if (y1 < y2) { diff --git a/src/swrenderer/things/r_wallsprite.cpp b/src/swrenderer/things/r_wallsprite.cpp index 25e33d9ce..f4cb600d6 100644 --- a/src/swrenderer/things/r_wallsprite.cpp +++ b/src/swrenderer/things/r_wallsprite.cpp @@ -247,13 +247,15 @@ namespace swrenderer void RenderWallSprite::DrawColumn(SpriteDrawerArgs &drawerargs, int x, FTexture *WallSpriteTile, const ProjectedWallTexcoords &walltexcoords, double texturemid, float maskedScaleY, bool sprflipvert, const short *mfloorclip, const short *mceilingclip) { + auto viewport = RenderViewport::Instance(); + float iscale = walltexcoords.VStep[x] * maskedScaleY; double spryscale = 1 / iscale; double sprtopscreen; if (sprflipvert) - sprtopscreen = CenterY + texturemid * spryscale; + sprtopscreen = viewport->CenterY + texturemid * spryscale; else - sprtopscreen = CenterY - texturemid * spryscale; + sprtopscreen = viewport->CenterY - texturemid * spryscale; drawerargs.DrawMaskedColumn(x, FLOAT2FIXED(iscale), WallSpriteTile, walltexcoords.UPos[x], spryscale, sprtopscreen, sprflipvert, mfloorclip, mceilingclip); } diff --git a/src/v_draw.cpp b/src/v_draw.cpp index 574fb09dd..f190d2cc2 100644 --- a/src/v_draw.cpp +++ b/src/v_draw.cpp @@ -147,7 +147,9 @@ void DCanvas::DrawTextureParms(FTexture *img, DrawParms &parms) static short bottomclipper[MAXWIDTH], topclipper[MAXWIDTH]; const BYTE *translation = NULL; - r_swtruecolor = IsBgra(); + auto viewport = RenderViewport::Instance(); + + viewport->r_swtruecolor = IsBgra(); if (APART(parms.colorOverlay) != 0) { @@ -165,7 +167,7 @@ void DCanvas::DrawTextureParms(FTexture *img, DrawParms &parms) parms.colorOverlay = PalEntry(parms.colorOverlay).InverseColor(); } // Note that this overrides DTA_Translation in software, but not in hardware. - if (!r_swtruecolor) + if (!viewport->r_swtruecolor) { FDynamicColormap *colormap = GetSpecialLights(MAKERGB(255, 255, 255), parms.colorOverlay & MAKEARGB(0, 255, 255, 255), 0); @@ -174,7 +176,7 @@ void DCanvas::DrawTextureParms(FTexture *img, DrawParms &parms) } else if (parms.remap != NULL) { - if (r_swtruecolor) + if (viewport->r_swtruecolor) translation = (const BYTE*)parms.remap->Palette; else translation = parms.remap->Remap; @@ -188,7 +190,7 @@ void DCanvas::DrawTextureParms(FTexture *img, DrawParms &parms) } else { - if (r_swtruecolor) + if (viewport->r_swtruecolor) drawerargs.SetTranslationMap(nullptr); else drawerargs.SetTranslationMap(identitymap); @@ -196,16 +198,16 @@ void DCanvas::DrawTextureParms(FTexture *img, DrawParms &parms) bool visible; FDynamicColormap *basecolormap = nullptr; - if (r_swtruecolor) + if (viewport->r_swtruecolor) visible = drawerargs.SetPatchStyle(parms.style, parms.Alpha, -1, parms.fillcolor, basecolormap); else visible = drawerargs.SetPatchStyle(parms.style, parms.Alpha, 0, parms.fillcolor, basecolormap); - - BYTE *destorgsave = dc_destorg; - int destheightsave = dc_destheight; - dc_destorg = screen->GetBuffer(); - dc_destheight = screen->GetHeight(); - if (dc_destorg == NULL) + + BYTE *destorgsave = viewport->dc_destorg; + int destheightsave = viewport->dc_destheight; + viewport->dc_destorg = screen->GetBuffer(); + viewport->dc_destheight = screen->GetHeight(); + if (viewport->dc_destorg == NULL) { I_FatalError("Attempt to write to buffer of hardware canvas"); } @@ -215,8 +217,8 @@ void DCanvas::DrawTextureParms(FTexture *img, DrawParms &parms) if (visible) { - double centeryback = CenterY; - CenterY = 0; + double centeryback = viewport->CenterY; + viewport->CenterY = 0; // There is not enough precision in the drawing routines to keep the full // precision for y0. :( @@ -292,11 +294,11 @@ void DCanvas::DrawTextureParms(FTexture *img, DrawParms &parms) frac += xiscale_i; } - CenterY = centeryback; + viewport->CenterY = centeryback; } - dc_destorg = destorgsave; - dc_destheight = destheightsave; + viewport->dc_destorg = destorgsave; + viewport->dc_destheight = destheightsave; if (ticdup != 0 && menuactive == MENU_Off) { @@ -1367,10 +1369,12 @@ void DCanvas::FillSimplePoly(FTexture *tex, FVector2 *points, int npoints, { return; } + + auto viewport = RenderViewport::Instance(); - BYTE *destorgsave = dc_destorg; - dc_destorg = screen->GetBuffer(); - if (dc_destorg == NULL) + BYTE *destorgsave = viewport->dc_destorg; + viewport->dc_destorg = screen->GetBuffer(); + if (viewport->dc_destorg == NULL) { I_FatalError("Attempt to write to buffer of hardware canvas"); } @@ -1497,7 +1501,7 @@ void DCanvas::FillSimplePoly(FTexture *tex, FVector2 *points, int npoints, pt1 = pt2; pt2--; if (pt2 < 0) pt2 = npoints; } while (pt1 != botpt); - dc_destorg = destorgsave; + viewport->dc_destorg = destorgsave; #endif }