This repository has been archived by the owner on Nov 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResources.pas
194 lines (165 loc) · 6.55 KB
/
Resources.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
(*
* Copyright (c) 2017 Yuriy Kotsarenko. All rights reserved.
* This software is subject to The MIT License.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*)
unit Resources;
interface
{$SCOPEDENUMS ON}
uses
Winapi.D3D11, System.SysUtils;
type
// Type of Direct3D context.
TContextType = (
// Hardware-accelerated context.
Hardware,
// Software rasterization using WARP device.
Software,
// Reference implementation (very slow).
Reference);
// Context-related exception.
ContextException = class(Exception);
// Context that has interfaces to important Direct3D interfaces.
TContext = record
private
FDevice: ID3D11Device;
FImmediateContext: ID3D11DeviceContext;
class function TryCreate(out Context: TContext; const ContextType: TContextType;
const DebugMode: Boolean): HResult; static;
public
// Creates Direct3D device and its immediate context for the specified context type and debug mode.
class function Create(const ContextType: TContextType;
const DebugMode: Boolean = False): TContext; overload; static;
{ In an automatic fashion, attempts to create hardware-accelerated context and if that fails, a WARP device, and
finally, a reference device, if any other option fails. Debug mode is enabled when compiled for Debug target.
When "TryWARPFirst" is set to True, the function tries creating WARP device before hardware-accelerated context. }
class function Create(const TryWARPFirst: Boolean = False): TContext; overload; static;
// Releases contained interfaces.
procedure Free;
// Creates Compute shader from external file.
function CreateShaderFromFile(const FileName: string): ID3D11ComputeShader;
// Reference to Direct3D 11 device.
property Device: ID3D11Device read FDevice;
// Reference to Direct3D 11 immediate context.
property ImmediateContext: ID3D11DeviceContext read FImmediateContext;
end;
// Calculates number of milliseconds that passed since application startup.
function GetStartTickCount: Cardinal;
implementation
uses
Winapi.Windows, Winapi.D3DCommon, Winapi.D3D11_1, System.Classes;
var
PerfFrequency: Int64 = 0;
PerfCounterStart: Int64 = 0;
function GetStartTickCount: Cardinal;
var
Counter: Int64;
begin
if PerfFrequency = 0 then
begin
QueryPerformanceFrequency(PerfFrequency);
QueryPerformanceCounter(PerfCounterStart);
end;
QueryPerformanceCounter(Counter);
Result := ((Counter - PerfCounterStart) * 1000) div PerfFrequency;
end;
class function TContext.TryCreate(out Context: TContext; const ContextType: TContextType;
const DebugMode: Boolean): HResult;
const
FeatureLevels: array[0..1] of D3D_FEATURE_LEVEL = (D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0);
DriverTypes: array[TContextType] of D3D_DRIVER_TYPE = (D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE);
var
DeviceCreationFlags: Cardinal;
begin
DeviceCreationFlags := 0;
if DebugMode then
DeviceCreationFlags := DeviceCreationFlags or Cardinal(D3D11_CREATE_DEVICE_DEBUG);
Result := D3D11CreateDevice(nil, DriverTypes[ContextType], 0, DeviceCreationFlags, @FeatureLevels[0], 2,
D3D11_SDK_VERSION, Context.FDevice, PCardinal(nil)^, Context.FImmediateContext);
end;
class function TContext.Create(const ContextType: TContextType; const DebugMode: Boolean): TContext;
var
Res: HResult;
begin
Res := TryCreate(Result, ContextType, DebugMode);
if Failed(Res) then
raise ContextException.Create(SysErrorMessage(Res));
end;
class function TContext.Create(const TryWARPFirst: Boolean): TContext;
const
DebugMode: Boolean = {$IFDEF DEBUG} True {$ELSE} False {$ENDIF};
var
Res, SecondRes: HResult;
begin
if not TryWARPFirst then
begin // Attempt to create hardware-accelerated Direct3D 11.x device and if such fails, try WARP device instead.
Res := TryCreate(Result, TContextType.Hardware, DebugMode);
if Failed(Res) then
begin
SecondRes := TryCreate(Result, TContextType.Software, DebugMode);
if Succeeded(SecondRes) then
Res := SecondRes;
end;
end
else
begin // Attempt to create WARP Direct3D 11.x device first and if this fails, try hardware-accelerated one.
Res := TryCreate(Result, TContextType.Software, DebugMode);
if Failed(Res) then
begin
SecondRes := TryCreate(Result, TContextType.Hardware, DebugMode);
if Succeeded(SecondRes) then
Res := SecondRes;
end;
end;
// If device creation failed, try creating a reference device.
if Failed(Res) then
begin
SecondRes := TryCreate(Result, TContextType.Reference, DebugMode);
if Succeeded(SecondRes) then
Res := SecondRes;
end;
if Failed(Res) then
raise ContextException.Create(SysErrorMessage(Res));
end;
procedure TContext.Free;
begin
FImmediateContext := nil;
FDevice := nil;
end;
function TContext.CreateShaderFromFile(const FileName: string): ID3D11ComputeShader;
var
MemStream: TMemoryStream;
FileStream: TFileStream;
Res: HResult;
begin
MemStream := TMemoryStream.Create;
try
FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
MemStream.LoadFromStream(FileStream);
finally
FileStream.Free;
end;
Res := FDevice.CreateComputeShader(MemStream.Memory, MemStream.Size, nil, Result);
finally
MemStream.Free;
end;
if Failed(Res) then
raise ContextException.Create(SysErrorMessage(Res));
end;
end.