Path: blob/21.2-virgl/src/gallium/drivers/d3d12/d3d12_context.h
4570 views
/*1* Copyright © Microsoft Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#ifndef D3D12_CONTEXT_H24#define D3D12_CONTEXT_H2526#include "d3d12_batch.h"27#include "d3d12_descriptor_pool.h"28#include "d3d12_pipeline_state.h"29#include "d3d12_nir_lower_texcmp.h"3031#include "dxil_nir_lower_int_samplers.h"3233#include "pipe/p_context.h"34#include "pipe/p_state.h"35#include "util/list.h"36#include "util/slab.h"37#include "util/u_suballoc.h"3839#include <directx/d3d12.h>4041#define D3D12_GFX_SHADER_STAGES (PIPE_SHADER_TYPES - 1)4243enum d3d12_dirty_flags44{45D3D12_DIRTY_NONE = 0,46D3D12_DIRTY_BLEND = (1 << 0),47D3D12_DIRTY_RASTERIZER = (1 << 1),48D3D12_DIRTY_ZSA = (1 << 2),49D3D12_DIRTY_VERTEX_ELEMENTS = (1 << 3),50D3D12_DIRTY_BLEND_COLOR = (1 << 4),51D3D12_DIRTY_STENCIL_REF = (1 << 5),52D3D12_DIRTY_SAMPLE_MASK = (1 << 6),53D3D12_DIRTY_VIEWPORT = (1 << 7),54D3D12_DIRTY_FRAMEBUFFER = (1 << 8),55D3D12_DIRTY_SCISSOR = (1 << 9),56D3D12_DIRTY_VERTEX_BUFFERS = (1 << 10),57D3D12_DIRTY_INDEX_BUFFER = (1 << 11),58D3D12_DIRTY_PRIM_MODE = (1 << 12),59D3D12_DIRTY_SHADER = (1 << 13),60D3D12_DIRTY_ROOT_SIGNATURE = (1 << 14),61D3D12_DIRTY_STREAM_OUTPUT = (1 << 15),62D3D12_DIRTY_STRIP_CUT_VALUE = (1 << 16),63};6465enum d3d12_shader_dirty_flags66{67D3D12_SHADER_DIRTY_CONSTBUF = (1 << 0),68D3D12_SHADER_DIRTY_SAMPLER_VIEWS = (1 << 1),69D3D12_SHADER_DIRTY_SAMPLERS = (1 << 2),70};7172#define D3D12_DIRTY_PSO (D3D12_DIRTY_BLEND | D3D12_DIRTY_RASTERIZER | D3D12_DIRTY_ZSA | \73D3D12_DIRTY_FRAMEBUFFER | D3D12_DIRTY_SAMPLE_MASK | \74D3D12_DIRTY_VERTEX_ELEMENTS | D3D12_DIRTY_PRIM_MODE | \75D3D12_DIRTY_SHADER | D3D12_DIRTY_ROOT_SIGNATURE | \76D3D12_DIRTY_STRIP_CUT_VALUE)7778#define D3D12_SHADER_DIRTY_ALL (D3D12_SHADER_DIRTY_CONSTBUF | D3D12_SHADER_DIRTY_SAMPLER_VIEWS | \79D3D12_SHADER_DIRTY_SAMPLERS)8081enum d3d12_binding_type {82D3D12_BINDING_CONSTANT_BUFFER,83D3D12_BINDING_SHADER_RESOURCE_VIEW,84D3D12_BINDING_SAMPLER,85D3D12_BINDING_STATE_VARS,86D3D12_NUM_BINDING_TYPES87};8889struct d3d12_sampler_state {90struct d3d12_descriptor_handle handle, handle_without_shadow;91bool is_integer_texture;92bool is_shadow_sampler;93enum pipe_tex_wrap wrap_r;94enum pipe_tex_wrap wrap_s;95enum pipe_tex_wrap wrap_t;96enum pipe_tex_filter filter;97float lod_bias;98float min_lod, max_lod;99float border_color[4];100enum pipe_compare_func compare_func;101};102103enum d3d12_blend_factor_flags {104D3D12_BLEND_FACTOR_NONE = 0,105D3D12_BLEND_FACTOR_COLOR = 1 << 0,106D3D12_BLEND_FACTOR_ALPHA = 1 << 1,107D3D12_BLEND_FACTOR_ANY = 1 << 2,108};109110struct d3d12_sampler_view {111struct pipe_sampler_view base;112struct d3d12_descriptor_handle handle;113unsigned mip_levels;114unsigned array_size;115unsigned swizzle_override_r:3; /**< PIPE_SWIZZLE_x for red component */116unsigned swizzle_override_g:3; /**< PIPE_SWIZZLE_x for green component */117unsigned swizzle_override_b:3; /**< PIPE_SWIZZLE_x for blue component */118unsigned swizzle_override_a:3; /**< PIPE_SWIZZLE_x for alpha component */119};120121static inline struct d3d12_sampler_view *122d3d12_sampler_view(struct pipe_sampler_view *pview)123{124return (struct d3d12_sampler_view *)pview;125}126127struct d3d12_stream_output_target {128struct pipe_stream_output_target base;129struct pipe_resource *fill_buffer;130unsigned fill_buffer_offset;131uint64_t cached_filled_size;132};133134struct d3d12_shader_state {135struct d3d12_shader *current;136unsigned state_dirty;137};138139struct blitter_context;140struct primconvert_context;141struct d3d12_validation_tools;142143#ifdef __cplusplus144class ResourceStateManager;145#endif146147struct d3d12_context {148struct pipe_context base;149struct slab_child_pool transfer_pool;150struct primconvert_context *primconvert;151struct blitter_context *blitter;152struct u_suballocator query_allocator;153struct u_suballocator so_allocator;154struct hash_table *pso_cache;155struct hash_table *root_signature_cache;156struct hash_table *gs_variant_cache;157158struct d3d12_batch batches[4];159unsigned current_batch_idx;160161struct pipe_constant_buffer cbufs[PIPE_SHADER_TYPES][PIPE_MAX_CONSTANT_BUFFERS];162struct pipe_framebuffer_state fb;163struct pipe_vertex_buffer vbs[PIPE_MAX_ATTRIBS];164D3D12_VERTEX_BUFFER_VIEW vbvs[PIPE_MAX_ATTRIBS];165unsigned num_vbs;166float flip_y;167bool need_zero_one_depth_range;168enum pipe_prim_type initial_api_prim;169struct pipe_viewport_state viewport_states[PIPE_MAX_VIEWPORTS];170D3D12_VIEWPORT viewports[PIPE_MAX_VIEWPORTS];171unsigned num_viewports;172struct pipe_scissor_state scissor_states[PIPE_MAX_VIEWPORTS];173D3D12_RECT scissors[PIPE_MAX_VIEWPORTS];174float blend_factor[4];175struct pipe_stencil_ref stencil_ref;176struct pipe_sampler_view *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_SAMPLER_VIEWS];177unsigned num_sampler_views[PIPE_SHADER_TYPES];178unsigned has_int_samplers;179struct d3d12_sampler_state *samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];180unsigned num_samplers[PIPE_SHADER_TYPES];181D3D12_INDEX_BUFFER_VIEW ibv;182dxil_wrap_sampler_state tex_wrap_states[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_SAMPLER_VIEWS];183dxil_texture_swizzle_state tex_swizzle_state[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_SAMPLER_VIEWS];184enum compare_func tex_compare_func[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_SAMPLER_VIEWS];185186struct {187bool enabled;188uint32_t pattern[32];189struct pipe_resource *texture;190struct pipe_sampler_view *sampler_view;191struct d3d12_sampler_state *sampler_cso;192} pstipple;193194struct pipe_stream_output_target *so_targets[PIPE_MAX_SO_BUFFERS];195D3D12_STREAM_OUTPUT_BUFFER_VIEW so_buffer_views[PIPE_MAX_SO_BUFFERS];196struct pipe_stream_output_target *fake_so_targets[PIPE_MAX_SO_BUFFERS];197D3D12_STREAM_OUTPUT_BUFFER_VIEW fake_so_buffer_views[PIPE_MAX_SO_BUFFERS];198unsigned fake_so_buffer_factor;199200struct d3d12_shader_selector *gfx_stages[D3D12_GFX_SHADER_STAGES];201202struct d3d12_gfx_pipeline_state gfx_pipeline_state;203unsigned shader_dirty[D3D12_GFX_SHADER_STAGES];204unsigned state_dirty;205unsigned cmdlist_dirty;206ID3D12PipelineState *current_pso;207bool reverse_depth_range;208209ID3D12Fence *cmdqueue_fence;210uint64_t fence_value;211ID3D12GraphicsCommandList *cmdlist;212213struct list_head active_queries;214bool queries_disabled;215216struct d3d12_descriptor_pool *sampler_pool;217struct d3d12_descriptor_handle null_sampler;218219PFN_D3D12_SERIALIZE_VERSIONED_ROOT_SIGNATURE D3D12SerializeVersionedRootSignature;220struct d3d12_validation_tools *validation_tools;221222struct d3d12_resource *current_predication;223224#ifdef __cplusplus225ResourceStateManager *resource_state_manager;226#else227void *resource_state_manager; /* opaque pointer; we don't know about classes in C */228#endif229struct pipe_query *timestamp_query;230231/* used by d3d12_blit.cpp */232void *stencil_resolve_vs, *stencil_resolve_fs, *stencil_resolve_fs_no_flip, *sampler_state;233};234235static inline struct d3d12_context *236d3d12_context(struct pipe_context *context)237{238return (struct d3d12_context *)context;239}240241static inline struct d3d12_batch *242d3d12_current_batch(struct d3d12_context *ctx)243{244assert(ctx->current_batch_idx < ARRAY_SIZE(ctx->batches));245return ctx->batches + ctx->current_batch_idx;246}247248#define d3d12_foreach_submitted_batch(ctx, batch) \249unsigned oldest = (ctx->current_batch_idx + 1) % ARRAY_SIZE(ctx->batches); \250while (ctx->batches[oldest].fence == NULL && oldest != ctx->current_batch_idx) \251oldest = (oldest + 1) % ARRAY_SIZE(ctx->batches); \252struct d3d12_batch *batch = &ctx->batches[oldest]; \253for (; oldest != ctx->current_batch_idx; \254oldest = (oldest + 1) % ARRAY_SIZE(ctx->batches), \255batch = &ctx->batches[oldest])256257struct pipe_context *258d3d12_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags);259260bool261d3d12_enable_fake_so_buffers(struct d3d12_context *ctx, unsigned factor);262263bool264d3d12_disable_fake_so_buffers(struct d3d12_context *ctx);265266void267d3d12_flush_cmdlist(struct d3d12_context *ctx);268269void270d3d12_flush_cmdlist_and_wait(struct d3d12_context *ctx);271272273enum d3d12_bind_invalidate_option {274D3D12_BIND_INVALIDATE_NONE,275D3D12_BIND_INVALIDATE_FULL,276};277278void279d3d12_transition_resource_state(struct d3d12_context* ctx,280struct d3d12_resource* res,281D3D12_RESOURCE_STATES state,282d3d12_bind_invalidate_option bind_invalidate);283284void285d3d12_transition_subresources_state(struct d3d12_context *ctx,286struct d3d12_resource *res,287unsigned start_level, unsigned num_levels,288unsigned start_layer, unsigned num_layers,289unsigned start_plane, unsigned num_planes,290D3D12_RESOURCE_STATES state,291d3d12_bind_invalidate_option bind_invalidate);292293void294d3d12_apply_resource_states(struct d3d12_context* ctx);295296void297d3d12_draw_vbo(struct pipe_context *pctx,298const struct pipe_draw_info *dinfo,299unsigned drawid_offset,300const struct pipe_draw_indirect_info *indirect,301const struct pipe_draw_start_count_bias *draws,302unsigned num_draws);303304void305d3d12_blit(struct pipe_context *pctx,306const struct pipe_blit_info *info);307308void309d3d12_context_query_init(struct pipe_context *pctx);310311bool312d3d12_need_zero_one_depth_range(struct d3d12_context *ctx);313314#endif315316317