bgfx/examples/21-deferred/fs_deferred_light_uav.sc
kingscallop 436b7fab9e
Adds UAV support for D3D12, Vulkan and reworked for OpenGL, D3D11 (#2119)
* Adds UAV support for D3D12, Vulkan and reworked support for OpenGL, D3D11

UAV support is now uniform across compute and draw.
To set a UAV you just use bgfx::setImage() and IMAGE2D in the shader, just like in compute.
Due to these changes shaders will have to be recompiled.

The changes include:
	- D3D11 requires patching of the UAV slot number (which is now done by modifying the DXBC instead of using a macro)
	- If the DXBC binary includes a debug chunk, that is also patched to match the new slot number
	- All the other renderers don't need any kind of patching
	- There are some shader annotations to better convert the UAV format used in hlsl to spirv

Possibility of further enhancements:
	- bgfx::setViewFrameBuffer() only supports binding to a framebuffer or, using BGFX_INVALID_HANDLE, to bind the default backbuffer. This doesn't allow for the case where there is no need to bind to either one of them, for example when using a fragment shader only to read and write to an UAV.

* Bump shader version, because they need to be recompiled.
2020-10-04 21:51:41 -07:00

41 lines
1.1 KiB
Python

$input v_texcoord0
/*
* Copyright 2011-2020 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "common.sh"
#include <bgfx_compute.sh>
SAMPLER2D(s_normal, 0);
SAMPLER2D(s_depth, 1);
IMAGE2D_RW(i_light, rgba8, 3);
uniform vec4 u_lightPosRadius[1];
uniform vec4 u_lightRgbInnerR[1];
uniform mat4 u_mtx;
void main()
{
vec3 normal = decodeNormalUint(texture2D(s_normal, v_texcoord0).xyz);
float deviceDepth = texture2D(s_depth, v_texcoord0).x;
float depth = toClipSpaceDepth(deviceDepth);
vec3 clip = vec3(v_texcoord0 * 2.0 - 1.0, depth);
#if !BGFX_SHADER_LANGUAGE_GLSL
clip.y = -clip.y;
#endif // !BGFX_SHADER_LANGUAGE_GLSL
vec3 wpos = clipToWorld(u_mtx, clip);
vec3 view = mul(u_view, vec4(wpos, 0.0) ).xyz;
view = -normalize(view);
ivec2 coord = ivec2(gl_FragCoord.xy);
vec3 lightColor = calcLight(wpos, normal, view, u_lightPosRadius[0].xyz, u_lightPosRadius[0].w, u_lightRgbInnerR[0].xyz, u_lightRgbInnerR[0].w);
vec4 color = imageLoad(i_light, coord);
imageStore(i_light, coord, color + vec4(toGamma(lightColor.xyz), 1.0));
}