2 * Copyright (c) 2007-2010 SlimDX Group
\r
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
5 * of this software and associated documentation files (the "Software"), to deal
\r
6 * in the Software without restriction, including without limitation the rights
\r
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
8 * copies of the Software, and to permit persons to whom the Software is
\r
9 * furnished to do so, subject to the following conditions:
\r
11 * The above copyright notice and this permission notice shall be included in
\r
12 * all copies or substantial portions of the Software.
\r
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
24 #include "../DataStream.h"
\r
26 #include "Direct3D11Exception.h"
\r
28 #include "Buffer11.h"
\r
29 #include "CommandList11.h"
\r
30 #include "Asynchronous11.h"
\r
31 #include "DeviceContext11.h"
\r
32 #include "InputAssemblerWrapper11.h"
\r
33 #include "OutputMergerWrapper11.h"
\r
34 #include "StreamOutputWrapper11.h"
\r
35 #include "RasterizerWrapper11.h"
\r
36 #include "VertexShaderWrapper11.h"
\r
37 #include "PixelShaderWrapper11.h"
\r
38 #include "GeometryShaderWrapper11.h"
\r
39 #include "DomainShaderWrapper11.h"
\r
40 #include "HullShaderWrapper11.h"
\r
41 #include "ComputeShaderWrapper11.h"
\r
42 #include "DepthStencilView11.h"
\r
43 #include "RenderTargetView11.h"
\r
44 #include "UnorderedAccessView11.h"
\r
45 #include "ShaderResourceView11.h"
\r
46 #include "Resource11.h"
\r
47 #include "ResourceRegion11.h"
\r
48 #include "Predicate11.h"
\r
49 #include "Device11.h"
\r
51 using namespace System;
\r
52 using namespace System::Runtime::InteropServices;
\r
56 namespace Direct3D11
\r
58 void DeviceContext::InitializeSubclasses()
\r
60 inputAssembler = gcnew InputAssemblerWrapper( InternalPointer );
\r
61 outputMerger = gcnew OutputMergerWrapper( InternalPointer );
\r
62 streamOutput = gcnew StreamOutputWrapper( InternalPointer );
\r
63 rasterizer = gcnew RasterizerWrapper( InternalPointer );
\r
64 vertexShader = gcnew VertexShaderWrapper( InternalPointer );
\r
65 pixelShader = gcnew PixelShaderWrapper( InternalPointer );
\r
66 geometryShader = gcnew GeometryShaderWrapper( InternalPointer );
\r
67 domainShader = gcnew DomainShaderWrapper( InternalPointer );
\r
68 hullShader = gcnew HullShaderWrapper( InternalPointer );
\r
69 computeShader = gcnew ComputeShaderWrapper( InternalPointer );
\r
72 DeviceContext::DeviceContext( ID3D11DeviceContext* pointer, ComObject^ owner )
\r
74 Construct( pointer, owner );
\r
75 InitializeSubclasses();
\r
78 DeviceContext::DeviceContext( IntPtr pointer )
\r
80 Construct( pointer, NativeInterface );
\r
81 InitializeSubclasses();
\r
84 DeviceContext^ DeviceContext::FromPointer( ID3D11DeviceContext* pointer, ComObject^ owner, ComObjectFlags flags )
\r
86 return ConstructFromPointer<DeviceContext,ID3D11DeviceContext>( pointer, owner, flags );
\r
89 DeviceContext^ DeviceContext::FromPointer( IntPtr pointer )
\r
91 return ConstructFromUserPointer<DeviceContext>( pointer );
\r
94 DeviceContext::DeviceContext( SlimDX::Direct3D11::Device^ device )
\r
96 ID3D11DeviceContext* context;
\r
98 HRESULT hr = device->InternalPointer->CreateDeferredContext( 0, &context );
\r
99 if( RECORD_D3D11( hr ).IsFailure )
\r
100 throw gcnew Direct3D11Exception( Result::Last );
\r
102 Construct( context );
\r
103 InitializeSubclasses();
\r
106 void DeviceContext::Begin( Asynchronous^ data )
\r
108 InternalPointer->Begin( data->InternalPointer );
\r
111 void DeviceContext::ClearDepthStencilView( DepthStencilView^ view, DepthStencilClearFlags flags, float depth, Byte stencil )
\r
113 InternalPointer->ClearDepthStencilView( view->InternalPointer, static_cast<UINT>( flags ), depth, stencil );
\r
116 void DeviceContext::ClearRenderTargetView( RenderTargetView^ view, Color4 color )
\r
118 const float nativeColor[] = { color.Red, color.Green, color.Blue, color.Alpha };
\r
119 InternalPointer->ClearRenderTargetView( view->InternalPointer, nativeColor );
\r
122 void DeviceContext::ClearState()
\r
124 InternalPointer->ClearState();
\r
127 void DeviceContext::ClearUnorderedAccessView( UnorderedAccessView^ unorderedAccessView, array<int>^ values )
\r
129 if( values->Length != 4 )
\r
130 throw gcnew ArgumentException( "Must supply exactly four clear values", "values" );
\r
132 const UINT nativeValues[] = { values[0], values[1], values[2], values[3] };
\r
133 InternalPointer->ClearUnorderedAccessViewUint( unorderedAccessView->InternalPointer, nativeValues );
\r
136 void DeviceContext::ClearUnorderedAccessView( UnorderedAccessView^ unorderedAccessView, array<float>^ values )
\r
138 if( values->Length != 4 )
\r
139 throw gcnew ArgumentException( "Must supply exactly four clear values", "values" );
\r
141 const FLOAT nativeValues[] = { values[0], values[1], values[2], values[3] };
\r
142 InternalPointer->ClearUnorderedAccessViewFloat( unorderedAccessView->InternalPointer, nativeValues );
\r
145 void DeviceContext::CopyResource( Resource^ source, Resource^ destination )
\r
147 InternalPointer->CopyResource( destination->InternalPointer, source->InternalPointer );
\r
150 void DeviceContext::CopyStructureCount( UnorderedAccessView^ unorderedAccessView, Buffer^ destinationBuffer, int destinationOffset )
\r
152 InternalPointer->CopyStructureCount( destinationBuffer->InternalPointer, destinationOffset, unorderedAccessView->InternalPointer );
\r
155 void DeviceContext::CopySubresourceRegion( Resource^ source, int sourceSubresource, ResourceRegion region, Resource^ destination, int destinationSubresource, int x, int y, int z )
\r
157 D3D11_BOX nativeRegion = region.CreateNativeVersion();
\r
158 InternalPointer->CopySubresourceRegion( destination->InternalPointer, destinationSubresource, x, y, z, source->InternalPointer, sourceSubresource, &nativeRegion );
\r
161 void DeviceContext::ResolveSubresource( Resource^ source, int sourceSubresource, Resource^ destination, int destinationSubresource, DXGI::Format format )
\r
163 InternalPointer->ResolveSubresource( destination->InternalPointer, destinationSubresource, source->InternalPointer, sourceSubresource, static_cast<DXGI_FORMAT>( format ) );
\r
166 void DeviceContext::UpdateSubresource( DataBox^ source, Resource^ resource, int subresource )
\r
168 InternalPointer->UpdateSubresource( resource->InternalPointer, static_cast<UINT>( subresource), 0, source->Data->PositionPointer, source->RowPitch,source->SlicePitch);
\r
171 void DeviceContext::UpdateSubresource( DataBox^ source, Resource^ resource, int subresource, ResourceRegion region )
\r
173 D3D11_BOX nativeRegion = region.CreateNativeVersion();
\r
174 InternalPointer->UpdateSubresource( resource->InternalPointer, static_cast<UINT>( subresource), &nativeRegion, source->Data->PositionPointer, source->RowPitch,source->SlicePitch);
\r
177 void DeviceContext::Dispatch( int threadGroupCountX, int threadGroupCountY, int threadGroupCountZ )
\r
179 InternalPointer->Dispatch( threadGroupCountX, threadGroupCountY, threadGroupCountZ );
\r
182 void DeviceContext::DispatchIndirect( Buffer^ arguments, int alignedOffset )
\r
184 InternalPointer->DispatchIndirect( arguments->InternalPointer, alignedOffset );
\r
187 void DeviceContext::Draw( int vertexCount, int startVertexLocation )
\r
189 InternalPointer->Draw( vertexCount, startVertexLocation );
\r
192 void DeviceContext::DrawInstanced( int vertexCountPerInstance, int instanceCount, int startVertexLocation, int startInstanceLocation )
\r
194 InternalPointer->DrawInstanced( vertexCountPerInstance, instanceCount, startVertexLocation, startInstanceLocation );
\r
197 void DeviceContext::DrawInstancedIndirect( Buffer^ arguments, int alignedOffset )
\r
199 InternalPointer->DrawInstancedIndirect( arguments->InternalPointer, alignedOffset );
\r
202 void DeviceContext::DrawIndexed( int indexCount, int startIndexLocation, int baseVertexLocation )
\r
204 InternalPointer->DrawIndexed( indexCount, startIndexLocation, baseVertexLocation );
\r
207 void DeviceContext::DrawIndexedInstanced( int indexCountPerInstance, int instanceCount, int startIndexLocation, int baseVertexLocation, int startInstanceLocation )
\r
209 InternalPointer->DrawIndexedInstanced( indexCountPerInstance, instanceCount, startIndexLocation, baseVertexLocation, startInstanceLocation );
\r
212 void DeviceContext::DrawIndexedInstancedIndirect( Buffer^ arguments, int alignedOffset )
\r
214 InternalPointer->DrawIndexedInstancedIndirect( arguments->InternalPointer, alignedOffset );
\r
217 void DeviceContext::DrawAuto()
\r
219 InternalPointer->DrawAuto();
\r
222 void DeviceContext::End( Asynchronous^ data )
\r
224 InternalPointer->End( data->InternalPointer );
\r
227 void DeviceContext::ExecuteCommandList( CommandList^ commands, bool restoreState )
\r
229 InternalPointer->ExecuteCommandList( commands->InternalPointer, restoreState );
\r
232 CommandList^ DeviceContext::FinishCommandList( bool restoreState )
\r
234 ID3D11CommandList* commands;
\r
236 HRESULT hr = InternalPointer->FinishCommandList( restoreState, &commands );
\r
237 if( RECORD_D3D11( hr ).IsFailure )
\r
240 return CommandList::FromPointer( commands );
\r
243 void DeviceContext::Flush()
\r
245 InternalPointer->Flush();
\r
248 void DeviceContext::GenerateMips( ShaderResourceView^ view )
\r
250 InternalPointer->GenerateMips( view->InternalPointer );
\r
253 float DeviceContext::GetMinimumLod( Resource^ resource )
\r
255 return InternalPointer->GetResourceMinLOD( resource->InternalPointer );
\r
258 void DeviceContext::SetMinimumLod( Resource^ resource, float minimumLod )
\r
260 InternalPointer->SetResourceMinLOD( resource->InternalPointer, minimumLod );
\r
263 DataStream^ DeviceContext::GetData( Asynchronous^ data )
\r
265 return GetData( data, AsynchronousFlags::None );
\r
268 DataStream^ DeviceContext::GetData( Asynchronous^ data, AsynchronousFlags flags )
\r
270 int size = data->InternalPointer->GetDataSize();
\r
271 DataStream^ result = gcnew DataStream( size, true, true );
\r
273 if( RECORD_D3D11( InternalPointer->GetData( data->InternalPointer, result->PositionPointer, size, static_cast<UINT>( flags ) ) ).IsFailure )
\r
278 // disables bogus warning:
\r
279 // SlimDX::Direct3D11::DeviceContext::GetData<T>' : recursive on
\r
280 // all control paths, function will cause runtime stack overflow
\r
281 #pragma warning(disable:4717)
\r
282 generic<typename T> where T : value class
\r
283 T DeviceContext::GetData( Asynchronous^ data )
\r
285 return GetData<T>( data, AsynchronousFlags::None );
\r
288 generic<typename T> where T : value class
\r
289 T DeviceContext::GetData( Asynchronous^ data, AsynchronousFlags flags )
\r
291 int size = Marshal::SizeOf(T::typeid);
\r
294 if( RECORD_D3D11( InternalPointer->GetData( data->InternalPointer, &result, size, static_cast<UINT>( flags ) ) ).IsFailure )
\r
299 #pragma warning(default:4717)
\r
301 void DeviceContext::GetPredication( [Out] Predicate^ %predicate, bool %predicateValue )
\r
303 ID3D11Predicate* pointer;
\r
306 InternalPointer->GetPredication( &pointer, &value );
\r
308 predicate = Predicate::FromPointer( pointer );
\r
309 predicateValue = value > 0;
\r
312 void DeviceContext::SetPredication( Predicate^ predicate, bool predicateValue )
\r
314 InternalPointer->SetPredication( predicate->InternalPointer, predicateValue );
\r
317 DataBox^ DeviceContext::MapSubresource( Resource^ resource, int subresource, int sizeInBytes, MapMode mode, MapFlags flags )
\r
319 D3D11_MAPPED_SUBRESOURCE mapped;
\r
320 HRESULT hr = InternalPointer->Map( resource->InternalPointer, subresource, static_cast<D3D11_MAP>( mode ), static_cast<UINT>( flags ), &mapped );
\r
321 if( RECORD_D3D11( hr ).IsFailure )
\r
324 DataBox^ box = gcnew DataBox( mapped.RowPitch, mapped.DepthPitch, gcnew DataStream( mapped.pData, sizeInBytes, true, true, false ) );
\r
328 void DeviceContext::UnmapSubresource( Resource^ resource, int subresource )
\r
330 InternalPointer->Unmap( resource->InternalPointer, subresource );
\r
333 DeviceContextType DeviceContext::Type::get()
\r
335 return static_cast<DeviceContextType>( InternalPointer->GetType() );
\r
338 InputAssemblerWrapper^ DeviceContext::InputAssembler::get()
\r
340 return inputAssembler;
\r
343 OutputMergerWrapper^ DeviceContext::OutputMerger::get()
\r
345 return outputMerger;
\r
348 StreamOutputWrapper^ DeviceContext::StreamOutput::get()
\r
350 return streamOutput;
\r
353 RasterizerWrapper^ DeviceContext::Rasterizer::get()
\r
358 VertexShaderWrapper^ DeviceContext::VertexShader::get()
\r
360 return vertexShader;
\r
363 PixelShaderWrapper^ DeviceContext::PixelShader::get()
\r
365 return pixelShader;
\r
368 GeometryShaderWrapper^ DeviceContext::GeometryShader::get()
\r
370 return geometryShader;
\r
373 DomainShaderWrapper^ DeviceContext::DomainShader::get()
\r
375 return domainShader;
\r
378 HullShaderWrapper^ DeviceContext::HullShader::get()
\r
383 ComputeShaderWrapper^ DeviceContext::ComputeShader::get()
\r
385 return computeShader;
\r