1
0
mirror of https://github.com/doitsujin/dxvk.git synced 2024-12-01 16:24:12 +01:00

[d3d9] Use BitMask helper

This commit is contained in:
Georg Lehmann 2021-08-13 18:48:09 +02:00 committed by Joshie
parent b037fc8882
commit 6103e3c800
3 changed files with 43 additions and 76 deletions

View File

@ -1492,8 +1492,8 @@ namespace dxvk {
// Clear render targets if we need to.
if (Flags & D3DCLEAR_TARGET) {
for (uint32_t rt = m_boundRTs; rt; rt &= rt - 1) {
const auto& rts = m_state.renderTargets[bit::tzcnt(rt)];
for (uint32_t rt : bit::BitMask(m_boundRTs)) {
const auto& rts = m_state.renderTargets[rt];
const auto& rtv = rts->GetRenderTargetView(srgb);
if (likely(rtv != nullptr)) {
@ -4307,9 +4307,8 @@ namespace dxvk {
if (managed && !m_d3d9Options.evictManagedOnUnlock && !readOnly) {
pResource->SetNeedsUpload(Subresource, true);
for (uint32_t tex = m_activeTextures; tex; tex &= tex - 1) {
for (uint32_t i : bit::BitMask(m_activeTextures)) {
// Guaranteed to not be nullptr...
const uint32_t i = bit::tzcnt(tex);
auto texInfo = GetCommonTexture(m_state.textures[i]);
if (texInfo == pResource) {
@ -5123,13 +5122,12 @@ namespace dxvk {
masks.samplerMask &= m_activeRTTextures;
m_activeHazardsRT = m_activeHazardsRT & (~rtMask);
for (uint32_t rt = masks.rtMask; rt; rt &= rt - 1) {
for (uint32_t sampler = masks.samplerMask; sampler; sampler &= sampler - 1) {
const uint32_t rtIdx = bit::tzcnt(rt);
for (uint32_t rtIdx : bit::BitMask(masks.rtMask)) {
for (uint32_t samplerIdx : bit::BitMask(masks.samplerMask)) {
D3D9Surface* rtSurf = m_state.renderTargets[rtIdx].ptr();
IDirect3DBaseTexture9* rtBase = rtSurf->GetBaseTexture();
IDirect3DBaseTexture9* texBase = m_state.textures[bit::tzcnt(sampler)];
IDirect3DBaseTexture9* texBase = m_state.textures[samplerIdx];
// HACK: Don't mark for hazards if we aren't rendering to mip 0!
// Some games use screenspace passes like this for blurring
@ -5150,9 +5148,7 @@ namespace dxvk {
if (m_state.depthStencil != nullptr &&
m_state.depthStencil->GetBaseTexture() != nullptr) {
uint32_t samplerMask = m_activeDSTextures & texMask;
for (uint32_t sampler = samplerMask; sampler; sampler &= sampler - 1) {
const uint32_t samplerIdx = bit::tzcnt(sampler);
for (uint32_t samplerIdx : bit::BitMask(samplerMask)) {
IDirect3DBaseTexture9* dsBase = m_state.depthStencil->GetBaseTexture();
IDirect3DBaseTexture9* texBase = m_state.textures[samplerIdx];
@ -5166,9 +5162,9 @@ namespace dxvk {
void D3D9DeviceEx::MarkRenderHazards() {
for (uint32_t rt = m_activeHazardsRT; rt; rt &= rt - 1) {
for (uint32_t rtIdx : bit::BitMask(m_activeHazardsRT)) {
// Guaranteed to not be nullptr...
auto tex = m_state.renderTargets[bit::tzcnt(rt)]->GetCommonTexture();
auto tex = m_state.renderTargets[rtIdx]->GetCommonTexture();
if (unlikely(!tex->MarkHazardous())) {
TransitionImage(tex, VK_IMAGE_LAYOUT_GENERAL);
m_flags.set(D3D9DeviceFlag::DirtyFramebuffer);
@ -5192,17 +5188,17 @@ namespace dxvk {
void D3D9DeviceEx::UploadManagedTextures(uint32_t mask) {
// Guaranteed to not be nullptr...
for (uint32_t tex = mask; tex; tex &= tex - 1)
UploadManagedTexture(GetCommonTexture(m_state.textures[bit::tzcnt(tex)]));
for (uint32_t texIdx : bit::BitMask(mask))
UploadManagedTexture(GetCommonTexture(m_state.textures[texIdx]));
m_activeTexturesToUpload &= ~mask;
}
void D3D9DeviceEx::GenerateTextureMips(uint32_t mask) {
for (uint32_t tex = mask; tex; tex &= tex - 1) {
for (uint32_t texIdx : bit::BitMask(mask)) {
// Guaranteed to not be nullptr...
auto texInfo = GetCommonTexture(m_state.textures[bit::tzcnt(tex)]);
auto texInfo = GetCommonTexture(m_state.textures[texIdx]);
if (texInfo->NeedsMipGen()) {
this->EmitGenerateMips(texInfo);
@ -5218,9 +5214,8 @@ namespace dxvk {
pResource->SetNeedsMipGen(true);
pResource->MarkAllWrittenByGPU();
for (uint32_t tex = m_activeTextures; tex; tex &= tex - 1) {
for (uint32_t i : bit::BitMask(m_activeTextures)) {
// Guaranteed to not be nullptr...
const uint32_t i = bit::tzcnt(tex);
auto texInfo = GetCommonTexture(m_state.textures[i]);
if (texInfo == pResource) {
@ -5235,9 +5230,8 @@ namespace dxvk {
void D3D9DeviceEx::MarkTextureMipsUnDirty(D3D9CommonTexture* pResource) {
pResource->SetNeedsMipGen(false);
for (uint32_t tex = m_activeTextures; tex; tex &= tex - 1) {
for (uint32_t i : bit::BitMask(m_activeTextures)) {
// Guaranteed to not be nullptr...
const uint32_t i = bit::tzcnt(tex);
auto texInfo = GetCommonTexture(m_state.textures[i]);
if (texInfo == pResource)
@ -5247,9 +5241,8 @@ namespace dxvk {
void D3D9DeviceEx::MarkTextureUploaded(D3D9CommonTexture* pResource) {
for (uint32_t tex = m_activeTextures; tex; tex &= tex - 1) {
for (uint32_t i : bit::BitMask(m_activeTextures)) {
// Guaranteed to not be nullptr...
const uint32_t i = bit::tzcnt(tex);
auto texInfo = GetCommonTexture(m_state.textures[i]);
if (texInfo == pResource)
@ -5389,8 +5382,7 @@ namespace dxvk {
// target bindings are updated. Set up the attachments.
VkSampleCountFlagBits sampleCount = VK_SAMPLE_COUNT_FLAG_BITS_MAX_ENUM;
for (uint32_t rt = m_boundRTs; rt; rt &= rt - 1) {
uint32_t i = bit::tzcnt(rt);
for (uint32_t i : bit::BitMask(m_boundRTs)) {
const DxvkImageCreateInfo& rtImageInfo = m_state.renderTargets[i]->GetCommonTexture()->GetImage()->info();
if (likely(sampleCount == VK_SAMPLE_COUNT_FLAG_BITS_MAX_ENUM))
@ -5854,16 +5846,16 @@ namespace dxvk {
void D3D9DeviceEx::UndirtySamplers(uint32_t mask) {
for (uint32_t dirty = mask; dirty; dirty &= dirty - 1)
BindSampler(bit::tzcnt(dirty));
for (uint32_t i : bit::BitMask(mask))
BindSampler(i);
m_dirtySamplerStates &= ~mask;
}
void D3D9DeviceEx::UndirtyTextures(uint32_t mask) {
for (uint32_t tex = mask; tex; tex &= tex - 1)
BindTexture(bit::tzcnt(tex));
for (uint32_t i : bit::BitMask(mask))
BindTexture(i);
m_dirtyTextures &= ~mask;
}
@ -5871,8 +5863,7 @@ namespace dxvk {
void D3D9DeviceEx::MarkTextureBindingDirty(IDirect3DBaseTexture9* texture) {
D3D9DeviceLock lock = LockDevice();
for (uint32_t tex = m_activeTextures; tex; tex &= tex - 1) {
const uint32_t i = bit::tzcnt(tex);
for (uint32_t i : bit::BitMask(m_activeTextures)) {
if (m_state.textures[i] == texture)
m_dirtyTextures |= 1u << i;
}

View File

@ -175,20 +175,17 @@ namespace dxvk {
template <typename Dst, typename Src>
void ApplyOrCapture(Dst* dst, const Src* src) {
if (m_captures.flags.test(D3D9CapturedStateFlag::StreamFreq)) {
for (uint32_t stream = m_captures.streamFreq.dword(0); stream; stream &= stream - 1) {
uint32_t idx = bit::tzcnt(stream);
for (uint32_t idx : bit::BitMask(m_captures.streamFreq.dword(0)))
dst->SetStreamSourceFreq(idx, src->streamFreq[idx]);
}
}
if (m_captures.flags.test(D3D9CapturedStateFlag::Indices))
dst->SetIndices(src->indices.ptr());
if (m_captures.flags.test(D3D9CapturedStateFlag::RenderStates)) {
for (uint32_t i = 0; i < m_captures.renderStates.dwordCount(); i++) {
for (uint32_t rs = m_captures.renderStates.dword(i); rs; rs &= rs - 1) {
uint32_t idx = i * 32 + bit::tzcnt(rs);
for (uint32_t rs : bit::BitMask(m_captures.renderStates.dword(i))) {
uint32_t idx = i * 32 + rs;
dst->SetRenderState(D3DRENDERSTATETYPE(idx), src->renderStates[idx]);
}
@ -196,21 +193,14 @@ namespace dxvk {
}
if (m_captures.flags.test(D3D9CapturedStateFlag::SamplerStates)) {
for (uint32_t sampler = m_captures.samplers.dword(0); sampler; sampler &= sampler - 1) {
uint32_t samplerIdx = bit::tzcnt(sampler);
for (uint32_t state = m_captures.samplerStates[samplerIdx].dword(0); state; state &= state - 1) {
uint32_t stateIdx = bit::tzcnt(state);
for (uint32_t samplerIdx : bit::BitMask(m_captures.samplers.dword(0))) {
for (uint32_t stateIdx : bit::BitMask(m_captures.samplerStates[samplerIdx].dword(0)))
dst->SetStateSamplerState(samplerIdx, D3DSAMPLERSTATETYPE(stateIdx), src->samplerStates[samplerIdx][stateIdx]);
}
}
}
if (m_captures.flags.test(D3D9CapturedStateFlag::VertexBuffers)) {
for (uint32_t vb = m_captures.vertexBuffers.dword(0); vb; vb &= vb - 1) {
uint32_t idx = bit::tzcnt(vb);
for (uint32_t idx : bit::BitMask(m_captures.vertexBuffers.dword(0))) {
const auto& vbo = src->vertexBuffers[idx];
dst->SetStreamSource(
idx,
@ -224,12 +214,9 @@ namespace dxvk {
dst->SetMaterial(&src->material);
if (m_captures.flags.test(D3D9CapturedStateFlag::Textures)) {
for (uint32_t tex = m_captures.textures.dword(0); tex; tex &= tex - 1) {
uint32_t idx = bit::tzcnt(tex);
for (uint32_t idx : bit::BitMask(m_captures.textures.dword(0)))
dst->SetStateTexture(idx, src->textures[idx]);
}
}
if (m_captures.flags.test(D3D9CapturedStateFlag::VertexShader))
dst->SetVertexShader(src->vertexShader.ptr());
@ -239,8 +226,8 @@ namespace dxvk {
if (m_captures.flags.test(D3D9CapturedStateFlag::Transforms)) {
for (uint32_t i = 0; i < m_captures.transforms.dwordCount(); i++) {
for (uint32_t trans = m_captures.transforms.dword(i); trans; trans &= trans - 1) {
uint32_t idx = i * 32 + bit::tzcnt(trans);
for (uint32_t trans : bit::BitMask(m_captures.transforms.dword(i))) {
uint32_t idx = i * 32 + trans;
dst->SetStateTransform(idx, reinterpret_cast<const D3DMATRIX*>(&src->transforms[idx]));
}
@ -248,16 +235,11 @@ namespace dxvk {
}
if (m_captures.flags.test(D3D9CapturedStateFlag::TextureStages)) {
for (uint32_t stage = m_captures.textureStages.dword(0); stage; stage &= stage - 1) {
uint32_t stageIdx = bit::tzcnt(stage);
for (uint32_t state = m_captures.textureStageStates[stageIdx].dword(0); state; state &= state - 1) {
uint32_t stateIdx = bit::tzcnt(state);
for (uint32_t stageIdx : bit::BitMask(m_captures.textureStages.dword(0))) {
for (uint32_t stateIdx : bit::BitMask(m_captures.textureStageStates[stageIdx].dword(0)))
dst->SetStateTextureStageState(stageIdx, D3D9TextureStageStateTypes(stateIdx), src->textureStages[stageIdx][stateIdx]);
}
}
}
if (m_captures.flags.test(D3D9CapturedStateFlag::Viewport))
dst->SetViewport(&src->viewport);
@ -266,25 +248,22 @@ namespace dxvk {
dst->SetScissorRect(&src->scissorRect);
if (m_captures.flags.test(D3D9CapturedStateFlag::ClipPlanes)) {
for (uint32_t plane = m_captures.clipPlanes.dword(0); plane; plane &= plane - 1) {
uint32_t idx = bit::tzcnt(plane);
for (uint32_t idx : bit::BitMask(m_captures.clipPlanes.dword(0)))
dst->SetClipPlane(idx, src->clipPlanes[idx].coeff);
}
}
if (m_captures.flags.test(D3D9CapturedStateFlag::VsConstants)) {
for (uint32_t i = 0; i < m_captures.vsConsts.fConsts.dwordCount(); i++) {
for (uint32_t consts = m_captures.vsConsts.fConsts.dword(i); consts; consts &= consts - 1) {
uint32_t idx = i * 32 + bit::tzcnt(consts);
for (uint32_t consts : bit::BitMask(m_captures.vsConsts.fConsts.dword(i))) {
uint32_t idx = i * 32 + consts;
dst->SetVertexShaderConstantF(idx, (float*)&src->vsConsts.fConsts[idx], 1);
}
}
for (uint32_t i = 0; i < m_captures.vsConsts.iConsts.dwordCount(); i++) {
for (uint32_t consts = m_captures.vsConsts.iConsts.dword(i); consts; consts &= consts - 1) {
uint32_t idx = i * 32 + bit::tzcnt(consts);
for (uint32_t consts : bit::BitMask(m_captures.vsConsts.iConsts.dword(i))) {
uint32_t idx = i * 32 + consts;
dst->SetVertexShaderConstantI(idx, (int*)&src->vsConsts.iConsts[idx], 1);
}
@ -298,16 +277,16 @@ namespace dxvk {
if (m_captures.flags.test(D3D9CapturedStateFlag::PsConstants)) {
for (uint32_t i = 0; i < m_captures.psConsts.fConsts.dwordCount(); i++) {
for (uint32_t consts = m_captures.psConsts.fConsts.dword(i); consts; consts &= consts - 1) {
uint32_t idx = i * 32 + bit::tzcnt(consts);
for (uint32_t consts : bit::BitMask(m_captures.psConsts.fConsts.dword(i))) {
uint32_t idx = i * 32 + consts;
dst->SetPixelShaderConstantF(idx, (float*)&src->psConsts.fConsts[idx], 1);
}
}
for (uint32_t i = 0; i < m_captures.psConsts.iConsts.dwordCount(); i++) {
for (uint32_t consts = m_captures.psConsts.iConsts.dword(i); consts; consts &= consts - 1) {
uint32_t idx = i * 32 + bit::tzcnt(consts);
for (uint32_t consts : bit::BitMask(m_captures.psConsts.iConsts.dword(i))) {
uint32_t idx = i * 32 + consts;
dst->SetPixelShaderConstantI(idx, (int*)&src->psConsts.iConsts[idx], 1);
}

View File

@ -199,11 +199,8 @@ namespace dxvk {
UINT CompactSparseList(T* pData, UINT Mask) {
uint32_t count = 0;
while (Mask != 0) {
uint32_t id = bit::tzcnt(Mask);
for (uint32_t id : bit::BitMask(Mask))
pData[count++] = pData[id];
Mask &= Mask - 1;
}
return count;
}