Vulkan Shader & Resources: Why Uniform and not Const Resources
86 观看
2回复
0 作者的声誉
We usually use const in c++ to imply that the value does not change (read only), why in GLSL/VK in the shader or resource definition they choose the word uniform ? Wodn`t be more consistent and use the keyword borrowed from c/c++
Beside that probably the uniform keyword in shader definitions give clues to the compiler to attach those resources as close to the hardware as possible, probably shared memory or registers ? Not sure on that.
That also probably why they mention in the VkSpec. that we need small ammounts of data for those type of resources. Like for eg: values of cosmological constants..etc
Is anything that I`m missing, or some bit of history that passed away ?
作者: user2095932 的来源 发布者: 2017 年 12 月 27 日回应 2
2像
4181 作者的声誉
Uniforms in GPU programming and const in C++ are focused on different things.
C++ const documents that a variable is not intended to be changed, with some compiler enforcement. As such it's more about using the type system to improve clarity and enforce intended usage -- important for large-project software engineering. You can still get around it with const_cast or other tricks, and the compiler can't assume you didn't, so it's not strictly enforced.
The important thing about uniforms is that they're, well, uniform. Meaning they have the same value whenever they are read within a draw call. Since there might be hundreds to millions of reads of that value in a single draw call, this allows it to be cached, and just one copy of it to be cached, or that it can be preloaded into registers (or cache) before shaders run, that it can be cached in a non-coherent cache, that a single read result can be broadcast across all SIMD lanes in a core, etc. For this to work, the fact that the contents can't change must be strictly enforced (with memory aliasing you can get around even this, now, but results are very much undefined if you do). So uniform really isn't about declaring intent to other programmers for software engineering benefits like const is, it's about declaring intent to the compiler and driver so they can optimize based on it.
D3D uses "const" and "constant buffer" rather than uniform, so clearly there is some overlap. Though that does lead to saying things like "how many times do you update constants per frame?" which when you think about it is kind of a weird thing to say :). The values are constant within shader code, but very much aren't constant at the API level.
作者: Jesse Hall 发布者: 2017 年 12 月 27 日1像
309468 作者的声誉
The etymology of the word is important here. The term "uniform" is derived from GLSL, which was inspired by the Renderman standard's shader terminology. In Renderman, "uniform" was used for values that "whose values are constant over whatever portion of the surface begin shaded". This was an alternative to "varying" which represented values interpolated across the surface.
"Constant" would imply that the value never changes. Uniform values do change; they simply don't change at the same frequency as other values. Input values change per-invocation, uniform values change per-draw call, and constant values don't change. Note that in GLSL, const
usually means "compile-time constant": a value that is set at compile time and is never changed.
A uniform variable in Vulkan ultimately comes from a resource that exists outside of the shader. Blocks of uniform variables fed by buffers, uniforms in push constants fed by push constant state are both external resources, set by the user. That's a fundamentally different concept from having a compile-time constant struct.
Since it's different from a constant struct, it needs a different term to request it.
作者: Nicol Bolas 发布者: 2017 年 12 月 28 日来自类别的问题 :
- resources 我可以列出给定包中的资源吗?
- resources Visual Studio插件,用于快速搜索解决方案中的文件
- resources 如何从Java jar文件读取资源文件?
- resources 遗传算法资源
- resources WPF - 图像'不是项目的一部分或其构建操作未设置为资源'
- resources Java资源作为文件
- shader GLSL的随机/噪声功能
- shader 条件和未使用的采样器/纹理添加到SM2 / 3像素着色器的性能有多大?
- shader GLSL着色器的正确文件扩展名是什么?
- shader 如何通过在glsl中注册来设置着色器常量?
- shader CSS着色器的浏览器?
- shader GLSL中的气缸冒名顶替者
- vulkan 渲染具有多个索引的网格
- vulkan vulkan api是否可以处理窗口创建?
- vulkan Vulkan API是否会针对Mac OS平台发布?
- vulkan 将apiVersion参数设置为0时发生VK_ERROR_INCOMPATIBLE_DRIVER
- vulkan 为什么Vulkan的VkBool32被实现为无符号int?
- vulkan Vulkan:vkGetInstanceProcAddress和vkGetDeviceProcAddress之间的区别