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

Duplicate kind concept for 2 different usecases #134

Closed
ppelikan opened this issue Oct 6, 2024 · 2 comments
Closed

Duplicate kind concept for 2 different usecases #134

ppelikan opened this issue Oct 6, 2024 · 2 comments

Comments

@ppelikan
Copy link

ppelikan commented Oct 6, 2024

First of all, thank you for your great work!

Problem:

In raylib.nim there is:

type
  Pixel* = concept
    proc kind(x: typedesc[Self]): PixelFormat

template kind*(x: typedesc[Color]): PixelFormat = UncompressedR8g8b8a8

and there is also:

type
  ShaderV* = concept
    proc kind(x: typedesc[Self]): ShaderUniformDataType

template kind*(x: typedesc[float32]): ShaderUniformDataType = Float
template kind*(x: typedesc[Vector2]): ShaderUniformDataType = Vec2
template kind*(x: typedesc[Vector3]): ShaderUniformDataType = Vec3
template kind*(x: typedesc[Vector4]): ShaderUniformDataType = Vec4
template kind*(x: typedesc[int32]): ShaderUniformDataType = Int

now let's say we want to make similar thing to this example: textures/raw_data.nim

but instead of using full RGBA PixelFormat, we want to use only one channel, so we define:

type GrayscalePixel = uint8

proc kind*(x: typedesc[GrayscalePixel]): PixelFormat = UncompressedGrayscale

(...)

  var pixels = newSeq[GrayscalePixel](width*height)
  for y in 0..<height:
    for x in 0..<width:
      if ((x div 32 + y div 32) div 1) mod 2 == 0:
        pixels[y*width + x] = 0
      else:
        pixels[y*width + x] = 250

  let checked = loadTextureFromData(pixels, width, height)

This will compile. But as soon as we try to use the setShaderValue() function (or use any custom shader in general), the code fails to compile, with errors like
type mismatch Expression: kind(T)
or

type mismatch

Expression: setShaderValue(myShader, myUniform, myValue)
[1] myShader: Shader
[2] myUniform: ShaderLocation
[3] myValue: Vector2
Expected one of (first mismatch at [position]):
[3] proc setShaderValue[T: ShaderV](shader: Shader; locIndex: ShaderLocation;
value: T)

or similar.

I'm still learning Nim, if this issue isn't legitimate, let me know to close it.

@planetis-m
Copy link
Owner

Yep, I fear my decision to use concepts was rushed and should have stayed with plain generics. I had a similar issue when two types failing to match the Pixel concept in the same module.

I can reproduce your issue, however if I redefine template kind*(x: typedesc[float32]): ShaderUniformDataType = Float in the current module it compiles. Tell me how it goes and depending, it might be time to remove concepts.

@ppelikan
Copy link
Author

ppelikan commented Oct 6, 2024

Yes, redefining it for shaders solved the problem, thanks!

I've also tried to modify the lib (raylib.nim file), to use separate kind name for PixelFormat, and kind2 name for ShaderUniformDataType. Strangely this didn't help.

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

No branches or pull requests

2 participants