1 /* RetroArch - A frontend for libretro.
2 * Copyright (C) 2014-2018 - Ali Bouhlel
3 *
4 * RetroArch is free software: you can redistribute it and/or modify it under the terms
5 * of the GNU General Public License as published by the Free Software Found-
6 * ation, either version 3 of the License, or (at your option) any later version.
7 *
8 * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 * PURPOSE. See the GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License along with RetroArch.
13 * If not, see <http://www.gnu.org/licenses/>.
14 */
15
16 #define CINTERFACE
17
18 #include <string.h>
19
20 #include "d3d11_common.h"
21 #include "d3dcompiler_common.h"
22
23 #if defined(HAVE_DYNAMIC) && !defined(__WINRT__)
24 #include <dynamic/dylib.h>
25
D3D11CreateDevice(IDXGIAdapter * pAdapter,D3D_DRIVER_TYPE DriverType,HMODULE Software,UINT Flags,CONST D3D_FEATURE_LEVEL * pFeatureLevels,UINT FeatureLevels,UINT SDKVersion,ID3D11Device ** ppDevice,D3D_FEATURE_LEVEL * pFeatureLevel,ID3D11DeviceContext ** ppImmediateContext)26 HRESULT WINAPI D3D11CreateDevice(
27 IDXGIAdapter* pAdapter,
28 D3D_DRIVER_TYPE DriverType,
29 HMODULE Software,
30 UINT Flags,
31 CONST D3D_FEATURE_LEVEL* pFeatureLevels,
32 UINT FeatureLevels,
33 UINT SDKVersion,
34 ID3D11Device** ppDevice,
35 D3D_FEATURE_LEVEL* pFeatureLevel,
36 ID3D11DeviceContext** ppImmediateContext)
37 {
38 static dylib_t d3d11_dll;
39 static PFN_D3D11_CREATE_DEVICE fp;
40
41 if (!d3d11_dll)
42 d3d11_dll = dylib_load("d3d11.dll");
43
44 if (!d3d11_dll)
45 return TYPE_E_CANTLOADLIBRARY;
46
47 if (!fp)
48 fp = (PFN_D3D11_CREATE_DEVICE)dylib_proc(
49 d3d11_dll, "D3D11CreateDevice");
50
51 if (!fp)
52 return TYPE_E_DLLFUNCTIONNOTFOUND;
53
54 return fp(
55 pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels, SDKVersion,
56 ppDevice, pFeatureLevel, ppImmediateContext);
57 }
58
D3D11CreateDeviceAndSwapChain(IDXGIAdapter * pAdapter,D3D_DRIVER_TYPE DriverType,HMODULE Software,UINT Flags,CONST D3D_FEATURE_LEVEL * pFeatureLevels,UINT FeatureLevels,UINT SDKVersion,CONST DXGI_SWAP_CHAIN_DESC * pSwapChainDesc,IDXGISwapChain ** ppSwapChain,ID3D11Device ** ppDevice,D3D_FEATURE_LEVEL * pFeatureLevel,ID3D11DeviceContext ** ppImmediateContext)59 HRESULT WINAPI D3D11CreateDeviceAndSwapChain(
60 IDXGIAdapter* pAdapter,
61 D3D_DRIVER_TYPE DriverType,
62 HMODULE Software,
63 UINT Flags,
64 CONST D3D_FEATURE_LEVEL* pFeatureLevels,
65 UINT FeatureLevels,
66 UINT SDKVersion,
67 CONST DXGI_SWAP_CHAIN_DESC* pSwapChainDesc,
68 IDXGISwapChain** ppSwapChain,
69 ID3D11Device** ppDevice,
70 D3D_FEATURE_LEVEL* pFeatureLevel,
71 ID3D11DeviceContext** ppImmediateContext)
72 {
73 static dylib_t d3d11_dll;
74 static PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN fp;
75
76 if (!d3d11_dll)
77 d3d11_dll = dylib_load("d3d11.dll");
78
79 if (!d3d11_dll)
80 return TYPE_E_CANTLOADLIBRARY;
81
82 if (!fp)
83 fp = (PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN)dylib_proc(
84 d3d11_dll, "D3D11CreateDeviceAndSwapChain");
85
86 if (!fp)
87 return TYPE_E_DLLFUNCTIONNOTFOUND;
88
89 return fp(
90 pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels, SDKVersion,
91 pSwapChainDesc, ppSwapChain, ppDevice, pFeatureLevel, ppImmediateContext);
92 }
93 #endif
94
d3d11_init_texture(D3D11Device device,d3d11_texture_t * texture)95 void d3d11_init_texture(D3D11Device device, d3d11_texture_t* texture)
96 {
97 bool is_render_target = texture->desc.BindFlags & D3D11_BIND_RENDER_TARGET;
98 UINT format_support = D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_SHADER_SAMPLE;
99
100 d3d11_release_texture(texture);
101
102 texture->desc.MipLevels = 1;
103 texture->desc.ArraySize = 1;
104 texture->desc.SampleDesc.Count = 1;
105 texture->desc.SampleDesc.Quality = 0;
106 texture->desc.BindFlags |= D3D11_BIND_SHADER_RESOURCE;
107 texture->desc.CPUAccessFlags =
108 texture->desc.Usage == D3D11_USAGE_DYNAMIC ? D3D11_CPU_ACCESS_WRITE : 0;
109
110 if (texture->desc.MiscFlags & D3D11_RESOURCE_MISC_GENERATE_MIPS)
111 {
112 unsigned width, height;
113
114 texture->desc.BindFlags |= D3D11_BIND_RENDER_TARGET;
115 width = texture->desc.Width >> 5;
116 height = texture->desc.Height >> 5;
117
118 while (width && height)
119 {
120 width >>= 1;
121 height >>= 1;
122 texture->desc.MipLevels++;
123 }
124 }
125
126 if (texture->desc.BindFlags & D3D11_BIND_RENDER_TARGET)
127 format_support |= D3D11_FORMAT_SUPPORT_RENDER_TARGET;
128
129 texture->desc.Format = d3d11_get_closest_match(device, texture->desc.Format, format_support);
130
131 D3D11CreateTexture2D(device, &texture->desc, NULL, &texture->handle);
132
133 {
134 D3D11_SHADER_RESOURCE_VIEW_DESC view_desc = { DXGI_FORMAT_UNKNOWN };
135 view_desc.Format = texture->desc.Format;
136 view_desc.ViewDimension = D3D_SRV_DIMENSION_TEXTURE2D;
137 view_desc.Texture2D.MostDetailedMip = 0;
138 view_desc.Texture2D.MipLevels = -1;
139 D3D11CreateTexture2DShaderResourceView(device, texture->handle, &view_desc, &texture->view);
140 }
141
142 if (is_render_target)
143 D3D11CreateTexture2DRenderTargetView(device, texture->handle, NULL, &texture->rt_view);
144 else
145 {
146 D3D11_TEXTURE2D_DESC desc = texture->desc;
147 desc.MipLevels = 1;
148 desc.BindFlags = 0;
149 desc.MiscFlags = 0;
150 desc.Usage = D3D11_USAGE_STAGING;
151 desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
152 D3D11CreateTexture2D(device, &desc, NULL, &texture->staging);
153 }
154
155 texture->size_data.x = texture->desc.Width;
156 texture->size_data.y = texture->desc.Height;
157 texture->size_data.z = 1.0f / texture->desc.Width;
158 texture->size_data.w = 1.0f / texture->desc.Height;
159 }
160
d3d11_update_texture(D3D11DeviceContext ctx,unsigned width,unsigned height,unsigned pitch,DXGI_FORMAT format,const void * data,d3d11_texture_t * texture)161 void d3d11_update_texture(
162 D3D11DeviceContext ctx,
163 unsigned width,
164 unsigned height,
165 unsigned pitch,
166 DXGI_FORMAT format,
167 const void* data,
168 d3d11_texture_t* texture)
169 {
170 D3D11_MAPPED_SUBRESOURCE mapped_texture;
171 D3D11_BOX frame_box = { 0, 0, 0, width, height, 1 };
172
173 if (!texture || !texture->staging)
174 return;
175
176 D3D11MapTexture2D(ctx, texture->staging,
177 0, D3D11_MAP_WRITE, 0, &mapped_texture);
178
179 #if 0
180 conv_rgb565_argb8888(mapped_texture.pData, data, width, height,
181 mapped_texture.RowPitch, pitch);
182 #else
183 dxgi_copy(
184 width, height, format, pitch, data,
185 texture->desc.Format, mapped_texture.RowPitch,
186 mapped_texture.pData);
187 #endif
188
189 D3D11UnmapTexture2D(ctx, texture->staging, 0);
190
191 D3D11CopyTexture2DSubresourceRegion(
192 ctx, texture->handle, 0, 0, 0, 0, texture->staging, 0, &frame_box);
193
194 if (texture->desc.MiscFlags & D3D11_RESOURCE_MISC_GENERATE_MIPS)
195 D3D11GenerateMips(ctx, texture->view);
196 }
197
198 DXGI_FORMAT
d3d11_get_closest_match(D3D11Device device,DXGI_FORMAT desired_format,UINT desired_format_support)199 d3d11_get_closest_match(D3D11Device device, DXGI_FORMAT desired_format, UINT desired_format_support)
200 {
201 DXGI_FORMAT default_list[] = {desired_format, DXGI_FORMAT_UNKNOWN};
202 DXGI_FORMAT* format = dxgi_get_format_fallback_list(desired_format);
203
204 if(!format)
205 format = default_list;
206
207 while (*format != DXGI_FORMAT_UNKNOWN)
208 {
209 UINT format_support;
210 if (SUCCEEDED(D3D11CheckFormatSupport(device, *format, &format_support)) &&
211 ((format_support & desired_format_support) == desired_format_support))
212 break;
213 format++;
214 }
215 assert(*format);
216 return *format;
217 }
218
d3d11_init_shader(D3D11Device device,const char * src,size_t size,const void * src_name,LPCSTR vs_entry,LPCSTR ps_entry,LPCSTR gs_entry,const D3D11_INPUT_ELEMENT_DESC * input_element_descs,UINT num_elements,d3d11_shader_t * out)219 bool d3d11_init_shader(
220 D3D11Device device,
221 const char* src,
222 size_t size,
223 const void* src_name,
224 LPCSTR vs_entry,
225 LPCSTR ps_entry,
226 LPCSTR gs_entry,
227 const D3D11_INPUT_ELEMENT_DESC* input_element_descs,
228 UINT num_elements,
229 d3d11_shader_t* out)
230 {
231 D3DBlob vs_code = NULL;
232 D3DBlob ps_code = NULL;
233 D3DBlob gs_code = NULL;
234
235 bool success = true;
236
237 if (!src) /* LPCWSTR filename */
238 {
239 if (vs_entry && !d3d_compile_from_file((LPCWSTR)src_name, vs_entry, "vs_4_0", &vs_code))
240 success = false;
241 if (ps_entry && !d3d_compile_from_file((LPCWSTR)src_name, ps_entry, "ps_4_0", &ps_code))
242 success = false;
243 if (gs_entry && !d3d_compile_from_file((LPCWSTR)src_name, gs_entry, "gs_4_0", &gs_code))
244 success = false;
245 }
246 else /* char array */
247 {
248 if (vs_entry && !d3d_compile(src, size, (LPCSTR)src_name, vs_entry, "vs_4_0", &vs_code))
249 success = false;
250 if (ps_entry && !d3d_compile(src, size, (LPCSTR)src_name, ps_entry, "ps_4_0", &ps_code))
251 success = false;
252 if (gs_entry && !d3d_compile(src, size, (LPCSTR)src_name, gs_entry, "gs_4_0", &gs_code))
253 success = false;
254 }
255
256 if (vs_code)
257 D3D11CreateVertexShader(
258 device, D3DGetBufferPointer(vs_code), D3DGetBufferSize(vs_code), NULL, &out->vs);
259
260 if (ps_code)
261 D3D11CreatePixelShader(
262 device, D3DGetBufferPointer(ps_code), D3DGetBufferSize(ps_code), NULL, &out->ps);
263
264 if (gs_code)
265 D3D11CreateGeometryShader(
266 device, D3DGetBufferPointer(gs_code), D3DGetBufferSize(gs_code), NULL, &out->gs);
267
268 if (vs_code && input_element_descs)
269 D3D11CreateInputLayout(
270 device, input_element_descs, num_elements, D3DGetBufferPointer(vs_code),
271 D3DGetBufferSize(vs_code), &out->layout);
272
273 Release(vs_code);
274 Release(ps_code);
275 Release(gs_code);
276
277 return success;
278 }
279