1
0
Fork 0
mirror of synced 2025-03-07 03:53:26 +01:00

wined3d: Use bindless textures for GLSL shaders if possible.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=44514
Signed-off-by: Andrew Wesie <awesie@gmail.com>
This commit is contained in:
Andrew Wesie 2024-03-11 21:56:53 -05:00 committed by Alexandre Julliard
parent 6e619d98a3
commit f6a1844dbe
8 changed files with 209 additions and 11 deletions

View file

@ -36132,7 +36132,7 @@ static void test_high_resource_count(void)
ID3D11DeviceContext_PSSetSamplers(context, 0, 2, samplers);
draw_quad(&test_context);
todo_wine_if (!damavand) check_texture_vec4(rt, &expect, 0);
check_texture_vec4(rt, &expect, 0);
ID3D11Texture2D_Release(rt);
ID3D11RenderTargetView_Release(rtv);

View file

@ -57,6 +57,7 @@ static const struct wined3d_extension_map gl_extension_map[] =
/* ARB */
{"GL_ARB_base_instance", ARB_BASE_INSTANCE },
{"GL_ARB_bindless_texture", ARB_BINDLESS_TEXTURE },
{"GL_ARB_blend_func_extended", ARB_BLEND_FUNC_EXTENDED },
{"GL_ARB_buffer_storage", ARB_BUFFER_STORAGE },
{"GL_ARB_clear_buffer_object", ARB_CLEAR_BUFFER_OBJECT },
@ -2114,6 +2115,12 @@ static void load_gl_funcs(struct wined3d_gl_info *gl_info)
/* GL_ARB_base_instance */
USE_GL_FUNC(glDrawArraysInstancedBaseInstance)
USE_GL_FUNC(glDrawElementsInstancedBaseVertexBaseInstance)
/* GL_ARB_bindless_texture */
USE_GL_FUNC(glGetTextureHandleARB)
USE_GL_FUNC(glGetTextureSamplerHandleARB)
USE_GL_FUNC(glIsTextureHandleResidentARB)
USE_GL_FUNC(glMakeTextureHandleResidentARB)
USE_GL_FUNC(glUniformHandleui64ARB)
/* GL_ARB_blend_func_extended */
USE_GL_FUNC(glBindFragDataLocationIndexed)
USE_GL_FUNC(glGetFragDataIndex)
@ -3607,6 +3614,14 @@ static BOOL wined3d_adapter_init_gl_caps(struct wined3d_adapter_gl *adapter_gl,
/* GL_ARB_half_float_vertex is a subset of GL_NV_half_float. */
gl_info->supported[ARB_HALF_FLOAT_VERTEX] = TRUE;
}
if (wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
{
/* ARB_bindless_texture does not let us use EXT_texture_sRGB_decode.
* We could use ARB_texture_view, but the main reason to use bindless
* textures is to avoid GL_MAX_TEXTURE_IMAGE_UNITS, so there's not much
* point. */
gl_info->supported[ARB_BINDLESS_TEXTURE] = FALSE;
}
if (gl_info->supported[ARB_FRAMEBUFFER_SRGB] && !gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
{
/* Current wined3d sRGB infrastructure requires EXT_texture_sRGB_decode

View file

@ -2616,6 +2616,37 @@ void wined3d_context_gl_bind_texture(struct wined3d_context_gl *context_gl, GLen
checkGLcall("bind texture");
}
GLuint64 wined3d_device_gl_get_dummy_bindless_handle(const struct wined3d_device_gl *device_gl,
enum wined3d_shader_resource_type type)
{
switch (type)
{
case WINED3D_SHADER_RESOURCE_BUFFER:
return device_gl->dummy_textures.bindless.tex_buffer;
case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
return device_gl->dummy_textures.bindless.tex_1d;
case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
return device_gl->dummy_textures.bindless.tex_2d;
case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
return device_gl->dummy_textures.bindless.tex_3d;
case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
return device_gl->dummy_textures.bindless.tex_cube;
case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
return device_gl->dummy_textures.bindless.tex_1d_array;
case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
return device_gl->dummy_textures.bindless.tex_2d_array;
case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
return device_gl->dummy_textures.bindless.tex_cube_array;
case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
return device_gl->dummy_textures.bindless.tex_2d_ms;
case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
return device_gl->dummy_textures.bindless.tex_2d_array;
default:
FIXME("Unhandled resource type %#x.\n", type);
return 0;
}
}
static void wined3d_context_gl_poll_fences(struct wined3d_context_gl *context_gl)
{
struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
@ -4027,6 +4058,9 @@ static void wined3d_context_gl_bind_shader_resources(struct wined3d_context_gl *
return;
}
if (gl_info->supported[ARB_BINDLESS_TEXTURE])
return;
tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl,
&shader->reg_maps.shader_version, &base, &count);
@ -4390,7 +4424,6 @@ static BOOL context_apply_draw_state(struct wined3d_context *context,
{
for (i = 0; i < WINED3D_SHADER_TYPE_GRAPHICS_COUNT; ++i)
wined3d_context_gl_bind_shader_resources(context_gl, state, i);
context->update_shader_resource_bindings = 0;
if (gl_info->limits.combined_samplers == gl_info->limits.graphics_samplers)
context->update_compute_shader_resource_bindings = 1;
}
@ -4412,6 +4445,7 @@ static BOOL context_apply_draw_state(struct wined3d_context *context,
device->shader_backend->shader_apply_draw_state(device->shader_priv, context, state);
context->shader_update_mask &= 1u << WINED3D_SHADER_TYPE_COMPUTE;
context->constant_update_mask = 0;
context->update_shader_resource_bindings = 0;
context->last_was_blit = FALSE;
context->last_was_ffp_blit = FALSE;
@ -4448,7 +4482,6 @@ static void wined3d_context_gl_apply_compute_state(struct wined3d_context_gl *co
if (context_gl->c.update_compute_shader_resource_bindings)
{
wined3d_context_gl_bind_shader_resources(context_gl, state, WINED3D_SHADER_TYPE_COMPUTE);
context_gl->c.update_compute_shader_resource_bindings = 0;
if (gl_info->limits.combined_samplers == gl_info->limits.graphics_samplers)
context_gl->c.update_shader_resource_bindings = 1;
}
@ -4475,6 +4508,7 @@ static void wined3d_context_gl_apply_compute_state(struct wined3d_context_gl *co
context_gl->c.last_was_blit = FALSE;
context_gl->c.last_was_ffp_blit = FALSE;
context_gl->c.shader_update_mask &= ~(1u << WINED3D_SHADER_TYPE_COMPUTE);
context_gl->c.update_compute_shader_resource_bindings = 0;
}
void wined3d_context_gl_end_transform_feedback(struct wined3d_context_gl *context_gl)

View file

@ -575,6 +575,18 @@ out:
if (hbm) DeleteObject(hbm);
}
static GLuint64 create_dummy_bindless_handle(const struct wined3d_gl_info *gl_info, GLuint texture)
{
GLuint64 handle;
if (!texture || !gl_info->supported[ARB_BINDLESS_TEXTURE])
return 0;
handle = GL_EXTCALL(glGetTextureHandleARB(texture));
GL_EXTCALL(glMakeTextureHandleResidentARB(handle));
return handle;
}
/* Context activation is done by the caller. */
static void wined3d_device_gl_create_dummy_textures(struct wined3d_device_gl *device_gl,
struct wined3d_context_gl *context_gl)
@ -705,6 +717,18 @@ static void wined3d_device_gl_create_dummy_textures(struct wined3d_device_gl *de
}
}
textures->bindless.tex_1d = create_dummy_bindless_handle(gl_info, textures->tex_1d);
textures->bindless.tex_2d = create_dummy_bindless_handle(gl_info, textures->tex_2d);
textures->bindless.tex_rect = create_dummy_bindless_handle(gl_info, textures->tex_rect);
textures->bindless.tex_3d = create_dummy_bindless_handle(gl_info, textures->tex_3d);
textures->bindless.tex_cube = create_dummy_bindless_handle(gl_info, textures->tex_cube);
textures->bindless.tex_cube_array = create_dummy_bindless_handle(gl_info, textures->tex_cube_array);
textures->bindless.tex_1d_array = create_dummy_bindless_handle(gl_info, textures->tex_1d_array);
textures->bindless.tex_2d_array = create_dummy_bindless_handle(gl_info, textures->tex_2d_array);
textures->bindless.tex_buffer = create_dummy_bindless_handle(gl_info, textures->tex_buffer);
textures->bindless.tex_2d_ms = create_dummy_bindless_handle(gl_info, textures->tex_2d_ms);
textures->bindless.tex_2d_ms_array = create_dummy_bindless_handle(gl_info, textures->tex_2d_ms_array);
checkGLcall("create dummy textures");
wined3d_context_gl_bind_dummy_textures(context_gl);

View file

@ -2401,7 +2401,9 @@ static void shader_generate_glsl_declarations(const struct wined3d_context_gl *c
break;
}
if (shader_glsl_use_layout_binding_qualifier(gl_info))
if (gl_info->supported[ARB_BINDLESS_TEXTURE])
shader_addline(buffer, "layout(bindless_sampler) ");
else if (shader_glsl_use_layout_binding_qualifier(gl_info))
shader_glsl_append_sampler_binding_qualifier(buffer, &context_gl->c, version, entry->bind_idx);
shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
@ -7535,6 +7537,8 @@ static void shader_glsl_generate_colour_key_test(struct wined3d_string_buffer *b
static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
const struct wined3d_gl_info *gl_info)
{
if (gl_info->supported[ARB_BINDLESS_TEXTURE])
shader_addline(buffer, "#extension GL_ARB_bindless_texture : enable\n");
if (gl_info->supported[ARB_CULL_DISTANCE])
shader_addline(buffer, "#extension GL_ARB_cull_distance : enable\n");
if (gl_info->supported[ARB_GPU_SHADER5])
@ -9666,6 +9670,7 @@ static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *
}
if (sampler_type)
{
/* We don't use bindless samplers for FFP shaders. */
if (shader_glsl_use_layout_binding_qualifier(gl_info))
shader_glsl_append_sampler_binding_qualifier(buffer, &context_gl->c, NULL, stage);
shader_addline(buffer, "uniform sampler%s ps_sampler%u;\n", sampler_type, stage);
@ -10752,6 +10757,58 @@ static void shader_glsl_update_graphics_program(struct shader_glsl_priv *priv,
context_gl->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
}
static void shader_glsl_load_bindless_samplers(struct shader_glsl_priv *priv, struct wined3d_context_gl *context_gl,
const struct wined3d_state *state, enum wined3d_shader_type shader_type)
{
struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
const struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
const struct glsl_context_data *ctx_data = context_gl->c.shader_backend_data;
const struct wined3d_shader *shader = state->shader[shader_type];
const struct wined3d_gl_info *gl_info = context_gl->gl_info;
const char *prefix = shader_glsl_get_prefix(shader_type);
/* Note that we don't use bindless samplers for FFP shaders. */
if (!shader)
return;
for (unsigned int i = 0; i < shader->reg_maps.sampler_map.count; ++i)
{
const struct wined3d_shader_sampler_map_entry *entry = &shader->reg_maps.sampler_map.entries[i];
struct wined3d_shader_resource_view *view;
struct wined3d_sampler *sampler;
GLuint64 handle;
GLint name_loc;
/* No need to bother with the texture unit map; we're binding directly to uniforms. */
string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, entry->bind_idx);
name_loc = GL_EXTCALL(glGetUniformLocation(ctx_data->glsl_program->id, sampler_name->buffer));
if (name_loc == -1)
ERR("No uniform location for shader %#x, binding %u, name %s.\n", shader_type, i, sampler_name->buffer);
if ((view = state->shader_resource_view[shader_type][entry->resource_idx]))
{
if (entry->sampler_idx == WINED3D_SAMPLER_DEFAULT)
sampler = device_gl->d.default_sampler;
else if (!(sampler = state->sampler[shader_type][entry->sampler_idx]))
sampler = device_gl->d.null_sampler;
handle = wined3d_shader_resource_view_gl_get_bindless_handle(
wined3d_shader_resource_view_gl(view), wined3d_sampler_gl(sampler), context_gl);
}
else
{
WARN("No resource view bound at index %u, %u.\n", shader_type, entry->resource_idx);
handle = wined3d_device_gl_get_dummy_bindless_handle(device_gl,
shader->reg_maps.resource_info[entry->resource_idx].type);
}
GL_EXTCALL(glUniformHandleui64ARB(name_loc, handle));
checkGLcall("glUniformHandleui64ARB");
}
string_buffer_release(&priv->string_buffers, sampler_name);
}
static void shader_glsl_apply_draw_state(void *shader_priv, struct wined3d_context *context,
const struct wined3d_state *state)
{
@ -10763,6 +10820,12 @@ static void shader_glsl_apply_draw_state(void *shader_priv, struct wined3d_conte
if (context->constant_update_mask)
shader_glsl_load_constants(priv, context, state);
if (context->update_shader_resource_bindings && context_gl->gl_info->supported[ARB_BINDLESS_TEXTURE])
{
for (unsigned int type = 0; type < WINED3D_SHADER_TYPE_GRAPHICS_COUNT; ++type)
shader_glsl_load_bindless_samplers(priv, context_gl, state, type);
}
}
static void shader_glsl_update_compute_program(struct shader_glsl_priv *priv,
@ -10799,6 +10862,9 @@ static void shader_glsl_apply_compute_state(void *shader_priv, struct wined3d_co
if (context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE))
shader_glsl_update_compute_program(priv, context_gl, state);
if (context->update_compute_shader_resource_bindings && context_gl->gl_info->supported[ARB_BINDLESS_TEXTURE])
shader_glsl_load_bindless_samplers(priv, context_gl, state, WINED3D_SHADER_TYPE_COMPUTE);
}
/* "context" is not necessarily the currently active context. */

View file

@ -1296,8 +1296,8 @@ void wined3d_gl_texture_swizzle_from_color_fixup(GLint swizzle[4], struct color_
}
/* Context activation is done by the caller. */
void wined3d_texture_gl_bind(struct wined3d_texture_gl *texture_gl,
struct wined3d_context_gl *context_gl, BOOL srgb)
GLuint wined3d_texture_gl_prepare_gl_texture(struct wined3d_texture_gl *texture_gl,
struct wined3d_context_gl *context_gl, BOOL srgb)
{
const struct wined3d_format *format = texture_gl->t.resource.format;
const struct wined3d_gl_info *gl_info = context_gl->gl_info;
@ -1320,10 +1320,7 @@ void wined3d_texture_gl_bind(struct wined3d_texture_gl *texture_gl,
target = texture_gl->target;
if (gl_tex->name)
{
wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
return;
}
return gl_tex->name;
gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
checkGLcall("glGenTextures");
@ -1332,7 +1329,7 @@ void wined3d_texture_gl_bind(struct wined3d_texture_gl *texture_gl,
if (!gl_tex->name)
{
ERR("Failed to generate a texture name.\n");
return;
return 0;
}
/* Initialise the state of the texture object to the OpenGL defaults, not
@ -1416,6 +1413,8 @@ void wined3d_texture_gl_bind(struct wined3d_texture_gl *texture_gl,
gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
checkGLcall("set format swizzle");
}
return gl_tex->name;
}
/* Context activation is done by the caller. */
@ -1445,6 +1444,13 @@ void wined3d_texture_gl_bind_and_dirtify(struct wined3d_texture_gl *texture_gl,
wined3d_texture_gl_bind(texture_gl, context_gl, srgb);
}
void wined3d_texture_gl_bind(struct wined3d_texture_gl *texture_gl,
struct wined3d_context_gl *context_gl, BOOL srgb)
{
wined3d_context_gl_bind_texture(context_gl, texture_gl->target,
wined3d_texture_gl_prepare_gl_texture(texture_gl, context_gl, srgb));
}
/* Context activation is done by the caller (state handler). */
/* This function relies on the correct texture being bound and loaded. */
void wined3d_texture_gl_apply_sampler_desc(struct wined3d_texture_gl *texture_gl,

View file

@ -1329,6 +1329,37 @@ static void shader_resource_view_gl_bind_and_dirtify(struct wined3d_shader_resou
wined3d_context_gl_bind_texture(context_gl, view_gl->gl_view.target, view_gl->gl_view.name);
}
GLuint64 wined3d_shader_resource_view_gl_get_bindless_handle(struct wined3d_shader_resource_view_gl *view_gl,
struct wined3d_sampler_gl *sampler_gl, struct wined3d_context_gl *context_gl)
{
const struct wined3d_gl_info *gl_info = context_gl->gl_info;
GLuint64 handle;
GLuint name;
if (view_gl->gl_view.name)
{
name = view_gl->gl_view.name;
}
else if (view_gl->v.resource->type == WINED3D_RTYPE_BUFFER)
{
FIXME("Buffer shader resources not supported.\n");
return 0;
}
else
{
struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(wined3d_texture_from_resource(view_gl->v.resource));
name = wined3d_texture_gl_prepare_gl_texture(texture_gl, context_gl, FALSE);
}
handle = GL_EXTCALL(glGetTextureSamplerHandleARB(name, sampler_gl->name));
checkGLcall("glGetTextureSamplerHandleARB");
/* It is an error to make a handle resident if it is already resident. */
if (!GL_EXTCALL(glIsTextureHandleResidentARB(handle)))
GL_EXTCALL(glMakeTextureHandleResidentARB(handle));
checkGLcall("glMakeTextureHandleResidentARB");
return handle;
}
void wined3d_shader_resource_view_gl_generate_mipmap(struct wined3d_shader_resource_view_gl *view_gl,
struct wined3d_context_gl *context_gl)
{

View file

@ -51,6 +51,7 @@ enum wined3d_gl_extension
APPLE_YCBCR_422,
/* ARB */
ARB_BASE_INSTANCE,
ARB_BINDLESS_TEXTURE,
ARB_BLEND_FUNC_EXTENDED,
ARB_BUFFER_STORAGE,
ARB_CLEAR_BUFFER_OBJECT,
@ -859,6 +860,21 @@ struct wined3d_dummy_textures
GLuint tex_buffer;
GLuint tex_2d_ms;
GLuint tex_2d_ms_array;
struct
{
GLuint64 tex_1d;
GLuint64 tex_2d;
GLuint64 tex_rect;
GLuint64 tex_3d;
GLuint64 tex_cube;
GLuint64 tex_cube_array;
GLuint64 tex_1d_array;
GLuint64 tex_2d_array;
GLuint64 tex_buffer;
GLuint64 tex_2d_ms;
GLuint64 tex_2d_ms_array;
} bindless;
};
struct wined3d_device_gl
@ -922,6 +938,8 @@ bool wined3d_device_gl_create_bo(struct wined3d_device_gl *device_gl,
void wined3d_device_gl_create_primary_opengl_context_cs(void *object);
void wined3d_device_gl_delete_opengl_contexts_cs(void *object);
HDC wined3d_device_gl_get_backup_dc(struct wined3d_device_gl *device_gl);
GLuint64 wined3d_device_gl_get_dummy_bindless_handle(const struct wined3d_device_gl *device_gl,
enum wined3d_shader_resource_type type);
GLbitfield wined3d_device_gl_get_memory_type_flags(unsigned int memory_type_idx);
GLbitfield wined3d_resource_gl_map_flags(const struct wined3d_bo_gl *bo, DWORD d3d_flags);
@ -1040,6 +1058,8 @@ void wined3d_texture_gl_bind_and_dirtify(struct wined3d_texture_gl *texture_gl,
HRESULT wined3d_texture_gl_init(struct wined3d_texture_gl *texture_gl, struct wined3d_device *device,
const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops);
GLuint wined3d_texture_gl_prepare_gl_texture(struct wined3d_texture_gl *texture_gl,
struct wined3d_context_gl *context_gl, BOOL srgb);
void wined3d_texture_gl_prepare_texture(struct wined3d_texture_gl *texture_gl,
struct wined3d_context_gl *context_gl, BOOL srgb);
void wined3d_texture_gl_set_compatible_renderbuffer(struct wined3d_texture_gl *texture_gl,
@ -1114,6 +1134,8 @@ void wined3d_shader_resource_view_gl_bind(struct wined3d_shader_resource_view_gl
struct wined3d_sampler_gl *sampler_gl, struct wined3d_context_gl *context_gl);
void wined3d_shader_resource_view_gl_generate_mipmap(struct wined3d_shader_resource_view_gl *srv_gl,
struct wined3d_context_gl *context_gl);
GLuint64 wined3d_shader_resource_view_gl_get_bindless_handle(struct wined3d_shader_resource_view_gl *view_gl,
struct wined3d_sampler_gl *sampler_gl, struct wined3d_context_gl *context_gl);
HRESULT wined3d_shader_resource_view_gl_init(struct wined3d_shader_resource_view_gl *view_gl,
const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
void *parent, const struct wined3d_parent_ops *parent_ops);