rendutil.cpp: API cleanup + minor related OSD render cleanups

This commit is contained in:
AJR 2022-01-22 13:30:35 -05:00
parent 6d2f9e1fda
commit defa0f3262
5 changed files with 104 additions and 134 deletions

View File

@ -2349,11 +2349,11 @@ void render_target::add_container_primitives(render_primitive_list &list, const
// clip the primitive // clip the primitive
if (!m_transform_container && PRIMFLAG_GET_VECTOR(curitem.flags())) if (!m_transform_container && PRIMFLAG_GET_VECTOR(curitem.flags()))
{ {
clipped = render_clip_line(&prim->bounds, &root_cliprect); clipped = render_clip_line(prim->bounds, root_cliprect);
} }
else else
{ {
clipped = render_clip_line(&prim->bounds, &cliprect); clipped = render_clip_line(prim->bounds, cliprect);
} }
break; break;
@ -2386,7 +2386,7 @@ void render_target::add_container_primitives(render_primitive_list &list, const
prim->texcoords = oriented_texcoords[finalorient]; prim->texcoords = oriented_texcoords[finalorient];
// apply clipping // apply clipping
clipped = render_clip_quad(&prim->bounds, &cliprect, &prim->texcoords); clipped = render_clip_quad(prim->bounds, cliprect, &prim->texcoords);
// apply the final orientation from the quad flags and then build up the final flags // apply the final orientation from the quad flags and then build up the final flags
prim->flags |= (curitem.flags() & ~(PRIMFLAG_TEXORIENT_MASK | PRIMFLAG_BLENDMODE_MASK | PRIMFLAG_TEXFORMAT_MASK)) prim->flags |= (curitem.flags() & ~(PRIMFLAG_TEXORIENT_MASK | PRIMFLAG_BLENDMODE_MASK | PRIMFLAG_TEXFORMAT_MASK))
@ -2446,7 +2446,7 @@ void render_target::add_container_primitives(render_primitive_list &list, const
prim->texcoords = oriented_texcoords[finalorient]; prim->texcoords = oriented_texcoords[finalorient];
// apply clipping // apply clipping
clipped = render_clip_quad(&prim->bounds, &cliprect, &prim->texcoords); clipped = render_clip_quad(prim->bounds, cliprect, &prim->texcoords);
// apply the final orientation from the quad flags and then build up the final flags // apply the final orientation from the quad flags and then build up the final flags
prim->flags |= (curitem.flags() & ~(PRIMFLAG_TEXORIENT_MASK | PRIMFLAG_BLENDMODE_MASK | PRIMFLAG_TEXFORMAT_MASK)) prim->flags |= (curitem.flags() & ~(PRIMFLAG_TEXORIENT_MASK | PRIMFLAG_BLENDMODE_MASK | PRIMFLAG_TEXFORMAT_MASK))
@ -2462,7 +2462,7 @@ void render_target::add_container_primitives(render_primitive_list &list, const
| PRIMFLAG_BLENDMODE(BLENDMODE_ALPHA); | PRIMFLAG_BLENDMODE(BLENDMODE_ALPHA);
// apply clipping // apply clipping
clipped = render_clip_quad(&prim->bounds, &cliprect, nullptr); clipped = render_clip_quad(prim->bounds, cliprect, nullptr);
} }
} }
break; break;
@ -2587,7 +2587,7 @@ void render_target::add_element_primitives(render_primitive_list &list, const ob
} }
// add to the list or free if we're clipped out // add to the list or free if we're clipped out
bool const clipped = render_clip_quad(&prim->bounds, &cliprect, &prim->texcoords); bool const clipped = render_clip_quad(prim->bounds, cliprect, &prim->texcoords);
list.append_or_return(*prim, clipped); list.append_or_return(*prim, clipped);
} }
} }

View File

