(D3D11) autogen mipmaps when needed.

This commit is contained in:
aliaspider 2018-01-25 04:57:49 +01:00
parent ccc9d36949
commit 2a54cbca75
5 changed files with 70 additions and 61 deletions

View File

@ -59,9 +59,6 @@ void d3d11_init_texture(D3D11Device device, d3d11_texture_t* texture)
Release(texture->staging);
Release(texture->view);
// .Usage = D3D11_USAGE_DYNAMIC,
// .CPUAccessFlags = D3D11_CPU_ACCESS_WRITE,
texture->desc.MipLevels = 1;
texture->desc.ArraySize = 1;
texture->desc.SampleDesc.Count = 1;
@ -69,7 +66,20 @@ void d3d11_init_texture(D3D11Device device, d3d11_texture_t* texture)
texture->desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
texture->desc.CPUAccessFlags =
texture->desc.Usage == D3D11_USAGE_DYNAMIC ? D3D11_CPU_ACCESS_WRITE : 0;
texture->desc.MiscFlags = 0;
if (texture->desc.MiscFlags & D3D11_RESOURCE_MISC_GENERATE_MIPS)
{
texture->desc.BindFlags |= D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
unsigned width = texture->desc.Width >> 5;
unsigned height = texture->desc.Height >> 5;
while (width && height)
{
width >>= 1;
height >>= 1;
texture->desc.MipLevels++;
}
}
D3D11CreateTexture2D(device, &texture->desc, NULL, &texture->handle);
{
@ -84,7 +94,9 @@ void d3d11_init_texture(D3D11Device device, d3d11_texture_t* texture)
{
D3D11_TEXTURE2D_DESC desc = texture->desc;
desc.MipLevels = 1;
desc.BindFlags = 0;
desc.MiscFlags = 0;
desc.Usage = D3D11_USAGE_STAGING;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
D3D11CreateTexture2D(device, &desc, NULL, &texture->staging);
@ -101,6 +113,7 @@ void d3d11_update_texture(
d3d11_texture_t* texture)
{
D3D11_MAPPED_SUBRESOURCE mapped_texture;
D3D11_BOX frame_box = { 0, 0, 0, width, height, 1 };
D3D11MapTexture2D(ctx, texture->staging, 0, D3D11_MAP_WRITE, 0, &mapped_texture);
@ -110,8 +123,11 @@ void d3d11_update_texture(
D3D11UnmapTexture2D(ctx, texture->staging, 0);
if (texture->desc.Usage == D3D11_USAGE_DEFAULT)
texture->dirty = true;
D3D11CopyTexture2DSubresourceRegion(
ctx, texture->handle, 0, 0, 0, 0, texture->staging, 0, &frame_box);
if (texture->desc.MiscFlags & D3D11_RESOURCE_MISC_GENERATE_MIPS)
D3D11GenerateMips(ctx, texture->view);
}
DXGI_FORMAT

View File

@ -2470,7 +2470,7 @@ typedef struct
D3D11Texture2D staging;
D3D11_TEXTURE2D_DESC desc;
D3D11ShaderResourceView view;
bool dirty;
D3D11SamplerState sampler;
} d3d11_texture_t;
typedef struct
@ -2488,7 +2488,7 @@ typedef struct
{
float scaling;
float rotation;
}params;
} params;
} d3d11_sprite_t;
typedef struct
@ -2519,20 +2519,18 @@ typedef struct
bool resize_viewport;
struct
{
d3d11_texture_t texture;
D3D11Buffer vbo;
D3D11SamplerState sampler;
bool enabled;
bool fullscreen;
d3d11_texture_t texture;
D3D11Buffer vbo;
bool enabled;
bool fullscreen;
} menu;
struct
{
d3d11_texture_t texture;
D3D11Buffer vbo;
D3D11Buffer ubo;
D3D11SamplerState sampler;
D3D11_VIEWPORT viewport;
int rotation;
d3d11_texture_t texture;
D3D11Buffer vbo;
D3D11Buffer ubo;
D3D11_VIEWPORT viewport;
int rotation;
} frame;
struct
{
@ -2567,3 +2565,10 @@ d3d11_get_closest_match_texture2D(D3D11Device device, DXGI_FORMAT desired_format
device, desired_format,
D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_SHADER_SAMPLE);
}
static inline d3d11_set_texture_and_sampler(
D3D11DeviceContext ctx, UINT slot, d3d11_texture_t* texture)
{
D3D11SetPShaderResources(ctx, slot, 1, &texture->view);
D3D11SetPShaderSamplers(ctx, slot, 1, &texture->sampler);
}

View File

