Feedback: named shaders attachable to actors at runtime

Remember, just because you request it, that doesn't mean you'll get it.

Moderator: GZDoom Developers

violgamba
Posts: 41
Joined: Wed Apr 10, 2024 4:53 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 10

Feedback: named shaders attachable to actors at runtime

Post by violgamba »

I've implemented a new feature for GZDoom, but I'd like feedback on its interface before submitting a pull request. The feature is "Named Hardware Shaders". The basic idea is that, rather than assigning a shader to an image at design time, you can, instead, setup a "named" shader, that can be assigned to an actor at runtime, via ZScript. Being assigned to the actor, the shader will be used when rendering whatever sprite the actor currently displays.

The interface for this feature (explained below) is where I'd like feedback. I think it's a pretty good interface, but I'm only one novice GZDoom user. Any comments or critiques are more than welcome. Also, if you like the feature as-is, posting to express this will (hopefully) help it be accepted into GZDoom more quickly.

Here's the interface:

1) In the GLDEFS file/lump, where you usually write "HardwareShader" definitions, you can write "NamedHardwareShader" definitions. The syntax is similar, except that there's no type, and the lumpname is replaced with a shader-name to be referenced in ZScript.

Example:

Code: Select all

NamedHardwareShader highlight1
{
	Shader "shaders/hilight1.fp"
}
2) In ZScript you call `TexMan.GetNamedShader(name)` to get the numerical id of the named shader defined in GLDEFS, then you set the new actor field "Shader" to it. This makes the actor render with the shader. Two special Shader ids are -1 (to use the default shader of the rendered image) and 0 (to enforce NO shader). For example:

Code: Select all

myActor.Shader = TexMan.GetNamedShader("highlight1"); // The actor will render with the "highlight" shader
myActor.Shader = -1; // The actor will render with whatever shaders are attached to its images in GLDEFS (or no shaders, if none are attached)
myActor.Shader = 0; // The actor will render with NO shaders, even if its images have shaders attached
So, my implementation works fine for the basic examples I've tried it with. I am new to GZDoom source code, so I wouldn't be surprised if there's a gotcha or two the main dev's will point out. The speed parameter might need some work, for example. However, I think that it's a viable solution. If you are curious, here's a link to the commit that adds this feature.

Thanks for any feedback you can provide!
User avatar
Cherno
Posts: 1321
Joined: Tue Dec 06, 2016 11:25 am

Re: Feedback: named shaders attachable to actors at runtime

Post by Cherno »

I posted this into the Shader channel on the Discord server for further discussion.
User avatar
phantombeta
Posts: 2119
Joined: Thu May 02, 2013 1:27 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Brazil

Re: Feedback: named shaders attachable to actors at runtime

Post by phantombeta »

It's nice to see someone finally getting around to implementing something like this. :D

Here's some issues I see:
Currently there doesn't seem to be any code handling serialization and deserialization of the Shader variable, so it'll be undefined behaviour if the player loads a saved game in a new session, because the shaders won't necessarily have the same indices.

This should ideally be a dedicated type like TextureID instead of a bare int, otherwise the user could put in arbitrary values and trigger undefined behaviour. As is a user could also store the return value from GetNamedShader in a different int variable, thinking it's safe, and have problems later involving saved games.
As a bonus, if it were a dedicated type it could also potentially be expanded later to support uniform values more easily.

The new actor field should also be named something else in the ZScript side, as there's already a Shader struct, and it'll break older mods if you add a field with the same name. (It might also be possible to version-lock the addition to a new ZScript version, but I'm not sure if that'd help here)

On the other hand, I personally think it might be a good idea to keep the variable itself hidden, and have the user set the shader using only functions instead, to make it less error-prone.
violgamba
Posts: 41
Joined: Wed Apr 10, 2024 4:53 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 10

Re: Feedback: named shaders attachable to actors at runtime

Post by violgamba »

@Cherno:
Thank you for posting this to Discord.

@phantombeta:
I'll look into serializing this and study TextureID to try and follow it more closely. I was on the fence between naming the Actor field "Shader" or "ShaderID". It seems like "ShaderID" is the way to go, especially with the increased parity with TextureID. Regarding using a setter & getter: I'd like to avoid this if possible, though I get your point about error-proofing. I'll check how TextureID handles it, as it should be pretty similar.

On the subject of following the style of TextureID: should I rename TexMan.GetNamedShader(name) to TexMan.CheckForShader(name) or maybe TexMan.CheckForNamedShader(name)? Honestly, I never really understood why it was called CheckForTexture() rather than GetTextureID().
User avatar
Cherno
Posts: 1321
Joined: Tue Dec 06, 2016 11:25 am

Re: Feedback: named shaders attachable to actors at runtime

Post by Cherno »

"ShaderInstanceID" might be more appropriate if I understand the issue correctly.
violgamba
Posts: 41
Joined: Wed Apr 10, 2024 4:53 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 10

Re: Feedback: named shaders attachable to actors at runtime

Post by violgamba »

@Cherno: Hmm. That could work. I'm inclined to mirror TextureID as closely as possible though, since it's so similar: a number representing a kind of resource, which can by accessed by name.
JBenko
Posts: 1
Joined: Thu Jul 29, 2021 4:37 pm
Graphics Processor: nVidia (Modern GZDoom)

Re: Feedback: named shaders attachable to actors at runtime

Post by JBenko »

Cherno wrote:
> I posted this into the Shader channel on the Discord server for further
> discussion.
What is the name of your discord server? I'd like to check out the Shader conversation.
User avatar
Caligari87
Admin
Posts: 6191
Joined: Thu Feb 26, 2004 3:02 pm
Preferred Pronouns: He/Him

Re: Feedback: named shaders attachable to actors at runtime

Post by Caligari87 »

JBenko wrote: Tue Jul 02, 2024 9:10 amWhat is the name of your discord server? I'd like to check out the Shader conversation.
Late reply, this but likely they mean the official ZDoom discord. The conversation is probably long buried by now, however.

8-)
Nehakakar
Posts: 3
Joined: Wed Sep 04, 2024 7:10 am
Operating System Version (Optional): Window 11
Graphics Processor: Not Listed

Re: Feedback: named shaders attachable to actors at runtime

Post by Nehakakar »

Rename the ZScript field to ShaderID to avoid conflicts and check TextureID for consistency.

Return to “Feature Suggestions [GZDoom]”