playcanvas / engine

JavaScript game engine built on WebGL, WebGPU, WebXR and glTF
https://playcanvas.com
MIT License
9.7k stars 1.36k forks source link

Add support for rendering to a mipLevel of RenderTarget on WebGL 2 #7052

Open mvaligursky opened 3 weeks ago

mvaligursky commented 3 weeks ago

see the API / WebGPU implementation here: https://github.com/playcanvas/engine/pull/7051

it might need these to be utilized:

    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_BASE_LEVEL, 0);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAX_LEVEL, 2);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAX_LOD, 2);

related issue: https://github.com/KhronosGroup/WebGL/issues/3614

I tried to implement it but without success, the PIX capture shows that we render to level 1 as set up, but when sampling the texture to display it on the screen, only level 0 was somehow exposed. The above parameters didn't seem to make any difference. Note: we create top level using texImage2D, and then create full chain using generateMipmap - it's possible we need to create the whole chain using texImage2D or texStorage2D.

mvaligursky commented 3 weeks ago

Also note that the WebGPU implementation only handles rendering to Mipmap. The ultimate goal is to be able to write a custom mipmapper (specifically to generate mip versions of a depth texture). For this we need two parts:

For WebGPU, we need to create a custom view with some levels. For WebGL we need to use this texParameteri calls from the description. Note that we could avoid writing a public API for this, as that could be tricky, and implement this under the hood only. WebgpuMipmapRenderer effectively does it already.