@ -35,9 +35,9 @@ static void d3d11_set_filtering(void* data, unsigned index, bool smooth)
d3d11_video_t* d3d11 = (d3d11_video_t*)data;
if (smooth)
d3d11->frame.sampler = d3d11->sampler_linear;
d3d11->frame.texture.sampler = d3d11->sampler_linear;
else
d3d11->frame.sampler = d3d11->sampler_nearest;
d3d11->frame.texture.sampler = d3d11->sampler_nearest;
}
static void d3d11_gfx_set_rotation(void* data, unsigned rotation)
@ -467,9 +467,6 @@ static bool d3d11_gfx_frame(
d3d11_update_texture(
d3d11->ctx, width, height, pitch, d3d11->format, frame, &d3d11->frame.texture);
D3D11CopyTexture2DSubresourceRegion(
d3d11->ctx, d3d11->frame.texture.handle, 0, 0, 0, 0, d3d11->frame.texture.staging, 0,
&frame_box);
}
{
@ -482,8 +479,7 @@ static bool d3d11_gfx_frame(
d3d11_update_viewport(d3d11, false);
D3D11SetViewports(d3d11->ctx, 1, &d3d11->frame.viewport);
D3D11SetPShaderResources(d3d11->ctx, 0, 1, &d3d11->frame.texture.view);
D3D11SetPShaderSamplers(d3d11->ctx, 0, 1, &d3d11->frame.sampler);
d3d11_set_texture_and_sampler(d3d11->ctx, 0, &d3d11->frame.texture);
D3D11SetVertexBuffers(d3d11->ctx, 0, 1, &d3d11->frame.vbo, &stride, &offset);
D3D11SetVShaderConstantBuffers(d3d11->ctx, 0, 1, &d3d11->frame.ubo);
@ -493,20 +489,11 @@ static bool d3d11_gfx_frame(
if (d3d11->menu.enabled && d3d11->menu.texture.handle)
{
if (d3d11->menu.texture.dirty)
{
D3D11CopyTexture2DSubresourceRegion(
d3d11->ctx, d3d11->menu.texture.handle, 0, 0, 0, 0, d3d11->menu.texture.staging,
0, NULL);
d3d11->menu.texture.dirty = false;
}
if (d3d11->menu.fullscreen)
D3D11SetViewports(d3d11->ctx, 1, &d3d11->viewport);
D3D11SetVertexBuffers(d3d11->ctx, 0, 1, &d3d11->menu.vbo, &stride, &offset);
D3D11SetVShaderConstantBuffers(d3d11->ctx, 0, 1, &d3d11->ubo);
D3D11SetPShaderResources(d3d11->ctx, 0, 1, &d3d11->menu.texture.view);
D3D11SetPShaderSamplers(d3d11->ctx, 0, 1, &d3d11->menu.sampler);
d3d11_set_texture_and_sampler(d3d11->ctx, 0, &d3d11->menu.texture);
D3D11Draw(d3d11->ctx, 4, 0);
}
@ -521,7 +508,6 @@ static bool d3d11_gfx_frame(
D3D11SetInputLayout(d3d11->ctx, d3d11->sprites.layout);
D3D11SetPrimitiveTopology(d3d11->ctx, D3D11_PRIMITIVE_TOPOLOGY_POINTLIST);
D3D11SetVertexBuffers(d3d11->ctx, 0, 1, &d3d11->sprites.vbo, &sprite_stride, &offset);
D3D11SetPShaderSamplers(d3d11->ctx, 0, 1, &d3d11->sampler_linear);
D3D11SetBlendState(d3d11->ctx, d3d11->blend_enable, NULL, D3D11_DEFAULT_SAMPLE_MASK);
d3d11->sprites.enabled = true;
@ -621,7 +607,7 @@ static void d3d11_set_menu_texture_frame(
}
d3d11_update_texture(d3d11->ctx, width, height, pitch, format, frame, &d3d11->menu.texture);
d3d11->menu.sampler = config_get_ptr()->bools.menu_linear_filter ? d3d11->sampler_linear
d3d11->menu.texture.sampler = config_get_ptr()->bools.menu_linear_filter ? d3d11->sampler_linear
: d3d11->sampler_nearest;
}
static void d3d11_set_menu_texture_enable(void* data, bool state, bool full_screen)
@ -672,15 +658,30 @@ static uintptr_t d3d11_gfx_load_texture(
{
d3d11_video_t* d3d11 = (d3d11_video_t*)video_data;
struct texture_image* image = (struct texture_image*)data;
D3D11_BOX frame_box = { 0, 0, 0, image->width, image->height, 1 };
if (!d3d11)
return 0;
d3d11_texture_t* texture = calloc(1, sizeof(*texture));
texture->desc.Width = image->width;
texture->desc.Height = image->height;
switch(filter_type)
{
case TEXTURE_FILTER_MIPMAP_LINEAR:
texture->desc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;
/* fallthrough */
case TEXTURE_FILTER_LINEAR:
texture->sampler = d3d11->sampler_linear;
break;
case TEXTURE_FILTER_MIPMAP_NEAREST:
texture->desc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;
/* fallthrough */
case TEXTURE_FILTER_NEAREST:
texture->sampler = d3d11->sampler_nearest;
break;
}
texture->desc.Width = image->width;
texture->desc.Height = image->height;
texture->desc.Format =
d3d11_get_closest_match_texture2D(d3d11->device, DXGI_FORMAT_B8G8R8A8_UNORM);
@ -689,8 +690,6 @@ static uintptr_t d3d11_gfx_load_texture(
d3d11_update_texture(
d3d11->ctx, image->width, image->height, 0, DXGI_FORMAT_B8G8R8A8_UNORM, image->pixels,
texture);
D3D11CopyTexture2DSubresourceRegion(
d3d11->ctx, texture->handle, 0, 0, 0, 0, texture->staging, 0, &frame_box);
return (uintptr_t)texture;
}

View File

@ -52,6 +52,7 @@ d3d11_font_init_font(void* data, const char* font_path, float font_size, bool is
}
font->atlas = font->font_driver->get_atlas(font->font_data);
font->texture.sampler = d3d11->sampler_linear;
font->texture.desc.Width = font->atlas->width;
font->texture.desc.Height = font->atlas->height;
font->texture.desc.Format =
@ -60,8 +61,7 @@ d3d11_font_init_font(void* data, const char* font_path, float font_size, bool is
d3d11_update_texture(
d3d11->ctx, font->atlas->width, font->atlas->height, font->atlas->width,
DXGI_FORMAT_A8_UNORM, font->atlas->buffer, &font->texture);
font->texture.dirty = true;
font->atlas->dirty = false;
font->atlas->dirty = false;
return font;
}
@ -208,20 +208,11 @@ static void d3d11_font_render_line(
d3d11_update_texture(
d3d11->ctx, font->atlas->width, font->atlas->height, font->atlas->width,
DXGI_FORMAT_A8_UNORM, font->atlas->buffer, &font->texture);
font->atlas->dirty = false;
font->texture.dirty = true;
}
if (font->texture.dirty)
{
D3D11_BOX frame_box = { 0, 0, 0, font->atlas->width, font->atlas->height, 1 };
D3D11CopyTexture2DSubresourceRegion(
d3d11->ctx, font->texture.handle, 0, 0, 0, 0, font->texture.staging, 0, &frame_box);
font->texture.dirty = false;
font->atlas->dirty = false;
}
D3D11SetPShader(d3d11->ctx, d3d11->sprites.ps_8bit, NULL, 0);
D3D11SetPShaderResources(d3d11->ctx, 0, 1, &font->texture.view);
d3d11_set_texture_and_sampler(d3d11->ctx, 0, &font->texture);
D3D11Draw(d3d11->ctx, count, d3d11->sprites.offset);
D3D11SetPShader(d3d11->ctx, d3d11->sprites.ps, NULL, 0);

View File

@ -52,10 +52,9 @@ static void menu_display_d3d11_draw(void* data)
if (!d3d11->sprites.enabled)
return;
if(d3d11->sprites.offset + 1 > d3d11->sprites.capacity)
if (d3d11->sprites.offset + 1 > d3d11->sprites.capacity)
d3d11->sprites.offset = 0;
D3D11_MAPPED_SUBRESOURCE mapped_vbo;
D3D11MapBuffer(d3d11->ctx, d3d11->sprites.vbo, 0, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &mapped_vbo);
d3d11_sprite_t* v = (d3d11_sprite_t*)mapped_vbo.pData + d3d11->sprites.offset;
@ -70,7 +69,7 @@ static void menu_display_d3d11_draw(void* data)
v->coords.w = 1.0f;
v->coords.h = 1.0f;
if(draw->scale_factor)
if (draw->scale_factor)
v->params.scaling = draw->scale_factor;
else
v->params.scaling = 1.0f;
@ -93,8 +92,7 @@ static void menu_display_d3d11_draw(void* data)
#if 0
D3D11SetPShader(d3d11->ctx, d3d11->sprites.ps, NULL, 0);
#endif
D3D11SetPShaderResources(d3d11->ctx, 0, 1, &((d3d11_texture_t*)draw->texture)->view);
d3d11_set_texture_and_sampler(d3d11->ctx, 0, (d3d11_texture_t*)draw->texture);
D3D11Draw(d3d11->ctx, 1, d3d11->sprites.offset);
d3d11->sprites.offset++;