[host] dxgi: decouple backends from the DXGI main struct
This commit is contained in:
1 parent
eb2796d40b
commit
54bd08c3cb
6 files changed
+361
-232
No files matched your search
@@ -31,26 +31,29 @@ struct D3D11Backend
|
||||
{
|
||||
RunningAvg avgMapTime;
|
||||
uint64_t usleepMapTime;
|
||||
};
|
||||
|
||||
struct D3D11TexImpl
|
||||
{
|
||||
ID3D11Texture2D ** cpu;
|
||||
unsigned textures;
|
||||
struct
|
||||
{
|
||||
uint64_t copyTime;
|
||||
ID3D11Texture2D ** cpu;
|
||||
}
|
||||
texture[0];
|
||||
};
|
||||
|
||||
#define TEXIMPL(x) ((struct D3D11TexImpl *)(x).impl)
|
||||
|
||||
static struct DXGIInterface * dxgi = NULL;
|
||||
static struct D3D11Backend * this = NULL;
|
||||
|
||||
static void d3d11_free(void);
|
||||
|
||||
static bool d3d11_create(struct DXGIInterface * intf)
|
||||
static bool d3d11_create(unsigned textures)
|
||||
{
|
||||
dxgi = intf;
|
||||
|
||||
DEBUG_ASSERT(!this);
|
||||
this = calloc(1, sizeof(struct D3D11Backend));
|
||||
this = calloc(1,
|
||||
sizeof(struct D3D11Backend) +
|
||||
sizeof(*this->texture) + textures);
|
||||
|
||||
if (!this)
|
||||
{
|
||||
DEBUG_ERROR("failed to allocate D3D11Backend struct");
|
||||
@@ -58,11 +61,16 @@ static bool d3d11_create(struct DXGIInterface * intf)
|
||||
}
|
||||
|
||||
this->avgMapTime = runningavg_new(10);
|
||||
this->textures = textures;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool d3d11_configure(unsigned width, unsigned height, DXGI_FORMAT format,
|
||||
unsigned * pitch)
|
||||
static bool d3d11_configure(
|
||||
unsigned width,
|
||||
unsigned height,
|
||||
DXGI_FORMAT format,
|
||||
unsigned bpp,
|
||||
unsigned * pitch)
|
||||
{
|
||||
HRESULT status;
|
||||
|
||||
@@ -81,32 +89,22 @@ static bool d3d11_configure(unsigned width, unsigned height, DXGI_FORMAT format,
|
||||
.MiscFlags = 0
|
||||
};
|
||||
|
||||
for (int i = 0; i < dxgi->maxTextures; ++i)
|
||||
comRef_defineLocal(ID3D11Texture2D, cpu);
|
||||
for(int i = 0; i < this->textures; ++i)
|
||||
{
|
||||
if (!(dxgi->texture[i].impl =
|
||||
(struct D3D11TexImpl *)calloc(sizeof(struct D3D11TexImpl), 1)))
|
||||
{
|
||||
DEBUG_ERROR("Failed to allocate D3D11TexImpl struct");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
struct D3D11TexImpl * teximpl = TEXIMPL(dxgi->texture[i]);
|
||||
|
||||
status = ID3D11Device_CreateTexture2D(*dxgi->device, &cpuTexDesc, NULL,
|
||||
(ID3D11Texture2D **)comRef_newGlobal(&teximpl->cpu));
|
||||
|
||||
status = ID3D11Device_CreateTexture2D(dxgi_getDevice(), &cpuTexDesc, NULL, cpu);
|
||||
if (FAILED(status))
|
||||
{
|
||||
DEBUG_WINERROR("Failed to create CPU texture", status);
|
||||
goto fail;
|
||||
}
|
||||
comRef_toGlobal(this->texture[i].cpu, cpu);
|
||||
}
|
||||
|
||||
// map the texture simply to get the pitch and stride
|
||||
D3D11_MAPPED_SUBRESOURCE mapping;
|
||||
status = ID3D11DeviceContext_Map(*dxgi->deviceContext,
|
||||
*(ID3D11Resource **)TEXIMPL(dxgi->texture[0])->cpu, 0,
|
||||
D3D11_MAP_READ, 0, &mapping);
|
||||
status = ID3D11DeviceContext_Map(dxgi_getContext(),
|
||||
*(ID3D11Resource **)this->texture[0].cpu, 0, D3D11_MAP_READ, 0, &mapping);
|
||||
|
||||
if (FAILED(status))
|
||||
{
|
||||
@@ -114,8 +112,8 @@ static bool d3d11_configure(unsigned width, unsigned height, DXGI_FORMAT format,
|
||||
goto fail;
|
||||
}
|
||||
|
||||
ID3D11DeviceContext_Unmap(*dxgi->deviceContext,
|
||||
*(ID3D11Resource **)TEXIMPL(dxgi->texture[0])->cpu, 0);
|
||||
ID3D11DeviceContext_Unmap(dxgi_getContext(),
|
||||
*(ID3D11Resource **)this->texture[0].cpu, 0);
|
||||
|
||||
*pitch = mapping.RowPitch;
|
||||
return true;
|
||||
@@ -129,81 +127,79 @@ static void d3d11_free(void)
|
||||
if (!this)
|
||||
return;
|
||||
|
||||
for (int i = 0; i < dxgi->maxTextures; ++i)
|
||||
{
|
||||
struct D3D11TexImpl * teximpl = TEXIMPL(dxgi->texture[i]);
|
||||
free(teximpl);
|
||||
teximpl = NULL;
|
||||
}
|
||||
|
||||
runningavg_free(&this->avgMapTime);
|
||||
free(this);
|
||||
this = NULL;
|
||||
}
|
||||
|
||||
static bool d3d11_copyFrame(Texture * tex, ID3D11Texture2D * src)
|
||||
static bool d3d11_preCopy(ID3D11Texture2D * src, unsigned textureIndex)
|
||||
{
|
||||
struct D3D11TexImpl * teximpl = TEXIMPL(*tex);
|
||||
ID3D11Texture2D * dst = *teximpl->cpu;
|
||||
|
||||
INTERLOCKED_SECTION(dxgi->deviceContextLock,
|
||||
{
|
||||
tex->copyTime = microtime();
|
||||
|
||||
if (tex->texDamageCount < 0)
|
||||
ID3D11DeviceContext_CopyResource(*dxgi->deviceContext,
|
||||
(ID3D11Resource *)dst, (ID3D11Resource *)src);
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < tex->texDamageCount; ++i)
|
||||
{
|
||||
FrameDamageRect * rect = tex->texDamageRects + i;
|
||||
D3D11_BOX box =
|
||||
{
|
||||
.left = rect->x,
|
||||
.top = rect->y,
|
||||
.front = 0,
|
||||
.back = 1,
|
||||
.right = rect->x + rect->width,
|
||||
.bottom = rect->y + rect->height,
|
||||
};
|
||||
|
||||
if (dxgi->outputFormat == CAPTURE_FMT_BGR)
|
||||
{
|
||||
box.left = box.left * 3 / 4;
|
||||
box.right = box.right * 3 / 4;
|
||||
}
|
||||
|
||||
ID3D11DeviceContext_CopySubresourceRegion(*dxgi->deviceContext,
|
||||
(ID3D11Resource *)dst, 0, box.left, box.top, 0,
|
||||
(ID3D11Resource *)src, 0, &box);
|
||||
}
|
||||
}
|
||||
|
||||
ID3D11DeviceContext_Flush(*dxgi->deviceContext);
|
||||
});
|
||||
dxgi_contextLock();
|
||||
this->texture[textureIndex].copyTime = microtime();
|
||||
return true;
|
||||
}
|
||||
|
||||
static CaptureResult d3d11_mapTexture(Texture * tex)
|
||||
static bool d3d11_copyFull(ID3D11Texture2D * src, unsigned textureIndex)
|
||||
{
|
||||
struct D3D11TexImpl * teximpl = TEXIMPL(*tex);
|
||||
D3D11_MAPPED_SUBRESOURCE map;
|
||||
ID3D11Texture2D * dst = *this->texture[textureIndex].cpu;
|
||||
|
||||
ID3D11DeviceContext_CopyResource(dxgi_getContext(),
|
||||
(ID3D11Resource *)dst, (ID3D11Resource *)src);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
static bool d3d11_copyRect(ID3D11Texture2D * src, unsigned textureIndex,
|
||||
FrameDamageRect * rect)
|
||||
{
|
||||
ID3D11Texture2D * dst = *this->texture[textureIndex].cpu;
|
||||
|
||||
D3D11_BOX box =
|
||||
{
|
||||
.left = rect->x,
|
||||
.top = rect->y,
|
||||
.front = 0,
|
||||
.back = 1,
|
||||
.right = rect->x + rect->width,
|
||||
.bottom = rect->y + rect->height,
|
||||
};
|
||||
|
||||
ID3D11DeviceContext_CopySubresourceRegion(
|
||||
dxgi_getContext(),
|
||||
(ID3D11Resource *)dst, 0, box.left, box.top, 0,
|
||||
(ID3D11Resource *)src, 0, &box);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool d3d11_postCopy(ID3D11Texture2D * src, unsigned textureIndex)
|
||||
{
|
||||
ID3D11DeviceContext_Flush(dxgi_getContext());
|
||||
dxgi_contextUnlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
static CaptureResult d3d11_mapTexture(unsigned textureIndex, void ** map)
|
||||
{
|
||||
ID3D11Resource * cpu = *(ID3D11Resource **)this->texture[textureIndex].cpu;
|
||||
|
||||
// sleep until it's close to time to map
|
||||
const uint64_t delta = microtime() - tex->copyTime;
|
||||
const uint64_t delta = microtime() - this->texture[textureIndex].copyTime;
|
||||
if (delta < this->usleepMapTime)
|
||||
usleep(this->usleepMapTime - delta);
|
||||
|
||||
D3D11_MAPPED_SUBRESOURCE mappedRes;
|
||||
|
||||
// try to map the resource, but don't wait for it
|
||||
for (int i = 0; ; ++i)
|
||||
{
|
||||
HRESULT status;
|
||||
|
||||
INTERLOCKED_SECTION(dxgi->deviceContextLock, {
|
||||
status = ID3D11DeviceContext_Map(*dxgi->deviceContext,
|
||||
(ID3D11Resource *)*teximpl->cpu, 0, D3D11_MAP_READ, 0x100000L, &map);
|
||||
});
|
||||
dxgi_contextLock();
|
||||
status = ID3D11DeviceContext_Map(dxgi_getContext(), cpu, 0, D3D11_MAP_READ,
|
||||
0x100000L, &mappedRes);
|
||||
dxgi_contextUnlock();
|
||||
if (status == DXGI_ERROR_WAS_STILL_DRAWING)
|
||||
{
|
||||
if (i == 100)
|
||||
@@ -222,23 +218,23 @@ static CaptureResult d3d11_mapTexture(Texture * tex)
|
||||
break;
|
||||
}
|
||||
|
||||
tex->map = map.pData;
|
||||
*map = mappedRes.pData;
|
||||
|
||||
// update the sleep average and sleep for 80% of the average on the next call
|
||||
runningavg_push(this->avgMapTime, microtime() - tex->copyTime);
|
||||
runningavg_push(this->avgMapTime,
|
||||
microtime() - this->texture[textureIndex].copyTime);
|
||||
|
||||
this->usleepMapTime = (uint64_t)(runningavg_calc(this->avgMapTime) * 0.8);
|
||||
return CAPTURE_RESULT_OK;
|
||||
}
|
||||
|
||||
static void d3d11_unmapTexture(Texture * tex)
|
||||
static void d3d11_unmapTexture(unsigned textureIndex)
|
||||
{
|
||||
struct D3D11TexImpl * teximpl = TEXIMPL(*tex);
|
||||
ID3D11Resource * cpu = *(ID3D11Resource **)this->texture[textureIndex].cpu;
|
||||
|
||||
INTERLOCKED_SECTION(dxgi->deviceContextLock, {
|
||||
ID3D11DeviceContext_Unmap(*dxgi->deviceContext,
|
||||
(ID3D11Resource *)*teximpl->cpu, 0);
|
||||
});
|
||||
tex->map = NULL;
|
||||
dxgi_contextLock();
|
||||
ID3D11DeviceContext_Unmap(dxgi_getContext(), cpu, 0);
|
||||
dxgi_contextUnlock();
|
||||
}
|
||||
|
||||
static void d3d11_preRelease(void)
|
||||
@@ -252,7 +248,10 @@ struct DXGICopyBackend copyBackendD3D11 = {
|
||||
.create = d3d11_create,
|
||||
.configure = d3d11_configure,
|
||||
.free = d3d11_free,
|
||||
.copyFrame = d3d11_copyFrame,
|
||||
.preCopy = d3d11_preCopy,
|
||||
.copyFull = d3d11_copyFull,
|
||||
.copyRect = d3d11_copyRect,
|
||||
.postCopy = d3d11_postCopy,
|
||||
.mapTexture = d3d11_mapTexture,
|
||||
.unmapTexture = d3d11_unmapTexture,
|
||||
.preRelease = d3d11_preRelease,
|
||||
|
||||
Reference in new issue
Block a user