Compute Shaders
作者:互联网
周一到周五,每天一篇,北京时间早上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,所以我们将在后面的内容中再来讨论这个话题)
本日的翻译就到这里,明天见,拜拜~~
第一时间获取最新桥段,请关注东汉书院以及图形之心公众号
东汉书院,等你来玩哦
标签:pipeline,compute,OpenGL,shader,Shaders,Compute,shaders 来源: https://blog.51cto.com/battlefire/2423323