Compute Shaders
admin
2023-02-11 08:20:03
0

周一到周五,每天一篇,北京时间早上7点准时更新~

The first sections of this chapter describe the graphics pipeline in OpenGL(本章的第一个部分描述了OpenGL的图形管线). However, OpenGL also includes the compute shader stage(OpenGL同样包含Compute Shader的阶段), which can almost be thought of as a separate pipeline that runs indepdendently of the other graphics-oriented stages(这个阶段独立的运行,不与其他绘图相关的渲染管线阶段有任何联系). Compute shaders are a way of getting at the computational power possessed by the graphics processor in the system(Compute shader的目的是为了让程序员得到图形显卡的计算能力). Unlike the graphics-centric vertex, tessellation, geometry, and fragment shaders, compute shaders could be considered as a special, single-stage pipeline all on their own(与其他shader不一样,compute shader可以被认为是一个单独的模块). Each compute shader operates on a single unit of work known as a work item(每个compute shader把每一个数据单元做为工作对象); these items are, in turn, collected together into small groups called local workgroups(那些工作对象被组织成为组,被叫做本地工作组). Collections of these workgroups can be sent into OpenGL’s compute pipeline to be processed(大量的这样的本地工作组可以被发送给OpenGL的compute shader处理阶段进行处理). The compute shader doesn’t have any fixed inputs or outputs besides a handful of built-in variables to tell the shader which item it is working on(除了一些用于告诉shader它的工作对象的一些内置的控制变量以外,这个shader没有任何固定的输入和输出的数据). All processing performed by a compute shader is explicitly written to memory by the shader itself, rather than being consumed by a subsequent pipeline stage(任何被显示写入内存的数据都是shader它自己完成的,而不是像其他shader一样,输出数据会被当成后面渲染阶段的输入). A very basic compute shader is shown in Listing 3.13 (Listing3.13展示了一个基本的compute shader)

#version 450 core
layout (local_size_x = 32, local_size_y = 32) in;
void main(void)
{
// Do nothing
}
Listing 3.13: Simple do-nothing compute shader

Compute shaders are otherwise just like any other shader stage in OpenGL(compute shaders就像是其他shader一样). To compile one, you create a shader object with the type GL_COMPUTE_SHADER, attach yourGLSL source code to it with glShaderSource(), compile it with glCompileShader(), and then link it into a program with glAttachShader() and glLinkProgram()(你需要先创建shader、然后attach它给一个shader source,然后编译它,链接成为GPU程序). The result is a program object with a compiled compute shader in it that can be launched to do work for you(编译好了之后,就可以用于进行大规模的并行运算了)

The shader in Listing 3.13 tells OpenGL that the size of the local workgroup will be 32 by 32 work items, but then proceeds to do nothing(Listing3.13中的代码告诉OpenGL本地工作组的大小是32x32,然而这个shader啥都没做). To create a compute shader that actually does something useful, you need to know a bit more about OpenGL—so we’ll revisit this topic later in the book(为了创建一个compute shader并干点什么,你必须先了解以下OpenGL,所以我们将在后面的内容中再来讨论这个话题)

本日的翻译就到这里,明天见,拜拜~~

第一时间获取最新桥段,请关注东汉书院以及图形之心公众号

东汉书院,等你来玩哦

相关内容

热门资讯

德国总理:美国正在被伊朗羞辱 德国之声4月27日报道,德国总理默茨在访问一所学校时表示,在当前的持续冲突中,伊朗领导层正试图羞辱美...
理响中国|“长”歌以行,风云激... 光阴如梭,东方潮阔。这里是中国的长三角,世界的长三角。无论过去、现在还是未来,这片土地都因时代而生,...
白宫:特朗普及其国安团队开会讨... 新华社华盛顿4月27日电 美国白宫新闻秘书莱维特27日在记者会上证实,总统特朗普及其国家安全团队当天...
人民日报刊文:日本放开杀伤性武... 日本放开杀伤性武器出口推高地缘冲突风险(国际论坛)常思纯《人民日报》(2026年04月28日 第 0...
医疗保障法草案二审:明确生育保... 满足多样化健康保障需求本报记者 彭 波4月27日,医疗保障法草案二审稿提请十四届全国人大常委会第二十...
天津一景区发生自转旋翼机事故1... 澎湃新闻记者 吕新文中国民用航空华北地区管理局4月22日公布《豪客通航“10•1”天津长芦汉盐旅游区...
卡塔尔埃米尔与美国总统特朗普通... 当地时间24日,卡塔尔埃米尔塔米姆与美国总统特朗普通电话,重点就中东地区局势以及伊朗与美国谈判问题交...
男子30年前被扣押2859克黄... 澎湃新闻记者 王鑫家住辽宁省大连市的潘永嘉近日向澎湃新闻反映称,三十年前,他在大连周水子机场被盖州市...
商务部:取消反制欧盟两家金融机... 中华人民共和国商务部令二〇二六年 第1号鉴于欧盟已取消对中国两家金融机构的制裁措施,现公布《关于取消...
过去24小时共有5艘船只通过霍... 总台记者当地时间24日获悉,过去24小时内,共有5艘船只通过霍尔木兹海峡,其中包括一艘伊朗油轮。(总...