Why is writing to a buffer from within a fragment shader disallowed in Metal?

后端 未结 2 1133
醉酒成梦
醉酒成梦 2021-02-05 02:47

As stated in the Metal Shading Language Guide:

Writes to a buffer or a texture are disallowed from a fragment function.

I understand tha

2条回答
  •  别那么骄傲
    2021-02-05 03:28

    I think you can neither write arbitrary pixels or texels on a fragment function on OpenGL or DirectX. One thing is the rendering API and other thing are the fragment or vertex functions.

    A fragment function is intended to produce as result a pixel / texel output, one per run, even is each one have multiple channels. Usually if you want to write to a buffer or texture you need to render something (a quad, triangle, or something using your fragment function over a surface (buffer or texture). As result each pixel / texel will be rendered using your fragment function. For example raycasting or raytracing fragment functions usually uses this approach.

    There is a good reason for not to allowing you to write arbitrary pixels / texels: parallelization. The fragment function is executed usually for lots of different pixels / texels at once at a time on most GPUs in a very high parallelization mode, each GPU has its own manner to parallelize (SMP, vectorial...) but all do very high paralelization. So you can write only by returning one pixel or texel channels output as the return of the fragment function in order to avoid common parallelization problems like races. This applies for each graphic library I know.

提交回复
热议问题