Coder Social home page Coder Social logo

Can't Set Float Uniform Variable about sokol HOT 3 CLOSED

Voxi0 avatar Voxi0 commented on July 22, 2024
Can't Set Float Uniform Variable

from sokol.

Comments (3)

floooh avatar floooh commented on July 22, 2024 1

By the way, using the '&' before 'SG_RANGE' gives me an error.

Ah right, that's a C vs C++ thing. In C++ you can do this instead (just drop the '&'):

sg_apply_uniforms(SG_SHADERSTAGE_FS, SLOT_fsParams, SG_RANGE(fs_params));

...or use the SG_RANGE_REF() macro which does the right thing both in C and C++:

sg_apply_uniforms(SG_SHADERSTAGE_FS, SLOT_fsParams, SG_RANGE_REF(fs_params));

from sokol.

floooh avatar floooh commented on July 22, 2024

Do you use the code-generated struct or your own (in the sg_apply_uniforms call)? E.g. in the sokol-shdc output there's the following struct generated:

#pragma pack(push,1)
SOKOL_SHDC_ALIGN(16) typedef struct fsParams_t {
    float time;
    uint8_t _pad_4[12];
} fsParams_t;
#pragma pack(pop

Note how this is padded to a multiple of 16 bytes. This is a side effect of 'flattening' uniform blocks into a vec4 array via SPIRVCross, which allows to update uniforms with a single glUniform4fv() call per uniform-block in the GL backend (instead of one individual glUniform call per uniform block member which is otherwise used).

PS: the #version 330 core at the top of your shader isn't necessary (and might even confuse sokol-shdc in some cases).

PPS: in general, and if possible, you should use the code-generated structs for passing uniforms into sokol-gfx, since the alignment and padding rules for uniform blocks are non-trivial, and very different from vanilla C structs.

PPPS: code example

const fsParams_t fs_params = { .time = (float)glfwGetTime() };
sg_apply_uniforms(SG_SHADERSTAGE_FS, SLOT_fsParams, &SG_RANGE(fs_params));

from sokol.

Voxi0 avatar Voxi0 commented on July 22, 2024

I know the #version 330 core isn't necessary but I was pretty desperate to get it working. I am using the generated struct yes, I'd rather not make my own or mess with the generated one. And, oh, the code example you provided actually works. I guess that's it, thanks.

By the way, using the '&' before 'SG_RANGE' gives me an error. I've seen it being used every example though. The error is 'Taking address of rvalue [-fpermissive]', the program runs fine regardless of this error. I have all warnings and errors enabled so that must be the case.

from sokol.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.