在OpenGL中使用整數類型紋理進行計算

Using Integer Textures in OpenGL for Calculation

 

    OpenGL could perfectly support integer textures through  'GL_EXT_gpu_shader4' and '

GL_EXT_texture_integer' extension. So we can do some GPGPU work with integer values in shaders. 

 

    First of all, prepare the integer texture:

   

    Take a look at the last funciton above: glTexImage2D();  There are something important here:

1. Internal Format of this texture should be one of 'GL_RGBA32UI' 'GL_RGBA32I' or something like these;

2.Format should be 'GL_RGBA_INTEGER' or something like this. Be careful the '_INTEGER' suffix;

3.Type should be one of 'GL_UNSIGNED_INT' and 'GL_INT' which depends on the internal format you chose.

4.The last parameter 'data' is an integer array.

 

    Then in the shader, we must use 'isampler2D' or 'usampler2D' for signed integer texture and unsigned integer texture respectively.

 

    At the end, we must write :

  on top of shaders which will use integer textures.

 

   Here is an example of fragment shader:

 

    We have a user-defined fragment out put varying 'FragOut' instead of 'gl_FragColor' here, because gl_FragColor will only accept float vectors but not integer ones.

 

 

 

在OpenGL中使用整數類型紋理進行計算


通過 'GL_EXT_gpu_shader4'和'GL_EXT_texture_integer' 擴展,OpenGL能夠支持整數型紋理。因此便可以使用該特性進行一些基於整數的GPU通用計算。

首先要創建一張整數型紋理:

   

    仔細看最後一個函數。幾個非常重要的地方:
1.Internal Format 參數要帶有’I‘或’UI‘的後綴,表明是有符號或無符號整數;
2.Format 參數要帶有’_INTEGER'後綴;
3.Type 參數必須爲 'GL_UNSIGNED_INT' 和 'GL_INT' 中的一個,具體是哪個取決於Internal Format 的選擇;
4.最後一個參數'data’是一個已經在內存中創建好的整數型數組。

紋理準備好後,在Shader中,要將採樣器類型前面加上‘i'或’ui‘的前綴,分別表明有符號和無符號整數,如'isampler2D'

最後,在Shader頂部,必須寫入

    才能實現對整數型紋理的操作。

下面是一個fragment shader的實例代碼:


注意我們使用了自己定義的片元易變變量 FragOut而非gl_FragColor來進行輸出,因爲gl_FragColor只支持浮點型數據。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章