每帧多次调用 SDL_SetTextureColorMod 是否安全/可接受?

Zam*_*lad 5 sdl sdl-2

作为渲染除颜色之外的多个相同纹理的简单方法,我将一个纯白色圆圈加载到然后SDL_Texture调用SDL_SetTextureColorMod()给它我想要制作圆圈的颜色。

如果纹理是单独的(示例 1),那么这一切都可以正常工作,但如果我共享SDL_Texture以便多个对象都引用它,则意味着SDL_SetTextureColorMod()必须在对象渲染纹理之前调用每个渲染帧,因为它上次给出的颜色可能已被另一个对象更改(示例 2)。

SDL_SetTextureColorMod()对于可能有相当多共享纹理的对象,调用每个渲染帧是否会导致重大性能问题?

需要它的原因是系统是使用具有基本引用计数的共享纹理功能来设计的(我知道可能有更好的方法使用智能指针来做到这一点,但这不是这里讨论的主题)。SDL_Texture让每个对象都有自己的副本,这样它只需设置颜色一次(或每当需要更改时)而不是每个渲染帧设置颜色会更好吗?

示例1:

SDL_Texture* tex1;
SDL_Texture* tex2;
SDL_Texture* tex3;
...
// All 3 instances have their own SDL_Texture
MyObject A(tex1);
MyObject B(tex2);
MyObject C(tex3);
...
// A call to set the color of the texture is only required once for each class
Run Code Online (Sandbox Code Playgroud)

示例2:

SDL_Texture* tex;
...
// All 3 instances share the SDL_Texture
MyObject A(tex);
MyObject B(tex);
MyObject C(tex);
...
// A call to set the color of the texture must be made before rendering 
// each object to ensure that any other object has not set a different color.
// E.g if the draw order was A, B, C and the color was set to be different
// for each object then before rendering B, each frame it would need to set 
// the color again otherwise it would have the color of A and the same 
// for the others    
Run Code Online (Sandbox Code Playgroud)

编辑:这也将扩展到SDL_SetTextureAlphaMod()SDL_SetTextureBlendMode()或其他类似的功能

作为参考,我正在使用 SDL2。

更新:最初认为对该函数的调用只是更新颜色混合标志。我做了更多的调查,这部分正确,可以在 - http://pastebin.com/pMjgVkmM的函数中看到

但是,如果渲染器分配了一个SetTextureColorMod()在大多数情况下都是这种情况的函数,那么它会映射到函数“SW_SetTextureColorMod()” - http://pastebin.com/qYtxD0TH

这反过来又调用SDL_SetSurfaceColorMod()- http://pastebin.com/GrsVibAz

我在这里担心的是,此时有一个潜在的调用,SDL_InvalidateMap我认为它可能会导致释放,尽管我不确定 - http://pastebin.com/r0HGJYHT

kel*_*tar 5

SDL_SetTextureColorMod不更新纹理纹理 - 它保存乘数颜色并引发 SDL_MODTEXTURE_COLOR 标志。该纹理上的后续 RenderCopy 将以固定颜色乘以纹素。

由于它不修改纹理,并且只是纹理标头中的一个小常量,因此在渲染帧中为同一纹理多次设置它是完全可以的。克隆纹理会适得其反(更多的内存使用,并且由于更高的内存带宽压力可能会降低帧速率)。