Pixel-Composer/scripts/shader_functions/shader_functions.gml

252 lines
7.1 KiB
Text
Raw Normal View History

function shader_set_i(uniform, value) {
2023-11-08 08:38:04 +01:00
INLINE
2023-10-03 11:27:36 +02:00
2023-03-25 12:27:04 +01:00
var shader = shader_current();
2023-09-15 20:12:02 +02:00
if(shader == -1) return;
2023-02-28 09:43:01 +01:00
if(is_array(value)) {
shader_set_i_array(shader, uniform, value);
return;
}
2023-10-03 11:27:36 +02:00
switch(argument_count) {
case 2 : shader_set_uniform_i(shader_get_uniform(shader, uniform), value); break;
case 3 : shader_set_uniform_i(shader_get_uniform(shader, uniform), value, argument[2]); break;
case 4 : shader_set_uniform_i(shader_get_uniform(shader, uniform), value, argument[2], argument[3]); break;
default:
var array = array_create(argument_count - 1);
for( var i = 1; i < argument_count; i++ )
array[i - 1] = argument[i];
shader_set_i_array(shader, uniform, array)
}
}
2023-02-28 09:43:01 +01:00
function shader_set_i_array(shader, uniform, array) {
2023-11-08 08:38:04 +01:00
INLINE
2023-10-03 11:27:36 +02:00
2023-02-28 09:43:01 +01:00
shader_set_uniform_i_array(shader_get_uniform(shader, uniform), array);
}
2023-02-28 09:43:01 +01:00
2024-06-24 13:49:54 +02:00
function shader_set_2(uniform, v) { INLINE var shader = shader_current(); shader_set_uniform_f(shader_get_uniform(shader, uniform), aGetF(v, 0), aGetF(v, 1)); }
function shader_set_3(uniform, v) { INLINE var shader = shader_current(); shader_set_uniform_f(shader_get_uniform(shader, uniform), aGetF(v, 0), aGetF(v, 1), aGetF(v, 2)); }
function shader_set_4(uniform, v) { INLINE var shader = shader_current(); shader_set_uniform_f(shader_get_uniform(shader, uniform), aGetF(v, 0), aGetF(v, 1), aGetF(v, 2), aGetF(v, 3)); }
function shader_set_f_array(uniform, value, max_length = 128) {
var shader = shader_current();
if(shader == -1) return;
if(array_empty(value)) return;
shader_set_uniform_f_array_safe(shader_get_uniform(shader, uniform), value, max_length);
}
function shader_set_f(uniform, value) {
2023-11-08 08:38:04 +01:00
INLINE
2023-10-03 11:27:36 +02:00
2023-03-25 12:27:04 +01:00
var shader = shader_current();
2023-09-15 20:12:02 +02:00
if(shader == -1) return;
2023-02-14 02:48:33 +01:00
if(is_array(value)) {
2023-11-24 10:41:53 +01:00
if(array_empty(value)) return;
2023-09-15 20:12:02 +02:00
shader_set_uniform_f_array_safe(shader_get_uniform(shader, uniform), value);
2023-02-14 02:48:33 +01:00
return;
}
2023-07-28 19:41:57 +02:00
2023-10-03 11:27:36 +02:00
switch(argument_count) {
case 2 : shader_set_uniform_f(shader_get_uniform(shader, uniform), value); break;
case 3 : shader_set_uniform_f(shader_get_uniform(shader, uniform), value, argument[2]); break;
case 4 : shader_set_uniform_f(shader_get_uniform(shader, uniform), value, argument[2], argument[3]); break;
case 5 : shader_set_uniform_f(shader_get_uniform(shader, uniform), value, argument[2], argument[3], argument[4]); break;
default:
var array = array_create(argument_count - 1);
for( var i = 1; i < argument_count; i++ )
array[i - 1] = argument[i];
shader_set_uniform_f_array(shader_get_uniform(shader, uniform), array)
2023-02-14 02:48:33 +01:00
}
2023-10-03 11:27:36 +02:00
if(argument_count == 2)
shader_set_uniform_f(shader_get_uniform(shader, uniform), value);
else if(argument_count == 3)
shader_set_uniform_f(shader_get_uniform(shader, uniform), value, argument[2]);
else if(argument_count == 4)
shader_set_uniform_f(shader_get_uniform(shader, uniform), value, argument[2], argument[3]);
else {
var array = array_create(argument_count - 1);
for( var i = 1; i < argument_count; i++ )
array[i - 1] = argument[i];
shader_set_uniform_f_array(shader_get_uniform(shader, uniform), array);
}
}
2023-02-14 02:48:33 +01:00
function shader_set_f_map(uniform, value, surface = noone, junc = noone) {
2023-12-22 14:46:54 +01:00
INLINE
shader_set_f(uniform, is_array(value)? value : [ value, value ]);
if(surface == noone) {
shader_set_i( uniform + "UseSurf", false);
} else {
shader_set_i( uniform + "UseSurf", junc.attributes.mapped && is_surface(surface));
shader_set_surface(uniform + "Surf", surface);
}
}
2023-12-22 14:46:54 +01:00
function shader_set_f_map_s(uniform, value, surface, junc) {
2023-12-22 14:46:54 +01:00
INLINE
shader_set_f(uniform, is_array(value)? value : [ value, value ]);
shader_set_i(uniform + "UseSurf", junc.attributes.mapped && is_surface(surface));
}
2023-12-22 14:46:54 +01:00
function shader_set_uniform_f_array_safe(uniform, array, max_length = 128) {
2023-11-08 08:38:04 +01:00
INLINE
2023-10-03 11:27:36 +02:00
2023-02-14 02:48:33 +01:00
if(!is_array(array)) return;
2023-10-03 11:27:36 +02:00
var _len = array_length(array);
if(_len == 0) return;
2023-11-24 10:41:53 +01:00
if(_len > max_length) array_resize(array, max_length)
2023-02-14 02:48:33 +01:00
shader_set_uniform_f_array(uniform, array);
}
2023-02-28 09:43:01 +01:00
function shader_set_surface(sampler, surface, linear = false, _repeat = false) {
2023-11-08 08:38:04 +01:00
INLINE
2023-10-03 11:27:36 +02:00
2023-03-25 12:27:04 +01:00
var shader = shader_current();
if(shader == -1) return noone;
2023-02-28 09:43:01 +01:00
var t = shader_get_sampler_index(shader, sampler);
if(is_instanceof(surface, dynaSurf))
surface = surface.surfaces[0];
if(!is_surface(surface)) return t;
2023-02-28 09:43:01 +01:00
texture_set_stage(t, surface_get_texture(surface));
gpu_set_tex_filter_ext(t, linear);
gpu_set_tex_repeat_ext(t, _repeat);
return t;
}
function shader_set_surface_dimension(uniform, surface) {
2023-11-08 08:38:04 +01:00
INLINE
2023-10-03 11:27:36 +02:00
2023-07-14 20:34:35 +02:00
var shader = shader_current();
if(!is_surface(surface)) return;
2023-09-15 20:12:02 +02:00
if(shader == -1) return;
2023-07-14 20:34:35 +02:00
var texture = surface_get_texture(surface);
var tw = texture_get_texel_width(texture);
var th = texture_get_texel_height(texture);
tw = 2048;
th = 2048;
shader_set_uniform_f(shader_get_uniform(shader, uniform), tw, th);
}
2023-07-14 20:34:35 +02:00
function shader_set_dim(uniform = "dimension", surf = noone) {
2023-11-08 08:38:04 +01:00
INLINE
2023-10-03 11:27:36 +02:00
2023-07-21 12:40:20 +02:00
if(!is_surface(surf)) return;
2023-09-08 21:37:36 +02:00
shader_set_f(uniform, surface_get_width_safe(surf), surface_get_height_safe(surf));
}
2023-07-21 12:40:20 +02:00
function shader_set_color(uniform, col, alpha = 1) {
2023-11-08 08:38:04 +01:00
INLINE
2023-10-03 11:27:36 +02:00
2023-07-21 12:40:20 +02:00
shader_set_f(uniform, colToVec4(col, alpha));
}
2023-07-21 12:40:20 +02:00
function shader_set_palette(pal, pal_uni = "palette", amo_uni = "paletteAmount", max_length = 1024) {
2023-11-08 08:38:04 +01:00
INLINE
2023-10-03 11:27:36 +02:00
2023-07-28 19:41:57 +02:00
shader_set_i(amo_uni, min(max_length, array_length(pal)));
2023-07-21 12:40:20 +02:00
var _pal = [];
2023-07-28 19:41:57 +02:00
for( var i = 0, n = min(max_length, array_length(pal)); i < n; i++ )
2023-07-21 12:40:20 +02:00
array_append(_pal, colToVec4(pal[i]));
if(array_length(_pal))
shader_set_f(pal_uni, _pal);
}
2023-07-21 12:40:20 +02:00
2023-03-21 03:01:53 +01:00
#region prebuild
enum BLEND {
normal,
add,
over,
alpha,
alphamulp,
2023-12-15 12:56:36 +01:00
subtract,
2023-03-21 03:01:53 +01:00
}
2023-10-03 11:27:36 +02:00
function shader_preset_interpolation(shader = sh_sample) {
2023-11-08 08:38:04 +01:00
INLINE
2023-10-03 11:27:36 +02:00
var intp = attributes.interpolate;
2023-10-03 11:27:36 +02:00
shader_set_uniform_i(shader_get_uniform(shader, "interpolation"), intp);
shader_set_uniform_i(shader_get_uniform(shader, "sampleMode"), attributes.oversample);
}
function shader_postset_interpolation() {
2023-11-08 08:38:04 +01:00
INLINE
2023-10-03 11:27:36 +02:00
gpu_set_tex_filter(false);
}
function shader_set_interpolation_surface(surface) {
2023-11-08 08:38:04 +01:00
INLINE
2023-10-03 11:27:36 +02:00
shader_set_f("sampleDimension", surface_get_width_safe(surface), surface_get_height_safe(surface));
}
2024-01-26 14:38:50 +01:00
function shader_set_interpolation(surface, _dim = noone) {
2023-11-08 08:38:04 +01:00
INLINE
2023-10-03 11:27:36 +02:00
var intp = attributes.interpolate;
2023-03-21 03:01:53 +01:00
2023-08-12 12:35:35 +02:00
shader_set_i("interpolation", intp);
2024-01-26 14:38:50 +01:00
shader_set_f("sampleDimension", _dim == noone? surface_get_dimension(surface) : _dim);
2023-10-03 11:27:36 +02:00
shader_set_i("sampleMode", attributes.oversample);
2023-03-21 03:01:53 +01:00
}
2023-08-01 19:21:51 +02:00
function surface_set_shader(surface, shader = sh_sample, clear = true, blend = BLEND.alpha) {
2023-03-30 08:33:53 +02:00
if(!is_surface(surface)) {
__surface_set = false;
return;
}
__surface_set = true;
2023-03-21 03:01:53 +01:00
surface_set_target(surface);
if(clear) DRAW_CLEAR;
switch(blend) {
case BLEND.add : BLEND_ADD; break;
case BLEND.over: BLEND_OVERRIDE; break;
case BLEND.alpha: BLEND_ALPHA; break;
case BLEND.alphamulp: BLEND_ALPHA_MULP; break;
}
2023-07-14 20:34:35 +02:00
if(shader == noone)
__shader_set = false;
else {
__shader_set = true;
shader_set(shader);
}
2023-03-21 03:01:53 +01:00
}
function surface_reset_shader() {
2023-03-30 08:33:53 +02:00
if(!__surface_set) return;
2024-05-18 13:46:01 +02:00
shader_set_i("interpolation", 0);
2023-03-21 03:01:53 +01:00
BLEND_NORMAL;
surface_reset_target();
2023-10-09 16:07:33 +02:00
gpu_set_tex_filter(false);
2023-07-14 20:34:35 +02:00
if(__shader_set)
shader_reset();
2023-03-21 03:01:53 +01:00
}
#endregion