Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Sampler.grad method for HXSL #1127

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Yanrishatum
Copy link
Contributor

Adds support for GLSL textureGrad / HLSL SampleGrad methods as Sampler.grad().
Requested by CLYDE from Discord.

Sample shader (ported from this ShaderToy shader):

class HxslGradTest extends hxsl.Shader {

	static var SRC = {
		
		@borrow(h3d.shader.Base2d) var texture: Sampler2D;

		@global var time: Float;
		var calculatedUV: Vec2;
		var pixelColor: Vec4;

		function fragment() {
			var blur = pow(.0625 - cos(calculatedUV.x * 20.0 + time) * .0625, 2.);
			var grad = vec2(blur, blur);
			pixelColor =
				texture.grad(calculatedUV + .5 * vec2( blur, blur), grad, grad) * .25 +
				texture.grad(calculatedUV + .5 * vec2( blur,-blur), grad, grad) * .25 +
				texture.grad(calculatedUV + .5 * vec2(-blur, blur), grad, grad) * .25 +
				texture.grad(calculatedUV + .5 * vec2(-blur,-blur), grad, grad) * .25;
		}
	}

}

@onehundredfeet
Copy link
Contributor

Do all platforms support this instruction? Are there any versions of GL or DX that don't?

@Yanrishatum
Copy link
Contributor Author

Yes, all platforms properly support this method. GL method is supported by GLSL/HLSL versions Heaps targets as well as WebGL.

@ncannasse
Copy link
Member

please note that we're using GLSL semantics, so while we can do tex.grad(...) , the toplevel function should be named textureGrad(...) and not "grad".

@Yanrishatum
Copy link
Contributor Author

It's not a top-level function thought? It follows same approach as all sampler methods - sampler.method(), i.e. texture() -> sampler.get(), texelFetch -> sampler.fetch(), textureSize -> sampler.size() and consequently textureGrad -> sampler.grad()
If we to follow GLSL semantics, we also should remake get, size and fetch methods.

@ncannasse
Copy link
Member

ncannasse commented Mar 16, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants