d3d11_context.h revision c463dfe4e4999a0d745c31bce9a2a2bbe6a05899
12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/************************************************************************** 22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * 32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * Copyright 2010 Luca Barbieri 42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * 52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * Permission is hereby granted, free of charge, to any person obtaining 62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * a copy of this software and associated documentation files (the 7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) * "Software"), to deal in the Software without restriction, including 82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * without limitation the rights to use, copy, modify, merge, publish, 92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * distribute, sublicense, and/or sell copies of the Software, and to 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * permit persons to whom the Software is furnished to do so, subject to 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * the following conditions: 122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * 13b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) * The above copyright notice and this permission notice (including the 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * next paragraph) shall be included in all copies or substantial 152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * portions of the Software. 162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * 172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE 212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) **************************************************************************/ 26b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 27b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)/* used to unbind things, we need 128 due to resources */ 28b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)static const void* zero_data[128]; 29b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 30b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#define UPDATE_VIEWS_SHIFT (D3D11_STAGES * 0) 31b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#define UPDATE_SAMPLERS_SHIFT (D3D11_STAGES * 1) 32b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#define UPDATE_VERTEX_BUFFERS (1 << (D3D11_STAGES * 2)) 33b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 34b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#if API >= 11 35b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)template<typename PtrTraits> 36b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)struct GalliumD3D11DeviceContext : 37b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) public GalliumD3D11DeviceChild<ID3D11DeviceContext> 38b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles){ 39b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#else 40b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)template<bool threadsafe> 41b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)struct GalliumD3D10Device : public GalliumD3D10ScreenImpl<threadsafe> 42b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles){ 43b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) typedef simple_ptr_traits PtrTraits; 44b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) typedef GalliumD3D10Device GalliumD3D10DeviceContext; 45b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#endif 46b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 47b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) refcnt_ptr<GalliumD3D11Shader<>, PtrTraits> shaders[D3D11_STAGES]; 48b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) refcnt_ptr<GalliumD3D11InputLayout, PtrTraits> input_layout; 49b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) refcnt_ptr<GalliumD3D11Buffer, PtrTraits> index_buffer; 50b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) refcnt_ptr<GalliumD3D11RasterizerState, PtrTraits> rasterizer_state; 51b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) refcnt_ptr<GalliumD3D11DepthStencilState, PtrTraits> depth_stencil_state; 52b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) refcnt_ptr<GalliumD3D11BlendState, PtrTraits> blend_state; 53b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) refcnt_ptr<GalliumD3D11DepthStencilView, PtrTraits> depth_stencil_view; 54b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) refcnt_ptr<GalliumD3D11Predicate, PtrTraits> render_predicate; 55b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 56b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) refcnt_ptr<GalliumD3D11Buffer, PtrTraits> constant_buffers[D3D11_STAGES][D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT]; 57b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) refcnt_ptr<GalliumD3D11ShaderResourceView, PtrTraits> shader_resource_views[D3D11_STAGES][D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT]; 58b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) refcnt_ptr<GalliumD3D11SamplerState, PtrTraits> samplers[D3D11_STAGES][D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT]; 59b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) refcnt_ptr<GalliumD3D11Buffer, PtrTraits> input_buffers[D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT]; 60b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) refcnt_ptr<GalliumD3D11RenderTargetView, PtrTraits> render_target_views[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT]; 61b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) refcnt_ptr<GalliumD3D11Buffer, PtrTraits> so_targets[D3D11_SO_BUFFER_SLOT_COUNT]; 62b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 63b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#if API >= 11 64b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) refcnt_ptr<ID3D11UnorderedAccessView, PtrTraits> cs_unordered_access_views[D3D11_PS_CS_UAV_REGISTER_COUNT]; 65b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) refcnt_ptr<ID3D11UnorderedAccessView, PtrTraits> om_unordered_access_views[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT]; 66b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#endif 67b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) D3D11_VIEWPORT viewports[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE]; 692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) D3D11_RECT scissor_rects[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE]; 702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned so_offsets[D3D11_SO_BUFFER_SLOT_COUNT]; 712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) D3D11_PRIMITIVE_TOPOLOGY primitive_topology; 722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DXGI_FORMAT index_format; 732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned index_offset; 742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) uint32_t strip_cut_index; 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) BOOL render_predicate_value; 762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) float blend_color[4]; 772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned sample_mask; 782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned stencil_ref; 792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool depth_clamp; 802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void* default_input_layout; 822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void* default_rasterizer; 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void* default_depth_stencil; 842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void* default_blend; 85b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) void* default_sampler; 862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void* default_shaders[D3D11_STAGES]; 872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // derived state 89b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) int primitive_mode; 902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) struct pipe_vertex_buffer vertex_buffers[D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT]; 912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) struct pipe_resource* so_buffers[D3D11_SO_BUFFER_SLOT_COUNT]; 922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) struct pipe_sampler_view* sampler_views[D3D11_STAGES][D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT]; 932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void* sampler_csos[D3D11_STAGES][D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT]; 942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) struct pipe_resource* buffers[D3D11_SO_BUFFER_SLOT_COUNT]; 952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned num_shader_resource_views[D3D11_STAGES]; 96b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) unsigned num_samplers[D3D11_STAGES]; 972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned num_vertex_buffers; 982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned num_render_target_views; 992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned num_viewports; 1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned num_scissor_rects; 1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned num_so_targets; 1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) struct pipe_context* pipe; 1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned update_flags; 1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool owns_pipe; 107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) unsigned context_flags; 1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) GalliumD3D11Caps caps; 1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) cso_context* cso_ctx; 1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) gen_mipmap_state* gen_mipmap; 1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if API >= 11 1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define SYNCHRONIZED do {} while(0) 1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) GalliumD3D11DeviceContext(GalliumD3D11Screen* device, pipe_context* pipe, bool owns_pipe, unsigned context_flags = 0) 1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) : GalliumD3D11DeviceChild<ID3D11DeviceContext>(device), pipe(pipe), owns_pipe(owns_pipe), context_flags(context_flags) 1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) { 1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) caps = device->screen_caps; 1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) init_context(); 1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ~GalliumD3D11DeviceContext() 1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) { 1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) destroy_context(); 127c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#else 1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define SYNCHRONIZED lock_t<maybe_mutex_t<threadsafe> > lock_(this->mutex) 1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) GalliumD3D10Device(pipe_screen* screen, pipe_context* pipe, bool owns_pipe, unsigned creation_flags, IDXGIAdapter* adapter) 1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) : GalliumD3D10ScreenImpl<threadsafe>(screen, pipe, owns_pipe, creation_flags, adapter), pipe(pipe), owns_pipe(owns_pipe), context_flags(0) 1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) { 1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) caps = this->screen_caps; 1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) init_context(); 1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ~GalliumD3D10Device() 1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) { 1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) destroy_context(); 1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif 1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void init_context() 1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) { 1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if(!pipe->begin_query) 1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) caps.queries = false; 1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if(!pipe->bind_gs_state) 149b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) { 150b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) caps.gs = false; 151b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) caps.stages = 2; 152b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) } 1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if(!pipe->set_stream_output_buffers) 1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) caps.so = false; 1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if(!pipe->set_geometry_sampler_views) 1567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) caps.stages_with_sampling &=~ (1 << PIPE_SHADER_GEOMETRY); 1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if(!pipe->set_fragment_sampler_views) 1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) caps.stages_with_sampling &=~ (1 << PIPE_SHADER_FRAGMENT); 1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if(!pipe->set_vertex_sampler_views) 160b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) caps.stages_with_sampling &=~ (1 << PIPE_SHADER_VERTEX); 1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) update_flags = 0; 1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // pipeline state 165868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) memset(viewports, 0, sizeof(viewports)); 1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) memset(scissor_rects, 0, sizeof(scissor_rects)); 1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) memset(so_offsets, 0, sizeof(so_offsets)); 1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) primitive_topology = D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED; 1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) index_format = DXGI_FORMAT_UNKNOWN; 1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) index_offset = 0; 171b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) strip_cut_index = 0xffffffff; 1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) render_predicate_value = 0; 1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) memset(blend_color, 0, sizeof(blend_color)); 1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) sample_mask = ~0; 1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) stencil_ref = 0; 1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) depth_clamp = 0; 177868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 178868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // derived state 179868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) primitive_mode = 0; 1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) memset(vertex_buffers, 0, sizeof(vertex_buffers)); 1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) memset(so_buffers, 0, sizeof(so_buffers)); 1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) memset(sampler_views, 0, sizeof(sampler_views)); 183b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) memset(sampler_csos, 0, sizeof(sampler_csos)); 184b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) memset(num_shader_resource_views, 0, sizeof(num_shader_resource_views)); 185b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) memset(num_samplers, 0, sizeof(num_samplers)); 1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) num_vertex_buffers = 0; 1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) num_render_target_views = 0; 1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) num_viewports = 0; 189b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) num_scissor_rects = 0; 190b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) num_so_targets = 0; 191b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 192b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) default_input_layout = pipe->create_vertex_elements_state(pipe, 0, 0); 193b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 194b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) struct pipe_rasterizer_state rasterizerd; 195b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) memset(&rasterizerd, 0, sizeof(rasterizerd)); 196b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) rasterizerd.gl_rasterization_rules = 1; 197b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) rasterizerd.cull_face = PIPE_FACE_BACK; 198b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) rasterizerd.flatshade_first = 1; 199b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) rasterizerd.line_width = 1.0f; 200b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) rasterizerd.point_size = 1.0f; 201b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) default_rasterizer = pipe->create_rasterizer_state(pipe, &rasterizerd); 202b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 203b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) struct pipe_depth_stencil_alpha_state depth_stencild; 204b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) memset(&depth_stencild, 0, sizeof(depth_stencild)); 205b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) depth_stencild.depth.enabled = TRUE; 206b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) depth_stencild.depth.writemask = 1; 207b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) depth_stencild.depth.func = PIPE_FUNC_LESS; 208b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) default_depth_stencil = pipe->create_depth_stencil_alpha_state(pipe, &depth_stencild); 209b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 210b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) struct pipe_blend_state blendd; 211b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) memset(&blendd, 0, sizeof(blendd)); 212 blendd.rt[0].colormask = 0xf; 213 default_blend = pipe->create_blend_state(pipe, &blendd); 214 215 struct pipe_sampler_state samplerd; 216 memset(&samplerd, 0, sizeof(samplerd)); 217 samplerd.normalized_coords = 1; 218 samplerd.min_img_filter = PIPE_TEX_FILTER_LINEAR; 219 samplerd.mag_img_filter = PIPE_TEX_FILTER_LINEAR; 220 samplerd.min_mip_filter = PIPE_TEX_MIPFILTER_LINEAR; 221 samplerd.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 222 samplerd.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 223 samplerd.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 224 samplerd.border_color.f[0] = 1.0f; 225 samplerd.border_color.f[1] = 1.0f; 226 samplerd.border_color.f[2] = 1.0f; 227 samplerd.border_color.f[3] = 1.0f; 228 samplerd.min_lod = -FLT_MAX; 229 samplerd.max_lod = FLT_MAX; 230 samplerd.max_anisotropy = 1; 231 default_sampler = pipe->create_sampler_state(pipe, &samplerd); 232 233 memset(&samplerd, 0, sizeof(samplerd)); 234 samplerd.normalized_coords = 0; 235 samplerd.min_img_filter = PIPE_TEX_FILTER_NEAREST; 236 samplerd.mag_img_filter = PIPE_TEX_FILTER_NEAREST; 237 samplerd.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 238 samplerd.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_BORDER; 239 samplerd.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_BORDER; 240 samplerd.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_BORDER; 241 samplerd.min_lod = -FLT_MAX; 242 samplerd.max_lod = FLT_MAX; 243 samplerd.max_anisotropy = 1; 244 245 for(unsigned s = 0; s < D3D11_STAGES; ++s) 246 for(unsigned i = 0; i < D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT; ++i) 247 sampler_csos[s][i] = default_sampler; 248 249 // TODO: should this really be empty shaders, or should they be all-passthrough? 250 memset(default_shaders, 0, sizeof(default_shaders)); 251 struct ureg_program *ureg; 252 ureg = ureg_create(TGSI_PROCESSOR_FRAGMENT); 253 ureg_END(ureg); 254 default_shaders[PIPE_SHADER_FRAGMENT] = ureg_create_shader_and_destroy(ureg, pipe); 255 256 ureg = ureg_create(TGSI_PROCESSOR_VERTEX); 257 ureg_END(ureg); 258 default_shaders[PIPE_SHADER_VERTEX] = ureg_create_shader_and_destroy(ureg, pipe); 259 260 cso_ctx = cso_create_context(pipe); 261 gen_mipmap = util_create_gen_mipmap(pipe, cso_ctx); 262 263 RestoreGalliumState(); 264 } 265 266 void destroy_context() 267 { 268 util_destroy_gen_mipmap(gen_mipmap); 269 cso_destroy_context(cso_ctx); 270 271 pipe->bind_vertex_elements_state(pipe, 0); 272 pipe->delete_vertex_elements_state(pipe, default_input_layout); 273 274 pipe->bind_rasterizer_state(pipe, 0); 275 pipe->delete_rasterizer_state(pipe, default_rasterizer); 276 277 pipe->bind_depth_stencil_alpha_state(pipe, 0); 278 pipe->delete_depth_stencil_alpha_state(pipe, default_depth_stencil); 279 280 pipe->bind_blend_state(pipe, 0); 281 pipe->delete_blend_state(pipe, default_blend); 282 283 pipe->bind_fragment_sampler_states(pipe, 0, 0); 284 pipe->bind_vertex_sampler_states(pipe, 0, 0); 285 if(pipe->bind_geometry_sampler_states) 286 pipe->bind_geometry_sampler_states(pipe, 0, 0); 287 pipe->delete_sampler_state(pipe, default_sampler); 288 289 pipe->bind_fs_state(pipe, 0); 290 pipe->delete_fs_state(pipe, default_shaders[PIPE_SHADER_FRAGMENT]); 291 292 pipe->bind_vs_state(pipe, 0); 293 pipe->delete_vs_state(pipe, default_shaders[PIPE_SHADER_VERTEX]); 294 295 if(owns_pipe) 296 pipe->destroy(pipe); 297 } 298 299 virtual unsigned STDMETHODCALLTYPE GetContextFlags(void) 300 { 301 return context_flags; 302 } 303#if API >= 11 304#define SET_SHADER_EXTRA_ARGS , \ 305 ID3D11ClassInstance *const *ppClassInstances, \ 306 unsigned count 307#define GET_SHADER_EXTRA_ARGS , \ 308 ID3D11ClassInstance **ppClassInstances, \ 309 unsigned *out_count 310#else 311#define SET_SHADER_EXTRA_ARGS 312#define GET_SHADER_EXTRA_ARGS 313#endif 314 315/* On Windows D3D11, SetConstantBuffers and SetShaderResources crash if passed a null pointer. 316 * Instead, you have to pass a pointer to nulls to unbind things. 317 * We do the same. 318 * TODO: is D3D10 the same? 319 */ 320 template<unsigned s> 321 void xs_set_shader(GalliumD3D11Shader<>* shader) 322 { 323 if(shader != shaders[s].p) 324 { 325 shaders[s] = shader; 326 void* shader_cso = shader ? shader->object : default_shaders[s]; 327 switch(s) 328 { 329 case PIPE_SHADER_VERTEX: 330 pipe->bind_vs_state(pipe, shader_cso); 331 break; 332 case PIPE_SHADER_FRAGMENT: 333 pipe->bind_fs_state(pipe, shader_cso); 334 break; 335 case PIPE_SHADER_GEOMETRY: 336 pipe->bind_gs_state(pipe, shader_cso); 337 break; 338 } 339 update_flags |= (1 << (UPDATE_SAMPLERS_SHIFT + s)) | (1 << (UPDATE_VIEWS_SHIFT + s)); 340 } 341 } 342 343 template<unsigned s> 344 void xs_set_constant_buffers(unsigned start, unsigned count, GalliumD3D11Buffer *const *constbufs) 345 { 346 for(unsigned i = 0; i < count; ++i) 347 { 348 if(constbufs[i] != constant_buffers[s][start + i].p) 349 { 350 constant_buffers[s][start + i] = constbufs[i]; 351 if(s < caps.stages && start + i < caps.constant_buffers[s]) 352 pipe->set_constant_buffer(pipe, s, start + i, constbufs[i] ? constbufs[i]->resource : NULL); 353 } 354 } 355 } 356 357 template<unsigned s> 358 void xs_set_shader_resources(unsigned start, unsigned count, GalliumD3D11ShaderResourceView *const *srvs) 359 { 360 int last_different = -1; 361 for(unsigned i = 0; i < count; ++i) 362 { 363 if(shader_resource_views[s][start + i].p != srvs[i]) 364 { 365 shader_resource_views[s][start + i] = srvs[i]; 366 sampler_views[s][start + i] = srvs[i] ? srvs[i]->object : 0; 367 last_different = i; 368 } 369 } 370 if(last_different >= 0) 371 { 372 num_shader_resource_views[s] = std::max(num_shader_resource_views[s], start + last_different + 1); 373 update_flags |= 1 << (UPDATE_VIEWS_SHIFT + s); 374 } 375 } 376 377 template<unsigned s> 378 void xs_set_samplers(unsigned start, unsigned count, GalliumD3D11SamplerState *const *samps) 379 { 380 int last_different = -1; 381 for(unsigned i = 0; i < count; ++i) 382 { 383 if(samplers[s][start + i].p != samps[i]) 384 { 385 samplers[s][start + i] = samps[i]; 386 sampler_csos[s][start + i] = samps[i] ? samps[i]->object : default_sampler; 387 last_different = i; 388 } 389 } 390 if(last_different >= 0) 391 { 392 num_samplers[s] = std::max(num_samplers[s], start + last_different + 1); 393 update_flags |= 1 << (UPDATE_SAMPLERS_SHIFT + s); 394 } 395 } 396 397#define IMPLEMENT_SHADER_STAGE(XS, Stage) \ 398 virtual void STDMETHODCALLTYPE XS##SetShader( \ 399 ID3D11##Stage##Shader *pShader \ 400 SET_SHADER_EXTRA_ARGS) \ 401 { \ 402 SYNCHRONIZED; \ 403 xs_set_shader<D3D11_STAGE_##XS>((GalliumD3D11Shader<>*)pShader); \ 404 } \ 405 virtual void STDMETHODCALLTYPE XS##GetShader(\ 406 ID3D11##Stage##Shader **ppShader \ 407 GET_SHADER_EXTRA_ARGS) \ 408 { \ 409 SYNCHRONIZED; \ 410 *ppShader = (ID3D11##Stage##Shader*)shaders[D3D11_STAGE_##XS].ref(); \ 411 } \ 412 virtual void STDMETHODCALLTYPE XS##SetConstantBuffers(\ 413 unsigned start, \ 414 unsigned count, \ 415 ID3D11Buffer *const* constant_buffers) \ 416 { \ 417 SYNCHRONIZED; \ 418 xs_set_constant_buffers<D3D11_STAGE_##XS>(start, count, (GalliumD3D11Buffer *const *)constant_buffers); \ 419 } \ 420 virtual void STDMETHODCALLTYPE XS##GetConstantBuffers(\ 421 unsigned start, \ 422 unsigned count, \ 423 ID3D11Buffer **out_constant_buffers) \ 424 { \ 425 SYNCHRONIZED; \ 426 for(unsigned i = 0; i < count; ++i) \ 427 out_constant_buffers[i] = constant_buffers[D3D11_STAGE_##XS][start + i].ref(); \ 428 } \ 429 virtual void STDMETHODCALLTYPE XS##SetShaderResources(\ 430 unsigned start, \ 431 unsigned count, \ 432 ID3D11ShaderResourceView *const *new_shader_resource_views) \ 433 { \ 434 SYNCHRONIZED; \ 435 xs_set_shader_resources<D3D11_STAGE_##XS>(start, count, (GalliumD3D11ShaderResourceView *const *)new_shader_resource_views); \ 436 } \ 437 virtual void STDMETHODCALLTYPE XS##GetShaderResources(\ 438 unsigned start, \ 439 unsigned count, \ 440 ID3D11ShaderResourceView **out_shader_resource_views) \ 441 { \ 442 SYNCHRONIZED; \ 443 for(unsigned i = 0; i < count; ++i) \ 444 out_shader_resource_views[i] = shader_resource_views[D3D11_STAGE_##XS][start + i].ref(); \ 445 } \ 446 virtual void STDMETHODCALLTYPE XS##SetSamplers(\ 447 unsigned start, \ 448 unsigned count, \ 449 ID3D11SamplerState *const *new_samplers) \ 450 { \ 451 SYNCHRONIZED; \ 452 xs_set_samplers<D3D11_STAGE_##XS>(start, count, (GalliumD3D11SamplerState *const *)new_samplers); \ 453 } \ 454 virtual void STDMETHODCALLTYPE XS##GetSamplers( \ 455 unsigned start, \ 456 unsigned count, \ 457 ID3D11SamplerState **out_samplers) \ 458 { \ 459 SYNCHRONIZED; \ 460 for(unsigned i = 0; i < count; ++i) \ 461 out_samplers[i] = samplers[D3D11_STAGE_##XS][start + i].ref(); \ 462 } 463 464#define DO_VS(x) x 465#define DO_GS(x) do {if(caps.gs) {x;}} while(0) 466#define DO_PS(x) x 467#define DO_HS(x) 468#define DO_DS(x) 469#define DO_CS(x) 470 IMPLEMENT_SHADER_STAGE(VS, Vertex) 471 IMPLEMENT_SHADER_STAGE(GS, Geometry) 472 IMPLEMENT_SHADER_STAGE(PS, Pixel) 473 474#if API >= 11 475 IMPLEMENT_SHADER_STAGE(HS, Hull) 476 IMPLEMENT_SHADER_STAGE(DS, Domain) 477 IMPLEMENT_SHADER_STAGE(CS, Compute) 478 479 virtual void STDMETHODCALLTYPE CSSetUnorderedAccessViews( 480 unsigned start, 481 unsigned count, 482 ID3D11UnorderedAccessView *const *new_unordered_access_views, 483 const unsigned *new_uav_initial_counts) 484 { 485 SYNCHRONIZED; 486 for(unsigned i = 0; i < count; ++i) 487 cs_unordered_access_views[start + i] = new_unordered_access_views[i]; 488 } 489 490 virtual void STDMETHODCALLTYPE CSGetUnorderedAccessViews( 491 unsigned start, 492 unsigned count, 493 ID3D11UnorderedAccessView **out_unordered_access_views) 494 { 495 SYNCHRONIZED; 496 for(unsigned i = 0; i < count; ++i) 497 out_unordered_access_views[i] = cs_unordered_access_views[start + i].ref(); 498 } 499#endif 500 501 template<unsigned s> 502 void update_stage() 503 { 504 if(update_flags & (1 << (UPDATE_VIEWS_SHIFT + s))) 505 { 506 while(num_shader_resource_views[s] && !sampler_views[s][num_shader_resource_views[s] - 1]) \ 507 --num_shader_resource_views[s]; 508 if((1 << s) & caps.stages_with_sampling) 509 { 510 const unsigned num_views_to_bind = num_shader_resource_views[s]; 511 switch(s) 512 { 513 case PIPE_SHADER_VERTEX: 514 pipe->set_vertex_sampler_views(pipe, num_views_to_bind, sampler_views[s]); 515 break; 516 case PIPE_SHADER_FRAGMENT: 517 pipe->set_fragment_sampler_views(pipe, num_views_to_bind, sampler_views[s]); 518 break; 519 case PIPE_SHADER_GEOMETRY: 520 pipe->set_geometry_sampler_views(pipe, num_views_to_bind, sampler_views[s]); 521 break; 522 } 523 } 524 } 525 526 if(update_flags & (1 << (UPDATE_SAMPLERS_SHIFT + s))) 527 { 528 while(num_samplers[s] && !sampler_csos[s][num_samplers[s] - 1]) 529 --num_samplers[s]; 530 if((1 << s) & caps.stages_with_sampling) 531 { 532 const unsigned num_samplers_to_bind = num_samplers[s]; 533 switch(s) 534 { 535 case PIPE_SHADER_VERTEX: 536 pipe->bind_vertex_sampler_states(pipe, num_samplers_to_bind, sampler_csos[s]); 537 break; 538 case PIPE_SHADER_FRAGMENT: 539 pipe->bind_fragment_sampler_states(pipe, num_samplers_to_bind, sampler_csos[s]); 540 break; 541 case PIPE_SHADER_GEOMETRY: 542 pipe->bind_geometry_sampler_states(pipe, num_samplers_to_bind, sampler_csos[s]); 543 break; 544 } 545 } 546 } 547 } 548 549 void update_state() 550 { 551 update_stage<D3D11_STAGE_PS>(); 552 update_stage<D3D11_STAGE_VS>(); 553 update_stage<D3D11_STAGE_GS>(); 554#if API >= 11 555 update_stage<D3D11_STAGE_HS>(); 556 update_stage<D3D11_STAGE_DS>(); 557 update_stage<D3D11_STAGE_CS>(); 558#endif 559 560 if(update_flags & UPDATE_VERTEX_BUFFERS) 561 { 562 while(num_vertex_buffers && !vertex_buffers[num_vertex_buffers - 1].buffer) 563 --num_vertex_buffers; 564 pipe->set_vertex_buffers(pipe, num_vertex_buffers, vertex_buffers); 565 } 566 567 update_flags = 0; 568 } 569 570 virtual void STDMETHODCALLTYPE IASetInputLayout( 571 ID3D11InputLayout *new_input_layout) 572 { 573 SYNCHRONIZED; 574 if(new_input_layout != input_layout.p) 575 { 576 input_layout = new_input_layout; 577 pipe->bind_vertex_elements_state(pipe, new_input_layout ? ((GalliumD3D11InputLayout*)new_input_layout)->object : default_input_layout); 578 } 579 } 580 581 virtual void STDMETHODCALLTYPE IAGetInputLayout( 582 ID3D11InputLayout **out_input_layout) 583 { 584 SYNCHRONIZED; 585 *out_input_layout = input_layout.ref(); 586 } 587 588 virtual void STDMETHODCALLTYPE IASetVertexBuffers( 589 unsigned start, 590 unsigned count, 591 ID3D11Buffer *const *new_vertex_buffers, 592 const unsigned *new_strides, 593 const unsigned *new_offsets) 594 { 595 SYNCHRONIZED; 596 int last_different = -1; 597 for(unsigned i = 0; i < count; ++i) 598 { 599 ID3D11Buffer* buffer = new_vertex_buffers[i]; 600 if(buffer != input_buffers[start + i].p 601 || vertex_buffers[start + i].buffer_offset != new_offsets[i] 602 || vertex_buffers[start + i].stride != new_offsets[i] 603 ) 604 { 605 input_buffers[start + i] = buffer; 606 vertex_buffers[start + i].buffer = buffer ? ((GalliumD3D11Buffer*)buffer)->resource : 0; 607 vertex_buffers[start + i].buffer_offset = new_offsets[i]; 608 vertex_buffers[start + i].stride = new_strides[i]; 609 last_different = i; 610 } 611 } 612 if(last_different >= 0) 613 { 614 num_vertex_buffers = std::max(num_vertex_buffers, start + count); 615 update_flags |= UPDATE_VERTEX_BUFFERS; 616 } 617 } 618 619 virtual void STDMETHODCALLTYPE IAGetVertexBuffers( 620 unsigned start, 621 unsigned count, 622 ID3D11Buffer **out_vertex_buffers, 623 unsigned *out_strides, 624 unsigned *out_offsets) 625 { 626 SYNCHRONIZED; 627 if(out_vertex_buffers) 628 { 629 for(unsigned i = 0; i < count; ++i) 630 out_vertex_buffers[i] = input_buffers[start + i].ref(); 631 } 632 633 if(out_offsets) 634 { 635 for(unsigned i = 0; i < count; ++i) 636 out_offsets[i] = vertex_buffers[start + i].buffer_offset; 637 } 638 639 if(out_strides) 640 { 641 for(unsigned i = 0; i < count; ++i) 642 out_strides[i] = vertex_buffers[start + i].stride; 643 } 644 } 645 646 void set_index_buffer() 647 { 648 pipe_index_buffer ib; 649 if(!index_buffer) 650 { 651 memset(&ib, 0, sizeof(ib)); 652 } 653 else 654 { 655 switch(index_format) { 656 case DXGI_FORMAT_R32_UINT: 657 ib.index_size = 4; 658 strip_cut_index = 0xffffffff; 659 break; 660 case DXGI_FORMAT_R16_UINT: 661 ib.index_size = 2; 662 strip_cut_index = 0xffff; 663 break; 664 default: 665 ib.index_size = 1; 666 strip_cut_index = 0xff; 667 break; 668 } 669 ib.offset = index_offset; 670 ib.buffer = index_buffer ? ((GalliumD3D11Buffer*)index_buffer.p)->resource : 0; 671 } 672 pipe->set_index_buffer(pipe, &ib); 673 } 674 675 virtual void STDMETHODCALLTYPE IASetIndexBuffer( 676 ID3D11Buffer *new_index_buffer, 677 DXGI_FORMAT new_index_format, 678 unsigned new_index_offset) 679 { 680 SYNCHRONIZED; 681 if(index_buffer.p != new_index_buffer || index_format != new_index_format || index_offset != new_index_offset) 682 { 683 index_buffer = new_index_buffer; 684 index_format = new_index_format; 685 index_offset = new_index_offset; 686 687 set_index_buffer(); 688 } 689 } 690 691 virtual void STDMETHODCALLTYPE IAGetIndexBuffer( 692 ID3D11Buffer **out_index_buffer, 693 DXGI_FORMAT *out_index_format, 694 unsigned *out_index_offset) 695 { 696 SYNCHRONIZED; 697 if(out_index_buffer) 698 *out_index_buffer = index_buffer.ref(); 699 if(out_index_format) 700 *out_index_format = index_format; 701 if(out_index_offset) 702 *out_index_offset = index_offset; 703 } 704 705 virtual void STDMETHODCALLTYPE IASetPrimitiveTopology( 706 D3D11_PRIMITIVE_TOPOLOGY new_primitive_topology) 707 { 708 SYNCHRONIZED; 709 if(primitive_topology != new_primitive_topology) 710 { 711 if(new_primitive_topology < D3D_PRIMITIVE_TOPOLOGY_COUNT) 712 primitive_mode = d3d_to_pipe_prim[new_primitive_topology]; 713 else 714 primitive_mode = 0; 715 primitive_topology = new_primitive_topology; 716 } 717 } 718 719 virtual void STDMETHODCALLTYPE IAGetPrimitiveTopology( 720 D3D11_PRIMITIVE_TOPOLOGY *out_primitive_topology) 721 { 722 SYNCHRONIZED; 723 *out_primitive_topology = primitive_topology; 724 } 725 726 virtual void STDMETHODCALLTYPE DrawIndexed( 727 unsigned index_count, 728 unsigned start_index_location, 729 int base_vertex_location) 730 { 731 SYNCHRONIZED; 732 if(update_flags) 733 update_state(); 734 735 pipe_draw_info info; 736 info.mode = primitive_mode; 737 info.indexed = TRUE; 738 info.count = index_count; 739 info.start = start_index_location; 740 info.index_bias = base_vertex_location; 741 info.min_index = 0; 742 info.max_index = ~0; 743 info.start_instance = 0; 744 info.instance_count = 1; 745 info.primitive_restart = FALSE; 746 747 pipe->draw_vbo(pipe, &info); 748 } 749 750 virtual void STDMETHODCALLTYPE Draw( 751 unsigned vertex_count, 752 unsigned start_vertex_location) 753 { 754 SYNCHRONIZED; 755 if(update_flags) 756 update_state(); 757 758 pipe_draw_info info; 759 info.mode = primitive_mode; 760 info.indexed = FALSE; 761 info.count = vertex_count; 762 info.start = start_vertex_location; 763 info.index_bias = 0; 764 info.min_index = 0; 765 info.max_index = ~0; 766 info.start_instance = 0; 767 info.instance_count = 1; 768 info.primitive_restart = TRUE; 769 info.restart_index = strip_cut_index; 770 771 pipe->draw_vbo(pipe, &info); 772 } 773 774 virtual void STDMETHODCALLTYPE DrawIndexedInstanced( 775 unsigned index_countPerInstance, 776 unsigned instance_count, 777 unsigned start_index_location, 778 int base_vertex_location, 779 unsigned start_instance_location) 780 { 781 SYNCHRONIZED; 782 if(update_flags) 783 update_state(); 784 785 pipe_draw_info info; 786 info.mode = primitive_mode; 787 info.indexed = TRUE; 788 info.count = index_countPerInstance; 789 info.start = start_index_location; 790 info.index_bias = base_vertex_location; 791 info.min_index = 0; 792 info.max_index = ~0; 793 info.start_instance = start_instance_location; 794 info.instance_count = instance_count; 795 info.primitive_restart = FALSE; 796 797 pipe->draw_vbo(pipe, &info); 798 } 799 800 virtual void STDMETHODCALLTYPE DrawInstanced( 801 unsigned vertex_countPerInstance, 802 unsigned instance_count, 803 unsigned start_vertex_location, 804 unsigned start_instance_location) 805 { 806 SYNCHRONIZED; 807 if(update_flags) 808 update_state(); 809 810 pipe_draw_info info; 811 info.mode = primitive_mode; 812 info.indexed = FALSE; 813 info.count = vertex_countPerInstance; 814 info.start = start_vertex_location; 815 info.index_bias = 0; 816 info.min_index = 0; 817 info.max_index = ~0; 818 info.start_instance = start_instance_location; 819 info.instance_count = instance_count; 820 info.primitive_restart = TRUE; 821 info.restart_index = strip_cut_index; 822 823 pipe->draw_vbo(pipe, &info); 824 } 825 826 virtual void STDMETHODCALLTYPE DrawAuto(void) 827 { 828 if(!caps.so) 829 return; 830 831 SYNCHRONIZED; 832 if(update_flags) 833 update_state(); 834 835 pipe->draw_stream_output(pipe, primitive_mode); 836 } 837 838 virtual void STDMETHODCALLTYPE DrawIndexedInstancedIndirect( 839 ID3D11Buffer *buffer, 840 unsigned aligned_byte_offset) 841 { 842 SYNCHRONIZED; 843 if(update_flags) 844 update_state(); 845 846 struct { 847 unsigned count; 848 unsigned instance_count; 849 unsigned start; 850 unsigned index_bias; 851 } data; 852 853 pipe_buffer_read(pipe, ((GalliumD3D11Buffer*)buffer)->resource, aligned_byte_offset, sizeof(data), &data); 854 855 pipe_draw_info info; 856 info.mode = primitive_mode; 857 info.indexed = TRUE; 858 info.start = data.start; 859 info.count = data.count; 860 info.index_bias = data.index_bias; 861 info.min_index = 0; 862 info.max_index = ~0; 863 info.start_instance = 0; 864 info.instance_count = data.instance_count; 865 info.primitive_restart = FALSE; 866 867 pipe->draw_vbo(pipe, &info); 868 } 869 870 virtual void STDMETHODCALLTYPE DrawInstancedIndirect( 871 ID3D11Buffer *buffer, 872 unsigned aligned_byte_offset) 873 { 874 SYNCHRONIZED; 875 if(update_flags) 876 update_state(); 877 878 struct { 879 unsigned count; 880 unsigned instance_count; 881 unsigned start; 882 } data; 883 884 pipe_buffer_read(pipe, ((GalliumD3D11Buffer*)buffer)->resource, aligned_byte_offset, sizeof(data), &data); 885 886 pipe_draw_info info; 887 info.mode = primitive_mode; 888 info.indexed = FALSE; 889 info.start = data.start; 890 info.count = data.count; 891 info.index_bias = 0; 892 info.min_index = 0; 893 info.max_index = ~0; 894 info.start_instance = 0; 895 info.instance_count = data.instance_count; 896 info.primitive_restart = TRUE; 897 info.restart_index = strip_cut_index; 898 899 pipe->draw_vbo(pipe, &info); 900 } 901 902#if API >= 11 903 virtual void STDMETHODCALLTYPE Dispatch( 904 unsigned thread_group_count_x, 905 unsigned thread_group_count_y, 906 unsigned thread_group_count_z) 907 { 908// uncomment this when this is implemented 909// SYNCHRONIZED; 910// if(update_flags) 911// update_state(); 912 } 913 914 virtual void STDMETHODCALLTYPE DispatchIndirect( 915 ID3D11Buffer *buffer, 916 unsigned aligned_byte_offset) 917 { 918// uncomment this when this is implemented 919// SYNCHRONIZED; 920// if(update_flags) 921// update_state(); 922 } 923#endif 924 925 void set_clip() 926 { 927 pipe_clip_state clip; 928 clip.nr = 0; 929 clip.depth_clamp = depth_clamp; 930 pipe->set_clip_state(pipe, &clip); 931 } 932 933 virtual void STDMETHODCALLTYPE RSSetState( 934 ID3D11RasterizerState *new_rasterizer_state) 935 { 936 SYNCHRONIZED; 937 if(new_rasterizer_state != rasterizer_state.p) 938 { 939 rasterizer_state = new_rasterizer_state; 940 pipe->bind_rasterizer_state(pipe, new_rasterizer_state ? ((GalliumD3D11RasterizerState*)new_rasterizer_state)->object : default_rasterizer); 941 bool new_depth_clamp = new_rasterizer_state ? ((GalliumD3D11RasterizerState*)new_rasterizer_state)->depth_clamp : false; 942 if(depth_clamp != new_depth_clamp) 943 { 944 depth_clamp = new_depth_clamp; 945 set_clip(); 946 } 947 } 948 } 949 950 virtual void STDMETHODCALLTYPE RSGetState( 951 ID3D11RasterizerState **out_rasterizer_state) 952 { 953 SYNCHRONIZED; 954 *out_rasterizer_state = rasterizer_state.ref(); 955 } 956 957 void set_viewport() 958 { 959 // TODO: is depth correct? it seems D3D10/11 uses a [-1,1]x[-1,1]x[0,1] cube 960 pipe_viewport_state viewport; 961 float half_width = viewports[0].Width * 0.5f; 962 float half_height = viewports[0].Height * 0.5f; 963 964 viewport.scale[0] = half_width; 965 viewport.scale[1] = -half_height; 966 viewport.scale[2] = (viewports[0].MaxDepth - viewports[0].MinDepth); 967 viewport.scale[3] = 1.0f; 968 viewport.translate[0] = half_width + viewports[0].TopLeftX; 969 viewport.translate[1] = half_height + viewports[0].TopLeftY; 970 viewport.translate[2] = viewports[0].MinDepth; 971 viewport.translate[3] = 1.0f; 972 pipe->set_viewport_state(pipe, &viewport); 973 } 974 975 virtual void STDMETHODCALLTYPE RSSetViewports( 976 unsigned count, 977 const D3D11_VIEWPORT *new_viewports) 978 { 979 SYNCHRONIZED; 980 if(count) 981 { 982 if(memcmp(&viewports[0], &new_viewports[0], sizeof(viewports[0]))) 983 { 984 viewports[0] = new_viewports[0]; 985 set_viewport(); 986 } 987 for(unsigned i = 1; i < count; ++i) 988 viewports[i] = new_viewports[i]; 989 } 990 else if(num_viewports) 991 { 992 // TODO: what should we do here? 993 memset(&viewports[0], 0, sizeof(viewports[0])); 994 set_viewport(); 995 } 996 num_viewports = count; 997 } 998 999 virtual void STDMETHODCALLTYPE RSGetViewports( 1000 unsigned *out_count, 1001 D3D11_VIEWPORT *out_viewports) 1002 { 1003 SYNCHRONIZED; 1004 if(out_viewports) 1005 { 1006 unsigned i; 1007 for(i = 0; i < std::min(*out_count, num_viewports); ++i) 1008 out_viewports[i] = viewports[i]; 1009 1010 memset(out_viewports + i, 0, (*out_count - i) * sizeof(D3D11_VIEWPORT)); 1011 } 1012 1013 *out_count = num_viewports; 1014 } 1015 1016 void set_scissor() 1017 { 1018 pipe_scissor_state scissor; 1019 scissor.minx = scissor_rects[0].left; 1020 scissor.miny = scissor_rects[0].top; 1021 scissor.maxx = scissor_rects[0].right; 1022 scissor.maxy = scissor_rects[0].bottom; 1023 pipe->set_scissor_state(pipe, &scissor); 1024 } 1025 1026 virtual void STDMETHODCALLTYPE RSSetScissorRects( 1027 unsigned count, 1028 const D3D11_RECT *new_rects) 1029 { 1030 SYNCHRONIZED; 1031 if(count) 1032 { 1033 if(memcmp(&scissor_rects[0], &new_rects[0], sizeof(scissor_rects[0]))) 1034 { 1035 scissor_rects[0] = new_rects[0]; 1036 set_scissor(); 1037 } 1038 for(unsigned i = 1; i < count; ++i) 1039 scissor_rects[i] = new_rects[i]; 1040 } 1041 else if(num_scissor_rects) 1042 { 1043 // TODO: what should we do here? 1044 memset(&scissor_rects[0], 0, sizeof(scissor_rects[0])); 1045 set_scissor(); 1046 } 1047 1048 num_scissor_rects = count; 1049 } 1050 1051 virtual void STDMETHODCALLTYPE RSGetScissorRects( 1052 unsigned *out_count, 1053 D3D11_RECT *out_rects) 1054 { 1055 SYNCHRONIZED; 1056 if(out_rects) 1057 { 1058 unsigned i; 1059 for(i = 0; i < std::min(*out_count, num_scissor_rects); ++i) 1060 out_rects[i] = scissor_rects[i]; 1061 1062 memset(out_rects + i, 0, (*out_count - i) * sizeof(D3D11_RECT)); 1063 } 1064 1065 *out_count = num_scissor_rects; 1066 } 1067 1068 virtual void STDMETHODCALLTYPE OMSetBlendState( 1069 ID3D11BlendState *new_blend_state, 1070 const float new_blend_factor[4], 1071 unsigned new_sample_mask) 1072 { 1073 SYNCHRONIZED; 1074 float white[4] = {1.0f, 1.0f, 1.0f, 1.0f}; 1075 1076 if(blend_state.p != new_blend_state) 1077 { 1078 pipe->bind_blend_state(pipe, new_blend_state ? ((GalliumD3D11BlendState*)new_blend_state)->object : default_blend); 1079 blend_state = new_blend_state; 1080 } 1081 1082 // Windows D3D11 does this, even though it's apparently undocumented 1083 if(!new_blend_factor) 1084 new_blend_factor = white; 1085 1086 if(memcmp(blend_color, new_blend_factor, sizeof(blend_color))) 1087 { 1088 pipe->set_blend_color(pipe, (struct pipe_blend_color*)new_blend_factor); 1089 memcpy(blend_color, new_blend_factor, sizeof(blend_color)); 1090 } 1091 1092 if(sample_mask != new_sample_mask) 1093 { 1094 pipe->set_sample_mask(pipe, new_sample_mask); 1095 sample_mask = new_sample_mask; 1096 } 1097 } 1098 1099 virtual void STDMETHODCALLTYPE OMGetBlendState( 1100 ID3D11BlendState **out_blend_state, 1101 float out_blend_factor[4], 1102 unsigned *out_sample_mask) 1103 { 1104 SYNCHRONIZED; 1105 if(out_blend_state) 1106 *out_blend_state = blend_state.ref(); 1107 if(out_blend_factor) 1108 memcpy(out_blend_factor, blend_color, sizeof(blend_color)); 1109 if(out_sample_mask) 1110 *out_sample_mask = sample_mask; 1111 } 1112 1113 void set_stencil_ref() 1114 { 1115 struct pipe_stencil_ref sref; 1116 sref.ref_value[0] = stencil_ref; 1117 sref.ref_value[1] = stencil_ref; 1118 pipe->set_stencil_ref(pipe, &sref); 1119 } 1120 1121 virtual void STDMETHODCALLTYPE OMSetDepthStencilState( 1122 ID3D11DepthStencilState *new_depth_stencil_state, 1123 unsigned new_stencil_ref) 1124 { 1125 SYNCHRONIZED; 1126 if(new_depth_stencil_state != depth_stencil_state.p) 1127 { 1128 pipe->bind_depth_stencil_alpha_state(pipe, new_depth_stencil_state ? ((GalliumD3D11DepthStencilState*)new_depth_stencil_state)->object : default_depth_stencil); 1129 depth_stencil_state = new_depth_stencil_state; 1130 } 1131 1132 if(new_stencil_ref != stencil_ref) 1133 { 1134 stencil_ref = new_stencil_ref; 1135 set_stencil_ref(); 1136 } 1137 } 1138 1139 virtual void STDMETHODCALLTYPE OMGetDepthStencilState( 1140 ID3D11DepthStencilState **out_depth_stencil_state, 1141 unsigned *out_stencil_ref) 1142 { 1143 SYNCHRONIZED; 1144 if(*out_depth_stencil_state) 1145 *out_depth_stencil_state = depth_stencil_state.ref(); 1146 if(out_stencil_ref) 1147 *out_stencil_ref = stencil_ref; 1148 } 1149 1150 void set_framebuffer() 1151 { 1152 struct pipe_framebuffer_state fb; 1153 memset(&fb, 0, sizeof(fb)); 1154 if(depth_stencil_view) 1155 { 1156 struct pipe_surface* surf = ((GalliumD3D11DepthStencilView*)depth_stencil_view.p)->object; 1157 fb.zsbuf = surf; 1158 if(surf->width > fb.width) 1159 fb.width = surf->width; 1160 if(surf->height > fb.height) 1161 fb.height = surf->height; 1162 } 1163 fb.nr_cbufs = num_render_target_views; 1164 unsigned i; 1165 for(i = 0; i < num_render_target_views; ++i) 1166 { 1167 if(render_target_views[i]) 1168 { 1169 struct pipe_surface* surf = ((GalliumD3D11RenderTargetView*)render_target_views[i].p)->object; 1170 fb.cbufs[i] = surf; 1171 if(surf->width > fb.width) 1172 fb.width = surf->width; 1173 if(surf->height > fb.height) 1174 fb.height = surf->height; 1175 } 1176 } 1177 1178 pipe->set_framebuffer_state(pipe, &fb); 1179 } 1180 1181 /* TODO: the docs say that we should unbind conflicting resources (e.g. those bound for read while we are binding them for write too), but we aren't. 1182 * Hopefully nobody relies on this happening 1183 */ 1184 1185 virtual void STDMETHODCALLTYPE OMSetRenderTargets( 1186 unsigned count, 1187 ID3D11RenderTargetView *const *new_render_target_views, 1188 ID3D11DepthStencilView *new_depth_stencil_view) 1189 { 1190 SYNCHRONIZED; 1191 if(!new_render_target_views) 1192 count = 0; 1193 if(count == num_render_target_views) 1194 { 1195 for(unsigned i = 0; i < count; ++i) 1196 { 1197 if(new_render_target_views[i] != render_target_views[i].p) 1198 goto changed; 1199 } 1200 return; 1201 } 1202changed: 1203 depth_stencil_view = new_depth_stencil_view; 1204 unsigned i; 1205 for(i = 0; i < count; ++i) 1206 { 1207 render_target_views[i] = new_render_target_views[i]; 1208#if API >= 11 1209 om_unordered_access_views[i] = (ID3D11UnorderedAccessView*)NULL; 1210#endif 1211 } 1212 for(; i < num_render_target_views; ++i) 1213 render_target_views[i] = (ID3D11RenderTargetView*)NULL; 1214 num_render_target_views = count; 1215 set_framebuffer(); 1216 } 1217 1218 virtual void STDMETHODCALLTYPE OMGetRenderTargets( 1219 unsigned count, 1220 ID3D11RenderTargetView **out_render_target_views, 1221 ID3D11DepthStencilView **out_depth_stencil_view) 1222 { 1223 SYNCHRONIZED; 1224 if(out_render_target_views) 1225 { 1226 unsigned i; 1227 for(i = 0; i < std::min(num_render_target_views, count); ++i) 1228 out_render_target_views[i] = render_target_views[i].ref(); 1229 1230 for(; i < count; ++i) 1231 out_render_target_views[i] = 0; 1232 } 1233 1234 if(out_depth_stencil_view) 1235 *out_depth_stencil_view = depth_stencil_view.ref(); 1236 } 1237 1238#if API >= 11 1239 /* TODO: what is this supposed to do _exactly_? are we doing the right thing? */ 1240 virtual void STDMETHODCALLTYPE OMSetRenderTargetsAndUnorderedAccessViews( 1241 unsigned rtv_count, 1242 ID3D11RenderTargetView *const *new_render_target_views, 1243 ID3D11DepthStencilView *new_depth_stencil_view, 1244 unsigned uav_start, 1245 unsigned uav_count, 1246 ID3D11UnorderedAccessView *const *new_unordered_access_views, 1247 const unsigned *new_uav_initial_counts) 1248 { 1249 SYNCHRONIZED; 1250 if(rtv_count != D3D11_KEEP_RENDER_TARGETS_AND_DEPTH_STENCIL) 1251 OMSetRenderTargets(rtv_count, new_render_target_views, new_depth_stencil_view); 1252 1253 if(uav_count != D3D11_KEEP_UNORDERED_ACCESS_VIEWS) 1254 { 1255 for(unsigned i = 0; i < uav_count; ++i) 1256 { 1257 om_unordered_access_views[uav_start + i] = new_unordered_access_views[i]; 1258 render_target_views[uav_start + i] = (ID3D11RenderTargetView*)0; 1259 } 1260 } 1261 } 1262 1263 virtual void STDMETHODCALLTYPE OMGetRenderTargetsAndUnorderedAccessViews( 1264 unsigned rtv_count, 1265 ID3D11RenderTargetView **out_render_target_views, 1266 ID3D11DepthStencilView **out_depth_stencil_view, 1267 unsigned uav_start, 1268 unsigned uav_count, 1269 ID3D11UnorderedAccessView **out_unordered_access_views) 1270 { 1271 SYNCHRONIZED; 1272 if(out_render_target_views) 1273 OMGetRenderTargets(rtv_count, out_render_target_views, out_depth_stencil_view); 1274 1275 if(out_unordered_access_views) 1276 { 1277 for(unsigned i = 0; i < uav_count; ++i) 1278 out_unordered_access_views[i] = om_unordered_access_views[uav_start + i].ref(); 1279 } 1280 } 1281#endif 1282 1283 virtual void STDMETHODCALLTYPE SOSetTargets( 1284 unsigned count, 1285 ID3D11Buffer *const *new_so_targets, 1286 const unsigned *new_offsets) 1287 { 1288 SYNCHRONIZED; 1289 unsigned i; 1290 if(!new_so_targets) 1291 count = 0; 1292 bool changed = false; 1293 for(i = 0; i < count; ++i) 1294 { 1295 ID3D11Buffer* buffer = new_so_targets[i]; 1296 if(buffer != so_targets[i].p || new_offsets[i] != so_offsets[i]) 1297 { 1298 so_buffers[i] = buffer ? ((GalliumD3D11Buffer*)buffer)->resource : 0; 1299 so_targets[i] = buffer; 1300 so_offsets[i] = new_offsets[i]; 1301 changed = true; 1302 } 1303 } 1304 for(; i < D3D11_SO_BUFFER_SLOT_COUNT; ++i) 1305 { 1306 if(so_targets[i].p || so_offsets[i]) 1307 { 1308 changed = true; 1309 so_targets[i] = (ID3D11Buffer*)0; 1310 so_offsets[i] = 0; 1311 } 1312 } 1313 num_so_targets = count; 1314 1315 if(changed && caps.so) 1316 pipe->set_stream_output_buffers(pipe, so_buffers, (int*)so_offsets, num_so_targets); 1317 } 1318 1319 virtual void STDMETHODCALLTYPE SOGetTargets( 1320 unsigned count, 1321 ID3D11Buffer **out_so_targets 1322#if API < 11 1323 , UINT *out_offsets 1324#endif 1325 ) 1326 { 1327 SYNCHRONIZED; 1328 for(unsigned i = 0; i < count; ++i) 1329 { 1330 out_so_targets[i] = so_targets[i].ref(); 1331#if API < 11 1332 out_offsets[i] = so_offsets[i]; 1333#endif 1334 } 1335 } 1336 1337 virtual void STDMETHODCALLTYPE Begin( 1338 ID3D11Asynchronous *async) 1339 { 1340 SYNCHRONIZED; 1341 if(caps.queries) 1342 pipe->begin_query(pipe, ((GalliumD3D11Asynchronous<>*)async)->query); 1343 } 1344 1345 virtual void STDMETHODCALLTYPE End( 1346 ID3D11Asynchronous *async) 1347 { 1348 SYNCHRONIZED; 1349 if(caps.queries) 1350 pipe->end_query(pipe, ((GalliumD3D11Asynchronous<>*)async)->query); 1351 } 1352 1353 virtual HRESULT STDMETHODCALLTYPE GetData( 1354 ID3D11Asynchronous *iasync, 1355 void *out_data, 1356 unsigned data_size, 1357 unsigned get_data_flags) 1358 { 1359 SYNCHRONIZED; 1360 if(!caps.queries) 1361 return E_NOTIMPL; 1362 1363 GalliumD3D11Asynchronous<>* async = (GalliumD3D11Asynchronous<>*)iasync; 1364 void* tmp_data = alloca(async->data_size); 1365 boolean ret = pipe->get_query_result(pipe, async->query, !(get_data_flags & D3D11_ASYNC_GETDATA_DONOTFLUSH), tmp_data); 1366 if(out_data) 1367 memcpy(out_data, tmp_data, std::min(async->data_size, data_size)); 1368 return ret ? S_OK : S_FALSE; 1369 } 1370 1371 void set_render_condition() 1372 { 1373 if(caps.render_condition) 1374 { 1375 if(!render_predicate) 1376 pipe->render_condition(pipe, 0, 0); 1377 else 1378 { 1379 GalliumD3D11Predicate* predicate = (GalliumD3D11Predicate*)render_predicate.p; 1380 if(!render_predicate_value && predicate->desc.Query == D3D11_QUERY_OCCLUSION_PREDICATE) 1381 { 1382 unsigned mode = (predicate->desc.MiscFlags & D3D11_QUERY_MISC_PREDICATEHINT) ? PIPE_RENDER_COND_NO_WAIT : PIPE_RENDER_COND_WAIT; 1383 pipe->render_condition(pipe, predicate->query, mode); 1384 } 1385 else 1386 { 1387 /* TODO: add inverted predication to Gallium*/ 1388 pipe->render_condition(pipe, 0, 0); 1389 } 1390 } 1391 } 1392 } 1393 1394 virtual void STDMETHODCALLTYPE SetPredication( 1395 ID3D11Predicate *new_predicate, 1396 BOOL new_predicate_value) 1397 { 1398 SYNCHRONIZED; 1399 if(render_predicate.p != new_predicate || render_predicate_value != new_predicate_value) 1400 { 1401 render_predicate = new_predicate; 1402 render_predicate_value = new_predicate_value; 1403 set_render_condition(); 1404 } 1405 } 1406 1407 virtual void STDMETHODCALLTYPE GetPredication( 1408 ID3D11Predicate **out_predicate, 1409 BOOL *out_predicate_value) 1410 { 1411 SYNCHRONIZED; 1412 if(out_predicate) 1413 *out_predicate = render_predicate.ref(); 1414 if(out_predicate_value) 1415 *out_predicate_value = render_predicate_value; 1416 } 1417 1418 static unsigned d3d11_subresource_to_level(struct pipe_resource* resource, unsigned subresource) 1419 { 1420 if(subresource <= resource->last_level) 1421 { 1422 return subresource; 1423 } 1424 else 1425 { 1426 unsigned levels = resource->last_level + 1; 1427 return subresource % levels; 1428 } 1429 } 1430 1431 static unsigned d3d11_subresource_to_face(struct pipe_resource* resource, unsigned subresource) 1432 { 1433 if(subresource <= resource->last_level) 1434 { 1435 return 0; 1436 } 1437 else 1438 { 1439 unsigned levels = resource->last_level + 1; 1440 return subresource / levels; 1441 } 1442 } 1443 1444 1445 /* TODO: deferred contexts will need a different implementation of this, 1446 * because we can't put the transfer info into the resource itself. 1447 * Also, there are very different restrictions, for obvious reasons. 1448 */ 1449 virtual HRESULT STDMETHODCALLTYPE Map( 1450 ID3D11Resource *iresource, 1451 unsigned subresource, 1452 D3D11_MAP map_type, 1453 unsigned map_flags, 1454 D3D11_MAPPED_SUBRESOURCE *mapped_resource) 1455 { 1456 SYNCHRONIZED; 1457 GalliumD3D11Resource<>* resource = (GalliumD3D11Resource<>*)iresource; 1458 if(resource->transfers.count(subresource)) 1459 return E_FAIL; 1460 unsigned level = d3d11_subresource_to_level(resource->resource, subresource); 1461 unsigned face = d3d11_subresource_to_face(resource->resource, subresource); 1462 pipe_box box = d3d11_to_pipe_box(resource->resource, level, 0); 1463 /* XXX the translation from subresource to level/face(zslice/array layer) isn't quite right */ 1464 unsigned usage = 0; 1465 if(map_type == D3D11_MAP_READ) 1466 usage = PIPE_TRANSFER_READ; 1467 else if(map_type == D3D11_MAP_WRITE) 1468 usage = PIPE_TRANSFER_WRITE; 1469 else if(map_type == D3D11_MAP_READ_WRITE) 1470 usage = PIPE_TRANSFER_READ_WRITE; 1471 else if(map_type == D3D11_MAP_WRITE_DISCARD) 1472 usage = PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD; 1473 else if(map_type == D3D11_MAP_WRITE_NO_OVERWRITE) 1474 usage = PIPE_TRANSFER_WRITE | PIPE_TRANSFER_NOOVERWRITE; 1475 else 1476 return E_INVALIDARG; 1477 if(map_type & D3D10_MAP_FLAG_DO_NOT_WAIT) 1478 usage |= PIPE_TRANSFER_DONTBLOCK; 1479 struct pipe_transfer* transfer = pipe->get_transfer(pipe, resource->resource, level, usage, &box); 1480 if(!transfer) { 1481 if(map_type & D3D10_MAP_FLAG_DO_NOT_WAIT) 1482 return DXGI_ERROR_WAS_STILL_DRAWING; 1483 else 1484 return E_FAIL; 1485 } 1486 resource->transfers[subresource] = transfer; 1487 mapped_resource->pData = pipe->transfer_map(pipe, transfer); 1488 mapped_resource->RowPitch = transfer->stride; 1489 mapped_resource->DepthPitch = transfer->layer_stride; 1490 return S_OK; 1491 } 1492 1493 virtual void STDMETHODCALLTYPE Unmap( 1494 ID3D11Resource *iresource, 1495 unsigned subresource) 1496 { 1497 SYNCHRONIZED; 1498 GalliumD3D11Resource<>* resource = (GalliumD3D11Resource<>*)iresource; 1499 std::unordered_map<unsigned, pipe_transfer*>::iterator i = resource->transfers.find(subresource); 1500 if(i != resource->transfers.end()) 1501 { 1502 pipe->transfer_unmap(pipe, i->second); 1503 pipe->transfer_destroy(pipe, i->second); 1504 resource->transfers.erase(i); 1505 } 1506 } 1507 1508 virtual void STDMETHODCALLTYPE CopySubresourceRegion( 1509 ID3D11Resource *dst_resource, 1510 unsigned dst_subresource, 1511 unsigned dst_x, 1512 unsigned dst_y, 1513 unsigned dst_z, 1514 ID3D11Resource *src_resource, 1515 unsigned src_subresource, 1516 const D3D11_BOX *src_box) 1517 { 1518 SYNCHRONIZED; 1519 GalliumD3D11Resource<>* dst = (GalliumD3D11Resource<>*)dst_resource; 1520 GalliumD3D11Resource<>* src = (GalliumD3D11Resource<>*)src_resource; 1521 unsigned dst_level = d3d11_subresource_to_level(dst->resource, dst_subresource); 1522 unsigned dst_face = d3d11_subresource_to_face(dst->resource, dst_subresource); 1523 unsigned src_level = d3d11_subresource_to_level(src->resource, src_subresource); 1524 unsigned src_face = d3d11_subresource_to_face(src->resource, src_subresource); 1525 /* XXX the translation from subresource to level/face(zslice/array layer) isn't quite right */ 1526 pipe_box box = d3d11_to_pipe_box(src->resource, src_level, src_box); 1527 { 1528 pipe->resource_copy_region(pipe, 1529 dst->resource, dst_level, dst_x, dst_y, dst_z, 1530 src->resource, src_level, &box); 1531 } 1532 } 1533 1534 virtual void STDMETHODCALLTYPE CopyResource( 1535 ID3D11Resource *dst_resource, 1536 ID3D11Resource *src_resource) 1537 { 1538 SYNCHRONIZED; 1539 GalliumD3D11Resource<>* dst = (GalliumD3D11Resource<>*)dst_resource; 1540 GalliumD3D11Resource<>* src = (GalliumD3D11Resource<>*)src_resource; 1541 unsigned level; 1542 for(level = 0; level <= dst->resource->last_level; ++level) 1543 { 1544 unsigned layers = 1; 1545 pipe_box box; 1546 if (dst->resource->target == PIPE_TEXTURE_CUBE) 1547 layers = 6; 1548 else if (dst->resource->target == PIPE_TEXTURE_3D) 1549 layers = u_minify(dst->resource->depth0, level); 1550 /* else layers = dst->resource->array_size; */ 1551 box.x = box.y = box.z = 0; 1552 box.width = u_minify(dst->resource->width0, level); 1553 box.height = u_minify(dst->resource->height0, level); 1554 box.depth = layers; 1555 pipe->resource_copy_region(pipe, 1556 dst->resource, level, 0, 0, 0, 1557 src->resource, level, &box); 1558 } 1559 } 1560 1561 virtual void STDMETHODCALLTYPE UpdateSubresource( 1562 ID3D11Resource *dst_resource, 1563 unsigned dst_subresource, 1564 const D3D11_BOX *pDstBox, 1565 const void *pSrcData, 1566 unsigned src_row_pitch, 1567 unsigned src_depth_pitch) 1568 { 1569 SYNCHRONIZED; 1570 GalliumD3D11Resource<>* dst = (GalliumD3D11Resource<>*)dst_resource; 1571 unsigned dst_level = d3d11_subresource_to_level(dst->resource, dst_subresource); 1572 /* XXX the translation from subresource to level/face(zslice/array layer) isn't quite right */ 1573 pipe_box box = d3d11_to_pipe_box(dst->resource, dst_level, pDstBox); 1574 pipe->transfer_inline_write(pipe, dst->resource, dst_level, PIPE_TRANSFER_WRITE, &box, pSrcData, src_row_pitch, src_depth_pitch); 1575 } 1576 1577#if API >= 11 1578 virtual void STDMETHODCALLTYPE CopyStructureCount( 1579 ID3D11Buffer *dst_buffer, 1580 unsigned dst_aligned_byte_offset, 1581 ID3D11UnorderedAccessView *src_view) 1582 { 1583 SYNCHRONIZED; 1584 } 1585#endif 1586 1587 virtual void STDMETHODCALLTYPE ClearRenderTargetView( 1588 ID3D11RenderTargetView *render_target_view, 1589 const float color[4]) 1590 { 1591 SYNCHRONIZED; 1592 GalliumD3D11RenderTargetView* view = ((GalliumD3D11RenderTargetView*)render_target_view); 1593 union pipe_color_union cc; 1594 cc.f[0] = color[0]; 1595 cc.f[1] = color[1]; 1596 cc.f[2] = color[2]; 1597 cc.f[3] = color[3]; 1598 pipe->clear_render_target(pipe, view->object, &cc, 0, 0, view->object->width, view->object->height); 1599 } 1600 1601 virtual void STDMETHODCALLTYPE ClearDepthStencilView( 1602 ID3D11DepthStencilView *depth_stencil_view, 1603 unsigned clear_flags, 1604 float depth, 1605 UINT8 stencil) 1606 { 1607 SYNCHRONIZED; 1608 GalliumD3D11DepthStencilView* view = ((GalliumD3D11DepthStencilView*)depth_stencil_view); 1609 unsigned flags = 0; 1610 if(clear_flags & D3D11_CLEAR_DEPTH) 1611 flags |= PIPE_CLEAR_DEPTH; 1612 if(clear_flags & D3D11_CLEAR_STENCIL) 1613 flags |= PIPE_CLEAR_STENCIL; 1614 pipe->clear_depth_stencil(pipe, view->object, flags, depth, stencil, 0, 0, view->object->width, view->object->height); 1615 } 1616 1617#if API >= 11 1618 virtual void STDMETHODCALLTYPE ClearUnorderedAccessViewUint( 1619 ID3D11UnorderedAccessView *unordered_access_view, 1620 const unsigned values[4]) 1621 { 1622 SYNCHRONIZED; 1623 } 1624 1625 virtual void STDMETHODCALLTYPE ClearUnorderedAccessViewFloat( 1626 ID3D11UnorderedAccessView *unordered_access_view, 1627 const float values[4]) 1628 { 1629 SYNCHRONIZED; 1630 } 1631#endif 1632 1633 void restore_gallium_state_blit_only() 1634 { 1635 pipe->bind_blend_state(pipe, blend_state.p ? blend_state.p->object : default_blend); 1636 pipe->bind_depth_stencil_alpha_state(pipe, depth_stencil_state.p ? depth_stencil_state.p->object : default_depth_stencil); 1637 pipe->bind_rasterizer_state(pipe, rasterizer_state.p ? rasterizer_state.p->object : default_rasterizer); 1638 pipe->bind_vertex_elements_state(pipe, input_layout.p ? input_layout.p->object : default_input_layout); 1639 pipe->bind_fs_state(pipe, shaders[D3D11_STAGE_PS].p ? shaders[D3D11_STAGE_PS].p->object : default_shaders[PIPE_SHADER_FRAGMENT]); 1640 pipe->bind_vs_state(pipe, shaders[D3D11_STAGE_VS].p ? shaders[D3D11_STAGE_VS].p->object : default_shaders[PIPE_SHADER_VERTEX]); 1641 if(caps.gs) 1642 pipe->bind_gs_state(pipe, shaders[D3D11_STAGE_GS].p ? shaders[D3D11_STAGE_GS].p->object : default_shaders[PIPE_SHADER_GEOMETRY]); 1643 set_framebuffer(); 1644 set_viewport(); 1645 set_clip(); 1646 set_render_condition(); 1647 // TODO: restore stream output 1648 1649 update_flags |= UPDATE_VERTEX_BUFFERS | (1 << (UPDATE_SAMPLERS_SHIFT + D3D11_STAGE_PS)) | (1 << (UPDATE_VIEWS_SHIFT + D3D11_STAGE_PS)); 1650 } 1651 1652 virtual void STDMETHODCALLTYPE RestoreGalliumStateBlitOnly() 1653 { 1654 SYNCHRONIZED; 1655 restore_gallium_state_blit_only(); 1656 } 1657 1658 virtual void STDMETHODCALLTYPE GenerateMips( 1659 ID3D11ShaderResourceView *shader_resource_view) 1660 { 1661 SYNCHRONIZED; 1662 1663 GalliumD3D11ShaderResourceView* view = (GalliumD3D11ShaderResourceView*)shader_resource_view; 1664 if(caps.gs) 1665 pipe->bind_gs_state(pipe, 0); 1666 if(caps.so) 1667 pipe->bind_stream_output_state(pipe, 0); 1668 if(pipe->render_condition) 1669 pipe->render_condition(pipe, 0, 0); 1670 for(unsigned layer = view->object->u.tex.first_layer; layer <= view->object->u.tex.last_layer; ++layer) 1671 util_gen_mipmap(gen_mipmap, view->object, layer, view->object->u.tex.first_level, view->object->u.tex.last_level, PIPE_TEX_FILTER_LINEAR); 1672 restore_gallium_state_blit_only(); 1673 } 1674 1675 virtual void STDMETHODCALLTYPE RestoreGalliumState() 1676 { 1677 SYNCHRONIZED; 1678 restore_gallium_state_blit_only(); 1679 1680 set_index_buffer(); 1681 set_stencil_ref(); 1682 pipe->set_blend_color(pipe, (struct pipe_blend_color*)blend_color); 1683 pipe->set_sample_mask(pipe, sample_mask); 1684 1685 for(unsigned s = 0; s < 3; ++s) 1686 { 1687 unsigned num = std::min(caps.constant_buffers[s], (unsigned)D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT); 1688 for(unsigned i = 0; i < num; ++i) 1689 pipe->set_constant_buffer(pipe, s, i, constant_buffers[s][i].p ? constant_buffers[s][i].p->resource : 0); 1690 } 1691 1692 if(caps.so) 1693 pipe->set_stream_output_buffers(pipe, so_buffers, (int*)so_offsets, num_so_targets); 1694 1695 update_flags |= (1 << (UPDATE_SAMPLERS_SHIFT + D3D11_STAGE_VS)) | (1 << (UPDATE_VIEWS_SHIFT + D3D11_STAGE_VS)); 1696 update_flags |= (1 << (UPDATE_SAMPLERS_SHIFT + D3D11_STAGE_GS)) | (1 << (UPDATE_VIEWS_SHIFT + D3D11_STAGE_GS)); 1697 1698 set_scissor(); 1699 } 1700 1701#if API >= 11 1702 /* TODO: hack SRVs or sampler states to handle this, or add to Gallium */ 1703 virtual void STDMETHODCALLTYPE SetResourceMinLOD( 1704 ID3D11Resource *iresource, 1705 float min_lod) 1706 { 1707 SYNCHRONIZED; 1708 GalliumD3D11Resource<>* resource = (GalliumD3D11Resource<>*)iresource; 1709 if(resource->min_lod != min_lod) 1710 { 1711 // TODO: actually do anything? 1712 resource->min_lod = min_lod; 1713 } 1714 } 1715 1716 virtual float STDMETHODCALLTYPE GetResourceMinLOD( 1717 ID3D11Resource *iresource) 1718 { 1719 SYNCHRONIZED; 1720 GalliumD3D11Resource<>* resource = (GalliumD3D11Resource<>*)iresource; 1721 return resource->min_lod; 1722 } 1723#endif 1724 1725 virtual void STDMETHODCALLTYPE ResolveSubresource( 1726 ID3D11Resource *dst_resource, 1727 unsigned dst_subresource, 1728 ID3D11Resource *src_resource, 1729 unsigned src_subresource, 1730 DXGI_FORMAT format) 1731 { 1732 SYNCHRONIZED; 1733 GalliumD3D11Resource<>* dst = (GalliumD3D11Resource<>*)dst_resource; 1734 GalliumD3D11Resource<>* src = (GalliumD3D11Resource<>*)src_resource; 1735 struct pipe_resolve_info info; 1736 1737 info.dst.res = dst->resource; 1738 info.src.res = src->resource; 1739 info.dst.level = 0; 1740 info.dst.layer = d3d11_subresource_to_face(dst->resource, dst_subresource); 1741 info.src.layer = d3d11_subresource_to_face(src->resource, src_subresource); 1742 1743 info.src.x0 = 0; 1744 info.src.x1 = info.src.res->width0; 1745 info.src.y0 = 0; 1746 info.src.y1 = info.src.res->height0; 1747 info.dst.x0 = 0; 1748 info.dst.x1 = info.dst.res->width0; 1749 info.dst.y0 = 0; 1750 info.dst.y1 = info.dst.res->height0; 1751 1752 info.mask = PIPE_MASK_RGBA | PIPE_MASK_ZS; 1753 1754 pipe->resource_resolve(pipe, &info); 1755 } 1756 1757#if API >= 11 1758 virtual void STDMETHODCALLTYPE ExecuteCommandList( 1759 ID3D11CommandList *command_list, 1760 BOOL restore_context_state) 1761 { 1762 SYNCHRONIZED; 1763 } 1764 1765 virtual HRESULT STDMETHODCALLTYPE FinishCommandList( 1766 BOOL restore_deferred_context_state, 1767 ID3D11CommandList **out_command_list) 1768 { 1769 SYNCHRONIZED; 1770 return E_NOTIMPL; 1771 } 1772#endif 1773 1774 virtual void STDMETHODCALLTYPE ClearState(void) 1775 { 1776 /* we don't take a lock here because we would deadlock otherwise 1777 * TODO: this is probably incorrect, because ClearState should likely be atomic. 1778 * However, I can't think of any correct usage that would be affected by this 1779 * being non-atomic, and making this atomic is quite expensive and complicates 1780 * the code 1781 */ 1782 1783 // we qualify all calls so that we avoid virtual dispatch and might get them inlined 1784 // TODO: make sure all this gets inlined, which might require more compiler flags 1785 // TODO: optimize this 1786#if API >= 11 1787 GalliumD3D11DeviceContext::PSSetShader(0, 0, 0); 1788 GalliumD3D11DeviceContext::GSSetShader(0, 0, 0); 1789 GalliumD3D11DeviceContext::VSSetShader(0, 0, 0); 1790 GalliumD3D11DeviceContext::HSSetShader(0, 0, 0); 1791 GalliumD3D11DeviceContext::DSSetShader(0, 0, 0); 1792 GalliumD3D11DeviceContext::CSSetShader(0, 0, 0); 1793#else 1794 GalliumD3D11DeviceContext::PSSetShader(0); 1795 GalliumD3D11DeviceContext::GSSetShader(0); 1796 GalliumD3D11DeviceContext::VSSetShader(0); 1797#endif 1798 1799 GalliumD3D11DeviceContext::IASetInputLayout(0); 1800 GalliumD3D11DeviceContext::IASetIndexBuffer(0, DXGI_FORMAT_UNKNOWN, 0); 1801 GalliumD3D11DeviceContext::RSSetState(0); 1802 GalliumD3D11DeviceContext::OMSetDepthStencilState(0, 0); 1803 GalliumD3D11DeviceContext::OMSetBlendState(0, (float*)zero_data, ~0); 1804 GalliumD3D11DeviceContext::SetPredication(0, 0); 1805 GalliumD3D11DeviceContext::IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_UNDEFINED); 1806 1807 GalliumD3D11DeviceContext::PSSetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, (ID3D11Buffer**)zero_data); 1808 GalliumD3D11DeviceContext::GSSetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, (ID3D11Buffer**)zero_data); 1809 GalliumD3D11DeviceContext::VSSetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, (ID3D11Buffer**)zero_data); 1810#if API >= 11 1811 GalliumD3D11DeviceContext::HSSetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, (ID3D11Buffer**)zero_data); 1812 GalliumD3D11DeviceContext::DSSetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, (ID3D11Buffer**)zero_data); 1813 GalliumD3D11DeviceContext::CSSetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, (ID3D11Buffer**)zero_data); 1814#endif 1815 1816 GalliumD3D11DeviceContext::IASetVertexBuffers(0, num_vertex_buffers, (ID3D11Buffer**)zero_data, (unsigned*)zero_data, (unsigned*)zero_data); 1817#if API >= 11 1818 GalliumD3D11DeviceContext::OMSetRenderTargetsAndUnorderedAccessViews(0, 0, 0 , 0, 0, 0, 0); 1819#else 1820 GalliumD3D11DeviceContext::OMSetRenderTargets(0, 0, 0 ); 1821#endif 1822 GalliumD3D11DeviceContext::SOSetTargets(0, 0, 0); 1823 1824 GalliumD3D11DeviceContext::PSSetShaderResources(0, num_shader_resource_views[D3D11_STAGE_PS], (ID3D11ShaderResourceView**)zero_data); 1825 GalliumD3D11DeviceContext::GSSetShaderResources(0, num_shader_resource_views[D3D11_STAGE_GS], (ID3D11ShaderResourceView**)zero_data); 1826 GalliumD3D11DeviceContext::VSSetShaderResources(0, num_shader_resource_views[D3D11_STAGE_VS], (ID3D11ShaderResourceView**)zero_data); 1827#if API >= 11 1828 GalliumD3D11DeviceContext::HSSetShaderResources(0, num_shader_resource_views[D3D11_STAGE_HS], (ID3D11ShaderResourceView**)zero_data); 1829 GalliumD3D11DeviceContext::DSSetShaderResources(0, num_shader_resource_views[D3D11_STAGE_DS], (ID3D11ShaderResourceView**)zero_data); 1830 GalliumD3D11DeviceContext::CSSetShaderResources(0, num_shader_resource_views[D3D11_STAGE_CS], (ID3D11ShaderResourceView**)zero_data); 1831#endif 1832 1833 GalliumD3D11DeviceContext::PSSetSamplers(0, num_shader_resource_views[D3D11_STAGE_PS], (ID3D11SamplerState**)zero_data); 1834 GalliumD3D11DeviceContext::GSSetSamplers(0, num_shader_resource_views[D3D11_STAGE_GS], (ID3D11SamplerState**)zero_data); 1835 GalliumD3D11DeviceContext::VSSetSamplers(0, num_shader_resource_views[D3D11_STAGE_VS], (ID3D11SamplerState**)zero_data); 1836#if API >= 11 1837 GalliumD3D11DeviceContext::HSSetSamplers(0, num_shader_resource_views[D3D11_STAGE_HS], (ID3D11SamplerState**)zero_data); 1838 GalliumD3D11DeviceContext::DSSetSamplers(0, num_shader_resource_views[D3D11_STAGE_DS], (ID3D11SamplerState**)zero_data); 1839 GalliumD3D11DeviceContext::CSSetSamplers(0, num_shader_resource_views[D3D11_STAGE_CS], (ID3D11SamplerState**)zero_data); 1840#endif 1841 1842 GalliumD3D11DeviceContext::RSSetViewports(0, 0); 1843 GalliumD3D11DeviceContext::RSSetScissorRects(0, 0); 1844 } 1845 1846 virtual void STDMETHODCALLTYPE Flush(void) 1847 { 1848 SYNCHRONIZED; 1849 pipe->flush(pipe, 0); 1850 } 1851 1852 /* In Direct3D 10, if the reference count of an object drops to 0, it is automatically 1853 * cleanly unbound from the pipeline. 1854 * In Direct3D 11, the pipeline holds a reference. 1855 * 1856 * Note that instead of always scanning the pipeline on destruction, we could 1857 * maintain the internal reference count on DirectX 10 and use it to check if an 1858 * object is still bound. 1859 * Presumably, on average, scanning is faster if the application is well written. 1860 */ 1861#if API < 11 1862#define IMPLEMENT_SIMPLE_UNBIND(name, member, gallium, def) \ 1863 void Unbind##name(ID3D11##name* state) \ 1864 { \ 1865 SYNCHRONIZED; \ 1866 if((void*)state == (void*)member.p) \ 1867 { \ 1868 member.p = 0; \ 1869 pipe->bind_##gallium##_state(pipe, default_##def); \ 1870 } \ 1871 } 1872 IMPLEMENT_SIMPLE_UNBIND(BlendState, blend_state, blend, blend) 1873 IMPLEMENT_SIMPLE_UNBIND(RasterizerState, rasterizer_state, rasterizer, rasterizer) 1874 IMPLEMENT_SIMPLE_UNBIND(DepthStencilState, depth_stencil_state, depth_stencil_alpha, depth_stencil) 1875 IMPLEMENT_SIMPLE_UNBIND(InputLayout, input_layout, vertex_elements, input_layout) 1876 IMPLEMENT_SIMPLE_UNBIND(PixelShader, shaders[D3D11_STAGE_PS], fs, shaders[D3D11_STAGE_PS]) 1877 IMPLEMENT_SIMPLE_UNBIND(VertexShader, shaders[D3D11_STAGE_VS], vs, shaders[D3D11_STAGE_VS]) 1878 IMPLEMENT_SIMPLE_UNBIND(GeometryShader, shaders[D3D11_STAGE_GS], gs, shaders[D3D11_STAGE_GS]) 1879 1880 void UnbindPredicate(ID3D11Predicate* predicate) 1881 { 1882 SYNCHRONIZED; 1883 if(predicate == render_predicate) 1884 { 1885 render_predicate.p = NULL; 1886 render_predicate_value = 0; 1887 pipe->render_condition(pipe, 0, 0); 1888 } 1889 } 1890 1891 void UnbindSamplerState(ID3D11SamplerState* state) 1892 { 1893 SYNCHRONIZED; 1894 for(unsigned s = 0; s < D3D11_STAGES; ++s) 1895 { 1896 for(unsigned i = 0; i < num_samplers[s]; ++i) 1897 { 1898 if(samplers[s][i] == state) 1899 { 1900 samplers[s][i].p = NULL; 1901 sampler_csos[s][i] = NULL; 1902 update_flags |= (1 << (UPDATE_SAMPLERS_SHIFT + s)); 1903 } 1904 } 1905 } 1906 } 1907 1908 void UnbindBuffer(ID3D11Buffer* buffer) 1909 { 1910 SYNCHRONIZED; 1911 if(buffer == index_buffer) 1912 { 1913 index_buffer.p = 0; 1914 index_format = DXGI_FORMAT_UNKNOWN; 1915 index_offset = 0; 1916 struct pipe_index_buffer ib; 1917 memset(&ib, 0, sizeof(ib)); 1918 pipe->set_index_buffer(pipe, &ib); 1919 } 1920 1921 for(unsigned i = 0; i < num_vertex_buffers; ++i) 1922 { 1923 if(buffer == input_buffers[i]) 1924 { 1925 input_buffers[i].p = 0; 1926 memset(&vertex_buffers[num_vertex_buffers], 0, sizeof(vertex_buffers[num_vertex_buffers])); 1927 update_flags |= UPDATE_VERTEX_BUFFERS; 1928 } 1929 } 1930 1931 for(unsigned s = 0; s < D3D11_STAGES; ++s) 1932 { 1933 for(unsigned i = 0; i < sizeof(constant_buffers) / sizeof(constant_buffers[0]); ++i) 1934 { 1935 if(constant_buffers[s][i] == buffer) 1936 { 1937 constant_buffers[s][i] = (ID3D10Buffer*)NULL; 1938 pipe->set_constant_buffer(pipe, s, i, NULL); 1939 } 1940 } 1941 } 1942 } 1943 1944 void UnbindDepthStencilView(ID3D11DepthStencilView * view) 1945 { 1946 SYNCHRONIZED; 1947 if(view == depth_stencil_view) 1948 { 1949 depth_stencil_view.p = NULL; 1950 set_framebuffer(); 1951 } 1952 } 1953 1954 void UnbindRenderTargetView(ID3D11RenderTargetView* view) 1955 { 1956 SYNCHRONIZED; 1957 bool any_bound = false; 1958 for(unsigned i = 0; i < num_render_target_views; ++i) 1959 { 1960 if(render_target_views[i] == view) 1961 { 1962 render_target_views[i].p = NULL; 1963 any_bound = true; 1964 } 1965 } 1966 if(any_bound) 1967 set_framebuffer(); 1968 } 1969 1970 void UnbindShaderResourceView(ID3D11ShaderResourceView* view) 1971 { 1972 SYNCHRONIZED; 1973 for(unsigned s = 0; s < D3D11_STAGES; ++s) 1974 { 1975 for(unsigned i = 0; i < num_shader_resource_views[s]; ++i) 1976 { 1977 if(shader_resource_views[s][i] == view) 1978 { 1979 shader_resource_views[s][i].p = NULL; 1980 sampler_views[s][i] = NULL; 1981 update_flags |= (1 << (UPDATE_VIEWS_SHIFT + s)); 1982 } 1983 } 1984 } 1985 } 1986#endif 1987 1988#undef SYNCHRONIZED 1989}; 1990 1991#if API >= 11 1992/* This approach serves two purposes. 1993 * First, we don't want to do an atomic operation to manipulate the reference 1994 * count every time something is bound/unbound to the pipeline, since they are 1995 * expensive. 1996 * Fortunately, the immediate context can only be used by a single thread, so 1997 * we don't have to use them, as long as a separate reference count is used 1998 * (see dual_refcnt_t). 1999 * 2000 * Second, we want to avoid the Device -> DeviceContext -> bound DeviceChild -> Device 2001 * garbage cycle. 2002 * To avoid it, DeviceChild doesn't hold a reference to Device as usual, but adds 2003 * one for each external reference count, while internal nonatomic_add_ref doesn't 2004 * add any. 2005 * 2006 * Note that ideally we would to eliminate the non-atomic op too, but this is more 2007 * complicated, since we would either need to use garbage collection and give up 2008 * deterministic destruction (especially bad for large textures), or scan the whole 2009 * pipeline state every time the reference count of object drops to 0, which risks 2010 * pathological slowdowns. 2011 * 2012 * Since this microoptimization should matter relatively little, let's avoid it for now. 2013 * 2014 * Note that deferred contexts don't use this, since as a whole, they must thread-safe. 2015 * Eliminating the atomic ops for deferred contexts seems substantially harder. 2016 * This might be a problem if they are used in a one-shot multithreaded rendering 2017 * fashion, where SMP cacheline bouncing on the reference count may be visible. 2018 * 2019 * The idea would be to attach a structure of reference counts indexed by deferred 2020 * context id to each object. Ideally, this should be organized like ext2 block pointers. 2021 * 2022 * Every deferred context would get a reference count in its own cacheline. 2023 * The external count is protected by a lock bit, and there is also a "lock bit" in each 2024 * internal count. 2025 * 2026 * When the external count has to be dropped to 0, the lock bit is taken and all internal 2027 * reference counts are scanned, taking a count of them. A flag would also be set on them. 2028 * Deferred context manipulation would notice the flag, and update the count. 2029 * Once the count goes to zero, the object is freed. 2030 * 2031 * The problem of this is that if the external reference count ping-pongs between 2032 * zero and non-zero, the scans will take a lot of time. 2033 * 2034 * The idea to solve this is to compute the scans in a binary-tree like fashion, where 2035 * each binary tree node would have a "determined bit", which would be invalidated 2036 * by manipulations. 2037 * 2038 * However, all this complexity might actually be a loss in most cases, so let's just 2039 * stick to a single atomic refcnt for now. 2040 * 2041 * Also, we don't even support deferred contexts yet, so this can wait. 2042 */ 2043struct nonatomic_device_child_ptr_traits 2044{ 2045 static void add_ref(void* p) 2046 { 2047 if(p) 2048 ((GalliumD3D11DeviceChild<>*)p)->nonatomic_add_ref(); 2049 } 2050 2051 static void release(void* p) 2052 { 2053 if(p) 2054 ((GalliumD3D11DeviceChild<>*)p)->nonatomic_release(); 2055 } 2056}; 2057 2058struct GalliumD3D11ImmediateDeviceContext 2059 : public GalliumD3D11DeviceContext<nonatomic_device_child_ptr_traits> 2060{ 2061 GalliumD3D11ImmediateDeviceContext(GalliumD3D11Screen* device, pipe_context* pipe, unsigned context_flags = 0) 2062 : GalliumD3D11DeviceContext<nonatomic_device_child_ptr_traits>(device, pipe, context_flags) 2063 { 2064 // not necessary, but tests that the API at least basically works 2065 ClearState(); 2066 } 2067 2068 /* we do this since otherwise we would have a garbage cycle between this and the device */ 2069 virtual ULONG STDMETHODCALLTYPE AddRef() 2070 { 2071 return this->device->AddRef(); 2072 } 2073 2074 virtual ULONG STDMETHODCALLTYPE Release() 2075 { 2076 return this->device->Release(); 2077 } 2078 2079 virtual D3D11_DEVICE_CONTEXT_TYPE STDMETHODCALLTYPE GetType() 2080 { 2081 return D3D11_DEVICE_CONTEXT_IMMEDIATE; 2082 } 2083}; 2084 2085static ID3D11DeviceContext* GalliumD3D11ImmediateDeviceContext_Create(GalliumD3D11Screen* device, struct pipe_context* pipe, bool owns_pipe) 2086{ 2087 return new GalliumD3D11ImmediateDeviceContext(device, pipe, owns_pipe); 2088} 2089 2090static void GalliumD3D11ImmediateDeviceContext_RestoreGalliumState(ID3D11DeviceContext* context) 2091{ 2092 ((GalliumD3D11ImmediateDeviceContext*)context)->RestoreGalliumState(); 2093} 2094 2095static void GalliumD3D11ImmediateDeviceContext_RestoreGalliumStateBlitOnly(ID3D11DeviceContext* context) 2096{ 2097 ((GalliumD3D11ImmediateDeviceContext*)context)->RestoreGalliumStateBlitOnly(); 2098} 2099 2100static void GalliumD3D11ImmediateDeviceContext_Destroy(ID3D11DeviceContext* context) 2101{ 2102 delete (GalliumD3D11ImmediateDeviceContext*)context; 2103} 2104#endif 2105