mirror of
https://github.com/stevenrobertson/cuburn.git
synced 2025-07-12 03:05:14 -04:00
Palette interpolation on device
This commit is contained in:
@ -313,6 +313,33 @@ void test_cr(const float *times, const float *knots, const float *t, float *r) {
|
||||
r[i] = catmull_rom(times, knots, t[i]);
|
||||
}
|
||||
|
||||
__global__
|
||||
void interp_palette_hsv(uchar4 *outs, const float *times, const float4 *sources,
|
||||
float tstart, float tstep) {
|
||||
float time = tstart + blockIdx.x * tstep;
|
||||
int idx = fmaxf(bitwise_binsearch(times, time), 1);
|
||||
|
||||
float4 left = sources[blockDim.x * (idx - 1) + threadIdx.x];
|
||||
float4 right = sources[blockDim.x * (idx) + threadIdx.x];
|
||||
|
||||
float lf = (times[idx] - time) / (times[idx] - times[idx-1]);
|
||||
float rf = 1.0f - lf;
|
||||
|
||||
float3 lhsv = rgb2hsv(make_float3(left.x, left.y, left.z));
|
||||
float3 rhsv = rgb2hsv(make_float3(right.x, right.y, right.z));
|
||||
|
||||
float3 hsv;
|
||||
hsv.x = lhsv.x * lf + rhsv.x * rf;
|
||||
hsv.y = lhsv.y * lf + rhsv.y * rf;
|
||||
hsv.z = lhsv.z * lf + rhsv.z * rf;
|
||||
float3 rgb = hsv2rgb(hsv);
|
||||
|
||||
uchar4 out;
|
||||
out.x = rgb.x * 255.0f;
|
||||
out.y = rgb.y * 255.0f;
|
||||
out.z = rgb.z * 255.0f;
|
||||
out.w = 255.0f * (left.z * lf + right.z * rf);
|
||||
outs[blockDim.x * blockIdx.x + threadIdx.x] = out;
|
||||
}
|
||||
""")
|
||||
|
||||
|
@ -35,6 +35,11 @@ class BaseCode(HunkOCode):
|
||||
#include<cuda.h>
|
||||
#include<stdint.h>
|
||||
#include<stdio.h>
|
||||
"""
|
||||
|
||||
decls = """
|
||||
float3 rgb2hsv(float3 rgb);
|
||||
float3 hsv2rgb(float3 hsv);
|
||||
"""
|
||||
|
||||
defs = r"""
|
||||
@ -134,7 +139,49 @@ void write_half(float &xy, float x, float y, float den) {
|
||||
: "=f"(xy) : "f"(x), "f"(y), "f"(den));
|
||||
}
|
||||
|
||||
__device__
|
||||
float3 rgb2hsv(float3 rgb) {
|
||||
float M = fmaxf(fmaxf(rgb.x, rgb.y), rgb.z);
|
||||
float m = fminf(fminf(rgb.x, rgb.y), rgb.z);
|
||||
float C = M - m;
|
||||
|
||||
float s = M > 0.0f ? C / M : 0.0f;
|
||||
|
||||
float h;
|
||||
if (s != 0.0f) {
|
||||
C = 1.0f / C;
|
||||
float rc = (M - rgb.x) * C;
|
||||
float gc = (M - rgb.y) * C;
|
||||
float bc = (M - rgb.z) * C;
|
||||
|
||||
if (rgb.x == M) h = bc - gc;
|
||||
else if (rgb.y == M) h = 2 + gc - bc;
|
||||
else h = 4 + gc - rc;
|
||||
|
||||
if (h < 0) h += 6;
|
||||
}
|
||||
return make_float3(h, s, M);
|
||||
}
|
||||
|
||||
__device__
|
||||
float3 hsv2rgb(float3 hsv) {
|
||||
|
||||
float whole = floorf(hsv.x);
|
||||
float frac = hsv.x - whole;
|
||||
float val = hsv.z;
|
||||
float min = val * (1 - hsv.y);
|
||||
float mid = val * (1 - (hsv.y * frac));
|
||||
float alt = val * (1 - (hsv.y * (1 - frac)));
|
||||
|
||||
float3 out;
|
||||
if (whole == 0.0f) { out.x = val; out.y = alt; out.z = min; }
|
||||
else if (whole == 1.0f) { out.x = mid; out.y = val; out.z = min; }
|
||||
else if (whole == 2.0f) { out.x = min; out.y = val; out.z = alt; }
|
||||
else if (whole == 3.0f) { out.x = min; out.y = mid; out.z = val; }
|
||||
else if (whole == 4.0f) { out.x = alt; out.y = min; out.z = val; }
|
||||
else { out.x = val; out.y = min; out.z = mid; }
|
||||
return out;
|
||||
}
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
|
Reference in New Issue
Block a user