Repository: microsoft/terminal
File: src/renderer/atlas/AtlasEngine.r.cpp (C++)

Code before:
        THROW_IF_FAILED(_r.deviceContext->Map(_r.customShaderConstantBuffer.get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped));
        assert(mapped.RowPitch >= sizeof(data));
        memcpy(mapped.pData, &data, sizeof(data));
        _r.deviceContext->Unmap(_r.customShaderConstantBuffer.get(), 0);
    }

    // Render with the custom shader.
    {
        // OM: Output Merger
        // customOffscreenTextureView was just rendered to via customOffscreenTextureTargetView and is
        // set as the output target. Before we can use it as an input we have to remove it as an output.
        _r.deviceContext->OMSetRenderTargets(1, _r.renderTargetView.addressof(), nullptr);

        // VS: Vertex Shader
        _r.deviceContext->VSSetShader(_r.customVertexShader.get(), nullptr, 0);

        // PS: Pixel Shader
        _r.deviceContext->PSSetShader(_r.customPixelShader.get(), nullptr, 0);
        _r.deviceContext->PSSetConstantBuffers(0, 1, _r.customShaderConstantBuffer.addressof());
        if (_r.customShaderTexture.TextureView)
        {
            ID3D11ShaderResourceView* const resourceViews[]{
                _r.customOffscreenTextureView.get(),
                _r.customShaderTexture.TextureView.get(),
            };
            _r.deviceContext->PSSetShaderResources(0, 2, static_cast<ID3D11ShaderResourceView* const*>(resourceViews));
        }
        else
        {
            _r.deviceContext->PSSetShaderResources(0, 1, _r.customOffscreenTextureView.addressof());
        }
        _r.deviceContext->PSSetSamplers(0, 1, _r.customShaderSamplerState.addressof());

        _r.deviceContext->Draw(4, 0);
    }

    // For the next frame we need to restore our context state.
    {
        // VS: Vertex Shader
        _r.deviceContext->VSSetShader(_r.vertexShader.get(), nullptr, 0);

        // PS: Pixel Shader
        _r.deviceContext->PSSetShader(_r.pixelShader.get(), nullptr, 0);
        _r.deviceContext->PSSetConstantBuffers(0, 1, _r.constantBuffer.addressof());
        const std::array resources{ _r.cellView.get(), _r.atlasView.get() };
        _r.deviceContext->PSSetShaderResources(0, gsl::narrow_cast<UINT>(resources.size()), resources.data());
        _r.deviceContext->PSSetSamplers(0, 0, nullptr);
    }
}

void AtlasEngine::_setShaderResources() const

Review comment (refactor):
You could deduplicate this code by making the length dependent on the existence of the texture view like so:
```cpp
ID3D11ShaderResourceView* const views[]{
    _r.customOffscreenTextureView.get(),
    _r.customShaderTexture.TextureView.get(),
};
const UINT numViews = _r.customShaderTexture.TextureView ? 2 : 1;
_r.deviceContext->PSSetShaderResources(0, numViews, &views[0]);
```

Code after:
            /* Software */ nullptr,
            /* Flags */ deviceFlags,
            /* pFeatureLevels */ featureLevels.data(),
            /* FeatureLevels */ gsl::narrow_cast<UINT>(featureLevels.size()),
            /* SDKVersion */ D3D11_SDK_VERSION,
            /* ppDevice */ device0.put(),
            /* pFeatureLevel */ &featureLevel,
            /* ppImmediateContext */ deviceContext0.put());
    }
#endif
    THROW_IF_FAILED(hr);

    auto device = device0.query<ID3D11Device2>();
    auto deviceContext = deviceContext0.query<ID3D11DeviceContext2>();

#ifndef NDEBUG
    if (IsDebuggerPresent())
    {
        if (const auto d3dInfoQueue = device.try_query<ID3D11InfoQueue>())
        {
            for (const auto severity : { D3D11_MESSAGE_SEVERITY_CORRUPTION, D3D11_MESSAGE_SEVERITY_ERROR, D3D11_MESSAGE_SEVERITY_WARNING, D3D11_MESSAGE_SEVERITY_INFO })
            {
                LOG_IF_FAILED(d3dInfoQueue->SetBreakOnSeverity(severity, true));
            }
        }
    }
#endif

    if (featureLevel < D3D_FEATURE_LEVEL_10_0)
    {
        d2dMode = true;
    }
    else if (featureLevel < D3D_FEATURE_LEVEL_11_0)
    {
        D3D11_FEATURE_DATA_D3D10_X_HARDWARE_OPTIONS options{};
        // I'm assuming if `CheckFeatureSupport` fails, it'll leave `options` untouched which will result in `d2dMode |= true`.
        std::ignore = device->CheckFeatureSupport(D3D11_FEATURE_D3D10_X_HARDWARE_OPTIONS, &options, sizeof(options));
        d2dMode |= !options.ComputeShaders_Plus_RawAndStructuredBuffers_Via_Shader_4_x;
    }

    _p.device = std::move(device);
    _p.deviceContext = std::move(deviceContext);

    if (d2dMode)
    {
        _b = std::make_unique<BackendD2D>();
    }
    else
    {
        _b = std::make_unique<BackendD3D>(_p);
    }