webgl:与其他程序/着色器共享纹理 [英] webgl: share texture with other program/shader

查看:90
本文介绍了webgl:与其他程序/着色器共享纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个着色器,它们使用不同的顶点数组和不同的统一值,但它们使用相同的纹理(精灵表/图集).它们是否可以共享相同的纹理(不会导致纹理两次发送到 gpu)?

i have two shaders that use different vertexarrays and different uniform values but they use the same texture (spritesheet/atlas). is it possible for them to share the same texture (without causing the texture to be sent to the gpu twice)?

背景:我的游戏在我的笔记本电脑上有一些严重的性能问题,它们似乎与 GPU 相关.我当前的实现使用了两个画布,一个用于我的背景,一个用于我的前景.然后他们为最终图像组成(绘制到第三个画布上).我的背景使用了 4 个纹理,而我的前景有一个大的 spritesheet.前景和背景都只使用一个绘制调用.

background: My game has some serious performance issues on my laptop and they seem to be gpu related. My current implementation uses two canvases, one for my background and one for my foreground. they then get composed for the final image (draw onto 3rd canvas). My background uses 4 textures, whereas my foreground has one large spritesheet. both foreground and background only use one draw call.

我希望通过将所有内容绘制到一张画布上并将所有纹理组合到一张 spritesheet 中来提高性能.这绝对有可能不会导致任何改进.我的背景使用噪声来混合纹理,主要问题很可能是着色器的复杂性.

I'm hoping to improve performance by drawing everything to one canvas and also by combining all textures into one spritesheet. its absolutely possible this will result in no improvement. my background uses noise to blend textures and its very possible that the main issue is the complexity of the shader.

推荐答案

它们是否可以共享相同的纹理(不会导致纹理两次发送到 gpu

is it possible for them to share the same texture (without causing the texture to be sent to the gpu twice

是的

纹理仅在您调用 gl.texImage2Dgl.texSubImage2D 时才会发送到 GPU.

Texture's are only sent to the GPU when you call gl.texImage2D or gl.texSubImage2D.

设置 WebGL 程序最常见的形式是

The most common form of setting a WebGL program is

在初始化时间

  • 创建/编译/链接程序
  • 创建/上传缓冲区(顶点数据)
  • 创建/上传纹理

在渲染时

  • 使用程序
  • 设置属性
  • 设置制服和绑定纹理
  • 画图

对于纹理,在初始化时间部分"通常意味着调用

For textures the "at init time part" generally means calling

gl.createTexture  // to create a teture
gl.bindTexture    // to assign the texture so follow commands will affect it.
gl.texImage2D     // to upload data
gl.texParameteri  // to set filtering
gl.generateMipmap // if you need mips

运行时

gl.activeTexture // to choose a texture unit
gl.bindTexture   // to assign an existing texture to the active texture unit
gl.uniform1i     // to tell the shader which unit to use for a specific sampler

就将您的纹理组合成纹理图集而言,是的,这可能会使您的程序运行得更快.这不是因为你上传的纹理更少,而是因为你可以用更少的绘制调用绘制更多的东西.查看本文底部附近的立方体示例.

As far as combing your textures into a texture atlas, yes, that will likely make your program run faster. That's not because you're uploading textures less though it's because you can draw more things with less draw calls. See the cube example near the bottom of this article.

这篇关于webgl:与其他程序/着色器共享纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