zoukankan      html  css  js  c++  java
  • 在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:

    1. glGenTextures (1, &imageTex);  
    2.     glBindTexture(GL_TEXTURE_2D,imageTex);  
    3.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  
    4.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);  
    5.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);  
    6.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);  
    7.     glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA32UI, 2,2,0,GL_RGBA_INTEGER,GL_UNSIGNED_INT,data);   

        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 :

    1. #extension GL_EXT_gpu_shader4 : enable  

      on top of shaders which will use integer textures.

       Here is an example of fragment shader:

    1. #extension GL_EXT_gpu_shader4 : enable  
    2. varying out uvec4 FragOut;  
    3. uniform usampler2D Tex1;  
    4. void main(void) {  
    5.     uvec4 texel = texture2D(Tex1,gl_TexCoord[0].st);  
    6.     FragOut = texel;  
    7. }  

        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. glGenTextures (1, &imageTex);  
    2.     glBindTexture(GL_TEXTURE_2D,imageTex);  
    3.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  
    4.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);  
    5.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);  
    6.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);  
    7.     glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA32UI,   
    8. 2,2,0,GL_RGBA_INTEGER,GL_UNSIGNED_INT,data);   
        仔细看最后一个函数。几个非常重要的地方:
    1.Internal Format 参数要带有’I‘或’UI‘的后缀,表明是有符号或无符号整数;
    2.Format 参数要带有’_INTEGER'后缀;
    3.Type 参数必须为 'GL_UNSIGNED_INT' 和 'GL_INT' 中的一个,具体是哪个取决于Internal Format 的选择;
    4.最后一个参数'data’是一个已经在内存中创建好的整数型数组。

    纹理准备好后,在Shader中,要将采样器类型前面加上‘i'或’ui‘的前缀,分别表明有符号和无符号整数,如'isampler2D'

    最后,在Shader顶部,必须写入
    1. #extension  
    2.  GL_EXT_gpu_shader4 : enable  
        才能实现对整数型纹理的操作。

    下面是一个fragment shader的实例代码:
    1. #extension   
    2. GL_EXT_gpu_shader4 : enable  
    3.   
    4. varying out uvec4 FragOut;  
    5. uniform usampler2D Tex1;  
    6.   
    7. void main(void) {  
    8.     uvec4 texel = texture2D(Tex1,gl_TexCoord[0].st);  
    9.     FragOut = texel;  
    10. }  


    注意我们使用了自己定义的片元易变变量 FragOut而非gl_FragColor来进行输出,因为gl_FragColor只支持浮点型数据。
  • 相关阅读:
    第九届蓝桥杯省赛c/c++真题明码题解答案,另类excel解法思路
    Windows下将Python源代码.py文件封装成exe可执行文件方法
    windows下python自带的pip安装速度过慢解决方案
    解决:Errors were encountered while processing
    ubuntu 安装 caffe 解决://home/xiaojie/anaconda/lib/libpng16.so.16:对‘inflateValidate@ZLIB_1.2.9’未定义的引用
    ubuntu安装caffe 解决:build_release/tools/caffe: error while loading shared libraries: libcudart.so.8.0: cannot open shar
    ubuntu 禁止内核更新
    ubutun 中notebook出现 Permission denied: Untitled.ipynb
    ubuntu下anaconda使用jupyter notebook加载tensorflow、pytorch
    ubuntu下用anaconda快速安装 pytorch
  • 原文地址:https://www.cnblogs.com/qwcbeyond/p/2040309.html
Copyright © 2011-2022 走看看