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,所以我们将在后面的内容中再来讨论这个话题)

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

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

东汉书院,等你来玩哦

相关内容

热门资讯

裸体在家犯法还是拍别人裸照犯法... 近日,#男子在家裸体被邻居拍照发群#一事引发热议。据报道,某小区一名男业主在家中未穿衣物,对面女业主...
“每天跟遛狗一样”,万亿资金,... 进入7月,市场波动加大,A股关于量化交易的争议再次达到顶峰。由于散户与量化机构之间的“体感温差”显著...
特朗普:对伊朗战争进展顺利 当地时间7月23日,美国总统特朗普再次称,“美国在对抗伊朗方面进展很顺利,表现得极其出色,好得超乎任...
国务院成立广西六蓝水库溃坝灾害... 7月6日,广西南宁横州市六蓝水库发生溃坝,造成重大人员伤亡。根据国家有关法律法规规定,国务院成立调查...
倚美搞事,菲律宾政府连“为奴”... 南海的风浪,很大一部分是菲律宾政府自己搅起来的。近年来,菲政府在仁爱礁、黄岩岛等地小动作频频,不仅频...
雨花台区企业摘得国家科学技术进... 晨报讯(南京晨报/爱南京记者 陈彦)记者从南京市雨花台区获悉,在新近揭晓的2025年度国家科学技术奖...
美国公布新关税措施,日本、澳大... 据法新社、澳大利亚广播公司(ABC)报道,针对美国将于美东时间24日以所谓“强迫劳动”为名对数十个国...
济南大数据集团激活数据要素潜能... 走进“泉数汇·数港”线下体验空间,健康画像、求职背调、“泉涌”工作站等数据产品一字排开,来访者可以像...
太保寿险河南分公司举办员工家庭... 5月22日,中国太保寿险河南分公司在郑州举办绩优干部员工家庭开放日活动,邀请员工家属走进企业,参观办...
“展现了推动人工智能普惠向善发... 7月17日至20日,2026世界人工智能大会暨人工智能全球治理高级别会议在上海举行,习近平主席出席开...