| 1 |
// By Emil Ernerfeldt 2018 |
| 2 |
// LICENSE: |
| 3 |
// This software is dual-licensed to the public domain and under the following |
| 4 |
// license: you are granted a perpetual, irrevocable license to copy, modify, |
| 5 |
// publish, and distribute this file as you see fit. |
| 6 |
|
| 7 |
#include "imgui.h" |
| 8 |
#include "imgui_internal.h" |
| 9 |
#ifndef IMGUI_DISABLE |
| 10 |
#include "imgui_sw.hpp" |
| 11 |
|
| 12 |
#include <algorithm> |
| 13 |
#include <math.h> |
| 14 |
#include <vector> |
| 15 |
#include <SDL.h> |
| 16 |
|
| 17 |
struct ImGui_ImplSW_Data |
| 18 |
{ |
| 19 |
SDL_Window* Window; |
| 20 |
|
| 21 |
ImGui_ImplSW_Data() { memset((void*)this, 0, sizeof(*this)); } |
| 22 |
}; |
| 23 |
|
| 24 |
static ImGui_ImplSW_Data* ImGui_ImplSW_GetBackendData() |
| 25 |
{ |
| 26 |
return ImGui::GetCurrentContext() ? (ImGui_ImplSW_Data*)ImGui::GetIO().BackendRendererUserData : nullptr; |
| 27 |
} |
| 28 |
|
| 29 |
struct PaintTarget |
| 30 |
{ |
| 31 |
uint32_t *pixels; |
| 32 |
int width; |
| 33 |
int height; |
| 34 |
}; |
| 35 |
|
| 36 |
// ---------------------------------------------------------------------------- |
| 37 |
|
| 38 |
#pragma pack(push, 1) |
| 39 |
union ColorInt |
| 40 |
{ |
| 41 |
struct { |
| 42 |
#ifdef TA_BIG_ENDIAN |
| 43 |
uint8_t a, r, g, b; |
| 44 |
#else |
| 45 |
uint8_t b, g, r, a; |
| 46 |
#endif |
| 47 |
}; |
| 48 |
uint32_t u32; |
| 49 |
ColorInt(): |
| 50 |
u32(0) {} |
| 51 |
|
| 52 |
ColorInt(uint32_t c): |
| 53 |
u32(c) {} |
| 54 |
|
| 55 |
ColorInt(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha): |
| 56 |
b(blue), |
| 57 |
g(green), |
| 58 |
r(red), |
| 59 |
a(alpha) {} |
| 60 |
|
| 61 |
static ColorInt bgra(uint32_t c) { |
| 62 |
return ColorInt((c&0xff00ff00)|((c&0xff)<<16)|((c&0xff0000)>>16)); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
ColorInt &operator*=(const ColorInt &other) |
| 67 |
{ |
| 68 |
r = (r * other.r + 255) >> 8; |
| 69 |
g = (g * other.g + 255) >> 8; |
| 70 |
b = (b * other.b + 255) >> 8; |
| 71 |
a = (a * other.a + 255) >> 8; |
| 72 |
return *this; |
| 73 |
} |
| 74 |
}; |
| 75 |
#pragma pack(pop) |
| 76 |
|
| 77 |
static inline uint32_t blend(const ColorInt &target, const ColorInt &source) |
| 78 |
{ |
| 79 |
if (source.a == 0) return target.u32; |
| 80 |
if (source.a >= 255) return source.u32; |
| 81 |
const unsigned char ia=255-source.a; |
| 82 |
return ( |
| 83 |
(target.a << 24u) | |
| 84 |
(((source.r * source.a + target.r * ia + 255) >> 8) << 16u) | |
| 85 |
(((source.g * source.a + target.g * ia + 255) >> 8) << 8u) | |
| 86 |
(((source.b * source.a + target.b * ia + 255) >> 8) << 0u) |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
// ---------------------------------------------------------------------------- |
| 91 |
// Used for interpolating vertex attributes (color and texture coordinates) in a triangle. |
| 92 |
|
| 93 |
struct Barycentric |
| 94 |
{ |
| 95 |
float w0, w1, w2; |
| 96 |
}; |
| 97 |
|
| 98 |
Barycentric operator*(const float f, const Barycentric &va) { return { f * va.w0, f * va.w1, f * va.w2 }; } |
| 99 |
|
| 100 |
void operator+=(Barycentric &a, const Barycentric &b) |
| 101 |
{ |
| 102 |
a.w0 += b.w0; |
| 103 |
a.w1 += b.w1; |
| 104 |
a.w2 += b.w2; |
| 105 |
} |
| 106 |
|
| 107 |
Barycentric operator+(const Barycentric &a, const Barycentric &b) |
| 108 |
{ |
| 109 |
return Barycentric{ a.w0 + b.w0, a.w1 + b.w1, a.w2 + b.w2 }; |
| 110 |
} |
| 111 |
|
| 112 |
// ---------------------------------------------------------------------------- |
| 113 |
// Useful operators on ImGui vectors: |
| 114 |
|
| 115 |
ImVec2 operator*(const float f, const ImVec2 &v) { return ImVec2{ f * v.x, f * v.y }; } |
| 116 |
|
| 117 |
ImVec4 operator*(const float f, const ImVec4 &v) { return ImVec4{ f * v.x, f * v.y, f * v.z, f * v.w }; } |
| 118 |
|
| 119 |
|
| 120 |
ColorInt operator*(const float other, const ColorInt& that) |
| 121 |
{ |
| 122 |
return ColorInt( |
| 123 |
(that.r * (int)(other * 256.0f)) >> 8, |
| 124 |
(that.g * (int)(other * 256.0f)) >> 8, |
| 125 |
(that.b * (int)(other * 256.0f)) >> 8, |
| 126 |
(that.a * (int)(other * 256.0f)) >> 8 |
| 127 |
); |
| 128 |
} |
| 129 |
|
| 130 |
ColorInt operator+(const ColorInt& l, const ColorInt& r) { |
| 131 |
return ColorInt(l.u32+r.u32); |
| 132 |
} |
| 133 |
|
| 134 |
// ---------------------------------------------------------------------------- |
| 135 |
// Copies of functions in ImGui, inlined for speed: |
| 136 |
|
| 137 |
inline ImVec4 color_convert_u32_to_float4(ImU32 in) |
| 138 |
{ |
| 139 |
const float s = 1.0f / 255.0f; |
| 140 |
return ImVec4(((in >> IM_COL32_R_SHIFT) & 0xFF) * s, |
| 141 |
((in >> IM_COL32_G_SHIFT) & 0xFF) * s, |
| 142 |
((in >> IM_COL32_B_SHIFT) & 0xFF) * s, |
| 143 |
((in >> IM_COL32_A_SHIFT) & 0xFF) * s); |
| 144 |
} |
| 145 |
|
| 146 |
inline ImU32 color_convert_float4_to_u32(const ImVec4 &in) |
| 147 |
{ |
| 148 |
ImU32 out; |
| 149 |
out = uint32_t(in.x * 255.0f + 0.5f) << IM_COL32_B_SHIFT; |
| 150 |
out |= uint32_t(in.y * 255.0f + 0.5f) << IM_COL32_G_SHIFT; |
| 151 |
out |= uint32_t(in.z * 255.0f + 0.5f) << IM_COL32_R_SHIFT; |
| 152 |
out |= uint32_t(in.w * 255.0f + 0.5f) << IM_COL32_A_SHIFT; |
| 153 |
return out; |
| 154 |
} |
| 155 |
|
| 156 |
// ---------------------------------------------------------------------------- |
| 157 |
// For fast and subpixel-perfect triangle rendering we used fixed point arithmetic. |
| 158 |
// To keep the code simple we use 64 bits to avoid overflows. |
| 159 |
// TODO: make it 32-bit or else |
| 160 |
|
| 161 |
using Int = int32_t; |
| 162 |
const Int kFixedBias = 1; |
| 163 |
|
| 164 |
struct Point |
| 165 |
{ |
| 166 |
Int x, y; |
| 167 |
}; |
| 168 |
|
| 169 |
Int orient2d(const Point &a, const Point &b, const Point &c) |
| 170 |
{ |
| 171 |
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); |
| 172 |
} |
| 173 |
|
| 174 |
Int as_int(float v) { return static_cast<Int>(floor(v * kFixedBias)); } |
| 175 |
|
| 176 |
Point as_point(ImVec2 v) { return Point{ as_int(v.x), as_int(v.y) }; } |
| 177 |
|
| 178 |
// ---------------------------------------------------------------------------- |
| 179 |
|
| 180 |
inline float min3(float a, float b, float c) |
| 181 |
{ |
| 182 |
if (a < b && a < c) { return a; } |
| 183 |
return b < c ? b : c; |
| 184 |
} |
| 185 |
|
| 186 |
inline float max3(float a, float b, float c) |
| 187 |
{ |
| 188 |
if (a > b && a > c) { return a; } |
| 189 |
return b > c ? b : c; |
| 190 |
} |
| 191 |
|
| 192 |
inline float barycentric(const ImVec2 &a, const ImVec2 &b, const ImVec2 &point) |
| 193 |
{ |
| 194 |
return (b.x - a.x) * (point.y - a.y) - (b.y - a.y) * (point.x - a.x); |
| 195 |
} |
| 196 |
|
| 197 |
inline uint8_t sample_font_texture(const SWTexture &texture, int x, int y) |
| 198 |
{ |
| 199 |
return ((const uint8_t*)texture.pixels)[x + y]; |
| 200 |
} |
| 201 |
|
| 202 |
inline uint32_t sample_texture(const SWTexture &texture, int x, int y) { return texture.pixels[x + y]; } |
| 203 |
|
| 204 |
static void paint_uniform_rectangle(const PaintTarget &target, |
| 205 |
const ImVec2 &min_f, |
| 206 |
const ImVec2 &max_f, |
| 207 |
const ColorInt &color) |
| 208 |
{ |
| 209 |
// don't if our rectangle is transparent |
| 210 |
if (color.a==0) return; |
| 211 |
|
| 212 |
// Integer bounding box [min, max): |
| 213 |
int min_x_i = (int)(min_f.x + 0.5f); |
| 214 |
int min_y_i = (int)(min_f.y + 0.5f); |
| 215 |
int max_x_i = (int)(max_f.x + 0.5f); |
| 216 |
int max_y_i = (int)(max_f.y + 0.5f); |
| 217 |
|
| 218 |
// Clamp to render target: |
| 219 |
min_x_i = std::max(min_x_i, 0); |
| 220 |
min_y_i = std::max(min_y_i, 0); |
| 221 |
max_x_i = std::min(max_x_i, target.width); |
| 222 |
max_y_i = std::min(max_y_i, target.height); |
| 223 |
|
| 224 |
if (color.a==255) { |
| 225 |
// fast path if alpha blending is not necessary |
| 226 |
for (int y = min_y_i; y < max_y_i; ++y) { |
| 227 |
uint32_t* target_pixel = &target.pixels[y * target.width + min_x_i - 1]; |
| 228 |
for (int x = min_x_i; x < max_x_i; ++x) { |
| 229 |
++target_pixel; |
| 230 |
*target_pixel = color.u32; |
| 231 |
} |
| 232 |
} |
| 233 |
} else { |
| 234 |
// We often blend the same colors over and over again, so optimize for this (saves 25% total cpu): |
| 235 |
uint32_t last_target_pixel = target.pixels[min_y_i * target.width + min_x_i]; |
| 236 |
const ColorInt* lastColorRef = (const ColorInt*)(&last_target_pixel); |
| 237 |
uint32_t last_output = blend(*lastColorRef, color); |
| 238 |
|
| 239 |
for (int y = min_y_i; y < max_y_i; ++y) { |
| 240 |
uint32_t* target_pixel = &target.pixels[y * target.width + min_x_i - 1]; |
| 241 |
for (int x = min_x_i; x < max_x_i; ++x) { |
| 242 |
++target_pixel; |
| 243 |
if (*target_pixel == last_target_pixel) { |
| 244 |
*target_pixel = last_output; |
| 245 |
continue; |
| 246 |
} |
| 247 |
last_target_pixel = *target_pixel; |
| 248 |
const ColorInt* colorRef = (const ColorInt*)(target_pixel); |
| 249 |
*target_pixel = blend(*colorRef, color); |
| 250 |
last_output = *target_pixel; |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
static void paint_uniform_textured_rectangle(const PaintTarget &target, |
| 257 |
const SWTexture &texture, |
| 258 |
const ImVec4 &clip_rect, |
| 259 |
const ImDrawVert &min_v, |
| 260 |
const ImDrawVert &max_v) |
| 261 |
{ |
| 262 |
const ImVec2 min_p = ImVec2(min_v.pos.x, min_v.pos.y); |
| 263 |
const ImVec2 max_p = ImVec2(max_v.pos.x, max_v.pos.y); |
| 264 |
|
| 265 |
float distanceX = max_p.x - min_p.x; |
| 266 |
float distanceY = max_p.y - min_p.y; |
| 267 |
if (distanceX == 0 || distanceY == 0) { return; } |
| 268 |
|
| 269 |
// Find bounding box: |
| 270 |
float min_x_f = min_p.x; |
| 271 |
float min_y_f = min_p.y; |
| 272 |
float max_x_f = max_p.x; |
| 273 |
float max_y_f = max_p.y; |
| 274 |
|
| 275 |
// Clip against clip_rect: |
| 276 |
min_x_f = std::max(min_x_f, clip_rect.x); |
| 277 |
min_y_f = std::max(min_y_f, clip_rect.y); |
| 278 |
max_x_f = std::min(max_x_f, clip_rect.z - 0.5f); |
| 279 |
max_y_f = std::min(max_y_f, clip_rect.w - 0.5f); |
| 280 |
|
| 281 |
// Integer bounding box [min, max): |
| 282 |
int min_x_i = (int)(min_x_f); |
| 283 |
int min_y_i = (int)(min_y_f); |
| 284 |
int max_x_i = (int)(max_x_f + 1.0f); |
| 285 |
int max_y_i = (int)(max_y_f + 1.0f); |
| 286 |
|
| 287 |
// Clip against render target: |
| 288 |
min_x_i = std::max(min_x_i, 0); |
| 289 |
min_y_i = std::max(min_y_i, 0); |
| 290 |
max_x_i = std::min(max_x_i, target.width); |
| 291 |
max_y_i = std::min(max_y_i, target.height); |
| 292 |
|
| 293 |
const auto topleft = ImVec2(min_x_i + 0.5f, min_y_i + 0.5f); |
| 294 |
const ImVec2 delta_uv_per_pixel = { |
| 295 |
(max_v.uv.x - min_v.uv.x) / distanceX, |
| 296 |
(max_v.uv.y - min_v.uv.y) / distanceY, |
| 297 |
}; |
| 298 |
const ImVec2 uv_topleft = { |
| 299 |
min_v.uv.x + (topleft.x - min_v.pos.x) * delta_uv_per_pixel.x, |
| 300 |
min_v.uv.y + (topleft.y - min_v.pos.y) * delta_uv_per_pixel.y, |
| 301 |
}; |
| 302 |
|
| 303 |
int startX = uv_topleft.x * (texture.width - 1.0f) + 0.5f; |
| 304 |
int startY = uv_topleft.y * (texture.height - 1.0f) + 0.5f; |
| 305 |
|
| 306 |
if (startX<0) startX=0; |
| 307 |
if (startX>texture.width-1) startX=texture.width-1; |
| 308 |
if (startY<0) startY=0; |
| 309 |
if (startY>texture.height-1) startY=texture.height-1; |
| 310 |
|
| 311 |
int currentX = startX; |
| 312 |
int currentY = startY * texture.width; |
| 313 |
|
| 314 |
float deltaX = delta_uv_per_pixel.x * texture.width; |
| 315 |
float deltaY = delta_uv_per_pixel.y * texture.height; |
| 316 |
|
| 317 |
const ColorInt colorRef = ColorInt::bgra(min_v.col); |
| 318 |
|
| 319 |
for (int y = min_y_i; y < max_y_i; ++y) { |
| 320 |
currentX = startX; |
| 321 |
uint32_t* target_pixel = &target.pixels[y * target.width - 1 + min_x_i]; |
| 322 |
for (int x = min_x_i; x < max_x_i; ++x) { |
| 323 |
++target_pixel; |
| 324 |
const ColorInt* targetColorRef = (const ColorInt*)(target_pixel); |
| 325 |
|
| 326 |
if (texture.isAlpha) { |
| 327 |
uint8_t texel = sample_font_texture(texture, currentX, currentY); |
| 328 |
if (deltaX != 0 && currentX < texture.width - 1) { currentX += 1; } |
| 329 |
|
| 330 |
// The font texture is all black or all white, so optimize for this: |
| 331 |
// anti-aliasing will be lost, but it doesn't matter |
| 332 |
if (texel & 0x80) { |
| 333 |
*target_pixel = blend(*targetColorRef, colorRef); |
| 334 |
} |
| 335 |
continue; |
| 336 |
|
| 337 |
} else { |
| 338 |
uint32_t texColor = sample_texture(texture, currentX, currentY); |
| 339 |
auto src_color = ColorInt(texColor); |
| 340 |
|
| 341 |
if (deltaX != 0 && currentX < texture.width - 1) { currentX += 1; } |
| 342 |
|
| 343 |
src_color *= colorRef; |
| 344 |
*target_pixel = blend(*targetColorRef, src_color); |
| 345 |
} |
| 346 |
} |
| 347 |
if (deltaY != 0 && currentY < (texture.height - 1)*texture.width) { currentY += texture.width; } |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
// When two triangles share an edge, we want to draw the pixels on that edge exactly once. |
| 352 |
// The edge will be the same, but the direction will be the opposite |
| 353 |
// (assuming the two triangles have the same winding order). |
| 354 |
// Which edge wins? This functions decides. |
| 355 |
static bool is_dominant_edge(ImVec2 edge) |
| 356 |
{ |
| 357 |
// return edge.x < 0 || (edge.x == 0 && edge.y > 0); |
| 358 |
return edge.y > 0 || (edge.y == 0 && edge.x < 0); |
| 359 |
} |
| 360 |
|
| 361 |
// Handles triangles in any winding order (CW/CCW) |
| 362 |
static void paint_triangle(const PaintTarget &target, |
| 363 |
const SWTexture *texture, |
| 364 |
const ImVec4 &clip_rect, |
| 365 |
const ImDrawVert &v0, |
| 366 |
const ImDrawVert &v1, |
| 367 |
const ImDrawVert &v2) |
| 368 |
{ |
| 369 |
const ImVec2 p0 = ImVec2(v0.pos.x, v0.pos.y); |
| 370 |
const ImVec2 p1 = ImVec2(v1.pos.x, v1.pos.y); |
| 371 |
const ImVec2 p2 = ImVec2(v2.pos.x, v2.pos.y); |
| 372 |
|
| 373 |
const auto rect_area = barycentric(p0, p1, p2);// Can be positive or negative depending on winding order |
| 374 |
if (rect_area == 0.0f) { return; } |
| 375 |
// if (rect_area < 0.0f) { return paint_triangle(target, texture, clip_rect, v0, v2, v1); } |
| 376 |
|
| 377 |
// Find bounding box: |
| 378 |
float min_x_f = min3(p0.x, p1.x, p2.x); |
| 379 |
float min_y_f = min3(p0.y, p1.y, p2.y); |
| 380 |
float max_x_f = max3(p0.x, p1.x, p2.x); |
| 381 |
float max_y_f = max3(p0.y, p1.y, p2.y); |
| 382 |
|
| 383 |
// Clip against clip_rect: |
| 384 |
min_x_f = std::max(min_x_f, clip_rect.x); |
| 385 |
min_y_f = std::max(min_y_f, clip_rect.y); |
| 386 |
max_x_f = std::min(max_x_f, clip_rect.z - 0.5f); |
| 387 |
max_y_f = std::min(max_y_f, clip_rect.w - 0.5f); |
| 388 |
|
| 389 |
// Integer bounding box [min, max): |
| 390 |
int min_x_i = (int)(min_x_f); |
| 391 |
int min_y_i = (int)(min_y_f); |
| 392 |
int max_x_i = (int)(max_x_f + 1.0f); |
| 393 |
int max_y_i = (int)(max_y_f + 1.0f); |
| 394 |
|
| 395 |
// Clip against render target: |
| 396 |
min_x_i = std::max(min_x_i, 0); |
| 397 |
min_y_i = std::max(min_y_i, 0); |
| 398 |
max_x_i = std::min(max_x_i, target.width); |
| 399 |
max_y_i = std::min(max_y_i, target.height); |
| 400 |
|
| 401 |
// ------------------------------------------------------------------------ |
| 402 |
// Set up interpolation of barycentric coordinates: |
| 403 |
|
| 404 |
const auto topleft = ImVec2(min_x_i + 0.5f, min_y_i + 0.5f); |
| 405 |
const auto dx = ImVec2(1, 0); |
| 406 |
const auto dy = ImVec2(0, 1); |
| 407 |
|
| 408 |
const auto w0_topleft = barycentric(p1, p2, topleft); |
| 409 |
const auto w1_topleft = barycentric(p2, p0, topleft); |
| 410 |
const auto w2_topleft = barycentric(p0, p1, topleft); |
| 411 |
|
| 412 |
const auto w0_dx = barycentric(p1, p2, topleft + dx) - w0_topleft; |
| 413 |
const auto w1_dx = barycentric(p2, p0, topleft + dx) - w1_topleft; |
| 414 |
const auto w2_dx = barycentric(p0, p1, topleft + dx) - w2_topleft; |
| 415 |
|
| 416 |
const auto w0_dy = barycentric(p1, p2, topleft + dy) - w0_topleft; |
| 417 |
const auto w1_dy = barycentric(p2, p0, topleft + dy) - w1_topleft; |
| 418 |
const auto w2_dy = barycentric(p0, p1, topleft + dy) - w2_topleft; |
| 419 |
|
| 420 |
const Barycentric bary_0{ 1, 0, 0 }; |
| 421 |
const Barycentric bary_1{ 0, 1, 0 }; |
| 422 |
const Barycentric bary_2{ 0, 0, 1 }; |
| 423 |
|
| 424 |
const auto inv_area = 1 / rect_area; |
| 425 |
const Barycentric bary_topleft = inv_area * (w0_topleft * bary_0 + w1_topleft * bary_1 + w2_topleft * bary_2); |
| 426 |
const Barycentric bary_dx = inv_area * (w0_dx * bary_0 + w1_dx * bary_1 + w2_dx * bary_2); |
| 427 |
const Barycentric bary_dy = inv_area * (w0_dy * bary_0 + w1_dy * bary_1 + w2_dy * bary_2); |
| 428 |
|
| 429 |
Barycentric bary_current_row = bary_topleft; |
| 430 |
|
| 431 |
// ------------------------------------------------------------------------ |
| 432 |
// For pixel-perfect inside/outside testing: |
| 433 |
|
| 434 |
const int sign = rect_area > 0 ? 1 : -1;// winding order? |
| 435 |
|
| 436 |
const int bias0i = is_dominant_edge(p2 - p1) ? 0 : -1; |
| 437 |
const int bias1i = is_dominant_edge(p0 - p2) ? 0 : -1; |
| 438 |
const int bias2i = is_dominant_edge(p1 - p0) ? 0 : -1; |
| 439 |
|
| 440 |
const auto p0i = as_point(p0); |
| 441 |
const auto p1i = as_point(p1); |
| 442 |
const auto p2i = as_point(p2); |
| 443 |
|
| 444 |
// ------------------------------------------------------------------------ |
| 445 |
|
| 446 |
const bool has_uniform_color = (v0.col == v1.col && v0.col == v2.col); |
| 447 |
|
| 448 |
const ColorInt c0 = ColorInt::bgra(v0.col); |
| 449 |
const ColorInt c1 = ColorInt::bgra(v1.col); |
| 450 |
const ColorInt c2 = ColorInt::bgra(v2.col); |
| 451 |
|
| 452 |
// We often blend the same colors over and over again, so optimize for this (saves 10% total cpu): |
| 453 |
uint32_t last_target_pixel = 0; |
| 454 |
const ColorInt* lastColorRef = (const ColorInt*)(&last_target_pixel); |
| 455 |
const ColorInt colorRef = ColorInt::bgra(v0.col); |
| 456 |
uint32_t last_output = blend(*lastColorRef, colorRef); |
| 457 |
|
| 458 |
for (int y = min_y_i; y < max_y_i; ++y) { |
| 459 |
auto bary = bary_current_row; |
| 460 |
|
| 461 |
bool has_been_inside_this_row = false; |
| 462 |
|
| 463 |
uint32_t* target_pixel = &target.pixels[y * target.width + min_x_i - 1]; |
| 464 |
|
| 465 |
for (int x = min_x_i; x < max_x_i; ++x) { |
| 466 |
const auto w0 = bary.w0; |
| 467 |
const auto w1 = bary.w1; |
| 468 |
const auto w2 = bary.w2; |
| 469 |
bary += bary_dx; |
| 470 |
|
| 471 |
++target_pixel; |
| 472 |
|
| 473 |
{ |
| 474 |
// Inside/outside test: |
| 475 |
const auto p = Point{ kFixedBias * x + kFixedBias / 2, kFixedBias * y + kFixedBias / 2 }; |
| 476 |
const auto w0i = sign * orient2d(p1i, p2i, p) + bias0i; |
| 477 |
const auto w1i = sign * orient2d(p2i, p0i, p) + bias1i; |
| 478 |
const auto w2i = sign * orient2d(p0i, p1i, p) + bias2i; |
| 479 |
if (w0i < 0 || w1i < 0 || w2i < 0) { |
| 480 |
if (has_been_inside_this_row) { |
| 481 |
break;// Gives a nice 10% speedup |
| 482 |
} else { |
| 483 |
continue; |
| 484 |
} |
| 485 |
} |
| 486 |
} |
| 487 |
has_been_inside_this_row = true; |
| 488 |
|
| 489 |
|
| 490 |
if (has_uniform_color && !texture) { |
| 491 |
if (*target_pixel == last_target_pixel) { |
| 492 |
*target_pixel = last_output; |
| 493 |
continue; |
| 494 |
} |
| 495 |
last_target_pixel = *target_pixel; |
| 496 |
*target_pixel = blend(*lastColorRef, colorRef); |
| 497 |
last_output = *target_pixel; |
| 498 |
continue; |
| 499 |
} |
| 500 |
|
| 501 |
ColorInt src_color; |
| 502 |
|
| 503 |
if (has_uniform_color) { |
| 504 |
src_color = c0; |
| 505 |
} else { |
| 506 |
src_color = w0 * c0 + w1 * c1 + w2 * c2; |
| 507 |
} |
| 508 |
|
| 509 |
if (texture) { |
| 510 |
if (!texture->isAlpha) { printf("warning: different texture\n"); } |
| 511 |
|
| 512 |
const ImVec2 uv = w0 * v0.uv + w1 * v1.uv + w2 * v2.uv; |
| 513 |
int x = uv.x * (texture->width - 1.0f) + 0.5f; |
| 514 |
int y = uv.y * (texture->height - 1.0f) + 0.5f; |
| 515 |
src_color.a = (src_color.a * sample_font_texture(*texture, x, y) + 255) >> 8; |
| 516 |
} |
| 517 |
|
| 518 |
if (!src_color.a) { continue; }// Transparent. |
| 519 |
if (src_color.a == 255) { |
| 520 |
// Opaque, no blending needed: |
| 521 |
*target_pixel = src_color.u32; |
| 522 |
continue; |
| 523 |
} |
| 524 |
|
| 525 |
const ColorInt* target_color = (const ColorInt*)target_pixel; |
| 526 |
*target_pixel = blend(*target_color, src_color); |
| 527 |
} |
| 528 |
|
| 529 |
bary_current_row += bary_dy; |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
static void paint_draw_cmd(const PaintTarget &target, |
| 534 |
const ImDrawVert *vertices, |
| 535 |
const ImDrawIdx *idx_buffer, |
| 536 |
const ImDrawCmd &pcmd, |
| 537 |
const ImVec2& white_uv) |
| 538 |
{ |
| 539 |
const SWTexture* texture = (const SWTexture*)(pcmd.GetTexID()); |
| 540 |
IM_ASSERT(texture); |
| 541 |
|
| 542 |
for (unsigned int i = 0; i + 3 <= pcmd.ElemCount;) { |
| 543 |
ImDrawVert v0 = vertices[idx_buffer[i + 0]]; |
| 544 |
ImDrawVert v1 = vertices[idx_buffer[i + 1]]; |
| 545 |
ImDrawVert v2 = vertices[idx_buffer[i + 2]]; |
| 546 |
|
| 547 |
// Text is common, and is made of textured rectangles. So let's optimize for it. |
| 548 |
// This assumes the ImGui way to layout text does not change. |
| 549 |
if (i + 6 <= pcmd.ElemCount && idx_buffer[i + 3] == idx_buffer[i + 0] |
| 550 |
&& idx_buffer[i + 4] == idx_buffer[i + 2]) { |
| 551 |
ImDrawVert v3 = vertices[idx_buffer[i + 5]]; |
| 552 |
|
| 553 |
if (v0.pos.x == v3.pos.x && v1.pos.x == v2.pos.x && v0.pos.y == v1.pos.y && v2.pos.y == v3.pos.y |
| 554 |
&& v0.uv.x == v3.uv.x && v1.uv.x == v2.uv.x && v0.uv.y == v1.uv.y && v2.uv.y == v3.uv.y) { |
| 555 |
const bool has_uniform_color = v0.col == v1.col && v0.col == v2.col && v0.col == v3.col; |
| 556 |
|
| 557 |
const bool has_texture = v0.uv != white_uv || v1.uv != white_uv || v2.uv != white_uv || v3.uv != white_uv; |
| 558 |
|
| 559 |
if (has_uniform_color && has_texture) { |
| 560 |
paint_uniform_textured_rectangle(target, *texture, pcmd.ClipRect, v0, v2); |
| 561 |
i += 6; |
| 562 |
continue; |
| 563 |
} |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
// A lot of the big stuff are uniformly colored rectangles, |
| 568 |
// so we can save a lot of CPU by detecting them: |
| 569 |
if (i + 6 <= pcmd.ElemCount) { |
| 570 |
ImDrawVert v3 = vertices[idx_buffer[i + 3]]; |
| 571 |
ImDrawVert v4 = vertices[idx_buffer[i + 4]]; |
| 572 |
ImDrawVert v5 = vertices[idx_buffer[i + 5]]; |
| 573 |
|
| 574 |
ImVec2 min, max; |
| 575 |
min.x = min3(v0.pos.x, v1.pos.x, v2.pos.x); |
| 576 |
min.y = min3(v0.pos.y, v1.pos.y, v2.pos.y); |
| 577 |
max.x = max3(v0.pos.x, v1.pos.x, v2.pos.x); |
| 578 |
max.y = max3(v0.pos.y, v1.pos.y, v2.pos.y); |
| 579 |
|
| 580 |
// Not the prettiest way to do this, but it catches all cases |
| 581 |
// of a rectangle split into two triangles. |
| 582 |
// TODO: Stop it from also assuming duplicate triangles is one rectangle. |
| 583 |
if ((v0.pos.x == min.x || v0.pos.x == max.x) && (v0.pos.y == min.y || v0.pos.y == max.y) |
| 584 |
&& (v1.pos.x == min.x || v1.pos.x == max.x) && (v1.pos.y == min.y || v1.pos.y == max.y) |
| 585 |
&& (v2.pos.x == min.x || v2.pos.x == max.x) && (v2.pos.y == min.y || v2.pos.y == max.y) |
| 586 |
&& (v3.pos.x == min.x || v3.pos.x == max.x) && (v3.pos.y == min.y || v3.pos.y == max.y) |
| 587 |
&& (v4.pos.x == min.x || v4.pos.x == max.x) && (v4.pos.y == min.y || v4.pos.y == max.y) |
| 588 |
&& (v5.pos.x == min.x || v5.pos.x == max.x) && (v5.pos.y == min.y || v5.pos.y == max.y)) { |
| 589 |
const bool has_uniform_color = |
| 590 |
v0.col == v1.col && v0.col == v2.col && v0.col == v3.col && v0.col == v4.col && v0.col == v5.col; |
| 591 |
|
| 592 |
min.x = std::max(min.x, pcmd.ClipRect.x); |
| 593 |
min.y = std::max(min.y, pcmd.ClipRect.y); |
| 594 |
max.x = std::min(max.x, pcmd.ClipRect.z - 0.5f); |
| 595 |
max.y = std::min(max.y, pcmd.ClipRect.w - 0.5f); |
| 596 |
|
| 597 |
if (max.x < min.x || max.y < min.y) { |
| 598 |
i += 6; |
| 599 |
continue; |
| 600 |
}// Completely clipped |
| 601 |
|
| 602 |
if (has_uniform_color) { |
| 603 |
const ColorInt colorRef = ColorInt::bgra(v0.col); |
| 604 |
paint_uniform_rectangle(target, min, max, colorRef); |
| 605 |
i += 6; |
| 606 |
continue; |
| 607 |
} |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
const bool has_texture = (v0.uv != white_uv || v1.uv != white_uv || v2.uv != white_uv); |
| 612 |
paint_triangle(target, has_texture ? texture : nullptr, pcmd.ClipRect, v0, v1, v2); |
| 613 |
i += 3; |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
static void paint_draw_list(const PaintTarget &target, const ImDrawList *cmd_list) |
| 618 |
{ |
| 619 |
const ImDrawIdx *idx_buffer = &cmd_list->IdxBuffer[0]; |
| 620 |
const ImDrawVert *vertices = cmd_list->VtxBuffer.Data; |
| 621 |
const ImVec2 white_uv = cmd_list->_Data->TexUvWhitePixel; |
| 622 |
|
| 623 |
for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.size(); cmd_i++) { |
| 624 |
const ImDrawCmd &pcmd = cmd_list->CmdBuffer[cmd_i]; |
| 625 |
if (pcmd.UserCallback) { |
| 626 |
pcmd.UserCallback(cmd_list, &pcmd); |
| 627 |
} else { |
| 628 |
paint_draw_cmd(target, vertices, idx_buffer, pcmd, white_uv); |
| 629 |
} |
| 630 |
idx_buffer += pcmd.ElemCount; |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
static void paint_imgui(uint32_t *pixels, ImDrawData *drawData, int fb_width, int fb_height) |
| 635 |
{ |
| 636 |
if (fb_width <= 0 || fb_height <= 0) return; |
| 637 |
|
| 638 |
PaintTarget target{ pixels, fb_width, fb_height }; |
| 639 |
|
| 640 |
for (int i = 0; i < drawData->CmdListsCount; ++i) { |
| 641 |
paint_draw_list(target, drawData->CmdLists[i]); |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
/// NEW STUFF |
| 646 |
|
| 647 |
bool ImGui_ImplSW_Init(SDL_Window* win) { |
| 648 |
ImGuiIO& io = ImGui::GetIO(); |
| 649 |
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 650 |
IM_ASSERT(io.BackendRendererUserData == nullptr); |
| 651 |
|
| 652 |
if (SDL_HasWindowSurface(win)==SDL_FALSE) { |
| 653 |
return false; |
| 654 |
} |
| 655 |
|
| 656 |
ImGui_ImplSW_Data* bd = IM_NEW(ImGui_ImplSW_Data)(); |
| 657 |
bd->Window = win; |
| 658 |
io.BackendRendererUserData = (void*)bd; |
| 659 |
io.BackendRendererName = "imgui_sw"; |
| 660 |
io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; |
| 661 |
|
| 662 |
platform_io.Renderer_TextureMaxWidth = platform_io.Renderer_TextureMaxHeight = (int)4096; |
| 663 |
|
| 664 |
return true; |
| 665 |
} |
| 666 |
|
| 667 |
void ImGui_ImplSW_Shutdown() { |
| 668 |
ImGui_ImplSW_Data* bd = ImGui_ImplSW_GetBackendData(); |
| 669 |
IM_ASSERT(bd != nullptr); |
| 670 |
ImGuiIO& io = ImGui::GetIO(); |
| 671 |
|
| 672 |
ImGui_ImplSW_DestroyDeviceObjects(); |
| 673 |
io.BackendRendererName = nullptr; |
| 674 |
io.BackendRendererUserData = nullptr; |
| 675 |
io.BackendFlags &= ~ImGuiBackendFlags_RendererHasTextures; |
| 676 |
IM_DELETE(bd); |
| 677 |
} |
| 678 |
|
| 679 |
void ImGui_ImplSW_NewFrame() { |
| 680 |
ImGui_ImplSW_Data* bd = ImGui_ImplSW_GetBackendData(); |
| 681 |
IM_ASSERT(bd != nullptr); |
| 682 |
} |
| 683 |
|
| 684 |
void ImGui_ImplSW_RenderDrawData(ImDrawData* draw_data) { |
| 685 |
ImGui_ImplSW_Data* bd = ImGui_ImplSW_GetBackendData(); |
| 686 |
IM_ASSERT(bd != nullptr); |
| 687 |
|
| 688 |
// update textures if needed |
| 689 |
if (draw_data->Textures!=NULL) { |
| 690 |
for (ImTextureData* i: *draw_data->Textures) { |
| 691 |
if (i->Status!=ImTextureStatus_OK) ImGui_ImplSW_UpdateTexture(i); |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
SDL_Surface* surf = SDL_GetWindowSurface(bd->Window); |
| 696 |
if (!surf) return; |
| 697 |
|
| 698 |
bool mustLock=SDL_MUSTLOCK(surf); |
| 699 |
if (mustLock) { |
| 700 |
if (SDL_LockSurface(surf)!=0) return; |
| 701 |
} |
| 702 |
paint_imgui((uint32_t*)surf->pixels,draw_data,surf->w,surf->h); |
| 703 |
// 0xAARRGGBB |
| 704 |
if (mustLock) { |
| 705 |
SDL_UnlockSurface(surf); |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
/// CREATE OBJECTS |
| 710 |
|
| 711 |
bool ImGui_ImplSW_CreateDeviceObjects() { |
| 712 |
return true; |
| 713 |
} |
| 714 |
|
| 715 |
void ImGui_ImplSW_DestroyDeviceObjects() { |
| 716 |
} |
| 717 |
|
| 718 |
void ImGui_ImplSW_UpdateTexture(ImTextureData* tex) { |
| 719 |
if (tex->Status==ImTextureStatus_WantCreate) { |
| 720 |
SWTexture* t=new SWTexture(tex->Width,tex->Height,tex->Format==ImTextureFormat_Alpha8); |
| 721 |
memcpy(t->pixels,tex->GetPixels(),tex->GetSizeInBytes()); |
| 722 |
|
| 723 |
tex->SetTexID((ImTextureID)t); |
| 724 |
tex->SetStatus(ImTextureStatus_OK); |
| 725 |
} else if (tex->Status==ImTextureStatus_WantUpdates) { |
| 726 |
SWTexture* t=(SWTexture*)tex->GetTexID(); |
| 727 |
|
| 728 |
// we don't support format changes (this should not happen) |
| 729 |
assert(t->isAlpha==(tex->Format==ImTextureFormat_Alpha8)); |
| 730 |
|
| 731 |
if (t->width!=tex->Width || t->height!=tex->Height) { |
| 732 |
// width/height changed; recreate texture |
| 733 |
SWTexture* newTex=new SWTexture(tex->Width,tex->Height,tex->Format==ImTextureFormat_Alpha8); |
| 734 |
|
| 735 |
// copy previous texture to new one |
| 736 |
int i_y=0; |
| 737 |
int i_y1=0; |
| 738 |
if (t->isAlpha) { |
| 739 |
for (int i=0; i<t->height; i++) { |
| 740 |
unsigned char* dataOld=(unsigned char*)t->pixels; |
| 741 |
unsigned char* dataNew=(unsigned char*)newTex->pixels; |
| 742 |
memcpy(&dataNew[i_y1],&dataOld[i_y],t->width); |
| 743 |
i_y1+=newTex->width; |
| 744 |
i_y+=t->width; |
| 745 |
} |
| 746 |
} else { |
| 747 |
for (int i=0; i<t->height; i++) { |
| 748 |
uint32_t* dataOld=t->pixels; |
| 749 |
uint32_t* dataNew=newTex->pixels; |
| 750 |
memcpy(&dataNew[i_y1],&dataOld[i_y],t->width*sizeof(uint32_t)); |
| 751 |
i_y1+=newTex->width; |
| 752 |
i_y+=t->width; |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
// delete previous texture and change texture ID |
| 757 |
tex->SetTexID((ImTextureID)newTex); |
| 758 |
delete t; |
| 759 |
t=newTex; |
| 760 |
} |
| 761 |
|
| 762 |
// update region |
| 763 |
if (t->isAlpha) { |
| 764 |
unsigned char* data=(unsigned char*)t->pixels; |
| 765 |
int i_y=t->width*tex->UpdateRect.y; |
| 766 |
for (int i=tex->UpdateRect.y; i<tex->UpdateRect.y+tex->UpdateRect.w; i++) { |
| 767 |
memcpy(&data[i_y],tex->GetPixelsAt(tex->UpdateRect.x,i),tex->UpdateRect.w); |
| 768 |
i_y+=t->width; |
| 769 |
} |
| 770 |
} else { |
| 771 |
uint32_t* data=t->pixels; |
| 772 |
int i_y=t->width*tex->UpdateRect.y; |
| 773 |
for (int i=tex->UpdateRect.y; i<tex->UpdateRect.y+tex->UpdateRect.w; i++) { |
| 774 |
memcpy(&data[i_y],tex->GetPixelsAt(tex->UpdateRect.x,i),tex->UpdateRect.w*sizeof(uint32_t)); |
| 775 |
i_y+=t->width; |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
tex->SetStatus(ImTextureStatus_OK); |
| 780 |
} else if (tex->Status==ImTextureStatus_WantDestroy && tex->UnusedFrames>0) { |
| 781 |
SWTexture* t=(SWTexture*)tex->GetTexID(); |
| 782 |
delete t; |
| 783 |
|
| 784 |
tex->SetTexID(ImTextureID_Invalid); |
| 785 |
tex->SetStatus(ImTextureStatus_Destroyed); |
| 786 |
} else { |
| 787 |
// ???????? |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
#endif // #ifndef IMGUI_DISABLE |