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)ide

#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 shaderui

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(編譯好了以後,就能夠用於進行大規模的並行運算了)this

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,因此咱們將在後面的內容中再來討論這個話題)翻譯

本日的翻譯就到這裏,明天見,拜拜~~code

第一時間獲取最新橋段,請關注東漢書院以及圖形之心公衆號orm

東漢書院,等你來玩哦對象

相關文章
相關標籤/搜索