@ -19,6 +19,7 @@
#include <csetjmp> #include <csetjmp>
#include <cstdlib> #include <cstdlib>
#include <tuple>
namespace { namespace {
@ -375,118 +376,118 @@ static void resample_argb_bitmap_bilinear(u32 *dest, u32 drowpixels, u32 dwidth,
} }
/*------------------------------------------------- //-------------------------------------------------
render_clip_line - clip a line to a rectangle // render_clip_line - clip a line to a rectangle
-------------------------------------------------*/ //-------------------------------------------------
bool render_clip_line(render_bounds *bounds, const render_bounds *clip) bool render_clip_line(render_bounds &bounds, const render_bounds &clip)
{ {
/* loop until we get a final result */ // loop until we get a final result
while (1) while (true)
{ {
u8 code0 = 0, code1 = 0; u8 code0 = 0, code1 = 0;
u8 thiscode; u8 thiscode;
float x, y; float x, y;
/* compute Cohen Sutherland bits for first coordinate */ // compute Cohen Sutherland bits for first coordinate
if (bounds->y0 > clip->y1) if (bounds.y0 > clip.y1)
code0 |= 1; code0 |= 1;
if (bounds->y0 < clip->y0) if (bounds.y0 < clip.y0)
code0 |= 2; code0 |= 2;
if (bounds->x0 > clip->x1) if (bounds.x0 > clip.x1)
code0 |= 4; code0 |= 4;
if (bounds->x0 < clip->x0) if (bounds.x0 < clip.x0)
code0 |= 8; code0 |= 8;
/* compute Cohen Sutherland bits for second coordinate */ // compute Cohen Sutherland bits for second coordinate
if (bounds->y1 > clip->y1) if (bounds.y1 > clip.y1)
code1 |= 1; code1 |= 1;
if (bounds->y1 < clip->y0) if (bounds.y1 < clip.y0)
code1 |= 2; code1 |= 2;
if (bounds->x1 > clip->x1) if (bounds.x1 > clip.x1)
code1 |= 4; code1 |= 4;
if (bounds->x1 < clip->x0) if (bounds.x1 < clip.x0)
code1 |= 8; code1 |= 8;
/* trivial accept: just return false */ // trivial accept: just return false
if ((code0 | code1) == 0) if ((code0 | code1) == 0)
return false; return false;
/* trivial reject: just return true */ // trivial reject: just return true
if ((code0 & code1) != 0) if ((code0 & code1) != 0)
return true; return true;
/* fix one of the OOB cases */ // fix one of the OOB cases
thiscode = code0 ? code0 : code1; thiscode = code0 ? code0 : code1;
/* off the bottom */ // off the bottom
if (thiscode & 1) if (thiscode & 1)
{ {
x = bounds->x0 + (bounds->x1 - bounds->x0) * (clip->y1 - bounds->y0) / (bounds->y1 - bounds->y0); x = bounds.x0 + (bounds.x1 - bounds.x0) * (clip.y1 - bounds.y0) / (bounds.y1 - bounds.y0);
y = clip->y1; y = clip.y1;
} }
/* off the top */ // off the top
else if (thiscode & 2) else if (thiscode & 2)
{ {
x = bounds->x0 + (bounds->x1 - bounds->x0) * (clip->y0 - bounds->y0) / (bounds->y1 - bounds->y0); x = bounds.x0 + (bounds.x1 - bounds.x0) * (clip.y0 - bounds.y0) / (bounds.y1 - bounds.y0);
y = clip->y0; y = clip.y0;
} }
/* off the right */ // off the right
else if (thiscode & 4) else if (thiscode & 4)
{ {
y = bounds->y0 + (bounds->y1 - bounds->y0) * (clip->x1 - bounds->x0) / (bounds->x1 - bounds->x0); y = bounds.y0 + (bounds.y1 - bounds.y0) * (clip.x1 - bounds.x0) / (bounds.x1 - bounds.x0);
x = clip->x1; x = clip.x1;
} }
/* off the left */ // off the left
else else
{ {
y = bounds->y0 + (bounds->y1 - bounds->y0) * (clip->x0 - bounds->x0) / (bounds->x1 - bounds->x0); y = bounds.y0 + (bounds.y1 - bounds.y0) * (clip.x0 - bounds.x0) / (bounds.x1 - bounds.x0);
x = clip->x0; x = clip.x0;
} }
/* fix the appropriate coordinate */ // fix the appropriate coordinate
if (thiscode == code0) if (thiscode == code0)
{ {
bounds->x0 = x; bounds.x0 = x;
bounds->y0 = y; bounds.y0 = y;
} }
else else
{ {
bounds->x1 = x; bounds.x1 = x;
bounds->y1 = y; bounds.y1 = y;
} }
} }
} }
/*------------------------------------------------- //-------------------------------------------------
render_clip_quad - clip a quad to a rectangle // render_clip_quad - clip a quad to a rectangle
-------------------------------------------------*/ //-------------------------------------------------
bool render_clip_quad(render_bounds *bounds, const render_bounds *clip, render_quad_texuv *texcoords) bool render_clip_quad(render_bounds &bounds, const render_bounds &clip, render_quad_texuv *texcoords)
{ {
/* ensure our assumptions about the bounds are correct */ // ensure our assumptions about the bounds are correct
assert(bounds->x0 <= bounds->x1); assert(bounds.x0 <= bounds.x1);
assert(bounds->y0 <= bounds->y1); assert(bounds.y0 <= bounds.y1);
/* trivial reject */ // trivial reject
if (bounds->y1 < clip->y0) if (bounds.y1 < clip.y0)
return true; return true;
if (bounds->y0 > clip->y1) if (bounds.y0 > clip.y1)
return true; return true;
if (bounds->x1 < clip->x0) if (bounds.x1 < clip.x0)
return true; return true;
if (bounds->x0 > clip->x1) if (bounds.x0 > clip.x1)
return true; return true;
/* clip top (x0,y0)-(x1,y1) */ // clip top (x0,y0)-(x1,y1)
if (bounds->y0 < clip->y0) if (bounds.y0 < clip.y0)
{ {
float frac = (clip->y0 - bounds->y0) / (bounds->y1 - bounds->y0); float frac = (clip.y0 - bounds.y0) / (bounds.y1 - bounds.y0);
bounds->y0 = clip->y0; bounds.y0 = clip.y0;
if (texcoords != nullptr) if (texcoords != nullptr)
{ {
texcoords->tl.u += (texcoords->bl.u - texcoords->tl.u) * frac; texcoords->tl.u += (texcoords->bl.u - texcoords->tl.u) * frac;
@ -496,11 +497,11 @@ bool render_clip_quad(render_bounds *bounds, const render_bounds *clip, render_q
} }
} }
/* clip bottom (x3,y3)-(x2,y2) */ // clip bottom (x3,y3)-(x2,y2)
if (bounds->y1 > clip->y1) if (bounds.y1 > clip.y1)
{ {
float frac = (bounds->y1 - clip->y1) / (bounds->y1 - bounds->y0); float frac = (bounds.y1 - clip.y1) / (bounds.y1 - bounds.y0);
bounds->y1 = clip->y1; bounds.y1 = clip.y1;
if (texcoords != nullptr) if (texcoords != nullptr)
{ {
texcoords->bl.u -= (texcoords->bl.u - texcoords->tl.u) * frac; texcoords->bl.u -= (texcoords->bl.u - texcoords->tl.u) * frac;
@ -510,11 +511,11 @@ bool render_clip_quad(render_bounds *bounds, const render_bounds *clip, render_q
} }
} }
/* clip left (x0,y0)-(x3,y3) */ // clip left (x0,y0)-(x3,y3)
if (bounds->x0 < clip->x0) if (bounds.x0 < clip.x0)
{ {
float frac = (clip->x0 - bounds->x0) / (bounds->x1 - bounds->x0); float frac = (clip.x0 - bounds.x0) / (bounds.x1 - bounds.x0);
bounds->x0 = clip->x0; bounds.x0 = clip.x0;
if (texcoords != nullptr) if (texcoords != nullptr)
{ {
texcoords->tl.u += (texcoords->tr.u - texcoords->tl.u) * frac; texcoords->tl.u += (texcoords->tr.u - texcoords->tl.u) * frac;
@ -524,11 +525,11 @@ bool render_clip_quad(render_bounds *bounds, const render_bounds *clip, render_q
} }
} }
/* clip right (x1,y1)-(x2,y2) */ // clip right (x1,y1)-(x2,y2)
if (bounds->x1 > clip->x1) if (bounds.x1 > clip.x1)
{ {
float frac = (bounds->x1 - clip->x1) / (bounds->x1 - bounds->x0); float frac = (bounds.x1 - clip.x1) / (bounds.x1 - bounds.x0);
bounds->x1 = clip->x1; bounds.x1 = clip.x1;
if (texcoords != nullptr) if (texcoords != nullptr)
{ {
texcoords->tr.u -= (texcoords->tr.u - texcoords->tl.u) * frac; texcoords->tr.u -= (texcoords->tr.u - texcoords->tl.u) * frac;
@ -541,14 +542,14 @@ bool render_clip_quad(render_bounds *bounds, const render_bounds *clip, render_q
} }
/*------------------------------------------------- //-------------------------------------------------
render_line_to_quad - convert a line and a // render_line_to_quad - convert a line and a
width to four points // width to four points
-------------------------------------------------*/ //-----------------------------------------------
void render_line_to_quad(const render_bounds *bounds, float width, float length_extension, render_bounds *bounds0, render_bounds *bounds1) std::pair<render_bounds, render_bounds> render_line_to_quad(const render_bounds &bounds, float width, float length_extension)
{ {
render_bounds modbounds = *bounds; render_bounds modbounds = bounds;
/* /*
High-level logic -- due to math optimizations, this info is lost below. High-level logic -- due to math optimizations, this info is lost below.
@ -595,18 +596,18 @@ void render_line_to_quad(const render_bounds *bounds, float width, float length_
D.y = p1.y - 0.5 * w * u.x D.y = p1.y - 0.5 * w * u.x
*/ */
/* we only care about the half-width */ // we only care about the half-width
float half_width = width * 0.5f; float half_width = width * 0.5f;
/* compute a vector from point 0 to point 1 */ // compute a vector from point 0 to point 1
float unitx = modbounds.x1 - modbounds.x0; float unitx = modbounds.x1 - modbounds.x0;
float unity = modbounds.y1 - modbounds.y0; float unity = modbounds.y1 - modbounds.y0;
/* points just use a +1/+1 unit vector; this gives a nice diamond pattern */ // points just use a +1/+1 unit vector; this gives a nice diamond pattern
if (unitx == 0 && unity == 0) if (unitx == 0 && unity == 0)
{ {
/* length of a unit vector (1,1) */ // length of a unit vector (1,1)
float unit_length = 0.70710678f; constexpr float unit_length = 0.70710678f;
unitx = unity = unit_length * half_width; unitx = unity = unit_length * half_width;
modbounds.x0 -= unitx; modbounds.x0 -= unitx;
@ -615,12 +616,12 @@ void render_line_to_quad(const render_bounds *bounds, float width, float length_
modbounds.y1 += unity; modbounds.y1 += unity;
} }
/* lines need to be divided by their length */ // lines need to be divided by their length
else else
{ {
float length = sqrtf(unitx * unitx + unity * unity); float length = sqrtf(unitx * unitx + unity * unity);
/* extend line length */ // extend line length
if (length_extension > 0.0f) if (length_extension > 0.0f)
{ {
float half_length_extension = length_extension *0.5f; float half_length_extension = length_extension *0.5f;
@ -634,27 +635,16 @@ void render_line_to_quad(const render_bounds *bounds, float width, float length_
modbounds.y1 += directiony * half_length_extension; modbounds.y1 += directiony * half_length_extension;
} }
/* prescale unitx and unity by the half-width */ // prescale unitx and unity by the half-width
float invlength = half_width / length; float invlength = half_width / length;
unitx *= invlength; unitx *= invlength;
unity *= invlength; unity *= invlength;
} }
/* rotate the unit vector by 90 degrees and add to point 0 */ // rotate the unit vector by 90 and -90 degrees and add to points 0 and 1
bounds0->x0 = modbounds.x0 - unity; return std::make_pair(
bounds0->y0 = modbounds.y0 + unitx; render_bounds{ modbounds.x0 - unity, modbounds.y0 + unitx, modbounds.x0 + unity, modbounds.y0 - unitx },
render_bounds{ modbounds.x1 - unity, modbounds.y1 + unitx, modbounds.x1 + unity, modbounds.y1 - unitx });
/* rotate the unit vector by -90 degrees and add to point 0 */
bounds0->x1 = modbounds.x0 + unity;
bounds0->y1 = modbounds.y0 - unitx;
/* rotate the unit vector by 90 degrees and add to point 1 */
bounds1->x0 = modbounds.x1 - unity;
bounds1->y0 = modbounds.y1 + unitx;
/* rotate the unit vector by -90 degrees and add to point 1 */
bounds1->x1 = modbounds.x1 + unity;
bounds1->y1 = modbounds.y1 - unitx;
} }

View File

@ -17,6 +17,7 @@
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include <utility>
/* ----- image formats ----- */ /* ----- image formats ----- */
@ -40,9 +41,9 @@ enum ru_imgformat
/* ----- render utilities ----- */ /* ----- render utilities ----- */
void render_resample_argb_bitmap_hq(bitmap_argb32 &dest, bitmap_argb32 &source, const render_color &color, bool force = false); void render_resample_argb_bitmap_hq(bitmap_argb32 &dest, bitmap_argb32 &source, const render_color &color, bool force = false);
bool render_clip_line(render_bounds *bounds, const render_bounds *clip); bool render_clip_line(render_bounds &bounds, const render_bounds &clip);
bool render_clip_quad(render_bounds *bounds, const render_bounds *clip, render_quad_texuv *texcoords); bool render_clip_quad(render_bounds &bounds, const render_bounds &clip, render_quad_texuv *texcoords);
void render_line_to_quad(const render_bounds *bounds, float width, float length_extension, render_bounds *bounds0, render_bounds *bounds1); std::pair<render_bounds, render_bounds> render_line_to_quad(const render_bounds &bounds, float width, float length_extension);
void render_load_msdib(bitmap_argb32 &bitmap, util::random_read &file); void render_load_msdib(bitmap_argb32 &bitmap, util::random_read &file);
void render_load_jpeg(bitmap_argb32 &bitmap, util::random_read &file); void render_load_jpeg(bitmap_argb32 &bitmap, util::random_read &file);
bool render_load_png(bitmap_argb32 &bitmap, util::random_read &file, bool load_as_alpha_to_existing = false); bool render_load_png(bitmap_argb32 &bitmap, util::random_read &file, bool load_as_alpha_to_existing = false);

View File

@ -1523,15 +1523,10 @@ void renderer_d3d9::batch_vector(const render_primitive &prim)
} }
// compute the effective width based on the direction of the line // compute the effective width based on the direction of the line
float effwidth = prim.width; float effwidth = std::max(prim.width, 2.0f);
if (effwidth < 2.0f)
{
effwidth = 2.0f;
}
// determine the bounds of a quad to draw this line // determine the bounds of a quad to draw this line
render_bounds b0, b1; auto [b0, b1] = render_line_to_quad(prim.bounds, effwidth, effwidth);
render_line_to_quad(&prim.bounds, effwidth, effwidth, &b0, &b1);
float lx = b1.x1 - b0.x1; float lx = b1.x1 - b0.x1;
float ly = b1.y1 - b0.y1; float ly = b1.y1 - b0.y1;
@ -1630,15 +1625,10 @@ void renderer_d3d9::draw_line(const render_primitive &prim)
} }
// compute the effective width based on the direction of the line // compute the effective width based on the direction of the line
float effwidth = prim.width; float effwidth = std::max(prim.width, 1.0f);
if (effwidth < 1.0f)
{
effwidth = 1.0f;
}
// determine the bounds of a quad to draw this line // determine the bounds of a quad to draw this line
render_bounds b0, b1; auto [b0, b1] = render_line_to_quad(prim.bounds, effwidth, 0.0f);
render_line_to_quad(&prim.bounds, effwidth, 0.0f, &b0, &b1);
vertex[0].x = b0.x0; vertex[0].x = b0.x0;
vertex[0].y = b0.y0; vertex[0].y = b0.y0;

View File

@ -1253,11 +1253,6 @@ int renderer_ogl::draw(const int update)
} }
#else #else
{ {
const line_aa_step *step = line_aa_4step;
render_bounds b0, b1;
float r, g, b, a;
float effwidth;
// we're not gonna play fancy here. close anything pending and let's go. // we're not gonna play fancy here. close anything pending and let's go.
if (pendingPrimitive!=GL_NO_PRIMITIVE && pendingPrimitive!=curPrimitive) if (pendingPrimitive!=GL_NO_PRIMITIVE && pendingPrimitive!=curPrimitive)
{ {
@ -1268,12 +1263,10 @@ int renderer_ogl::draw(const int update)
set_blendmode(sdl, PRIMFLAG_GET_BLENDMODE(prim.flags)); set_blendmode(sdl, PRIMFLAG_GET_BLENDMODE(prim.flags));
// compute the effective width based on the direction of the line // compute the effective width based on the direction of the line
effwidth = prim.width(); float effwidth = std::max(prim.width(), 0.5f);
if (effwidth < 0.5f)
effwidth = 0.5f;
// determine the bounds of a quad to draw this line // determine the bounds of a quad to draw this line
render_line_to_quad(&prim.bounds, effwidth, 0.0f, &b0, &b1); auto [b0, b1] = render_line_to_quad(prim.bounds, effwidth, 0.0f);
// fix window position // fix window position
b0.x0 += hofs; b0.x0 += hofs;
@ -1286,7 +1279,7 @@ int renderer_ogl::draw(const int update)
b1.y1 += vofs; b1.y1 += vofs;
// iterate over AA steps // iterate over AA steps
for (step = PRIMFLAG_GET_ANTIALIAS(prim.flags) ? line_aa_4step : line_aa_1step; step->weight != 0; step++) for (const line_aa_step *step = PRIMFLAG_GET_ANTIALIAS(prim.flags) ? line_aa_4step : line_aa_1step; step->weight != 0; step++)
{ {
glBegin(GL_TRIANGLE_STRIP); glBegin(GL_TRIANGLE_STRIP);
@ -1303,14 +1296,10 @@ int renderer_ogl::draw(const int update)
glVertex2f(b1.x1 + step->xoffs, b1.y1 + step->yoffs); glVertex2f(b1.x1 + step->xoffs, b1.y1 + step->yoffs);
// determine the color of the line // determine the color of the line
r = (prim.color.r * step->weight); float r = std::min(prim.color.r * step->weight, 1.0f);
g = (prim.color.g * step->weight); float g = std::min(prim.color.g * step->weight, 1.0f);
b = (prim.color.b * step->weight); float b = std::min(prim.color.b * step->weight, 1.0f);
a = (prim.color.a * 255.0f); float a = std::min(prim.color.a * 255.0f, 1.0f);
if (r > 1.0) r = 1.0;
if (g > 1.0) g = 1.0;
if (b > 1.0) b = 1.0;
if (a > 1.0) a = 1.0;
glColor4f(r, g, b, a); glColor4f(r, g, b, a);
// texture = texture_update(window, &prim, 0); // texture = texture_update(window, &prim, 0);