blob: 8f208d73b31812825dcbe3b7408e566e8090621e [file] [log] [blame]
Jamie Madillea247592014-08-28 10:37:08 -04001//
2// Copyright 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Langdad5ed32014-02-10 12:59:17 -05007#include "libGLESv2/renderer/d3d/HLSLCompiler.h"
8#include "libGLESv2/Program.h"
9#include "libGLESv2/main.h"
10
11#include "common/utilities.h"
12
13#include "third_party/trace_event/trace_event.h"
14
15namespace rx
16{
17
Geoff Lang3935e512014-09-23 13:15:19 -040018CompileConfig::CompileConfig()
19 : flags(0),
20 name()
21{
22}
23
24CompileConfig::CompileConfig(UINT flags, const std::string &name)
25 : flags(flags),
26 name(name)
27{
28}
29
Geoff Langdad5ed32014-02-10 12:59:17 -050030HLSLCompiler::HLSLCompiler()
31 : mD3DCompilerModule(NULL),
32 mD3DCompileFunc(NULL)
33{
34}
35
36HLSLCompiler::~HLSLCompiler()
37{
38 release();
39}
40
41bool HLSLCompiler::initialize()
42{
43 TRACE_EVENT0("gpu", "initializeCompiler");
44#if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES)
45 // Find a D3DCompiler module that had already been loaded based on a predefined list of versions.
Jamie Madill07d49ef2014-07-25 11:52:38 -040046 static const char *d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES;
Geoff Langdad5ed32014-02-10 12:59:17 -050047
48 for (size_t i = 0; i < ArraySize(d3dCompilerNames); ++i)
49 {
Jamie Madill07d49ef2014-07-25 11:52:38 -040050 if (GetModuleHandleExA(0, d3dCompilerNames[i], &mD3DCompilerModule))
Geoff Langdad5ed32014-02-10 12:59:17 -050051 {
52 break;
53 }
54 }
55#endif // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES
56
57 if (!mD3DCompilerModule)
58 {
59 // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with.
60 mD3DCompilerModule = LoadLibrary(D3DCOMPILER_DLL);
61 }
62
63 if (!mD3DCompilerModule)
64 {
65 ERR("No D3D compiler module found - aborting!\n");
66 return false;
67 }
68
Geoff Lang3935e512014-09-23 13:15:19 -040069 mD3DCompileFunc = reinterpret_cast<pD3DCompile>(GetProcAddress(mD3DCompilerModule, "D3DCompile"));
Geoff Langdad5ed32014-02-10 12:59:17 -050070 ASSERT(mD3DCompileFunc);
71
72 return mD3DCompileFunc != NULL;
73}
74
75void HLSLCompiler::release()
76{
77 if (mD3DCompilerModule)
78 {
79 FreeLibrary(mD3DCompilerModule);
80 mD3DCompilerModule = NULL;
81 mD3DCompileFunc = NULL;
82 }
83}
84
Geoff Lang3935e512014-09-23 13:15:19 -040085ID3DBlob *HLSLCompiler::compileToBinary(gl::InfoLog &infoLog, const std::string &hlsl, const std::string &profile,
86 const std::vector<CompileConfig> &configs) const
Geoff Langdad5ed32014-02-10 12:59:17 -050087{
88 ASSERT(mD3DCompilerModule && mD3DCompileFunc);
89
Geoff Lang3935e512014-09-23 13:15:19 -040090 for (size_t i = 0; i < configs.size(); ++i)
Geoff Langdad5ed32014-02-10 12:59:17 -050091 {
92 ID3DBlob *errorMessage = NULL;
93 ID3DBlob *binary = NULL;
94
Geoff Lang3935e512014-09-23 13:15:19 -040095 HRESULT result = mD3DCompileFunc(hlsl.c_str(), hlsl.length(), gl::g_fakepath, NULL, NULL, "main", profile.c_str(),
96 configs[i].flags, 0, &binary, &errorMessage);
Nicolas Capens93faad92014-05-10 12:14:13 -040097
Geoff Langdad5ed32014-02-10 12:59:17 -050098 if (errorMessage)
99 {
100 const char *message = (const char*)errorMessage->GetBufferPointer();
101
102 infoLog.appendSanitized(message);
103 TRACE("\n%s", hlsl);
104 TRACE("\n%s", message);
105
106 SafeRelease(errorMessage);
107 }
108
109 if (SUCCEEDED(result))
110 {
Geoff Lang3935e512014-09-23 13:15:19 -0400111 return binary;
Geoff Langdad5ed32014-02-10 12:59:17 -0500112 }
113 else
114 {
115 if (result == E_OUTOFMEMORY)
116 {
Geoff Lang3935e512014-09-23 13:15:19 -0400117 return gl::error<ID3DBlob*>(GL_OUT_OF_MEMORY, NULL);
Geoff Langdad5ed32014-02-10 12:59:17 -0500118 }
119
Geoff Lang3935e512014-09-23 13:15:19 -0400120 infoLog.append("Warning: D3D shader compilation failed with %s flags.", configs[i].name.c_str());
Jamie Madill2c976a42014-08-04 11:37:53 -0400121
Geoff Lang3935e512014-09-23 13:15:19 -0400122 if (i + 1 < configs.size())
Geoff Langdad5ed32014-02-10 12:59:17 -0500123 {
Geoff Lang3935e512014-09-23 13:15:19 -0400124 infoLog.append(" Retrying with %s.\n", configs[i + 1].name.c_str());
Geoff Langdad5ed32014-02-10 12:59:17 -0500125 }
126 }
127 }
128
129 return NULL;
130}
131
132}