diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3768d55dd9..c671c0f3f0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -844,6 +844,7 @@ set( NOT_COMPILED_SOURCE_FILES ) set( FASTMATH_PCH_SOURCES + swrenderer/r_swcanvas.cpp swrenderer/r_swrenderer.cpp swrenderer/r_memory.cpp swrenderer/r_renderthread.cpp diff --git a/src/swrenderer/r_swcanvas.cpp b/src/swrenderer/r_swcanvas.cpp new file mode 100644 index 0000000000..ce089272bd --- /dev/null +++ b/src/swrenderer/r_swcanvas.cpp @@ -0,0 +1,858 @@ +/* +** r_swcanvas.cpp +** Draw to a canvas using software rendering +** +**--------------------------------------------------------------------------- +** Copyright 1998-2008 Randy Heit +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions +** are met: +** +** 1. Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** 2. Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** 3. The name of the author may not be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**--------------------------------------------------------------------------- +** +*/ + +#include "swrenderer/viewport/r_viewport.h" +#include "swrenderer/scene/r_light.h" +#include "swrenderer/r_renderthread.h" +#include "v_palette.h" +#include "v_video.h" +#include "m_png.h" +#include "colormatcher.h" +#include "r_swcanvas.h" +#include "textures/textures.h" +#include "r_data/voxels.h" +#include "drawers/r_draw_rgba.h" +#include "drawers/r_drawers.h" + +EXTERN_CVAR(Bool, r_blendmethod) + +using namespace swrenderer; + +void SWCanvas::DrawTexture(DCanvas *canvas, FTexture *img, DrawParms &parms) +{ + static short bottomclipper[MAXWIDTH], topclipper[MAXWIDTH]; + + auto viewport = RenderViewport::Instance(); + viewport->RenderTarget = canvas; + viewport->RenderTarget->Lock(true); + + lighttable_t *translation = nullptr; + FDynamicColormap *basecolormap = &identitycolormap; + int shade = 0; + + if (APART(parms.colorOverlay) != 0) + { + // The software renderer cannot invert the source without inverting the overlay + // too. That means if the source is inverted, we need to do the reverse of what + // the invert overlay flag says to do. + INTBOOL invertoverlay = (parms.style.Flags & STYLEF_InvertOverlay); + + if (parms.style.Flags & STYLEF_InvertSource) + { + invertoverlay = !invertoverlay; + } + if (invertoverlay) + { + parms.colorOverlay = PalEntry(parms.colorOverlay).InverseColor(); + } + // Note that this overrides the translation in software, but not in hardware. + FDynamicColormap *colormap = GetSpecialLights(MAKERGB(255, 255, 255), parms.colorOverlay & MAKEARGB(0, 255, 255, 255), 0); + + if (viewport->RenderTarget->IsBgra()) + { + basecolormap = colormap; + shade = (APART(parms.colorOverlay)*NUMCOLORMAPS / 255) << FRACBITS; + } + else + { + translation = &colormap->Maps[(APART(parms.colorOverlay)*NUMCOLORMAPS / 255) * 256]; + } + } + else if (parms.remap != NULL) + { + if (viewport->RenderTarget->IsBgra()) + translation = (lighttable_t *)parms.remap->Palette; + else + translation = parms.remap->Remap; + } + + SpriteDrawerArgs drawerargs; + + drawerargs.SetTranslationMap(translation); + drawerargs.SetLight(basecolormap, 0.0f, shade); + bool visible = drawerargs.SetStyle(parms.style, parms.Alpha, -1, parms.fillcolor, basecolormap); + + double x0 = parms.x - parms.left * parms.destwidth / parms.texwidth; + double y0 = parms.y - parms.top * parms.destheight / parms.texheight; + + if (visible) + { + double centeryback = viewport->CenterY; + viewport->CenterY = 0; + + // There is not enough precision in the drawing routines to keep the full + // precision for y0. :( + double sprtopscreen; + modf(y0, &sprtopscreen); + + double yscale = parms.destheight / img->GetHeight(); + double iyscale = 1 / yscale; + + double spryscale = yscale; + assert(spryscale > 0); + + bool sprflipvert = false; + fixed_t iscale = FLOAT2FIXED(1 / spryscale); + //dc_texturemid = (CenterY - 1 - sprtopscreen) * iscale / 65536; + fixed_t frac = 0; + double xiscale = img->GetWidth() / parms.destwidth; + double x2 = x0 + parms.destwidth; + + short *mfloorclip; + short *mceilingclip; + + if (bottomclipper[0] != parms.dclip) + { + fillshort(bottomclipper, screen->GetWidth(), (short)parms.dclip); + } + if (parms.uclip != 0) + { + if (topclipper[0] != parms.uclip) + { + fillshort(topclipper, screen->GetWidth(), (short)parms.uclip); + } + mceilingclip = topclipper; + } + else + { + mceilingclip = zeroarray; + } + mfloorclip = bottomclipper; + + if (parms.flipX) + { + frac = (img->GetWidth() << FRACBITS) - 1; + xiscale = -xiscale; + } + + if (parms.windowleft > 0 || parms.windowright < parms.texwidth) + { + double wi = MIN(parms.windowright, parms.texwidth); + double xscale = parms.destwidth / parms.texwidth; + x0 += parms.windowleft * xscale; + frac += FLOAT2FIXED(parms.windowleft); + x2 -= (parms.texwidth - wi) * xscale; + } + if (x0 < parms.lclip) + { + frac += FLOAT2FIXED((parms.lclip - x0) * xiscale); + x0 = parms.lclip; + } + if (x2 > parms.rclip) + { + x2 = parms.rclip; + } + + int x = int(x0); + int x2_i = int(x2); + fixed_t xiscale_i = FLOAT2FIXED(xiscale); + + static RenderThread thread(nullptr); + thread.DrawQueue->ThreadedRender = false; + while (x < x2_i) + { + drawerargs.DrawMaskedColumn(&thread, x, iscale, img, frac, spryscale, sprtopscreen, sprflipvert, mfloorclip, mceilingclip, !parms.masked); + x++; + frac += xiscale_i; + } + + viewport->CenterY = centeryback; + } + + viewport->RenderTarget->Unlock(); + viewport->RenderTarget = canvas; +} + +void SWCanvas::FillSimplePoly(DCanvas *canvas, FTexture *tex, FVector2 *points, int npoints, + double originx, double originy, double scalex, double scaley, DAngle rotation, + FDynamicColormap *colormap, PalEntry flatcolor, int lightlevel, int bottomclip) +{ + // Use an equation similar to player sprites to determine shade + fixed_t shade = LIGHT2SHADE(lightlevel) - 12 * FRACUNIT; + float topy, boty, leftx, rightx; + int toppt, botpt, pt1, pt2; + int i; + int y1, y2, y; + fixed_t x; + bool dorotate = rotation != 0.; + double cosrot, sinrot; + + if (--npoints < 2) + { // not a polygon or we're not locked + return; + } + + if (bottomclip <= 0) + { + bottomclip = canvas->GetHeight(); + } + + // Find the extents of the polygon, in particular the highest and lowest points. + for (botpt = toppt = 0, boty = topy = points[0].Y, leftx = rightx = points[0].X, i = 1; i <= npoints; ++i) + { + if (points[i].Y < topy) + { + topy = points[i].Y; + toppt = i; + } + if (points[i].Y > boty) + { + boty = points[i].Y; + botpt = i; + } + if (points[i].X < leftx) + { + leftx = points[i].X; + } + if (points[i].X > rightx) + { + rightx = points[i].X; + } + } + if (topy >= bottomclip || // off the bottom of the screen + boty <= 0 || // off the top of the screen + leftx >= canvas->GetWidth() || // off the right of the screen + rightx <= 0) // off the left of the screen + { + return; + } + + auto viewport = RenderViewport::Instance(); + viewport->RenderTarget = canvas; + + viewport->RenderTarget->Lock(true); + + scalex /= tex->Scale.X; + scaley /= tex->Scale.Y; + + // Use the CRT's functions here. + cosrot = cos(rotation.Radians()); + sinrot = sin(rotation.Radians()); + + // Setup constant texture mapping parameters. + SpanDrawerArgs drawerargs; + drawerargs.SetTexture(tex); + if (colormap) + drawerargs.SetLight(colormap, 0, clamp(shade >> FRACBITS, 0, NUMCOLORMAPS - 1)); + else + drawerargs.SetLight(&identitycolormap, 0, 0); + if (drawerargs.TextureWidthBits() != 0) + { + scalex = double(1u << (32 - drawerargs.TextureWidthBits())) / scalex; + drawerargs.SetTextureUStep(xs_RoundToInt(cosrot * scalex)); + } + else + { // Texture is one pixel wide. + scalex = 0; + drawerargs.SetTextureUStep(0); + } + if (drawerargs.TextureHeightBits() != 0) + { + scaley = double(1u << (32 - drawerargs.TextureHeightBits())) / scaley; + drawerargs.SetTextureVStep(xs_RoundToInt(sinrot * scaley)); + } + else + { // Texture is one pixel tall. + scaley = 0; + drawerargs.SetTextureVStep(0); + } + + int width = canvas->GetWidth(); + + // Travel down the right edge and create an outline of that edge. + static short spanend[MAXHEIGHT]; + pt1 = toppt; + pt2 = toppt + 1; if (pt2 > npoints) pt2 = 0; + y1 = xs_RoundToInt(points[pt1].Y + 0.5f); + do + { + x = FLOAT2FIXED(points[pt1].X + 0.5f); + y2 = xs_RoundToInt(points[pt2].Y + 0.5f); + if (y1 >= y2 || (y1 < 0 && y2 < 0) || (y1 >= bottomclip && y2 >= bottomclip)) + { + } + else + { + fixed_t xinc = FLOAT2FIXED((points[pt2].X - points[pt1].X) / (points[pt2].Y - points[pt1].Y)); + int y3 = MIN(y2, bottomclip); + if (y1 < 0) + { + x += xinc * -y1; + y1 = 0; + } + for (y = y1; y < y3; ++y) + { + spanend[y] = clamp(x >> FRACBITS, -1, width); + x += xinc; + } + } + y1 = y2; + pt1 = pt2; + pt2++; if (pt2 > npoints) pt2 = 0; + } while (pt1 != botpt); + + static RenderThread thread(nullptr); + thread.DrawQueue->ThreadedRender = false; + + // Travel down the left edge and fill it in. + pt1 = toppt; + pt2 = toppt - 1; if (pt2 < 0) pt2 = npoints; + y1 = xs_RoundToInt(points[pt1].Y + 0.5f); + do + { + x = FLOAT2FIXED(points[pt1].X + 0.5f); + y2 = xs_RoundToInt(points[pt2].Y + 0.5f); + if (y1 >= y2 || (y1 < 0 && y2 < 0) || (y1 >= bottomclip && y2 >= bottomclip)) + { + } + else + { + fixed_t xinc = FLOAT2FIXED((points[pt2].X - points[pt1].X) / (points[pt2].Y - points[pt1].Y)); + int y3 = MIN(y2, bottomclip); + if (y1 < 0) + { + x += xinc * -y1; + y1 = 0; + } + for (y = y1; y < y3; ++y) + { + int x1 = x >> FRACBITS; + int x2 = spanend[y]; + if (x2 > x1 && x2 > 0 && x1 < width) + { + x1 = MAX(x1, 0); + x2 = MIN(x2, width); +#if 0 + memset(this->Buffer + y * this->Pitch + x1, (int)tex, x2 - x1); +#else + drawerargs.SetDestY(y); + drawerargs.SetDestX1(x1); + drawerargs.SetDestX2(x2 - 1); + + DVector2 tex(x1 - originx, y - originy); + if (dorotate) + { + double t = tex.X; + tex.X = t * cosrot - tex.Y * sinrot; + tex.Y = tex.Y * cosrot + t * sinrot; + } + drawerargs.SetTextureUPos(xs_RoundToInt(tex.X * scalex)); + drawerargs.SetTextureVPos(xs_RoundToInt(tex.Y * scaley)); + + drawerargs.DrawSpan(&thread); +#endif + } + x += xinc; + } + } + y1 = y2; + pt1 = pt2; + pt2--; if (pt2 < 0) pt2 = npoints; + } while (pt1 != botpt); + + viewport->RenderTarget->Unlock(); + viewport->RenderTarget = screen; +} + +void SWCanvas::DrawLine(DCanvas *canvas, int x0, int y0, int x1, int y1, int palColor, uint32 realcolor) +{ + const int WeightingScale = 0; + const int WEIGHTBITS = 6; + const int WEIGHTSHIFT = 16 - WEIGHTBITS; + const int NUMWEIGHTS = (1 << WEIGHTBITS); + const int WEIGHTMASK = (NUMWEIGHTS - 1); + + if (palColor < 0) + { + palColor = PalFromRGB(realcolor); + } + + canvas->Lock(); + int deltaX, deltaY, xDir; + + if (y0 > y1) + { + int temp = y0; y0 = y1; y1 = temp; + temp = x0; x0 = x1; x1 = temp; + } + + PUTTRANSDOT(canvas, x0, y0, palColor, 0); + + if ((deltaX = x1 - x0) >= 0) + { + xDir = 1; + } + else + { + xDir = -1; + deltaX = -deltaX; + } + + if ((deltaY = y1 - y0) == 0) + { // horizontal line + if (x0 > x1) + { + swapvalues(x0, x1); + } + if (canvas->IsBgra()) + { + uint32_t fillColor = GPalette.BaseColors[palColor].d; + uint32_t *spot = (uint32_t*)canvas->GetBuffer() + y0*canvas->GetPitch() + x0; + for (int i = 0; i <= deltaX; i++) + spot[i] = fillColor; + } + else + { + memset(canvas->GetBuffer() + y0*canvas->GetPitch() + x0, palColor, deltaX + 1); + } + } + else if (deltaX == 0) + { // vertical line + if (canvas->IsBgra()) + { + uint32_t fillColor = GPalette.BaseColors[palColor].d; + uint32_t *spot = (uint32_t*)canvas->GetBuffer() + y0*canvas->GetPitch() + x0; + int pitch = canvas->GetPitch(); + do + { + *spot = fillColor; + spot += pitch; + } while (--deltaY != 0); + } + else + { + BYTE *spot = canvas->GetBuffer() + y0*canvas->GetPitch() + x0; + int pitch = canvas->GetPitch(); + do + { + *spot = palColor; + spot += pitch; + } while (--deltaY != 0); + } + } + else if (deltaX == deltaY) + { // diagonal line. + if (canvas->IsBgra()) + { + uint32_t fillColor = GPalette.BaseColors[palColor].d; + uint32_t *spot = (uint32_t*)canvas->GetBuffer() + y0*canvas->GetPitch() + x0; + int advance = canvas->GetPitch() + xDir; + do + { + *spot = fillColor; + spot += advance; + } while (--deltaY != 0); + } + else + { + BYTE *spot = canvas->GetBuffer() + y0*canvas->GetPitch() + x0; + int advance = canvas->GetPitch() + xDir; + do + { + *spot = palColor; + spot += advance; + } while (--deltaY != 0); + } + } + else + { + // line is not horizontal, diagonal, or vertical + fixed_t errorAcc = 0; + + if (deltaY > deltaX) + { // y-major line + fixed_t errorAdj = (((unsigned)deltaX << 16) / (unsigned)deltaY) & 0xffff; + if (xDir < 0) + { + if (WeightingScale == 0) + { + while (--deltaY) + { + errorAcc += errorAdj; + y0++; + int weighting = (errorAcc >> WEIGHTSHIFT) & WEIGHTMASK; + PUTTRANSDOT(canvas, x0 - (errorAcc >> 16), y0, palColor, weighting); + PUTTRANSDOT(canvas, x0 - (errorAcc >> 16) - 1, y0, + palColor, WEIGHTMASK - weighting); + } + } + else + { + while (--deltaY) + { + errorAcc += errorAdj; + y0++; + int weighting = ((errorAcc * WeightingScale) >> (WEIGHTSHIFT + 8)) & WEIGHTMASK; + PUTTRANSDOT(canvas, x0 - (errorAcc >> 16), y0, palColor, weighting); + PUTTRANSDOT(canvas, x0 - (errorAcc >> 16) - 1, y0, + palColor, WEIGHTMASK - weighting); + } + } + } + else + { + if (WeightingScale == 0) + { + while (--deltaY) + { + errorAcc += errorAdj; + y0++; + int weighting = (errorAcc >> WEIGHTSHIFT) & WEIGHTMASK; + PUTTRANSDOT(canvas, x0 + (errorAcc >> 16), y0, palColor, weighting); + PUTTRANSDOT(canvas, x0 + (errorAcc >> 16) + xDir, y0, + palColor, WEIGHTMASK - weighting); + } + } + else + { + while (--deltaY) + { + errorAcc += errorAdj; + y0++; + int weighting = ((errorAcc * WeightingScale) >> (WEIGHTSHIFT + 8)) & WEIGHTMASK; + PUTTRANSDOT(canvas, x0 + (errorAcc >> 16), y0, palColor, weighting); + PUTTRANSDOT(canvas, x0 + (errorAcc >> 16) + xDir, y0, + palColor, WEIGHTMASK - weighting); + } + } + } + } + else + { // x-major line + fixed_t errorAdj = (((DWORD)deltaY << 16) / (DWORD)deltaX) & 0xffff; + + if (WeightingScale == 0) + { + while (--deltaX) + { + errorAcc += errorAdj; + x0 += xDir; + int weighting = (errorAcc >> WEIGHTSHIFT) & WEIGHTMASK; + PUTTRANSDOT(canvas, x0, y0 + (errorAcc >> 16), palColor, weighting); + PUTTRANSDOT(canvas, x0, y0 + (errorAcc >> 16) + 1, + palColor, WEIGHTMASK - weighting); + } + } + else + { + while (--deltaX) + { + errorAcc += errorAdj; + x0 += xDir; + int weighting = ((errorAcc * WeightingScale) >> (WEIGHTSHIFT + 8)) & WEIGHTMASK; + PUTTRANSDOT(canvas, x0, y0 + (errorAcc >> 16), palColor, weighting); + PUTTRANSDOT(canvas, x0, y0 + (errorAcc >> 16) + 1, + palColor, WEIGHTMASK - weighting); + } + } + } + PUTTRANSDOT(canvas, x1, y1, palColor, 0); + } + canvas->Unlock(); +} + +void SWCanvas::DrawPixel(DCanvas *canvas, int x, int y, int palColor, uint32 realcolor) +{ + if (palColor < 0) + { + palColor = PalFromRGB(realcolor); + } + + canvas->GetBuffer()[canvas->GetPitch() * y + x] = (BYTE)palColor; +} + +void SWCanvas::PUTTRANSDOT(DCanvas *canvas, int xx, int yy, int basecolor, int level) +{ + static int oldyy; + static int oldyyshifted; + + if (yy == oldyy + 1) + { + oldyy++; + oldyyshifted += canvas->GetPitch(); + } + else if (yy == oldyy - 1) + { + oldyy--; + oldyyshifted -= canvas->GetPitch(); + } + else if (yy != oldyy) + { + oldyy = yy; + oldyyshifted = yy * canvas->GetPitch(); + } + + if (canvas->IsBgra()) + { + uint32_t *spot = (uint32_t*)canvas->GetBuffer() + oldyyshifted + xx; + + uint32_t fg = swrenderer::LightBgra::shade_pal_index_simple(basecolor, swrenderer::LightBgra::calc_light_multiplier(0)); + uint32_t fg_red = (((fg >> 16) & 0xff) * (63 - level)) >> 6; + uint32_t fg_green = (((fg >> 8) & 0xff) * (63 - level)) >> 6; + uint32_t fg_blue = ((fg & 0xff) * (63 - level)) >> 6; + + uint32_t bg_red = (((*spot >> 16) & 0xff) * level) >> 6; + uint32_t bg_green = (((*spot >> 8) & 0xff) * level) >> 6; + uint32_t bg_blue = (((*spot) & 0xff) * level) >> 6; + + uint32_t red = fg_red + bg_red; + uint32_t green = fg_green + bg_green; + uint32_t blue = fg_blue + bg_blue; + + *spot = 0xff000000 | (red << 16) | (green << 8) | blue; + } + else if (!r_blendmethod) + { + BYTE *spot = canvas->GetBuffer() + oldyyshifted + xx; + DWORD *bg2rgb = Col2RGB8[1 + level]; + DWORD *fg2rgb = Col2RGB8[63 - level]; + DWORD fg = fg2rgb[basecolor]; + DWORD bg = bg2rgb[*spot]; + bg = (fg + bg) | 0x1f07c1f; + *spot = RGB32k.All[bg&(bg >> 15)]; + } + else + { + BYTE *spot = canvas->GetBuffer() + oldyyshifted + xx; + + uint32_t r = (GPalette.BaseColors[*spot].r * (64 - level) + GPalette.BaseColors[basecolor].r * level) / 64; + uint32_t g = (GPalette.BaseColors[*spot].g * (64 - level) + GPalette.BaseColors[basecolor].g * level) / 64; + uint32_t b = (GPalette.BaseColors[*spot].b * (64 - level) + GPalette.BaseColors[basecolor].b * level) / 64; + + *spot = (BYTE)RGB256k.RGB[r][g][b]; + } +} + +void SWCanvas::Clear(DCanvas *canvas, int left, int top, int right, int bottom, int palcolor, uint32 color) +{ + int x, y; + + if (left == right || top == bottom) + { + return; + } + + assert(left < right); + assert(top < bottom); + + int Width = canvas->GetWidth(); + int Height = canvas->GetHeight(); + int Pitch = canvas->GetPitch(); + + if (left >= Width || right <= 0 || top >= Height || bottom <= 0) + { + return; + } + left = MAX(0, left); + right = MIN(Width, right); + top = MAX(0, top); + bottom = MIN(Height, bottom); + + if (palcolor < 0) + { + palcolor = PalFromRGB(color); + } + + if (canvas->IsBgra()) + { + uint32_t fill_color = GPalette.BaseColors[palcolor]; + + uint32_t *dest = (uint32_t*)canvas->GetBuffer() + top * Pitch + left; + x = right - left; + for (y = top; y < bottom; y++) + { + for (int i = 0; i < x; i++) + dest[i] = fill_color; + dest += Pitch; + } + } + else + { + BYTE *dest = canvas->GetBuffer() + top * Pitch + left; + x = right - left; + for (y = top; y < bottom; y++) + { + memset(dest, palcolor, x); + dest += Pitch; + } + } +} + +void SWCanvas::Dim(DCanvas *canvas, PalEntry color, float damount, int x1, int y1, int w, int h) +{ + if (damount == 0.f) + return; + + int Width = canvas->GetWidth(); + int Height = canvas->GetHeight(); + int Pitch = canvas->GetPitch(); + + if (x1 >= Width || y1 >= Height) + { + return; + } + if (x1 + w > Width) + { + w = Width - x1; + } + if (y1 + h > Height) + { + h = Height - y1; + } + if (w <= 0 || h <= 0) + { + return; + } + + if (canvas->IsBgra()) + { + uint32_t *spot = (uint32_t*)canvas->GetBuffer() + x1 + y1*Pitch; + int gap = Pitch - w; + + uint32_t fg = color.d; + uint32_t fg_red = (fg >> 16) & 0xff; + uint32_t fg_green = (fg >> 8) & 0xff; + uint32_t fg_blue = fg & 0xff; + + uint32_t alpha = (uint32_t)clamp(damount * 256 + 0.5f, 0.0f, 256.0f); + uint32_t inv_alpha = 256 - alpha; + + fg_red *= alpha; + fg_green *= alpha; + fg_blue *= alpha; + + for (int y = h; y != 0; y--) + { + for (int x = w; x != 0; x--) + { + uint32_t bg_red = (*spot >> 16) & 0xff; + uint32_t bg_green = (*spot >> 8) & 0xff; + uint32_t bg_blue = (*spot) & 0xff; + + uint32_t red = (fg_red + bg_red * inv_alpha) / 256; + uint32_t green = (fg_green + bg_green * inv_alpha) / 256; + uint32_t blue = (fg_blue + bg_blue * inv_alpha) / 256; + + *spot = 0xff000000 | (red << 16) | (green << 8) | blue; + spot++; + } + spot += gap; + } + } + else + { + DWORD *bg2rgb; + DWORD fg; + + BYTE *spot = canvas->GetBuffer() + x1 + y1*Pitch; + int gap = Pitch - w; + + int alpha = (int)((float)64 * damount); + int ialpha = 64 - alpha; + int dimmedcolor_r = color.r * alpha; + int dimmedcolor_g = color.g * alpha; + int dimmedcolor_b = color.b * alpha; + + if (!r_blendmethod) + { + { + int amount; + + amount = (int)(damount * 64); + bg2rgb = Col2RGB8[64 - amount]; + + fg = (((color.r * amount) >> 4) << 20) | + ((color.g * amount) >> 4) | + (((color.b * amount) >> 4) << 10); + } + + for (int y = h; y != 0; y--) + { + for (int x = w; x != 0; x--) + { + DWORD bg; + + bg = bg2rgb[(*spot) & 0xff]; + bg = (fg + bg) | 0x1f07c1f; + *spot = RGB32k.All[bg&(bg >> 15)]; + spot++; + } + spot += gap; + } + } + else + { + for (int y = h; y != 0; y--) + { + for (int x = w; x != 0; x--) + { + uint32_t r = (dimmedcolor_r + GPalette.BaseColors[*spot].r * ialpha) >> 8; + uint32_t g = (dimmedcolor_g + GPalette.BaseColors[*spot].g * ialpha) >> 8; + uint32_t b = (dimmedcolor_b + GPalette.BaseColors[*spot].b * ialpha) >> 8; + *spot = (BYTE)RGB256k.RGB[r][g][b]; + spot++; + } + spot += gap; + } + } + } +} + +int SWCanvas::PalFromRGB(uint32 rgb) +{ + // For routines that take RGB colors, cache the previous lookup in case there + // are several repetitions with the same color. + static int LastPal = -1; + static uint32 LastRGB; + + if (LastPal >= 0 && LastRGB == rgb) + { + return LastPal; + } + // Quick check for black and white. + if (rgb == MAKEARGB(255, 0, 0, 0)) + { + LastPal = GPalette.BlackIndex; + } + else if (rgb == MAKEARGB(255, 255, 255, 255)) + { + LastPal = GPalette.WhiteIndex; + } + else + { + LastPal = ColorMatcher.Pick(RPART(rgb), GPART(rgb), BPART(rgb)); + } + LastRGB = rgb; + return LastPal; +} diff --git a/src/swrenderer/r_swcanvas.h b/src/swrenderer/r_swcanvas.h new file mode 100644 index 0000000000..a3f80113b9 --- /dev/null +++ b/src/swrenderer/r_swcanvas.h @@ -0,0 +1,22 @@ + +#pragma once + +#include "v_video.h" +#include "r_data/colormaps.h" + +class SWCanvas +{ +public: + static void DrawTexture(DCanvas *canvas, FTexture *img, DrawParms &parms); + static void FillSimplePoly(DCanvas *canvas, FTexture *tex, FVector2 *points, int npoints, + double originx, double originy, double scalex, double scaley, DAngle rotation, + FDynamicColormap *colormap, PalEntry flatcolor, int lightlevel, int bottomclip); + static void DrawLine(DCanvas *canvas, int x0, int y0, int x1, int y1, int palColor, uint32 realcolor); + static void DrawPixel(DCanvas *canvas, int x, int y, int palColor, uint32 realcolor); + static void Clear(DCanvas *canvas, int left, int top, int right, int bottom, int palcolor, uint32 color); + static void Dim(DCanvas *canvas, PalEntry color, float damount, int x1, int y1, int w, int h); + +private: + static void PUTTRANSDOT(DCanvas *canvas, int xx, int yy, int basecolor, int level); + static int PalFromRGB(uint32 rgb); +}; diff --git a/src/v_draw.cpp b/src/v_draw.cpp index ad9d1aa5bb..a58638e8b6 100644 --- a/src/v_draw.cpp +++ b/src/v_draw.cpp @@ -32,7 +32,7 @@ ** */ -// #define NO_SWRENDER // set this if you want to exclude the software renderer. Without software renderer the base implementations of DrawTextureV and FillSimplePoly need to be disabled because they depend on it. +// #define NO_SWRENDER // set this if you want to exclude the software renderer. Without the software renderer software canvas drawing does nothing. #include #include @@ -42,12 +42,9 @@ #include "m_swap.h" #include "r_defs.h" #include "r_utility.h" +#include "r_renderer.h" #ifndef NO_SWRENDER -#include "swrenderer/drawers/r_draw.h" -#include "swrenderer/drawers/r_draw_rgba.h" -#include "swrenderer/scene/r_light.h" -#include "swrenderer/viewport/r_viewport.h" -#include "swrenderer/r_renderthread.h" +#include "swrenderer/r_swcanvas.h" #endif #include "r_data/r_translate.h" #include "doomstat.h" @@ -72,7 +69,6 @@ CUSTOM_CVAR(Int, uiscale, 2, CVAR_ARCHIVE | CVAR_NOINITCALL) StatusBar->ScreenSizeChanged(); } } -EXTERN_CVAR(Bool, r_blendmethod) // [RH] Stretch values to make a 320x200 image best fit the screen // without using fractional steppings @@ -86,11 +82,6 @@ int CleanXfac_1, CleanYfac_1, CleanWidth_1, CleanHeight_1; CVAR (Bool, hud_scale, true, CVAR_ARCHIVE); -// For routines that take RGB colors, cache the previous lookup in case there -// are several repetitions with the same color. -static int LastPal = -1; -static uint32 LastRGB; - DEFINE_ACTION_FUNCTION(_Screen, GetWidth) { PARAM_PROLOGUE; @@ -112,30 +103,6 @@ DEFINE_ACTION_FUNCTION(_Screen, PaletteColor) ACTION_RETURN_INT(index); } -static int PalFromRGB(uint32 rgb) -{ - if (LastPal >= 0 && LastRGB == rgb) - { - return LastPal; - } - // Quick check for black and white. - if (rgb == MAKEARGB(255,0,0,0)) - { - LastPal = GPalette.BlackIndex; - } - else if (rgb == MAKEARGB(255,255,255,255)) - { - LastPal = GPalette.WhiteIndex; - } - else - { - LastPal = ColorMatcher.Pick(RPART(rgb), GPART(rgb), BPART(rgb)); - } - LastRGB = rgb; - return LastPal; -} - - void DCanvas::DrawTexture (FTexture *img, double x, double y, int tags_first, ...) { Va_List tags; @@ -189,154 +156,13 @@ DEFINE_ACTION_FUNCTION(_Screen, DrawHUDTexture) void DCanvas::DrawTextureParms(FTexture *img, DrawParms &parms) { #ifndef NO_SWRENDER - using namespace swrenderer; - - static short bottomclipper[MAXWIDTH], topclipper[MAXWIDTH]; - - auto viewport = RenderViewport::Instance(); - viewport->RenderTarget = screen; - viewport->RenderTarget->Lock(true); - - lighttable_t *translation = nullptr; - FDynamicColormap *basecolormap = &identitycolormap; - int shade = 0; - - if (APART(parms.colorOverlay) != 0) - { - // The software renderer cannot invert the source without inverting the overlay - // too. That means if the source is inverted, we need to do the reverse of what - // the invert overlay flag says to do. - INTBOOL invertoverlay = (parms.style.Flags & STYLEF_InvertOverlay); - - if (parms.style.Flags & STYLEF_InvertSource) - { - invertoverlay = !invertoverlay; - } - if (invertoverlay) - { - parms.colorOverlay = PalEntry(parms.colorOverlay).InverseColor(); - } - // Note that this overrides the translation in software, but not in hardware. - FDynamicColormap *colormap = GetSpecialLights(MAKERGB(255, 255, 255), parms.colorOverlay & MAKEARGB(0, 255, 255, 255), 0); - - if (viewport->RenderTarget->IsBgra()) - { - basecolormap = colormap; - shade = (APART(parms.colorOverlay)*NUMCOLORMAPS / 255) << FRACBITS; - } - else - { - translation = &colormap->Maps[(APART(parms.colorOverlay)*NUMCOLORMAPS / 255) * 256]; - } - } - else if (parms.remap != NULL) - { - if (viewport->RenderTarget->IsBgra()) - translation = (lighttable_t *)parms.remap->Palette; - else - translation = parms.remap->Remap; - } - - SpriteDrawerArgs drawerargs; - - drawerargs.SetTranslationMap(translation); - drawerargs.SetLight(basecolormap, 0.0f, shade); - bool visible = drawerargs.SetStyle(parms.style, parms.Alpha, -1, parms.fillcolor, basecolormap); - - double x0 = parms.x - parms.left * parms.destwidth / parms.texwidth; - double y0 = parms.y - parms.top * parms.destheight / parms.texheight; - - if (visible) - { - double centeryback = viewport->CenterY; - viewport->CenterY = 0; - - // There is not enough precision in the drawing routines to keep the full - // precision for y0. :( - double sprtopscreen; - modf(y0, &sprtopscreen); - - double yscale = parms.destheight / img->GetHeight(); - double iyscale = 1 / yscale; - - double spryscale = yscale; - assert(spryscale > 0); - - bool sprflipvert = false; - fixed_t iscale = FLOAT2FIXED(1 / spryscale); - //dc_texturemid = (CenterY - 1 - sprtopscreen) * iscale / 65536; - fixed_t frac = 0; - double xiscale = img->GetWidth() / parms.destwidth; - double x2 = x0 + parms.destwidth; - - short *mfloorclip; - short *mceilingclip; - - if (bottomclipper[0] != parms.dclip) - { - fillshort(bottomclipper, screen->GetWidth(), (short)parms.dclip); - } - if (parms.uclip != 0) - { - if (topclipper[0] != parms.uclip) - { - fillshort(topclipper, screen->GetWidth(), (short)parms.uclip); - } - mceilingclip = topclipper; - } - else - { - mceilingclip = zeroarray; - } - mfloorclip = bottomclipper; - - if (parms.flipX) - { - frac = (img->GetWidth() << FRACBITS) - 1; - xiscale = -xiscale; - } - - if (parms.windowleft > 0 || parms.windowright < parms.texwidth) - { - double wi = MIN(parms.windowright, parms.texwidth); - double xscale = parms.destwidth / parms.texwidth; - x0 += parms.windowleft * xscale; - frac += FLOAT2FIXED(parms.windowleft); - x2 -= (parms.texwidth - wi) * xscale; - } - if (x0 < parms.lclip) - { - frac += FLOAT2FIXED((parms.lclip - x0) * xiscale); - x0 = parms.lclip; - } - if (x2 > parms.rclip) - { - x2 = parms.rclip; - } - - int x = int(x0); - int x2_i = int(x2); - fixed_t xiscale_i = FLOAT2FIXED(xiscale); - - static RenderThread thread(nullptr); - thread.DrawQueue->ThreadedRender = false; - while (x < x2_i) - { - drawerargs.DrawMaskedColumn(&thread, x, iscale, img, frac, spryscale, sprtopscreen, sprflipvert, mfloorclip, mceilingclip, !parms.masked); - x++; - frac += xiscale_i; - } - - viewport->CenterY = centeryback; - } + SWCanvas::DrawTexture(this, img, parms); +#endif if (ticdup != 0 && menuactive == MENU_Off) { NetUpdate(); } - - viewport->RenderTarget->Unlock(); -#endif } bool DCanvas::SetTextureParms(DrawParms *parms, FTexture *img, double xx, double yy) const @@ -1047,274 +873,18 @@ void DCanvas::FillBorder (FTexture *img) } } -void DCanvas::PUTTRANSDOT (int xx, int yy, int basecolor, int level) -{ - static int oldyy; - static int oldyyshifted; - - if (yy == oldyy+1) - { - oldyy++; - oldyyshifted += GetPitch(); - } - else if (yy == oldyy-1) - { - oldyy--; - oldyyshifted -= GetPitch(); - } - else if (yy != oldyy) - { - oldyy = yy; - oldyyshifted = yy * GetPitch(); - } - - if (IsBgra()) - { - uint32_t *spot = (uint32_t*)GetBuffer() + oldyyshifted + xx; - - uint32_t fg = swrenderer::LightBgra::shade_pal_index_simple(basecolor, swrenderer::LightBgra::calc_light_multiplier(0)); - uint32_t fg_red = (((fg >> 16) & 0xff) * (63 - level)) >> 6; - uint32_t fg_green = (((fg >> 8) & 0xff) * (63 - level)) >> 6; - uint32_t fg_blue = ((fg & 0xff) * (63 - level)) >> 6; - - uint32_t bg_red = (((*spot >> 16) & 0xff) * level) >> 6; - uint32_t bg_green = (((*spot >> 8) & 0xff) * level) >> 6; - uint32_t bg_blue = (((*spot) & 0xff) * level) >> 6; - - uint32_t red = fg_red + bg_red; - uint32_t green = fg_green + bg_green; - uint32_t blue = fg_blue + bg_blue; - - *spot = 0xff000000 | (red << 16) | (green << 8) | blue; - } - else if (!r_blendmethod) - { - BYTE *spot = GetBuffer() + oldyyshifted + xx; - DWORD *bg2rgb = Col2RGB8[1+level]; - DWORD *fg2rgb = Col2RGB8[63-level]; - DWORD fg = fg2rgb[basecolor]; - DWORD bg = bg2rgb[*spot]; - bg = (fg+bg) | 0x1f07c1f; - *spot = RGB32k.All[bg&(bg>>15)]; - } - else - { - BYTE *spot = GetBuffer() + oldyyshifted + xx; - - uint32_t r = (GPalette.BaseColors[*spot].r * (64 - level) + GPalette.BaseColors[basecolor].r * level) / 64; - uint32_t g = (GPalette.BaseColors[*spot].g * (64 - level) + GPalette.BaseColors[basecolor].g * level) / 64; - uint32_t b = (GPalette.BaseColors[*spot].b * (64 - level) + GPalette.BaseColors[basecolor].b * level) / 64; - - *spot = (BYTE)RGB256k.RGB[r][g][b]; - } -} - void DCanvas::DrawLine(int x0, int y0, int x1, int y1, int palColor, uint32 realcolor) -//void DrawTransWuLine (int x0, int y0, int x1, int y1, BYTE palColor) { - const int WeightingScale = 0; - const int WEIGHTBITS = 6; - const int WEIGHTSHIFT = 16-WEIGHTBITS; - const int NUMWEIGHTS = (1< y1) - { - int temp = y0; y0 = y1; y1 = temp; - temp = x0; x0 = x1; x1 = temp; - } - - PUTTRANSDOT (x0, y0, palColor, 0); - - if ((deltaX = x1 - x0) >= 0) - { - xDir = 1; - } - else - { - xDir = -1; - deltaX = -deltaX; - } - - if ((deltaY = y1 - y0) == 0) - { // horizontal line - if (x0 > x1) - { - swapvalues (x0, x1); - } - if (IsBgra()) - { - uint32_t fillColor = GPalette.BaseColors[palColor].d; - uint32_t *spot = (uint32_t*)GetBuffer() + y0*GetPitch() + x0; - for (int i = 0; i <= deltaX; i++) - spot[i] = fillColor; - } - else - { - memset (GetBuffer() + y0*GetPitch() + x0, palColor, deltaX+1); - } - } - else if (deltaX == 0) - { // vertical line - if (IsBgra()) - { - uint32_t fillColor = GPalette.BaseColors[palColor].d; - uint32_t *spot = (uint32_t*)GetBuffer() + y0*GetPitch() + x0; - int pitch = GetPitch(); - do - { - *spot = fillColor; - spot += pitch; - } while (--deltaY != 0); - } - else - { - BYTE *spot = GetBuffer() + y0*GetPitch() + x0; - int pitch = GetPitch(); - do - { - *spot = palColor; - spot += pitch; - } while (--deltaY != 0); - } - } - else if (deltaX == deltaY) - { // diagonal line. - if (IsBgra()) - { - uint32_t fillColor = GPalette.BaseColors[palColor].d; - uint32_t *spot = (uint32_t*)GetBuffer() + y0*GetPitch() + x0; - int advance = GetPitch() + xDir; - do - { - *spot = fillColor; - spot += advance; - } while (--deltaY != 0); - } - else - { - BYTE *spot = GetBuffer() + y0*GetPitch() + x0; - int advance = GetPitch() + xDir; - do - { - *spot = palColor; - spot += advance; - } while (--deltaY != 0); - } - } - else - { - // line is not horizontal, diagonal, or vertical - fixed_t errorAcc = 0; - - if (deltaY > deltaX) - { // y-major line - fixed_t errorAdj = (((unsigned)deltaX << 16) / (unsigned)deltaY) & 0xffff; - if (xDir < 0) - { - if (WeightingScale == 0) - { - while (--deltaY) - { - errorAcc += errorAdj; - y0++; - int weighting = (errorAcc >> WEIGHTSHIFT) & WEIGHTMASK; - PUTTRANSDOT (x0 - (errorAcc >> 16), y0, palColor, weighting); - PUTTRANSDOT (x0 - (errorAcc >> 16) - 1, y0, - palColor, WEIGHTMASK - weighting); - } - } - else - { - while (--deltaY) - { - errorAcc += errorAdj; - y0++; - int weighting = ((errorAcc * WeightingScale) >> (WEIGHTSHIFT+8)) & WEIGHTMASK; - PUTTRANSDOT (x0 - (errorAcc >> 16), y0, palColor, weighting); - PUTTRANSDOT (x0 - (errorAcc >> 16) - 1, y0, - palColor, WEIGHTMASK - weighting); - } - } - } - else - { - if (WeightingScale == 0) - { - while (--deltaY) - { - errorAcc += errorAdj; - y0++; - int weighting = (errorAcc >> WEIGHTSHIFT) & WEIGHTMASK; - PUTTRANSDOT (x0 + (errorAcc >> 16), y0, palColor, weighting); - PUTTRANSDOT (x0 + (errorAcc >> 16) + xDir, y0, - palColor, WEIGHTMASK - weighting); - } - } - else - { - while (--deltaY) - { - errorAcc += errorAdj; - y0++; - int weighting = ((errorAcc * WeightingScale) >> (WEIGHTSHIFT+8)) & WEIGHTMASK; - PUTTRANSDOT (x0 + (errorAcc >> 16), y0, palColor, weighting); - PUTTRANSDOT (x0 + (errorAcc >> 16) + xDir, y0, - palColor, WEIGHTMASK - weighting); - } - } - } - } - else - { // x-major line - fixed_t errorAdj = (((DWORD) deltaY << 16) / (DWORD) deltaX) & 0xffff; - - if (WeightingScale == 0) - { - while (--deltaX) - { - errorAcc += errorAdj; - x0 += xDir; - int weighting = (errorAcc >> WEIGHTSHIFT) & WEIGHTMASK; - PUTTRANSDOT (x0, y0 + (errorAcc >> 16), palColor, weighting); - PUTTRANSDOT (x0, y0 + (errorAcc >> 16) + 1, - palColor, WEIGHTMASK - weighting); - } - } - else - { - while (--deltaX) - { - errorAcc += errorAdj; - x0 += xDir; - int weighting = ((errorAcc * WeightingScale) >> (WEIGHTSHIFT+8)) & WEIGHTMASK; - PUTTRANSDOT (x0, y0 + (errorAcc >> 16), palColor, weighting); - PUTTRANSDOT (x0, y0 + (errorAcc >> 16) + 1, - palColor, WEIGHTMASK - weighting); - } - } - } - PUTTRANSDOT (x1, y1, palColor, 0); - } - Unlock(); +#ifndef NO_SWRENDER + SWCanvas::DrawLine(this, x0, y0, x1, y1, palColor, realcolor); +#endif } void DCanvas::DrawPixel(int x, int y, int palColor, uint32 realcolor) { - if (palColor < 0) - { - palColor = PalFromRGB(realcolor); - } - - Buffer[Pitch * y + x] = (BYTE)palColor; +#ifndef NO_SWRENDER + SWCanvas::DrawPixel(this, x, y, palColor, realcolor); +#endif } //========================================================================== @@ -1327,59 +897,16 @@ void DCanvas::DrawPixel(int x, int y, int palColor, uint32 realcolor) void DCanvas::Clear (int left, int top, int right, int bottom, int palcolor, uint32 color) { - int x, y; - - if (left == right || top == bottom) +#ifndef NO_SWRENDER + if (palcolor < 0 && APART(color) != 255) { - return; - } - - assert(left < right); - assert(top < bottom); - - if (left >= Width || right <= 0 || top >= Height || bottom <= 0) - { - return; - } - left = MAX(0,left); - right = MIN(Width,right); - top = MAX(0,top); - bottom = MIN(Height,bottom); - - if (palcolor < 0) - { - if (APART(color) != 255) - { - Dim(color, APART(color)/255.f, left, top, right - left, bottom - top); - return; - } - - palcolor = PalFromRGB(color); - } - - if (IsBgra()) - { - uint32_t fill_color = GPalette.BaseColors[palcolor]; - - uint32_t *dest = (uint32_t*)Buffer + top * Pitch + left; - x = right - left; - for (y = top; y < bottom; y++) - { - for (int i = 0; i < x; i++) - dest[i] = fill_color; - dest += Pitch; - } + Dim(color, APART(color) / 255.f, left, top, right - left, bottom - top); } else { - BYTE *dest = Buffer + top * Pitch + left; - x = right - left; - for (y = top; y < bottom; y++) - { - memset(dest, palcolor, x); - dest += Pitch; - } + SWCanvas::Clear(this, left, top, right, bottom, palcolor, color); } +#endif } DEFINE_ACTION_FUNCTION(_Screen, Clear) @@ -1395,6 +922,21 @@ DEFINE_ACTION_FUNCTION(_Screen, Clear) return 0; } +//========================================================================== +// +// DCanvas :: Dim +// +// Applies a colored overlay to an area of the screen. +// +//========================================================================== + +void DCanvas::Dim(PalEntry color, float damount, int x1, int y1, int w, int h) +{ +#ifndef NO_SWRENDER + SWCanvas::Dim(this, color, damount, x1, y1, w, h); +#endif +} + //========================================================================== // // DCanvas :: FillSimplePoly @@ -1415,185 +957,7 @@ void DCanvas::FillSimplePoly(FTexture *tex, FVector2 *points, int npoints, FDynamicColormap *colormap, PalEntry flatcolor, int lightlevel, int bottomclip) { #ifndef NO_SWRENDER - using namespace swrenderer; - - // Use an equation similar to player sprites to determine shade - fixed_t shade = LIGHT2SHADE(lightlevel) - 12*FRACUNIT; - float topy, boty, leftx, rightx; - int toppt, botpt, pt1, pt2; - int i; - int y1, y2, y; - fixed_t x; - bool dorotate = rotation != 0.; - double cosrot, sinrot; - - if (--npoints < 2 || Buffer == NULL) - { // not a polygon or we're not locked - return; - } - - if (bottomclip <= 0) - { - bottomclip = Height; - } - - // Find the extents of the polygon, in particular the highest and lowest points. - for (botpt = toppt = 0, boty = topy = points[0].Y, leftx = rightx = points[0].X, i = 1; i <= npoints; ++i) - { - if (points[i].Y < topy) - { - topy = points[i].Y; - toppt = i; - } - if (points[i].Y > boty) - { - boty = points[i].Y; - botpt = i; - } - if (points[i].X < leftx) - { - leftx = points[i].X; - } - if (points[i].X > rightx) - { - rightx = points[i].X; - } - } - if (topy >= bottomclip || // off the bottom of the screen - boty <= 0 || // off the top of the screen - leftx >= Width || // off the right of the screen - rightx <= 0) // off the left of the screen - { - return; - } - - auto viewport = RenderViewport::Instance(); - - scalex /= tex->Scale.X; - scaley /= tex->Scale.Y; - - // Use the CRT's functions here. - cosrot = cos(rotation.Radians()); - sinrot = sin(rotation.Radians()); - - // Setup constant texture mapping parameters. - SpanDrawerArgs drawerargs; - drawerargs.SetTexture(tex); - if (colormap) - drawerargs.SetLight(colormap, 0, clamp(shade >> FRACBITS, 0, NUMCOLORMAPS - 1)); - else - drawerargs.SetLight(&identitycolormap, 0, 0); - if (drawerargs.TextureWidthBits() != 0) - { - scalex = double(1u << (32 - drawerargs.TextureWidthBits())) / scalex; - drawerargs.SetTextureUStep(xs_RoundToInt(cosrot * scalex)); - } - else - { // Texture is one pixel wide. - scalex = 0; - drawerargs.SetTextureUStep(0); - } - if (drawerargs.TextureHeightBits() != 0) - { - scaley = double(1u << (32 - drawerargs.TextureHeightBits())) / scaley; - drawerargs.SetTextureVStep(xs_RoundToInt(sinrot * scaley)); - } - else - { // Texture is one pixel tall. - scaley = 0; - drawerargs.SetTextureVStep(0); - } - - // Travel down the right edge and create an outline of that edge. - static short spanend[MAXHEIGHT]; - pt1 = toppt; - pt2 = toppt + 1; if (pt2 > npoints) pt2 = 0; - y1 = xs_RoundToInt(points[pt1].Y + 0.5f); - do - { - x = FLOAT2FIXED(points[pt1].X + 0.5f); - y2 = xs_RoundToInt(points[pt2].Y + 0.5f); - if (y1 >= y2 || (y1 < 0 && y2 < 0) || (y1 >= bottomclip && y2 >= bottomclip)) - { - } - else - { - fixed_t xinc = FLOAT2FIXED((points[pt2].X - points[pt1].X) / (points[pt2].Y - points[pt1].Y)); - int y3 = MIN(y2, bottomclip); - if (y1 < 0) - { - x += xinc * -y1; - y1 = 0; - } - for (y = y1; y < y3; ++y) - { - spanend[y] = clamp(x >> FRACBITS, -1, Width); - x += xinc; - } - } - y1 = y2; - pt1 = pt2; - pt2++; if (pt2 > npoints) pt2 = 0; - } while (pt1 != botpt); - - static RenderThread thread(nullptr); - thread.DrawQueue->ThreadedRender = false; - - // Travel down the left edge and fill it in. - pt1 = toppt; - pt2 = toppt - 1; if (pt2 < 0) pt2 = npoints; - y1 = xs_RoundToInt(points[pt1].Y + 0.5f); - do - { - x = FLOAT2FIXED(points[pt1].X + 0.5f); - y2 = xs_RoundToInt(points[pt2].Y + 0.5f); - if (y1 >= y2 || (y1 < 0 && y2 < 0) || (y1 >= bottomclip && y2 >= bottomclip)) - { - } - else - { - fixed_t xinc = FLOAT2FIXED((points[pt2].X - points[pt1].X) / (points[pt2].Y - points[pt1].Y)); - int y3 = MIN(y2, bottomclip); - if (y1 < 0) - { - x += xinc * -y1; - y1 = 0; - } - for (y = y1; y < y3; ++y) - { - int x1 = x >> FRACBITS; - int x2 = spanend[y]; - if (x2 > x1 && x2 > 0 && x1 < Width) - { - x1 = MAX(x1, 0); - x2 = MIN(x2, Width); -#if 0 - memset(this->Buffer + y * this->Pitch + x1, (int)tex, x2 - x1); -#else - drawerargs.SetDestY(y); - drawerargs.SetDestX1(x1); - drawerargs.SetDestX2(x2 - 1); - - DVector2 tex(x1 - originx, y - originy); - if (dorotate) - { - double t = tex.X; - tex.X = t * cosrot - tex.Y * sinrot; - tex.Y = tex.Y * cosrot + t * sinrot; - } - drawerargs.SetTextureUPos(xs_RoundToInt(tex.X * scalex)); - drawerargs.SetTextureVPos(xs_RoundToInt(tex.Y * scaley)); - - drawerargs.DrawSpan(&thread); -#endif - } - x += xinc; - } - } - y1 = y2; - pt1 = pt2; - pt2--; if (pt2 < 0) pt2 = npoints; - } while (pt1 != botpt); + SWCanvas::FillSimplePoly(this, tex, points, npoints, originx, originy, scalex, scaley, rotation, colormap, flatcolor, lightlevel, bottomclip); #endif } diff --git a/src/v_video.cpp b/src/v_video.cpp index 0f9086bc20..56f45c3160 100644 --- a/src/v_video.cpp +++ b/src/v_video.cpp @@ -337,136 +337,6 @@ void DCanvas::Dim (PalEntry color) Dim (dimmer, amount, 0, 0, Width, Height); } -//========================================================================== -// -// DCanvas :: Dim -// -// Applies a colored overlay to an area of the screen. -// -//========================================================================== - -void DCanvas::Dim (PalEntry color, float damount, int x1, int y1, int w, int h) -{ - if (damount == 0.f) - return; - - int gap; - int x, y; - - if (x1 >= Width || y1 >= Height) - { - return; - } - if (x1 + w > Width) - { - w = Width - x1; - } - if (y1 + h > Height) - { - h = Height - y1; - } - if (w <= 0 || h <= 0) - { - return; - } - - gap = Pitch - w; - - if (IsBgra()) - { - uint32_t *spot = (uint32_t*)Buffer + x1 + y1*Pitch; - - uint32_t fg = color.d; - uint32_t fg_red = (fg >> 16) & 0xff; - uint32_t fg_green = (fg >> 8) & 0xff; - uint32_t fg_blue = fg & 0xff; - - uint32_t alpha = (uint32_t)clamp(damount * 256 + 0.5f, 0.0f, 256.0f); - uint32_t inv_alpha = 256 - alpha; - - fg_red *= alpha; - fg_green *= alpha; - fg_blue *= alpha; - - for (y = h; y != 0; y--) - { - for (x = w; x != 0; x--) - { - uint32_t bg_red = (*spot >> 16) & 0xff; - uint32_t bg_green = (*spot >> 8) & 0xff; - uint32_t bg_blue = (*spot) & 0xff; - - uint32_t red = (fg_red + bg_red * inv_alpha) / 256; - uint32_t green = (fg_green + bg_green * inv_alpha) / 256; - uint32_t blue = (fg_blue + bg_blue * inv_alpha) / 256; - - *spot = 0xff000000 | (red << 16) | (green << 8) | blue; - spot++; - } - spot += gap; - } - } - else - { - BYTE *spot = Buffer + x1 + y1*Pitch; - - DWORD *bg2rgb; - DWORD fg; - - spot = Buffer + x1 + y1*Pitch; - gap = Pitch - w; - - int alpha = (int)((float)64 * damount); - int ialpha = 64 - alpha; - int dimmedcolor_r = color.r * alpha; - int dimmedcolor_g = color.g * alpha; - int dimmedcolor_b = color.b * alpha; - - if (!r_blendmethod) - { - { - int amount; - - amount = (int)(damount * 64); - bg2rgb = Col2RGB8[64 - amount]; - - fg = (((color.r * amount) >> 4) << 20) | - ((color.g * amount) >> 4) | - (((color.b * amount) >> 4) << 10); - } - - for (y = h; y != 0; y--) - { - for (x = w; x != 0; x--) - { - DWORD bg; - - bg = bg2rgb[(*spot)&0xff]; - bg = (fg+bg) | 0x1f07c1f; - *spot = RGB32k.All[bg&(bg>>15)]; - spot++; - } - spot += gap; - } - } - else - { - for (y = h; y != 0; y--) - { - for (x = w; x != 0; x--) - { - uint32_t r = (dimmedcolor_r + GPalette.BaseColors[*spot].r * ialpha) >> 8; - uint32_t g = (dimmedcolor_g + GPalette.BaseColors[*spot].g * ialpha) >> 8; - uint32_t b = (dimmedcolor_b + GPalette.BaseColors[*spot].b * ialpha) >> 8; - *spot = (BYTE)RGB256k.RGB[r][g][b]; - spot++; - } - spot += gap; - } - } - } -} - //========================================================================== // // DCanvas :: GetScreenshotBuffer diff --git a/src/v_video.h b/src/v_video.h index c06b6dd927..8cd7be4f87 100644 --- a/src/v_video.h +++ b/src/v_video.h @@ -300,8 +300,6 @@ private: // Keep track of canvases, for automatic destruction at exit DCanvas *Next; static DCanvas *CanvasChain; - - void PUTTRANSDOT (int xx, int yy, int basecolor, int level); }; // A canvas in system memory.