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

GLES: Avoid global set of enums, and make enums for each function's valid inputs #5

Open
rdunnington opened this issue Nov 30, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@rdunnington
Copy link
Collaborator

rdunnington commented Nov 30, 2023

OpenGL/ES defines a large number of values that have virtually no type safety - you can often pass completely unrelated values such as:

  • GL_BUFFER_USAGE
  • GL_DEPTH_FUNC
  • GL_VIEWPORT
    to parameters that take an unsigned integer, and get no type checking to help you fail faster. A straightforward example would be:
const ShaderType = enum(GLenum) {
	Vertex = 0x8B31,
	Fragment = 0x8B30,
	Compute = 0x91B9,
};
pub fn glCreateShader(shader_type: ShaderType) GLuint {}

Note that in the native API, you could pass any integer, where with this system you could only pass valid parameters. This gets a lot of value when dealing with more complicated functions:

// Raw openGL binding:
extern fn glFramebufferTexture2D(target: GLenum, attachment: GLenum, textarget: GLenum, texture: GLuint, level: GLint) void;
// Zig binding:
const FramebufferTarget = enum (GLenum) { ... };
const FramebufferAttachment = enum (GLenum) { ... };
const TextureTarget = enum (GLenum) { ... };
pub fn framebufferTexture2D(target: FramebufferTarget, attachment: FramebufferAttachment, textarget: TextureTarget, texture: Texture, level: GLint) void {}

For variable-sized values such as fn glActiveTexture(texture: GLenum) void, which takes GL_TEXTUREi to GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS - 1 values, these can still be generated for the user at comptime into a bounded typesafe enum.

@rdunnington rdunnington added the enhancement New feature or request label Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant