C语言 表面和纹理的区别(SDL / 一般)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21392755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between surface and texture (SDL / general)
提问by ps-aux
Can anyone explain to me in simple words what is the difference between texture and surface? I saw it used in SDL2as SDL_Surfaceand SDL_Texture. SDL_Textureis created from SDL_Surfacewhich in turn is created from image/bitmap. Both are collection of pixels. But I do not see the main difference between them (has to do something with GPU?)
谁能用简单的话向我解释纹理和表面之间的区别是什么?我看到它在SDL2asSDL_Surface和 中使用SDL_Texture。SDL_Texture是从创建的SDL_Surface,反过来又是从图像/位图创建的。两者都是像素的集合。但我没有看到它们之间的主要区别(必须用 GPU 做点什么?)
I tried to google it but all explanations I found were too complex to understand them without digging deeper into computer graphics stuff.
我试图用谷歌搜索它,但我发现的所有解释都太复杂了,如果不深入研究计算机图形学的东西,就无法理解它们。
采纳答案by Petr Abdulin
Basically your assumption "has to do something with GPU?" is right.
基本上你的假设“必须用 GPU 做点什么?” 是对的。
SDL_Surfaceis used in softwarerendering. With software rendering, as saloomi2012correctly noticed, you are using regular RAM to store image data. Thus, in most cases you can access data buffer associated with surface directly, modifying its content, i.e. it is using CPU, hence the softwarename.
SDL_Surface用于软件渲染。使用软件渲染,正如saloomi2012正确注意到的那样,您正在使用常规 RAM 来存储图像数据。因此,在大多数情况下,您可以直接访问与表面关联的数据缓冲区,修改其内容,即它正在使用 CPU,因此软件名称。
SDL_Textureon the other hand, is used in a hardwarerendering, textures are stored in VRAM and you don't have access to it directly as with SDL_Surface. The rendering operations are accelerated by GPU, using, internally, either OpenGL or DirectX (available only on Windows) API, which in turn are using your video hardware, hence hardwarerendering name.
SDL_Texture另一方面,用于硬件渲染,纹理存储在 VRAM 中,您无法像SDL_Surface. 渲染操作由 GPU 加速,在内部使用 OpenGL 或 DirectX(仅在 Windows 上可用)API,它们又使用您的视频硬件,因此硬件渲染名称。
Needless to say that hardware rendering is by orders of magnitude faster than software rendering and should be always be considered as primary option.
毋庸置疑,硬件渲染比软件渲染快几个数量级,应始终被视为主要选项。
回答by saloomi2012
SDL_Textureis loaded in your video card's VRAM instead of regular RAM.
SDL_Texture加载到您的视频卡的 VRAM 而不是常规 RAM 中。
回答by Evander
Surfaces use your RAM and Textures use your video card that is more fast than the surfaces.
表面使用您的 RAM,而纹理使用比表面更快的视频卡。
回答by omotto
There is more information about that in:
有更多关于它的信息:
https://thenumbat.github.io/cpp-course/sdl2/05/05.html
https://thenumbat.github.io/cpp-course/sdl2/05/05.html
As mentioned in the last lesson, textures are the GPU rendering equivalent of surfaces. Hence, textures are almost always created from surfaces, using the function SDL_CreateTextureFromSurface(). This function more or less does what you'd expect—the parameters are the rendering context and a surface to create the texture from. As with other creation functions, it will return NULL on failure.
正如在上一课中提到的,纹理是表面的 GPU 渲染等价物。因此,纹理几乎总是从表面创建,使用函数 SDL_CreateTextureFromSurface()。这个函数或多或少会做你所期望的——参数是渲染上下文和一个用来创建纹理的表面。与其他创建函数一样,它会在失败时返回 NULL。
I hope it helps you!
我希望它能帮助你!

