2012-09-30 05:52:03 +00:00
|
|
|
#include "quakedef.h"
|
|
|
|
#ifdef D3D11QUAKE
|
|
|
|
#include "winquake.h"
|
2015-09-06 03:30:28 +00:00
|
|
|
#include "shader.h"
|
2012-09-30 05:52:03 +00:00
|
|
|
#define COBJMACROS
|
|
|
|
#include <d3d11.h>
|
|
|
|
extern ID3D11Device *pD3DDev11;
|
|
|
|
extern ID3D11DeviceContext *d3ddevctx;
|
|
|
|
|
2014-03-30 08:55:06 +00:00
|
|
|
extern D3D_FEATURE_LEVEL d3dfeaturelevel;
|
|
|
|
#define D3D_HAVE_FULL_NPOT() (d3dfeaturelevel>=D3D_FEATURE_LEVEL_10_0)
|
|
|
|
|
|
|
|
void D3D11BE_UnbindAllTextures(void);
|
|
|
|
|
2013-11-21 23:02:28 +00:00
|
|
|
|
2014-10-05 20:04:11 +00:00
|
|
|
ID3D11ShaderResourceView *D3D11_Image_View(const texid_t id)
|
2012-09-30 05:52:03 +00:00
|
|
|
{
|
2014-10-05 20:04:11 +00:00
|
|
|
if (!id || !id->ptr)
|
2012-12-04 19:37:57 +00:00
|
|
|
return NULL;
|
2014-10-05 20:04:11 +00:00
|
|
|
if (!id->ptr2)
|
|
|
|
ID3D11Device_CreateShaderResourceView(pD3DDev11, (ID3D11Resource *)id->ptr, NULL, (ID3D11ShaderResourceView**)&id->ptr2);
|
|
|
|
return id->ptr2;
|
2012-09-30 05:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void D3D11_DestroyTexture (texid_t tex)
|
|
|
|
{
|
2014-10-05 20:04:11 +00:00
|
|
|
if (!tex)
|
2012-12-04 19:37:57 +00:00
|
|
|
return;
|
|
|
|
|
2014-10-05 20:04:11 +00:00
|
|
|
if (tex->ptr2)
|
|
|
|
ID3D11ShaderResourceView_Release((ID3D11ShaderResourceView*)tex->ptr2);
|
|
|
|
tex->ptr2 = NULL;
|
2014-03-30 08:55:06 +00:00
|
|
|
|
2014-10-05 20:04:11 +00:00
|
|
|
if (tex->ptr)
|
|
|
|
ID3D11Texture2D_Release((ID3D11Texture2D*)tex->ptr);
|
|
|
|
tex->ptr = NULL;
|
2012-09-30 05:52:03 +00:00
|
|
|
}
|
|
|
|
|
2014-10-05 20:04:11 +00:00
|
|
|
#if 0
|
2014-03-30 08:55:06 +00:00
|
|
|
static void Upload_Texture_32(ID3D11Texture2D *tex, unsigned int *data, int datawidth, int dataheight, unsigned int flags)
|
2012-09-30 05:52:03 +00:00
|
|
|
{
|
2012-12-04 19:37:57 +00:00
|
|
|
int x, y;
|
|
|
|
unsigned int *dest;
|
2012-09-30 05:52:03 +00:00
|
|
|
// unsigned char swapbuf[4];
|
|
|
|
// unsigned char swapbuf2[4];
|
2012-12-04 19:37:57 +00:00
|
|
|
D3D11_MAPPED_SUBRESOURCE lock;
|
2012-09-30 05:52:03 +00:00
|
|
|
|
|
|
|
D3D11_TEXTURE2D_DESC desc;
|
2012-11-27 03:23:19 +00:00
|
|
|
if (!tex)
|
|
|
|
return;
|
2012-09-30 05:52:03 +00:00
|
|
|
|
2012-12-04 19:37:57 +00:00
|
|
|
desc.Width = 0;
|
|
|
|
desc.Height = 0;
|
2012-09-30 05:52:03 +00:00
|
|
|
ID3D11Texture2D_GetDesc(tex, &desc);
|
2012-12-04 19:37:57 +00:00
|
|
|
#if 0
|
2012-09-30 05:52:03 +00:00
|
|
|
if (width == desc.Width && height == desc.Height)
|
|
|
|
{
|
|
|
|
ID3D11DeviceContext_UpdateSubresource(d3ddevctx, (ID3D11Resource*)tex, 0, NULL, data, width*4, width*height*4);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Con_Printf("Wrong size!\n");
|
|
|
|
return;
|
2012-12-04 19:37:57 +00:00
|
|
|
#else
|
2012-09-30 05:52:03 +00:00
|
|
|
if (FAILED(ID3D11DeviceContext_Map(d3ddevctx, (ID3D11Resource*)tex, 0, D3D11_MAP_WRITE_DISCARD, 0, &lock)))
|
|
|
|
{
|
|
|
|
Con_Printf("Dynamic texture update failed\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-30 08:55:06 +00:00
|
|
|
if (datawidth == desc.Width && dataheight == desc.Height)
|
2012-09-30 05:52:03 +00:00
|
|
|
{
|
2014-03-30 08:55:06 +00:00
|
|
|
for (y = 0; y < dataheight; y++)
|
2012-09-30 05:52:03 +00:00
|
|
|
{
|
|
|
|
dest = (unsigned int *)((char *)lock.pData + lock.RowPitch*y);
|
2014-03-30 08:55:06 +00:00
|
|
|
for (x = 0; x < datawidth; x++)
|
2012-09-30 05:52:03 +00:00
|
|
|
{
|
2012-12-04 19:37:57 +00:00
|
|
|
// *(unsigned int*)swapbuf2 = *(unsigned int*)swapbuf = data[x];
|
|
|
|
// swapbuf[0] = swapbuf2[2];
|
|
|
|
// swapbuf[2] = swapbuf2[0];
|
|
|
|
dest[x] = data[x];//*(unsigned int*)swapbuf;
|
2012-09-30 05:52:03 +00:00
|
|
|
}
|
2014-03-30 08:55:06 +00:00
|
|
|
data += datawidth;
|
2012-09-30 05:52:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
int iny;
|
|
|
|
unsigned int *row, *inrow;
|
|
|
|
|
|
|
|
for (y = 0; y < desc.Height; y++)
|
|
|
|
{
|
|
|
|
row = (unsigned int*)((char *)lock.pData + lock.RowPitch*y);
|
2014-03-30 08:55:06 +00:00
|
|
|
iny = (y * dataheight) / desc.Height;
|
|
|
|
inrow = data + datawidth*iny;
|
2012-09-30 05:52:03 +00:00
|
|
|
for (x = 0; x < desc.Width; x++)
|
|
|
|
{
|
2012-12-04 19:37:57 +00:00
|
|
|
//*(unsigned int*)swapbuf2 = *(unsigned int*)swapbuf = inrow[(x * width)/desc.Width];
|
|
|
|
//swapbuf[0] = swapbuf2[2];
|
|
|
|
//swapbuf[2] = swapbuf2[0];
|
2014-03-30 08:55:06 +00:00
|
|
|
row[x] = inrow[(x * datawidth)/desc.Width];//*(unsigned int*)swapbuf;
|
2012-09-30 05:52:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ID3D11DeviceContext_Unmap(d3ddevctx, (ID3D11Resource*)tex, 0);
|
|
|
|
#endif
|
|
|
|
}
|
2014-10-05 20:04:11 +00:00
|
|
|
#endif
|
2012-09-30 05:52:03 +00:00
|
|
|
|
2017-08-14 16:38:44 +00:00
|
|
|
qboolean D3D11_LoadTextureMips(image_t *tex, const struct pendingtextureinfo *mips)
|
2012-09-30 05:52:03 +00:00
|
|
|
{
|
2015-05-03 19:57:46 +00:00
|
|
|
int bytesperpixel = 4;
|
|
|
|
int bcbytes = 0;
|
2014-10-05 20:04:11 +00:00
|
|
|
HRESULT hr;
|
|
|
|
D3D11_TEXTURE2D_DESC tdesc = {0};
|
|
|
|
D3D11_SUBRESOURCE_DATA subresdesc[sizeof(mips->mip) / sizeof(mips->mip[0])];
|
|
|
|
int i;
|
2012-09-30 05:52:03 +00:00
|
|
|
|
2015-09-06 03:30:28 +00:00
|
|
|
if (!sh_config.texfmt[mips->encoding])
|
2015-09-06 05:50:16 +00:00
|
|
|
{
|
|
|
|
Con_Printf("Texture encoding %i not supported by d3d11\n", mips->encoding);
|
2015-09-06 03:30:28 +00:00
|
|
|
return false;
|
2015-09-06 05:50:16 +00:00
|
|
|
}
|
2015-09-06 03:30:28 +00:00
|
|
|
|
2014-10-05 20:04:11 +00:00
|
|
|
tdesc.Width = mips->mip[0].width;
|
|
|
|
tdesc.Height = mips->mip[0].height;
|
|
|
|
tdesc.ArraySize = 1;
|
|
|
|
tdesc.SampleDesc.Count = 1;
|
|
|
|
tdesc.SampleDesc.Quality = 0;
|
|
|
|
tdesc.Usage = mips->mip[0].data?D3D11_USAGE_IMMUTABLE:D3D11_USAGE_DYNAMIC;
|
|
|
|
tdesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
|
|
|
tdesc.CPUAccessFlags = (mips->mip[0].data)?0:D3D11_CPU_ACCESS_WRITE;
|
|
|
|
tdesc.MiscFlags = 0;
|
2012-09-30 05:52:03 +00:00
|
|
|
|
2014-10-14 16:42:48 +00:00
|
|
|
if (tex->flags & IF_RENDERTARGET)
|
|
|
|
{
|
|
|
|
tdesc.BindFlags |= D3D11_BIND_RENDER_TARGET;
|
|
|
|
tdesc.Usage = D3D11_USAGE_DEFAULT;
|
|
|
|
tdesc.CPUAccessFlags = 0;
|
|
|
|
}
|
|
|
|
|
2014-10-05 20:04:11 +00:00
|
|
|
if (mips->type == PTI_CUBEMAP)
|
2012-09-30 05:52:03 +00:00
|
|
|
{
|
2014-10-05 20:04:11 +00:00
|
|
|
tdesc.ArraySize *= 6;
|
|
|
|
tdesc.MiscFlags |= D3D11_RESOURCE_MISC_TEXTURECUBE;
|
2012-09-30 05:52:03 +00:00
|
|
|
}
|
2014-10-05 20:04:11 +00:00
|
|
|
else if (mips->type == PTI_3D)
|
|
|
|
return false; //nyi
|
2012-09-30 05:52:03 +00:00
|
|
|
|
2014-10-11 19:39:45 +00:00
|
|
|
//d3d11.1 formats
|
|
|
|
#define DXGI_FORMAT_B4G4R4A4_UNORM 115
|
|
|
|
|
2014-10-05 20:04:11 +00:00
|
|
|
switch(mips->encoding)
|
2012-09-30 05:52:03 +00:00
|
|
|
{
|
2014-10-11 19:39:45 +00:00
|
|
|
default:
|
|
|
|
return false;
|
2014-10-14 16:42:48 +00:00
|
|
|
case PTI_DEPTH16:
|
|
|
|
tdesc.Format = DXGI_FORMAT_D16_UNORM;
|
|
|
|
tdesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
|
2015-05-03 19:57:46 +00:00
|
|
|
bytesperpixel = 2;
|
2014-10-14 16:42:48 +00:00
|
|
|
break;
|
|
|
|
case PTI_DEPTH24:
|
|
|
|
tdesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
|
|
|
|
tdesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
|
2015-05-03 19:57:46 +00:00
|
|
|
bytesperpixel = 3;
|
2014-10-14 16:42:48 +00:00
|
|
|
break;
|
|
|
|
case PTI_DEPTH32:
|
|
|
|
tdesc.Format = DXGI_FORMAT_D32_FLOAT;
|
|
|
|
tdesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
|
2015-05-03 19:57:46 +00:00
|
|
|
bytesperpixel = 4;
|
2014-10-14 16:42:48 +00:00
|
|
|
break;
|
|
|
|
case PTI_DEPTH24_8:
|
|
|
|
tdesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
|
|
|
|
tdesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
|
2015-05-03 19:57:46 +00:00
|
|
|
bytesperpixel = 4;
|
2014-10-14 16:42:48 +00:00
|
|
|
break;
|
2014-10-11 19:39:45 +00:00
|
|
|
case PTI_RGB565:
|
|
|
|
tdesc.Format = DXGI_FORMAT_B5G6R5_UNORM;
|
2015-05-03 19:57:46 +00:00
|
|
|
bytesperpixel = 2;
|
2014-10-11 19:39:45 +00:00
|
|
|
break;
|
|
|
|
// case PTI_RGBA5551:
|
|
|
|
// tdesc.Format = DXGI_FORMAT_A1B5G5R5_UNORM;
|
2015-05-03 19:57:46 +00:00
|
|
|
// bytesperpixel = 2;
|
2014-10-11 19:39:45 +00:00
|
|
|
// break;
|
|
|
|
case PTI_ARGB1555:
|
|
|
|
tdesc.Format = DXGI_FORMAT_B5G5R5A1_UNORM;
|
2015-05-03 19:57:46 +00:00
|
|
|
bytesperpixel = 2;
|
2014-10-11 19:39:45 +00:00
|
|
|
break;
|
|
|
|
case PTI_RGBA4444:
|
|
|
|
tdesc.Format = DXGI_FORMAT_B4G4R4A4_UNORM;
|
2015-05-03 19:57:46 +00:00
|
|
|
bytesperpixel = 2;
|
2014-10-11 19:39:45 +00:00
|
|
|
break;
|
|
|
|
// case PTI_ARGB4444:
|
|
|
|
// tdesc.Format = DXGI_FORMAT_A4B4G4R4_UNORM;
|
2015-05-03 19:57:46 +00:00
|
|
|
// bytesperpixel = 2;
|
2014-10-11 19:39:45 +00:00
|
|
|
// break;
|
2014-10-05 20:04:11 +00:00
|
|
|
case PTI_RGBA8:
|
2015-09-06 03:30:28 +00:00
|
|
|
tdesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
|
|
bytesperpixel = 4;
|
|
|
|
break;
|
2014-10-11 19:39:45 +00:00
|
|
|
case PTI_RGBX8: //d3d11 has no alphaless format. be sure to proprly disable alpha in the shader.
|
2014-10-05 20:04:11 +00:00
|
|
|
tdesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
2015-05-03 19:57:46 +00:00
|
|
|
bytesperpixel = 4;
|
2012-09-30 05:52:03 +00:00
|
|
|
break;
|
2014-10-05 20:04:11 +00:00
|
|
|
case PTI_BGRA8:
|
|
|
|
tdesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
|
2015-05-03 19:57:46 +00:00
|
|
|
bytesperpixel = 4;
|
2012-09-30 05:52:03 +00:00
|
|
|
break;
|
2014-10-05 20:04:11 +00:00
|
|
|
case PTI_BGRX8:
|
|
|
|
tdesc.Format = DXGI_FORMAT_B8G8R8X8_UNORM;
|
2015-05-03 19:57:46 +00:00
|
|
|
bytesperpixel = 4;
|
2012-09-30 05:52:03 +00:00
|
|
|
break;
|
|
|
|
|
2017-07-12 08:15:27 +00:00
|
|
|
case PTI_RGBA8_SRGB:
|
|
|
|
tdesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
|
|
|
|
bytesperpixel = 4;
|
|
|
|
break;
|
|
|
|
case PTI_RGBX8_SRGB: //d3d11 has no alphaless format. be sure to proprly disable alpha in the shader.
|
|
|
|
tdesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
|
|
|
|
bytesperpixel = 4;
|
|
|
|
break;
|
|
|
|
case PTI_BGRA8_SRGB:
|
|
|
|
tdesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
|
|
|
|
bytesperpixel = 4;
|
|
|
|
break;
|
|
|
|
case PTI_BGRX8_SRGB:
|
|
|
|
tdesc.Format = DXGI_FORMAT_B8G8R8X8_UNORM_SRGB;
|
|
|
|
bytesperpixel = 4;
|
|
|
|
break;
|
|
|
|
|
2014-10-05 20:04:11 +00:00
|
|
|
case PTI_S3RGB1: //d3d11 provides no way to disable alpha with dxt1. be sure to proprly disable alpha in the shader.
|
|
|
|
case PTI_S3RGBA1:
|
|
|
|
tdesc.Format = DXGI_FORMAT_BC1_UNORM;
|
2015-05-03 19:57:46 +00:00
|
|
|
bcbytes = 8;
|
2014-10-05 20:04:11 +00:00
|
|
|
break;
|
|
|
|
case PTI_S3RGBA3:
|
|
|
|
tdesc.Format = DXGI_FORMAT_BC2_UNORM;
|
2015-05-03 19:57:46 +00:00
|
|
|
bcbytes = 16;
|
2014-10-05 20:04:11 +00:00
|
|
|
break;
|
|
|
|
case PTI_S3RGBA5:
|
|
|
|
tdesc.Format = DXGI_FORMAT_BC3_UNORM;
|
2015-05-03 19:57:46 +00:00
|
|
|
bcbytes = 16;
|
2014-10-05 20:04:11 +00:00
|
|
|
break;
|
2012-09-30 05:52:03 +00:00
|
|
|
}
|
|
|
|
|
2014-10-05 20:04:11 +00:00
|
|
|
if (!mips->mip[0].data)
|
2012-09-30 05:52:03 +00:00
|
|
|
{
|
2014-10-05 20:04:11 +00:00
|
|
|
subresdesc[0].pSysMem = NULL;
|
|
|
|
//one mip, but no data. happens with rendertargets
|
|
|
|
tdesc.MipLevels = 1;
|
2012-09-30 05:52:03 +00:00
|
|
|
}
|
2014-10-05 20:04:11 +00:00
|
|
|
else
|
2013-11-21 23:02:28 +00:00
|
|
|
{
|
2014-10-05 20:04:11 +00:00
|
|
|
for (i = 0; i < mips->mipcount; i++)
|
2013-11-21 23:02:28 +00:00
|
|
|
{
|
2014-10-05 20:04:11 +00:00
|
|
|
subresdesc[i].pSysMem = mips->mip[i].data;
|
2015-05-03 19:57:46 +00:00
|
|
|
if (bcbytes)
|
|
|
|
{
|
|
|
|
subresdesc[i].SysMemPitch = ((mips->mip[i].width+3)/4) * bcbytes;
|
|
|
|
subresdesc[i].SysMemSlicePitch = mips->mip[i].datasize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
subresdesc[i].SysMemPitch = mips->mip[i].width*bytesperpixel;
|
|
|
|
subresdesc[i].SysMemSlicePitch = mips->mip[i].datasize;//mips->mip[i].width*mips->mip[i].height*bytesperpixel;
|
|
|
|
}
|
2013-11-21 23:02:28 +00:00
|
|
|
}
|
2014-10-05 20:04:11 +00:00
|
|
|
tdesc.MipLevels = i/tdesc.ArraySize;
|
2013-11-21 23:02:28 +00:00
|
|
|
}
|
|
|
|
|
2014-10-05 20:04:11 +00:00
|
|
|
D3D11_DestroyTexture(tex);
|
|
|
|
hr = ID3D11Device_CreateTexture2D(pD3DDev11, &tdesc, (mips->mip[0].data?subresdesc:NULL), (ID3D11Texture2D**)&tex->ptr);
|
2013-11-21 23:02:28 +00:00
|
|
|
|
2014-10-05 20:04:11 +00:00
|
|
|
return !FAILED(hr);
|
2012-09-30 05:52:03 +00:00
|
|
|
}
|
2014-10-05 20:04:11 +00:00
|
|
|
void D3D11_UploadLightmap(lightmapinfo_t *lm)
|
2012-09-30 05:52:03 +00:00
|
|
|
{
|
2014-10-05 20:04:11 +00:00
|
|
|
extern cvar_t gl_lightmap_nearest;
|
|
|
|
struct pendingtextureinfo mips;
|
|
|
|
image_t *tex;
|
|
|
|
lm->modified = false;
|
|
|
|
if (!TEXVALID(lm->lightmap_texture))
|
2012-09-30 05:52:03 +00:00
|
|
|
{
|
2014-10-05 20:04:11 +00:00
|
|
|
lm->lightmap_texture = Image_CreateTexture("***lightmap***", NULL, (gl_lightmap_nearest.ival?IF_NEAREST:IF_LINEAR));
|
|
|
|
if (!lm->lightmap_texture)
|
|
|
|
return;
|
2012-09-30 05:52:03 +00:00
|
|
|
}
|
2014-10-05 20:04:11 +00:00
|
|
|
tex = lm->lightmap_texture;
|
|
|
|
|
|
|
|
mips.extrafree = NULL;
|
|
|
|
mips.type = PTI_2D;
|
|
|
|
mips.mip[0].data = lm->lightmaps;
|
|
|
|
mips.mip[0].needfree = false;
|
|
|
|
mips.mip[0].width = lm->width;
|
|
|
|
mips.mip[0].height = lm->height;
|
2016-07-21 19:27:59 +00:00
|
|
|
mips.mip[0].datasize = lm->width*lm->height*4;
|
2017-05-28 15:42:32 +00:00
|
|
|
switch (lightmap_fmt)
|
|
|
|
{
|
|
|
|
case TF_BGRA32:
|
2015-09-06 03:30:28 +00:00
|
|
|
mips.encoding = PTI_BGRX8;
|
2017-05-28 15:42:32 +00:00
|
|
|
break;
|
|
|
|
case TF_RGBA32:
|
|
|
|
mips.encoding = PTI_RGBX8;
|
|
|
|
break;
|
2017-09-20 11:27:13 +00:00
|
|
|
default:
|
|
|
|
Sys_Error("D3D11_UploadLightmap: Unsupported format");
|
|
|
|
return;
|
2017-05-28 15:42:32 +00:00
|
|
|
}
|
2014-10-05 20:04:11 +00:00
|
|
|
mips.mipcount = 1;
|
|
|
|
D3D11_LoadTextureMips(tex, &mips);
|
|
|
|
tex->width = lm->width;
|
|
|
|
tex->height = lm->height;
|
|
|
|
|
|
|
|
lm->lightmap_texture = tex;
|
2012-09-30 05:52:03 +00:00
|
|
|
}
|
|
|
|
|
2013-11-21 23:02:28 +00:00
|
|
|
#ifdef RTLIGHTS
|
|
|
|
static const int shadowfmt = 1;
|
|
|
|
static const int shadowfmts[][3] =
|
|
|
|
{
|
|
|
|
//sampler, creation, render
|
|
|
|
{DXGI_FORMAT_R24_UNORM_X8_TYPELESS, DXGI_FORMAT_R24G8_TYPELESS, DXGI_FORMAT_D24_UNORM_S8_UINT},
|
|
|
|
{DXGI_FORMAT_R16_UNORM, DXGI_FORMAT_R16_TYPELESS, DXGI_FORMAT_D16_UNORM},
|
|
|
|
{DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM}
|
|
|
|
};
|
2014-10-05 20:04:11 +00:00
|
|
|
image_t shadowmap_texture[2];
|
2013-11-21 23:02:28 +00:00
|
|
|
ID3D11DepthStencilView *shadowmap_dsview[2];
|
|
|
|
ID3D11RenderTargetView *shadowmap_rtview[2];
|
|
|
|
texid_t D3D11_GetShadowMap(int id)
|
|
|
|
{
|
2014-10-05 20:04:11 +00:00
|
|
|
texid_t tex = &shadowmap_texture[id];
|
|
|
|
if (!tex->ptr)
|
2014-03-30 08:55:06 +00:00
|
|
|
{
|
|
|
|
return r_nulltex;
|
|
|
|
}
|
2014-10-05 20:04:11 +00:00
|
|
|
if (!tex->ptr2)
|
2013-11-21 23:02:28 +00:00
|
|
|
{
|
|
|
|
D3D11_SHADER_RESOURCE_VIEW_DESC desc;
|
|
|
|
desc.Format = shadowfmts[shadowfmt][0];
|
|
|
|
desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
|
|
|
|
desc.Texture2D.MostDetailedMip = 0;
|
|
|
|
desc.Texture2D.MipLevels = -1;
|
2014-10-05 20:04:11 +00:00
|
|
|
ID3D11Device_CreateShaderResourceView(pD3DDev11, (ID3D11Resource *)tex->ptr, &desc, (ID3D11ShaderResourceView**)&tex->ptr2);
|
2013-11-21 23:02:28 +00:00
|
|
|
}
|
2014-10-05 20:04:11 +00:00
|
|
|
return tex;
|
2013-11-21 23:02:28 +00:00
|
|
|
}
|
|
|
|
void D3D11_TerminateShadowMap(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < sizeof(shadowmap_texture)/sizeof(shadowmap_texture[0]); i++)
|
|
|
|
{
|
|
|
|
if (shadowmap_dsview[i])
|
|
|
|
ID3D11DepthStencilView_Release(shadowmap_dsview[i]);
|
|
|
|
shadowmap_dsview[i] = NULL;
|
2014-10-05 20:04:11 +00:00
|
|
|
D3D11_DestroyTexture(&shadowmap_texture[i]);
|
2013-11-21 23:02:28 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-30 08:55:06 +00:00
|
|
|
qboolean D3D11_BeginShadowMap(int id, int w, int h)
|
2013-11-21 23:02:28 +00:00
|
|
|
{
|
|
|
|
D3D11_TEXTURE2D_DESC texdesc;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
if (!shadowmap_dsview[id] && !shadowmap_rtview[id])
|
|
|
|
{
|
|
|
|
memset(&texdesc, 0, sizeof(texdesc));
|
|
|
|
|
|
|
|
texdesc.Width = w;
|
|
|
|
texdesc.Height = h;
|
|
|
|
texdesc.MipLevels = 1;
|
|
|
|
texdesc.ArraySize = 1;
|
|
|
|
texdesc.Format = shadowfmts[shadowfmt][1];
|
|
|
|
texdesc.SampleDesc.Count = 1;
|
|
|
|
texdesc.SampleDesc.Quality = 0;
|
|
|
|
texdesc.Usage = D3D11_USAGE_DEFAULT;
|
|
|
|
texdesc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
|
|
|
|
texdesc.CPUAccessFlags = 0;
|
|
|
|
texdesc.MiscFlags = 0;
|
|
|
|
|
|
|
|
if (shadowfmt == 2)
|
|
|
|
texdesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
|
|
|
|
|
|
|
|
// Create the texture
|
2014-10-05 20:04:11 +00:00
|
|
|
if (!shadowmap_texture[id].ptr)
|
2014-03-30 08:55:06 +00:00
|
|
|
{
|
2014-10-05 20:04:11 +00:00
|
|
|
hr = ID3D11Device_CreateTexture2D(pD3DDev11, &texdesc, NULL, (ID3D11Texture2D **)&shadowmap_texture[id].ptr);
|
2014-03-30 08:55:06 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-21 23:02:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (shadowfmt == 2)
|
|
|
|
{
|
2014-10-05 20:04:11 +00:00
|
|
|
hr = ID3D11Device_CreateRenderTargetView(pD3DDev11, (ID3D11Resource *)shadowmap_texture[id].ptr, NULL, &shadowmap_rtview[id]);
|
2013-11-21 23:02:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
D3D11_DEPTH_STENCIL_VIEW_DESC rtdesc;
|
|
|
|
rtdesc.Format = shadowfmts[shadowfmt][2];
|
|
|
|
rtdesc.Flags = 0;
|
|
|
|
rtdesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
|
|
|
|
rtdesc.Texture2D.MipSlice = 0;
|
2014-10-05 20:04:11 +00:00
|
|
|
hr = ID3D11Device_CreateDepthStencilView(pD3DDev11, (ID3D11Resource *)shadowmap_texture[id].ptr, &rtdesc, &shadowmap_dsview[id]);
|
2013-11-21 23:02:28 +00:00
|
|
|
}
|
|
|
|
if (FAILED(hr))
|
2014-03-30 08:55:06 +00:00
|
|
|
return false;
|
2013-11-21 23:02:28 +00:00
|
|
|
}
|
2014-03-30 08:55:06 +00:00
|
|
|
D3D11BE_UnbindAllTextures();
|
2013-11-21 23:02:28 +00:00
|
|
|
if (shadowfmt == 2)
|
|
|
|
{
|
|
|
|
float colours[4] = {0, 1, 0, 0};
|
|
|
|
colours[0] = frandom();
|
|
|
|
colours[1] = frandom();
|
|
|
|
colours[2] = frandom();
|
|
|
|
ID3D11DeviceContext_OMSetRenderTargets(d3ddevctx, 1, &shadowmap_rtview[id], shadowmap_dsview[id]);
|
|
|
|
ID3D11DeviceContext_ClearRenderTargetView(d3ddevctx, shadowmap_rtview[id], colours);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ID3D11DeviceContext_OMSetRenderTargets(d3ddevctx, 0, NULL, shadowmap_dsview[id]);
|
|
|
|
ID3D11DeviceContext_ClearDepthStencilView(d3ddevctx, shadowmap_dsview[id], D3D11_CLEAR_DEPTH, 1.0f, 0);
|
|
|
|
}
|
2014-03-30 08:55:06 +00:00
|
|
|
return true;
|
2013-11-21 23:02:28 +00:00
|
|
|
}
|
|
|
|
void D3D11_EndShadowMap(void)
|
|
|
|
{
|
|
|
|
extern ID3D11RenderTargetView *fb_backbuffer;
|
|
|
|
extern ID3D11DepthStencilView *fb_backdepthstencil;
|
|
|
|
ID3D11DeviceContext_OMSetRenderTargets(d3ddevctx, 1, &fb_backbuffer, fb_backdepthstencil);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-09-30 05:52:03 +00:00
|
|
|
#endif
|