Set Hue for data stage (and for control stage; and saturation and value)

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Qon
Smart Inserter
Smart Inserter
Posts: 1240
Joined: Thu Mar 17, 2016 6:27 am
Contact:

Re: Set Hue for data stage

Post by Qon » Sat May 25, 2019 4:47 pm

eradicator wrote:
Sat May 25, 2019 2:51 pm
@Qon:
Except full GLSL support would be instantly abused and impossible to make secure.
GLSL can't access a filesystem though? Any examples of how it would be abused or insecure?
Why would it be worse than Lua? GLSL runs on the GPU so to me it seems harder to access the wrong things even in case of a fatal buffer overflow in some implementation. Maybe I'm overlooking something?
And even if the language has support for 'bad' functions you can 'just' not include those in the implementation that mods can access.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 3643
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Set Hue for data stage

Post by eradicator » Sat May 25, 2019 6:11 pm

Qon wrote:
Sat May 25, 2019 4:47 pm
eradicator wrote:
Sat May 25, 2019 2:51 pm
@Qon:
Except full GLSL support would be instantly abused and impossible to make secure.
GLSL can't access a filesystem though? Any examples of how it would be abused or insecure?
Why would it be worse than Lua? GLSL runs on the GPU so to me it seems harder to access the wrong things even in case of a fatal buffer overflow in some implementation. Maybe I'm overlooking something?
And even if the language has support for 'bad' functions you can 'just' not include those in the implementation that mods can access.
Or maybe i know less about GLSL than you think and i just talk without thinking sometimes p:. If you can "just exclude" low level access i'm wrong. My main "worry" is that graphics card drivers are not known for being the spear head of security oriented programming. Memory leaks, privilidge escalation, ye know, the usual stuff. So it'd be yet another sandbox (alongside lua) that wube would have to worry about. And as it's also "yet another thing to learn" most modders would be confronted with "do i learn Photoshop™®💀 or GLSL", which probably wouldn't end favourible to GLSL.
Author of: Hand Crank Generator, Screenshot Hotkey 2.0
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2223
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Set Hue for data stage

Post by darkfrei » Sat May 25, 2019 7:10 pm

By the first starting the stage "Checking sprites" needs about 57 seconds. The second game starting in the same day needs for "Checking sprites" and "Loading sprites" only 9 seconds.
The game checks sprites and computes some stuff for using after. It can be another graphics calculation time.

Qon
Smart Inserter
Smart Inserter
Posts: 1240
Joined: Thu Mar 17, 2016 6:27 am
Contact:

Re: Set Hue for data stage

Post by Qon » Sat May 25, 2019 7:16 pm

eradicator wrote:
Sat May 25, 2019 6:11 pm
Or maybe i know less about GLSL than you think and i just talk without thinking sometimes p:. If you can "just exclude" low level access i'm wrong. My main "worry" is that graphics card drivers are not known for being the spear head of security oriented programming. Memory leaks, privilidge escalation, ye know, the usual stuff. So it'd be yet another sandbox (alongside lua) that wube would have to worry about.
Well you wouldn't interact with graphics card driver directly. You just make mathematical functions that operate on vectors (colors) that are executed for every pixel.
eradicator wrote:
Sat May 25, 2019 6:11 pm
And as it's also "yet another thing to learn" most modders would be confronted with "do i learn Photoshop™®💀 or GLSL", which probably wouldn't end favourible to GLSL.
It takes about as many seconds to learn GLSL as Lua. It has C-like syntax and some mathematical functions and syntax sugar for the vector types but is otherwise simpler and lacks some features.

Here's realtime fullscreen color rotation in GLSL:

Code: Select all

// hsv2rgb() taken from https://www.shadertoy.com/view/XslGzl
vec3 hsv2rgb(vec3 c) {
    vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
    vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
    return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
// http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl
vec3 rgb2hsv(vec3 c) { 
    vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
    vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
    vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));

    float d = q.x - min(q.w, q.y);
    float e = 1.0e-10;
    return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
vec4 colorize(float c) {
    float hue = mix(0.0, 1.0, 1.0 - c);
    float sat = 1.0;
    float lum = 1.0;
    vec3 hsv = vec3(hue, sat, lum);
    vec3 rgb = hsv2rgb(hsv);
    return vec4(rgb, 1.0);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
    vec2 uv = fragCoord.xy / iResolution.xy;
    vec3 texColor = texture(iChannel0,uv).rgb;
    float a = (texColor.r + texColor.g + texColor.b)/3.0;
    hsv.x = mod(hsv.x + 0.5, 1.0);
    vec3 rgb = hsv2rgb(hsv);
    fragColor = vec4(rgb, 1.0);
}
Try it in your browser. Click iChannel 0 in the bottom and select a video (Claude Van Damme has colours) or your webcam. The demo has several filters, like greyscale and 'thermal camera'. Click and drag the output video with your mouse, mouse.x is used to determine how to choose which part gets which filter.

Qon
Smart Inserter
Smart Inserter
Posts: 1240
Joined: Thu Mar 17, 2016 6:27 am
Contact:

Re: Set Hue for data stage

Post by Qon » Thu Jun 06, 2019 9:50 pm

Qon wrote:
Sat May 25, 2019 2:46 pm
I meant "on the fly" as in while being loaded in prototype stage, not in Factorio runtime. Though that would be even cooler.
I've changed by mind. I want it for prototype and runtime. Real-time ray marching displays inside Factorio? Real-time animated icons? Turn Factorio into 3D? Why not? :lol:

There are so many cool things we could do with proper shaders. The modern Factorio night vision is an example. What if we could do things like that in mods?
Qon wrote:
Wed Jun 05, 2019 10:30 pm
Posila, do you accept bribes to change your mind? 8-)
posila wrote:
Thu Jun 06, 2019 9:44 pm
Back to topic (to other things, I might respond in the hue thread)...
Posila is ready to take our bribes! Let's cooperate and make an irresistible offer! :lol:
(I'm broke so I'm relying on you!)

posila
Factorio Staff
Factorio Staff
Posts: 3936
Joined: Thu Jun 11, 2015 1:35 pm
Contact:

Re: Set Hue for data stage

Post by posila » Fri Jun 07, 2019 3:51 pm

Not so fast Qon! I'll try to start on positive note before inevitably spiraling down to my cynical asshole self and start ranting :)

Hue shift as preprocess step is less likely to happen than preprocessing layered icons. But I have been thinking how to make runtime hue shift and desaturation happen. The problem is following. Because of our art-style, which utilizes semi-transparency a lot, we must render sprites from back to front. Proper way to do rendering is to group meshes by material and render many objects with common material at once, then switch to another material and draw all objects that have that material at once, etc. (Material is essentialy, tuple of texture(s), shader, shader parameters and possibly some other pipeline states). Because we don't have a choice of order in which we draw sprites, only option to keep number of draw calls low, and batches of work sent to GPU high, is to use the same material for all sprites, which means put all sprites into single texture (which is not possible anymore), use the same shader and parameters, etc.

So, runtime hue shift would esentially had to be calculated on all sprites, regardless if you want to hue shift it or not (same as currently tint is applied to all sprites, but often times it is just {1,1,1,1}). While it would be breeze for modern GPUs that are often not running in the highest power mode when running Factorio anyway, some people run the game on old garbage integrated GPUs, because their dedicated GPU is overheating for whatever reason (wink wink), or simply just don't have better hardware available at all, and maybe even bought Factorio because it is a 2D game and expected to not have high GPU requirements. We need to be very careful and make sure we don't add something that would degrade performance on not-so-good hardware while not being possible to be disabled in graphics options (if art requires hue-shift, we can't just decide it's not going to be hue-shifted on some computers).

Maybe guarding it by branch in the shader would be good enough, but I am afraid that just presence of the branch would increase shader register pressure and reduce on-GPU paralellization. I liked this formula (https://cmwdexint.com/2015/02/03/awkwar ... ft-shader/) it is much cheaper than proper RGB->HSV->RGB conversion and might provide similar capabilities (I have not tested it yet).

----------------

The above is also one of the reasons there won't be access to shaders in prototypes. When doing the rendering rewrite, I thought there could at least definitions for fullscreen post-process passes, but no. The reality is, the graphics programing feels extremely fragile, as every minor change or feature that should just work causes problems for someone (e.g. 71649). I think our new rendering backend is major improvement for almost everybody and is quite stable, but it certainly doesn't feel that way. I can't even imagine support hell that would begin if we allowed mods to introduce their own shaders.

I hope our next game will use 3D rendering, or at least 2D art style that is very light on transparency. At this point, I want to push Factorio to 1.0 state and want to avoid adding unecessery complexity or complications that might delay that goal further.

Qon
Smart Inserter
Smart Inserter
Posts: 1240
Joined: Thu Mar 17, 2016 6:27 am
Contact:

Re: Set Hue for data stage

Post by Qon » Fri Jun 07, 2019 5:08 pm

So what I'm hearing is that custom per-sprite shaders can't run in runtime. But it's possible in prototype stage? :D
posila wrote:
Fri Jun 07, 2019 3:51 pm
The above is also one of the reasons there won't be access to shaders in prototypes.
What is a "shader in prototype"? Is that a shader that is static (set in data stage) but gives dynamic results because it executes in runtime (IE no shader compilation in runtime)? Or is it shader full-stop in prototype stage? Shaders that are executed once in prototype stage to generate static sprites should work, right? The sprites that are made are loaded via a shader instead of a file (and give the shader access to a list of files) and doesn't affect render order or make the graphics pipeline any different.

:)

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 3643
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Set Hue for data stage

Post by eradicator » Fri Jun 07, 2019 5:55 pm

Qon wrote:
Fri Jun 07, 2019 5:08 pm
So what I'm hearing is that custom per-sprite shaders can't run in runtime. But it's possible in prototype stage? :D
posila wrote:
Fri Jun 07, 2019 3:51 pm
The above is also one of the reasons there won't be access to shaders in prototypes.
What is a "shader in prototype"? Is that a shader that is static (set in data stage) but gives dynamic results because it executes in runtime (IE no shader compilation in runtime)? Or is it shader full-stop in prototype stage? Shaders that are executed once in prototype stage to generate static sprites should work, right? The sprites that are made are loaded via a shader instead of a file (and give the shader access to a list of files) and doesn't affect render order or make the graphics pipeline any different.

:)
Yea. I don't need them to run at runtime. It'd already be super-awesome if they could be used to generate/change static sprites that behave as if they were loaded from a file. I.e. similar to how pre-composing layered icons would work if it existed.
Author of: Hand Crank Generator, Screenshot Hotkey 2.0
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Qon
Smart Inserter
Smart Inserter
Posts: 1240
Joined: Thu Mar 17, 2016 6:27 am
Contact:

Re: Set Hue for data stage

Post by Qon » Fri Jun 07, 2019 6:14 pm

eradicator wrote:
Fri Jun 07, 2019 5:55 pm
Yea. I don't need them to run at runtime. It'd already be super-awesome if they could be used to generate/change static sprites that behave as if they were loaded from a file. I.e. similar to how pre-composing layered icons would work if it existed.
Yeah, we wouldn't have to ask for hue rotations, saturation/hue masking, (de)saturation, contrast changes and so much more. One feature to rule them all!

Post Reply

Return to “Modding interface requests”

Who is online

Users browsing this forum: No registered users