简体   繁体   English

OpenGL-附加到纹理

[英]Opengl - appending to a texture

I want to create a texture system where I add to a texture, not overwrite it. 我想创建一个纹理系统,在其中添加纹理, 而不是覆盖它。 My texture has integer values (32 bit). 我的纹理具有整数值(32位)。 What I want: Ex. 我想要的是: I have an integer pixel with bits 100, I want to add 10 to it so it becomes 110. 我有一个位为100的整数像素,我想对其添加10,使其变为110。

My current implementation has two textures, one with the previous texture, and a texture to write on. 我当前的实现有两个纹理,一个具有先前的纹理,另一个是要写入的纹理。 The previous texture's values are read and then rewritten with the new data. 读取先前的纹理值,然后用新数据重写。 Is there a better method to do so because using two textures feel very inefficient? 是否有更好的方法,因为使用两个纹理感觉效率很低?

Depending on what you mean by "appending", you could use additive blending : 根据您“附加”的意思,可以使用添加混合

glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_ONE, GL_ONE);

then, the routput of your fragment shader will by added to the current contents of the color buffer. 然后,片段着色器的输出将添加到颜色缓冲区的当前内容中。 If you use a FBO to render into the texture, you can directly add to this texture. 如果使用FBO渲染到纹理中,则可以直接添加到该纹理中。

You should just be careful to not create any feedback loops, so your fragment shader's result should not depend on any sample of the very same texture you render to. 您应该小心不要创建任何反馈循环,因此片段着色器的结果不应依赖于渲染到的纹理相同的任何样本。

UPDATE 更新

As noted in the comment, the texture in question has GL_RED_INTEGER format. 如评论中所述,所讨论的纹理具有GL_RED_INTEGER格式。 Unfortunately, the blending is only applied on floating-point color buffers (including normalized integers), and never on unnormalized integers. 不幸的是,混合仅应用于浮点颜色缓冲区(包括归一化的整数),而不应用于未归一化的整数。

However, there is another potential approach. 但是,还有另一种可能的方法。 The rules for the "feedback loops" I mentioned before have been relaxed with recent OpenGL. 我最近提到的“反馈循环”规则在最近的OpenGL中已经放宽了。 The extension GL_ARB_texture_barrier explicitely allowes a fragment shader to read pixels from the same texture it is writing to: 扩展名GL_ARB_texture_barrier明确允许片段着色器从其写入的相同纹理读取像素:

Specifically, the values of rendered fragments are undefined if any shader stage fetches texels and the same texels are written via fragment shader outputs, even if the reads and writes are not in the same Draw call, unless any of the following exceptions apply : 具体来说,如果有任何着色器阶段获取纹理像素,并且通过片段着色器输出写入相同的纹理像素,则渲染的片段的值是不确定的,即使读写不在同一个Draw调用中, 除非以下任何例外情况都适用

  • The reads and writes are from/to disjoint sets of texels (after accounting for texture filtering rules). 读写来自不相交的纹素集(考虑纹理过滤规则之后)。

  • There is only a single read and write of each texel, and the read is in the fragment shader invocation that writes the same texel (eg using "texelFetch2D(sampler, ivec2(gl_FragCoord.xy), 0);"). 每个纹理元素只有一个读和写操作,并且在片段着色器调用中写入相同的纹理元素(例如,使用“ texelFetch2D(sampler,ivec2(gl_FragCoord.xy),0);”)。

  • [...] [...]

This extension has been promoted to a core feature of OpenGL 4.5. 此扩展已提升为OpenGL 4.5的核心功能。 This is quite new and not available on a lot of platforms, so it is unclear if you can use it... 这是相当新的东西,在许多平台上都不可用,因此尚不清楚是否可以使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM