Skip to content

support uniform textures and other compose methods#125

Draft
fisherevans wants to merge 2 commits into
gopxl:mainfrom
fisherevans:fisher/support-uniform-textures
Draft

support uniform textures and other compose methods#125
fisherevans wants to merge 2 commits into
gopxl:mainfrom
fisherevans:fisher/support-uniform-textures

Conversation

@fisherevans
Copy link
Copy Markdown

@fisherevans fisherevans commented Oct 8, 2025

Changes

Two independent additions to the opengl backend.

Sampler uniforms (SetUniformTexture)

Canvas.SetUniformTexture(name, tex, unit) binds a *glhf.Texture to a named sampler2D in the canvas's fragment shader. The texture is activated on the given unit and bound each frame during draw.

var overlayTex *glhf.Texture  // e.g. a normal map or light mask
canvas.SetFragmentShader(myShaderSrc)
canvas.SetUniformTexture("uOverlay", overlayTex, 0)

In the shader:

uniform sampler2D uTexture;  // pixel's built-in sprite texture
uniform sampler2D uOverlay;

void main() {
    vec4 base = texture(uTexture, vTexCoords);
    vec4 mask = texture(uOverlay, vTexCoords);
    fragColor = base * mask;
}

The two default shader constants are now exported (BaseCanvasVertexShader, BaseCanvasFragmentShader) so custom shaders can extend the defaults without duplicating the source.

Compose methods

Two new Porter-Duff composition modes for canvas.SetComposeMethod:

Both modes have software implementations in Compose() and hardware blend function registrations in the opengl backend. The hardware path uses raw go-gl/gl calls for BlendEquation and BlendFuncSeparate since glhf v2.0.0 does not yet expose those wrappers (the companion glhf PR #8 adds them — once that lands these can be simplified).

Test plan

  • go build ./... unchanged.
  • go test ./... — new tests/compose_test.go covers ComposeMultiply and ComposeScreen with opaque, transparent, black, and white inputs.

- Add doc comments to Canvas.SetUniformTexture and GLShader.SetUniformTexture
  describing the sampler binding semantics and the unit index constraint.
- Drop unnecessary else after early return in GLShader.SetUniformTexture.
- Remove dead commented-out clamp-to-[0,1] snippet from ComposeMultiply.
- Revert .gitignore to upstream state (.idea/ belongs in a global ignore).
- Add tests/compose_test.go with table-driven cases for ComposeMultiply
  and ComposeScreen covering opaque, black, white, and transparent inputs.
@fisherevans fisherevans marked this pull request as draft April 21, 2026 17:20
Comment thread backends/opengl/canvas.go
}
ct.shader.s.SetUniformAttr(loc, u.Value())
}
gl.ActiveTexture(gl.TEXTURE0)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe cache the active texture before this loop and restore it here (I'm assuming that that's the intention behind this call)

Comment thread backends/opengl/canvas.go
}

c.shader = NewGLShader(baseCanvasFragmentShader)
c.shader = NewGLShader(BaseCanvasFragmentShader)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the change to exporting the base shader?

Comment thread backends/opengl/canvas.go
case pixel.ComposeMultiply:
glhf.BlendFunc(glhf.BlendFactor(gl.DST_COLOR), glhf.Zero)
case pixel.ComposeScreen:
gl.BlendEquation(gl.FUNC_ADD)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could these be added back down to glhf?

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.

2 participants