2023-06-15 05:21:36 +10:00
|
|
|
#version 450
|
|
|
|
|
|
|
|
// See compose.slang for copyright and other information.
|
|
|
|
|
2023-09-15 10:55:13 +10:00
|
|
|
#include "../../../misc/shaders/crop_and_scale/crop_and_scale.slang"
|
2023-06-15 05:21:36 +10:00
|
|
|
#include "parameters.slang"
|
|
|
|
|
|
|
|
layout(push_constant) uniform Push {
|
|
|
|
vec4 InputSize;
|
|
|
|
vec4 FinalViewportSize;
|
2023-09-11 00:15:41 +10:00
|
|
|
uint Rotation;
|
2023-09-15 10:55:13 +10:00
|
|
|
// Own settings
|
2023-09-11 00:15:41 +10:00
|
|
|
float EXTEND_H;
|
|
|
|
float EXTEND_V;
|
2023-06-15 05:21:36 +10:00
|
|
|
float MIRROR_BLUR;
|
2023-09-15 10:55:13 +10:00
|
|
|
float SAMPLE_SIZE;
|
|
|
|
// From crop and scale, scaling section
|
2023-06-15 05:21:36 +10:00
|
|
|
float FORCE_ASPECT_RATIO;
|
|
|
|
float ASPECT_H;
|
|
|
|
float ASPECT_V;
|
2023-09-11 00:15:41 +10:00
|
|
|
float FORCE_INTEGER_SCALING_H;
|
|
|
|
float FORCE_INTEGER_SCALING_V;
|
2023-09-15 10:55:13 +10:00
|
|
|
float OVERSCALE;
|
|
|
|
// From crop and scale, cropping section
|
|
|
|
float OS_CROP_TOP;
|
|
|
|
float OS_CROP_BOTTOM;
|
|
|
|
float OS_CROP_LEFT;
|
|
|
|
float OS_CROP_RIGHT;
|
|
|
|
float CENTER_AFTER_CROPPING;
|
2023-06-15 05:21:36 +10:00
|
|
|
}
|
|
|
|
param;
|
|
|
|
|
|
|
|
layout(std140, set = 0, binding = 0) uniform UBO { mat4 MVP; }
|
|
|
|
global;
|
|
|
|
|
|
|
|
#pragma stage vertex
|
|
|
|
layout(location = 0) in vec4 Position;
|
|
|
|
layout(location = 1) in vec2 TexCoord;
|
|
|
|
layout(location = 0) out vec2 vTexCoord;
|
2023-09-11 00:15:41 +10:00
|
|
|
layout(location = 1) out vec2 tx_coord;
|
|
|
|
layout(location = 2) out vec4 input_corners;
|
2023-06-15 05:21:36 +10:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
gl_Position = global.MVP * Position;
|
|
|
|
vTexCoord = TexCoord;
|
2023-09-11 00:15:41 +10:00
|
|
|
const vec4 crop = vec4(param.OS_CROP_TOP, param.OS_CROP_LEFT,
|
|
|
|
param.OS_CROP_BOTTOM, param.OS_CROP_RIGHT);
|
|
|
|
const vec2 scale_o2i = get_scale_o2i(
|
|
|
|
param.InputSize.xy, param.FinalViewportSize.xy, crop, param.Rotation,
|
|
|
|
param.CENTER_AFTER_CROPPING, param.FORCE_ASPECT_RATIO,
|
|
|
|
vec2(param.ASPECT_H, param.ASPECT_V),
|
2023-09-15 10:55:13 +10:00
|
|
|
vec2(param.FORCE_INTEGER_SCALING_H, param.FORCE_INTEGER_SCALING_V), param.OVERSCALE,
|
2023-09-11 00:15:41 +10:00
|
|
|
/* output_size_is_final_viewport_size = */ true);
|
|
|
|
tx_coord = o2i(vTexCoord, param.InputSize.xy, crop, param.Rotation,
|
|
|
|
param.CENTER_AFTER_CROPPING, scale_o2i);
|
|
|
|
input_corners = get_input_corners(param.InputSize.xy, crop, param.Rotation);
|
2023-06-15 05:21:36 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma stage fragment
|
|
|
|
layout(location = 0) in vec2 vTexCoord;
|
2023-09-11 00:15:41 +10:00
|
|
|
layout(location = 1) in vec2 tx_coord;
|
|
|
|
layout(location = 2) in vec4 input_corners;
|
2023-06-15 05:21:36 +10:00
|
|
|
layout(location = 0) out vec4 FragColor;
|
2023-06-23 08:09:07 +10:00
|
|
|
layout(set = 0, binding = 2) uniform sampler2D Source;
|
2023-06-15 05:21:36 +10:00
|
|
|
|
2023-06-17 04:04:32 +10:00
|
|
|
float min_of_vec4(vec4 i) { return min(min(i.x, i.y), min(i.z, i.w)); }
|
2023-06-15 05:21:36 +10:00
|
|
|
|
|
|
|
int argmin(vec4 i) {
|
2023-06-17 04:04:32 +10:00
|
|
|
const vec4 m = vec4(min_of_vec4(i));
|
2023-06-15 05:21:36 +10:00
|
|
|
const vec4 ma = step(i, m) * vec4(0.0, -1.0, -2.0, -3.0);
|
2023-06-17 04:04:32 +10:00
|
|
|
return -int(min_of_vec4(ma));
|
2023-06-15 05:21:36 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a coordinate in [0, w) when repeating that interval (and optionally
|
|
|
|
// mirroring).
|
|
|
|
float mirrored_repeat(float w, float x) {
|
|
|
|
const float phase = mod(x, w);
|
|
|
|
if (param.MIRROR_BLUR < 0.5) {
|
|
|
|
return phase;
|
|
|
|
}
|
|
|
|
const int period = int(x / w);
|
|
|
|
return period % 2 == 1 != x > 0.0 ? phase : w - phase;
|
|
|
|
}
|
|
|
|
|
2023-09-11 00:15:41 +10:00
|
|
|
float extend_left(vec2 coord, vec4 input_corners) {
|
|
|
|
return input_corners.x +
|
|
|
|
mirrored_repeat(param.SAMPLE_SIZE, coord.x - input_corners.x);
|
2023-06-15 05:21:36 +10:00
|
|
|
}
|
|
|
|
|
2023-09-11 00:15:41 +10:00
|
|
|
float extend_right(vec2 coord, vec4 input_corners) {
|
|
|
|
return input_corners.z -
|
|
|
|
mirrored_repeat(param.SAMPLE_SIZE, input_corners.z - coord.x);
|
2023-06-15 05:21:36 +10:00
|
|
|
}
|
|
|
|
|
2023-09-11 00:15:41 +10:00
|
|
|
float extend_top(vec2 coord, vec4 input_corners) {
|
|
|
|
return input_corners.y +
|
|
|
|
mirrored_repeat(param.SAMPLE_SIZE, coord.y - input_corners.y);
|
2023-06-15 05:21:36 +10:00
|
|
|
}
|
|
|
|
|
2023-09-11 00:15:41 +10:00
|
|
|
float extend_bottom(vec2 coord, vec4 input_corners) {
|
|
|
|
return input_corners.w -
|
|
|
|
mirrored_repeat(param.SAMPLE_SIZE, input_corners.w - coord.y);
|
2023-06-15 05:21:36 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function samples in a very specific way which is the foundation for
|
|
|
|
// blurring later.
|
|
|
|
// - If the sample coordinate is outside of the cropped input, Either black is
|
|
|
|
// returned if blur extension is turned off, or a repeated pattern from the
|
|
|
|
// sampling frame band is returned.
|
|
|
|
// - If the coordinate is inside the cropped input and within the frame band
|
|
|
|
// given by SAMPLE_SIZE, the original texture sample is returned.
|
|
|
|
// - If the coordinate is further inside than the frame band, a mirrored
|
|
|
|
// repeating sample is returned. The side of the frame that is sampled is given
|
|
|
|
// by the one that is closest to the sampled point.
|
2023-09-11 00:15:41 +10:00
|
|
|
vec3 sample_mirrored_frame(sampler2D tex, vec2 tx_coord, vec4 input_corners) {
|
|
|
|
const vec2 extend_fill =
|
|
|
|
get_rotated_size(vec2(param.EXTEND_H, param.EXTEND_V), param.Rotation);
|
|
|
|
if (tx_coord.x < input_corners.x) {
|
|
|
|
if (extend_fill.x < 0.5) {
|
2023-06-15 05:21:36 +10:00
|
|
|
return vec3(0.0);
|
|
|
|
}
|
2023-09-11 00:15:41 +10:00
|
|
|
if (tx_coord.y < input_corners.y) {
|
2023-06-15 05:21:36 +10:00
|
|
|
// Top left corner extension
|
2023-09-11 00:15:41 +10:00
|
|
|
if (extend_fill.y < 0.5) {
|
2023-06-15 05:21:36 +10:00
|
|
|
return vec3(0.0);
|
|
|
|
}
|
2023-09-11 00:15:41 +10:00
|
|
|
return texture(tex, vec2(extend_left(tx_coord, input_corners),
|
|
|
|
extend_top(tx_coord, input_corners)) *
|
2023-06-15 05:21:36 +10:00
|
|
|
param.InputSize.zw)
|
|
|
|
.rgb;
|
2023-09-11 00:15:41 +10:00
|
|
|
} else if (tx_coord.y < input_corners.w) {
|
2023-06-15 05:21:36 +10:00
|
|
|
// Left extension
|
2023-09-11 00:15:41 +10:00
|
|
|
return texture(tex, vec2(extend_left(tx_coord, input_corners),
|
|
|
|
tx_coord.y) *
|
|
|
|
param.InputSize.zw)
|
2023-06-15 05:21:36 +10:00
|
|
|
.rgb;
|
|
|
|
} else {
|
|
|
|
// Bottom left corner extension
|
2023-09-11 00:15:41 +10:00
|
|
|
if (extend_fill.y < 0.5) {
|
2023-06-15 05:21:36 +10:00
|
|
|
return vec3(0.0);
|
|
|
|
}
|
2023-09-11 00:15:41 +10:00
|
|
|
return texture(tex, vec2(extend_left(tx_coord, input_corners),
|
|
|
|
extend_bottom(tx_coord, input_corners)) *
|
2023-06-15 05:21:36 +10:00
|
|
|
param.InputSize.zw)
|
|
|
|
.rgb;
|
|
|
|
}
|
2023-09-11 00:15:41 +10:00
|
|
|
} else if (tx_coord.x < input_corners.z) {
|
|
|
|
if (tx_coord.y < input_corners.y) {
|
|
|
|
if (extend_fill.y < 0.5) {
|
2023-06-15 05:21:36 +10:00
|
|
|
return vec3(0.0);
|
|
|
|
}
|
|
|
|
// Top extension
|
2023-09-11 00:15:41 +10:00
|
|
|
return texture(tex, vec2(tx_coord.x,
|
|
|
|
extend_top(tx_coord, input_corners)) *
|
|
|
|
param.InputSize.zw)
|
2023-06-15 05:21:36 +10:00
|
|
|
.rgb;
|
2023-09-11 00:15:41 +10:00
|
|
|
} else if (tx_coord.y < input_corners.w) {
|
|
|
|
const vec4 inner_corners =
|
|
|
|
input_corners + vec4(param.SAMPLE_SIZE, param.SAMPLE_SIZE,
|
2023-06-15 05:21:36 +10:00
|
|
|
-param.SAMPLE_SIZE, -param.SAMPLE_SIZE);
|
2023-09-11 00:15:41 +10:00
|
|
|
if (any(lessThan(tx_coord, inner_corners.xy)) ||
|
|
|
|
any(greaterThanEqual(tx_coord, inner_corners.zw))) {
|
2023-06-15 05:21:36 +10:00
|
|
|
// In frame band
|
2023-09-11 00:15:41 +10:00
|
|
|
return texture(tex, tx_coord * param.InputSize.zw).rgb;
|
2023-06-15 05:21:36 +10:00
|
|
|
}
|
|
|
|
// Innermost -- mirrored repeat sampling from nearest side
|
2023-09-11 00:15:41 +10:00
|
|
|
const vec4 distances = vec4(
|
|
|
|
tx_coord.x - inner_corners.x, inner_corners.z - tx_coord.x,
|
|
|
|
tx_coord.y - inner_corners.y, inner_corners.w - tx_coord.y);
|
2023-06-15 05:21:36 +10:00
|
|
|
switch (argmin(distances)) {
|
|
|
|
case 0:
|
|
|
|
// left
|
2023-09-11 00:15:41 +10:00
|
|
|
return texture(tex,
|
|
|
|
vec2(extend_left(tx_coord, input_corners),
|
|
|
|
tx_coord.y) *
|
|
|
|
param.InputSize.zw)
|
2023-06-15 05:21:36 +10:00
|
|
|
.rgb;
|
|
|
|
case 1:
|
|
|
|
// right
|
2023-09-11 00:15:41 +10:00
|
|
|
return texture(tex,
|
|
|
|
vec2(extend_right(tx_coord, input_corners),
|
|
|
|
tx_coord.y) *
|
|
|
|
param.InputSize.zw)
|
2023-06-15 05:21:36 +10:00
|
|
|
.rgb;
|
|
|
|
case 2:
|
|
|
|
// top
|
2023-09-11 00:15:41 +10:00
|
|
|
return texture(tex,
|
|
|
|
vec2(tx_coord.x,
|
|
|
|
extend_top(tx_coord, input_corners)) *
|
|
|
|
param.InputSize.zw)
|
2023-06-15 05:21:36 +10:00
|
|
|
.rgb;
|
|
|
|
case 3:
|
|
|
|
default:
|
|
|
|
// bottom
|
2023-09-11 00:15:41 +10:00
|
|
|
return texture(tex, vec2(tx_coord.x,
|
|
|
|
extend_bottom(tx_coord,
|
|
|
|
input_corners)) *
|
|
|
|
param.InputSize.zw)
|
2023-06-15 05:21:36 +10:00
|
|
|
.rgb;
|
|
|
|
}
|
|
|
|
} else {
|
2023-09-11 00:15:41 +10:00
|
|
|
if (extend_fill.y < 0.5) {
|
2023-06-15 05:21:36 +10:00
|
|
|
return vec3(0.0);
|
|
|
|
}
|
|
|
|
// Bottom extension
|
2023-09-11 00:15:41 +10:00
|
|
|
return texture(tex, vec2(tx_coord.x,
|
|
|
|
extend_bottom(tx_coord, input_corners)) *
|
|
|
|
param.InputSize.zw)
|
2023-06-15 05:21:36 +10:00
|
|
|
.rgb;
|
|
|
|
}
|
|
|
|
} else {
|
2023-09-11 00:15:41 +10:00
|
|
|
if (extend_fill.x < 0.5) {
|
2023-06-15 05:21:36 +10:00
|
|
|
return vec3(0.0);
|
|
|
|
}
|
2023-09-11 00:15:41 +10:00
|
|
|
if (tx_coord.y < input_corners.y) {
|
2023-06-15 05:21:36 +10:00
|
|
|
// Top right corner extension
|
2023-09-11 00:15:41 +10:00
|
|
|
if (extend_fill.y < 0.5) {
|
2023-06-15 05:21:36 +10:00
|
|
|
return vec3(0.0);
|
|
|
|
}
|
2023-09-11 00:15:41 +10:00
|
|
|
return texture(tex, vec2(extend_right(tx_coord, input_corners),
|
|
|
|
extend_top(tx_coord, input_corners)) *
|
2023-06-15 05:21:36 +10:00
|
|
|
param.InputSize.zw)
|
|
|
|
.rgb;
|
2023-09-11 00:15:41 +10:00
|
|
|
} else if (tx_coord.y < input_corners.w) {
|
2023-06-15 05:21:36 +10:00
|
|
|
// Right extension
|
2023-09-11 00:15:41 +10:00
|
|
|
return texture(tex, vec2(extend_right(tx_coord, input_corners),
|
|
|
|
tx_coord.y) *
|
|
|
|
param.InputSize.zw)
|
2023-06-15 05:21:36 +10:00
|
|
|
.rgb;
|
|
|
|
} else {
|
|
|
|
// Bottom right corner extension
|
2023-09-11 00:15:41 +10:00
|
|
|
if (extend_fill.y < 0.5) {
|
2023-06-15 05:21:36 +10:00
|
|
|
return vec3(0.0);
|
|
|
|
}
|
2023-09-11 00:15:41 +10:00
|
|
|
return texture(tex, vec2(extend_right(tx_coord, input_corners),
|
|
|
|
extend_bottom(tx_coord, input_corners)) *
|
2023-06-15 05:21:36 +10:00
|
|
|
param.InputSize.zw)
|
|
|
|
.rgb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void main() {
|
2023-09-11 00:15:41 +10:00
|
|
|
FragColor =
|
|
|
|
vec4(sample_mirrored_frame(Source, tx_coord, input_corners), 1.0);
|
2023-06-15 05:21:36 +10:00
|
|
|
}
|