Hash :
f7ed7054
Author :
Date :
2014-09-23T13:39:31
Move the code that writes the temporary shader file to the shader compiler. BUG=angle:755 Change-Id: I5fdc3ae112ea3f59694a22da6199b99a568ed0a1 Reviewed-on: https://chromium-review.googlesource.com/219530 Reviewed-by: Brandon Jones <bajones@chromium.org> Tested-by: Geoff Lang <geofflang@chromium.org>
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
//
// Copyright 2014 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "libGLESv2/renderer/d3d/HLSLCompiler.h"
#include "libGLESv2/Program.h"
#include "libGLESv2/main.h"
#include "common/utilities.h"
#include "third_party/trace_event/trace_event.h"
namespace rx
{
CompileConfig::CompileConfig()
: flags(0),
name()
{
}
CompileConfig::CompileConfig(UINT flags, const std::string &name)
: flags(flags),
name(name)
{
}
HLSLCompiler::HLSLCompiler()
: mD3DCompilerModule(NULL),
mD3DCompileFunc(NULL)
{
}
HLSLCompiler::~HLSLCompiler()
{
release();
}
bool HLSLCompiler::initialize()
{
TRACE_EVENT0("gpu", "initializeCompiler");
#if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES)
// Find a D3DCompiler module that had already been loaded based on a predefined list of versions.
static const char *d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES;
for (size_t i = 0; i < ArraySize(d3dCompilerNames); ++i)
{
if (GetModuleHandleExA(0, d3dCompilerNames[i], &mD3DCompilerModule))
{
break;
}
}
#endif // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES
if (!mD3DCompilerModule)
{
// Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with.
mD3DCompilerModule = LoadLibrary(D3DCOMPILER_DLL);
}
if (!mD3DCompilerModule)
{
ERR("No D3D compiler module found - aborting!\n");
return false;
}
mD3DCompileFunc = reinterpret_cast<pD3DCompile>(GetProcAddress(mD3DCompilerModule, "D3DCompile"));
ASSERT(mD3DCompileFunc);
return mD3DCompileFunc != NULL;
}
void HLSLCompiler::release()
{
if (mD3DCompilerModule)
{
FreeLibrary(mD3DCompilerModule);
mD3DCompilerModule = NULL;
mD3DCompileFunc = NULL;
}
}
ID3DBlob *HLSLCompiler::compileToBinary(gl::InfoLog &infoLog, const std::string &hlsl, const std::string &profile,
const std::vector<CompileConfig> &configs) const
{
ASSERT(mD3DCompilerModule && mD3DCompileFunc);
if (gl::perfActive())
{
std::string sourcePath = getTempPath();
std::string sourceText = FormatString("#line 2 \"%s\"\n\n%s", sourcePath.c_str(), hlsl.c_str());
writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
}
for (size_t i = 0; i < configs.size(); ++i)
{
ID3DBlob *errorMessage = NULL;
ID3DBlob *binary = NULL;
HRESULT result = mD3DCompileFunc(hlsl.c_str(), hlsl.length(), gl::g_fakepath, NULL, NULL, "main", profile.c_str(),
configs[i].flags, 0, &binary, &errorMessage);
if (errorMessage)
{
const char *message = (const char*)errorMessage->GetBufferPointer();
infoLog.appendSanitized(message);
TRACE("\n%s", hlsl);
TRACE("\n%s", message);
SafeRelease(errorMessage);
}
if (SUCCEEDED(result))
{
return binary;
}
else
{
if (result == E_OUTOFMEMORY)
{
return gl::error<ID3DBlob*>(GL_OUT_OF_MEMORY, NULL);
}
infoLog.append("Warning: D3D shader compilation failed with %s flags.", configs[i].name.c_str());
if (i + 1 < configs.size())
{
infoLog.append(" Retrying with %s.\n", configs[i + 1].name.c_str());
}
}
}
return NULL;
}
}