mirror of
https://github.com/Ed94/raddebugger.git
synced 2026-07-18 07:01:31 -07:00
blut shader improvements, use bilinear sampling for 2x fewer samples
This commit is contained in:
committed by
Ryan Fleury
parent
a77d457f51
commit
1705fdd421
@@ -1183,99 +1183,140 @@ r_window_submit(OS_Handle window, R_Handle window_equip, R_PassList *passes)
|
||||
case R_PassKind_Blur:
|
||||
{
|
||||
R_PassParams_Blur *params = pass->params_blur;
|
||||
ID3D11SamplerState *sampler = r_d3d11_state->samplers[R_Tex2DSampleKind_Nearest];
|
||||
ID3D11SamplerState *sampler = r_d3d11_state->samplers[R_Tex2DSampleKind_Linear];
|
||||
ID3D11VertexShader *vshad = r_d3d11_state->vshads[R_D3D11_VShadKind_Blur];
|
||||
ID3D11PixelShader *pshad = r_d3d11_state->pshads[R_D3D11_PShadKind_Blur];
|
||||
ID3D11Buffer *uniforms_buffer = r_d3d11_state->uniform_type_kind_buffers[R_D3D11_VShadKind_Blur];
|
||||
|
||||
//- rjf: perform blur on each axis
|
||||
ID3D11RenderTargetView *rtvs[Axis2_COUNT] =
|
||||
|
||||
// rjf: setup output merger
|
||||
d_ctx->OMSetDepthStencilState(r_d3d11_state->noop_depth_stencil, 0);
|
||||
d_ctx->OMSetBlendState(r_d3d11_state->main_blend_state, 0, 0xffffffff);
|
||||
|
||||
// rjf: set up viewport
|
||||
Vec2S32 resolution = wnd->last_resolution;
|
||||
D3D11_VIEWPORT viewport = { 0.0f, 0.0f, (F32)resolution.x, (F32)resolution.y, 0.0f, 1.0f };
|
||||
d_ctx->RSSetViewports(1, &viewport);
|
||||
d_ctx->RSSetState(r_d3d11_state->main_rasterizer);
|
||||
|
||||
// rjf: setup input assembly
|
||||
d_ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
|
||||
d_ctx->IASetInputLayout(0);
|
||||
|
||||
// rjf: setup shaders
|
||||
d_ctx->VSSetShader(vshad, 0, 0);
|
||||
d_ctx->VSSetConstantBuffers(0, 1, &uniforms_buffer);
|
||||
d_ctx->PSSetShader(pshad, 0, 0);
|
||||
d_ctx->PSSetSamplers(0, 1, &sampler);
|
||||
|
||||
// rjf: setup scissor rect
|
||||
{
|
||||
wnd->stage_scratch_color_rtv,
|
||||
wnd->stage_color_rtv,
|
||||
};
|
||||
ID3D11ShaderResourceView *srvs[Axis2_COUNT] =
|
||||
{
|
||||
wnd->stage_color_srv,
|
||||
wnd->stage_scratch_color_srv,
|
||||
};
|
||||
for(Axis2 axis = (Axis2)0; axis < Axis2_COUNT; axis = (Axis2)(axis+1))
|
||||
{
|
||||
// rjf: setup output merger
|
||||
d_ctx->OMSetRenderTargets(1, &rtvs[axis], 0);
|
||||
d_ctx->OMSetDepthStencilState(r_d3d11_state->noop_depth_stencil, 0);
|
||||
d_ctx->OMSetBlendState(r_d3d11_state->main_blend_state, 0, 0xffffffff);
|
||||
|
||||
// rjf: set up viewport
|
||||
Vec2S32 resolution = wnd->last_resolution;
|
||||
D3D11_VIEWPORT viewport = { 0.0f, 0.0f, (F32)resolution.x, (F32)resolution.y, 0.0f, 1.0f };
|
||||
d_ctx->RSSetViewports(1, &viewport);
|
||||
d_ctx->RSSetState(r_d3d11_state->main_rasterizer);
|
||||
|
||||
// rjf: setup input assembly
|
||||
d_ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
|
||||
d_ctx->IASetInputLayout(0);
|
||||
|
||||
// rjf: set up uniforms
|
||||
{
|
||||
F32 stdev = (params->blur_size-1.f)/2.f;
|
||||
F32 one_over_root_2pi_stdev2 = 1/sqrt_f32(2*pi32*stdev*stdev);
|
||||
F32 euler32 = 2.718281828459045f;
|
||||
R_D3D11_Uniforms_Blur uniforms = {0};
|
||||
uniforms.viewport_size = v2f32(resolution.x, resolution.y);
|
||||
uniforms.rect = params->rect;
|
||||
uniforms.blur_size = params->blur_size;
|
||||
uniforms.is_vertical = (F32)!!axis;
|
||||
MemoryCopyArray(uniforms.corner_radii.v, params->corner_radii);
|
||||
F32 kernel_x = 0;
|
||||
uniforms.kernel[0].v[0] = 1.f;
|
||||
if(stdev > 0.f)
|
||||
{
|
||||
for(U64 idx = 0; idx < ArrayCount(uniforms.kernel); idx += 1)
|
||||
{
|
||||
for(U64 v_idx = 0; v_idx < ArrayCount(uniforms.kernel[idx].v); v_idx += 1)
|
||||
{
|
||||
uniforms.kernel[idx].v[v_idx] = one_over_root_2pi_stdev2*pow_f32(euler32, -kernel_x*kernel_x/(2.f*stdev*stdev));
|
||||
kernel_x += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(uniforms.kernel[0].v[0] > 1.f)
|
||||
{
|
||||
MemoryZeroArray(uniforms.kernel);
|
||||
uniforms.kernel[0].v[0] = 1.f;
|
||||
}
|
||||
D3D11_MAPPED_SUBRESOURCE sub_rsrc = {0};
|
||||
r_d3d11_state->device_ctx->Map(uniforms_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &sub_rsrc);
|
||||
MemoryCopy((U8 *)sub_rsrc.pData, &uniforms, sizeof(uniforms));
|
||||
r_d3d11_state->device_ctx->Unmap(uniforms_buffer, 0);
|
||||
}
|
||||
|
||||
// rjf: setup shaders
|
||||
d_ctx->VSSetShader(vshad, 0, 0);
|
||||
d_ctx->VSSetConstantBuffers(0, 1, &uniforms_buffer);
|
||||
d_ctx->PSSetShader(pshad, 0, 0);
|
||||
d_ctx->PSSetConstantBuffers(0, 1, &uniforms_buffer);
|
||||
d_ctx->PSSetShaderResources(0, 1, &srvs[axis]);
|
||||
d_ctx->PSSetSamplers(0, 1, &sampler);
|
||||
|
||||
// rjf: setup scissor rect
|
||||
{
|
||||
D3D11_RECT rect = {0};
|
||||
D3D11_RECT rect = { 0 };
|
||||
rect.left = 0;
|
||||
rect.right = (LONG)wnd->last_resolution.x;
|
||||
rect.top = 0;
|
||||
rect.bottom = (LONG)wnd->last_resolution.y;
|
||||
d_ctx->RSSetScissorRects(1, &rect);
|
||||
}
|
||||
|
||||
// rjf: draw
|
||||
d_ctx->Draw(4, 0);
|
||||
|
||||
// rjf: unset srv
|
||||
ID3D11ShaderResourceView *srv = 0;
|
||||
d_ctx->PSSetShaderResources(0, 1, &srv);
|
||||
}
|
||||
|
||||
// rjf: set up uniforms
|
||||
R_D3D11_Uniforms_Blur uniforms = { 0 };
|
||||
{
|
||||
F32 weights[ArrayCount(uniforms.kernel)*2] = {0};
|
||||
|
||||
F32 blur_size = Min(params->blur_size, ArrayCount(weights));
|
||||
U64 blur_count = (U64)round_f32(blur_size);
|
||||
|
||||
F32 stdev = (blur_size-1.f)/2.f;
|
||||
F32 one_over_root_2pi_stdev2 = 1/sqrt_f32(2*pi32*stdev*stdev);
|
||||
F32 euler32 = 2.718281828459045f;
|
||||
|
||||
weights[0] = 1.f;
|
||||
if(stdev > 0.f)
|
||||
{
|
||||
for(U64 idx = 0; idx < blur_count; idx += 1)
|
||||
{
|
||||
F32 kernel_x = (F32)idx;
|
||||
weights[idx] = one_over_root_2pi_stdev2*pow_f32(euler32, -kernel_x*kernel_x/(2.f*stdev*stdev));
|
||||
}
|
||||
}
|
||||
if(weights[0] > 1.f)
|
||||
{
|
||||
MemoryZeroArray(weights);
|
||||
weights[0] = 1.f;
|
||||
}
|
||||
else
|
||||
{
|
||||
// prepare weights & offsets for bilinear lookup
|
||||
// blur filter wants to calculate w0*pixel[pos] + w1*pixel[pos+1] + ...
|
||||
// with bilinear filter we can do this calulation by doing only w*sample(pos+t) = w*((1-t)*pixel[pos] + t*pixel[pos+1])
|
||||
// we can see w0=w*(1-t) and w1=w*t
|
||||
// thus w=w0+w1 and t=w1/w
|
||||
for (U64 idx = 1; idx < blur_count; idx += 2)
|
||||
{
|
||||
F32 w0 = weights[idx + 0];
|
||||
F32 w1 = weights[idx + 1];
|
||||
F32 w = w0 + w1;
|
||||
F32 t = w1 / w;
|
||||
|
||||
// each kernel element is float2(weight, offset)
|
||||
// weights & offsets are adjusted for bilinear sampling
|
||||
// zw elements are not used, a bit of waste but it allows for simpler shader code
|
||||
uniforms.kernel[(idx+1)/2] = v4f32(w, (F32)idx + t, 0, 0);
|
||||
}
|
||||
uniforms.kernel[0].x = weights[0];
|
||||
}
|
||||
|
||||
// technically we need just direction be different
|
||||
// but there are 256 bytes of usable space anyway for each constant buffer chunk
|
||||
|
||||
uniforms.passes[Axis2_X].viewport_size = v2f32(resolution.x, resolution.y);
|
||||
uniforms.passes[Axis2_X].rect = params->rect;
|
||||
uniforms.passes[Axis2_X].direction = v2f32(1.f / resolution.x, 0);
|
||||
uniforms.passes[Axis2_X].blur_count = 1 + blur_count / 2; // 2x smaller because of bilinear sampling
|
||||
MemoryCopyArray(uniforms.passes[Axis2_X].corner_radii.v, params->corner_radii);
|
||||
|
||||
uniforms.passes[Axis2_Y].viewport_size = v2f32(resolution.x, resolution.y);
|
||||
uniforms.passes[Axis2_Y].rect = params->rect;
|
||||
uniforms.passes[Axis2_Y].direction = v2f32(0, 1.f / resolution.y);
|
||||
uniforms.passes[Axis2_Y].blur_count = 1 + blur_count / 2; // 2x smaller because of bilinear sampling
|
||||
MemoryCopyArray(uniforms.passes[Axis2_Y].corner_radii.v, params->corner_radii);
|
||||
|
||||
D3D11_MAPPED_SUBRESOURCE sub_rsrc = {0};
|
||||
r_d3d11_state->device_ctx->Map(uniforms_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &sub_rsrc);
|
||||
MemoryCopy((U8 *)sub_rsrc.pData, &uniforms, sizeof(uniforms));
|
||||
r_d3d11_state->device_ctx->Unmap(uniforms_buffer, 0);
|
||||
}
|
||||
|
||||
ID3D11Buffer* uniforms_buffers[] = { uniforms_buffer, uniforms_buffer };
|
||||
|
||||
U32 uniform_offset[Axis2_COUNT][2] =
|
||||
{
|
||||
{ 0 * sizeof(R_D3D11_Uniforms_BlurPass) / 16, OffsetOf(R_D3D11_Uniforms_Blur, kernel) / 16 },
|
||||
{ 1 * sizeof(R_D3D11_Uniforms_BlurPass) / 16, OffsetOf(R_D3D11_Uniforms_Blur, kernel) / 16 },
|
||||
};
|
||||
|
||||
U32 uniform_count[Axis2_COUNT][2] =
|
||||
{
|
||||
{ sizeof(R_D3D11_Uniforms_BlurPass) / 16, sizeof(uniforms.kernel) / 16 },
|
||||
{ sizeof(R_D3D11_Uniforms_BlurPass) / 16, sizeof(uniforms.kernel) / 16 },
|
||||
};
|
||||
|
||||
// rjf: for unsetting srv
|
||||
ID3D11ShaderResourceView* srv = 0;
|
||||
|
||||
// horizontal pass
|
||||
d_ctx->OMSetRenderTargets(1, &wnd->stage_scratch_color_rtv, 0);
|
||||
d_ctx->PSSetConstantBuffers1(0, ArrayCount(uniforms_buffers), uniforms_buffers, uniform_offset[Axis2_X], uniform_count[Axis2_X]);
|
||||
d_ctx->PSSetShaderResources(0, 1, &wnd->stage_color_srv);
|
||||
d_ctx->Draw(4, 0);
|
||||
d_ctx->PSSetShaderResources(0, 1, &srv);
|
||||
|
||||
// vertical pass
|
||||
d_ctx->OMSetRenderTargets(1, &wnd->stage_color_rtv, 0);
|
||||
d_ctx->PSSetConstantBuffers1(0, ArrayCount(uniforms_buffers), uniforms_buffers, uniform_offset[Axis2_Y], uniform_count[Axis2_Y]);
|
||||
d_ctx->PSSetShaderResources(0, 1, &wnd->stage_scratch_color_srv);
|
||||
d_ctx->Draw(4, 0);
|
||||
d_ctx->PSSetShaderResources(0, 1, &srv);
|
||||
}break;
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user