Skip to content

Commit

Permalink
Add compiler argument shim
Browse files Browse the repository at this point in the history
  • Loading branch information
bylaws committed Apr 1, 2023
1 parent 19d1998 commit b721d83
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
1 change: 1 addition & 0 deletions tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

blob-patcher.py is a script for generating adrenotools loadable drivers from an extracted ROM zip
qtimapper-shim allows newer drivers to work on devices that lack support for the new mapper HAL
acc-shim allows for suppling arguments to the underlying LLVM-based shader compiler library


12 changes: 12 additions & 0 deletions tools/acc-shim/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Adreno Compiler Collection parameter shim

### TODO
Make some sort of API for this so parameters can be changed through adrenotools in realtime.

### Compilation
```
$ sed -i 's/libllvm-glnext/notreal-glnext/g' libllvm-glnext.so
$ mv libllvm-glnext.so notreal-glnext.so
$ aarch64-linux-android28-clang vk_acc_shim.cpp -o notllvm-glnext.so --shared -fpic
$ sed -i 's/libllvm-glnext/notllvm-glnext/g' vulkan.adreno.so
```
108 changes: 108 additions & 0 deletions tools/acc-shim/vk_acc_shim.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <stdint.h>
#include <dlfcn.h>
#include <string.h>
#include <android/log.h>

thread_local int satisfy_driver_emutls;

// Check libllvm-glnext.so in older drivers for a full list of these, newer ones are slightly more awkward due to the split
enum class OptionType : uint32_t {
ArgsStringList = 1,
QcArgsString = 4,
OptLevelInt = 0x101,
NoOptsInt = 0x10D,
};

struct StringListVal {
uint32_t len;
const char **entry;
};

struct Option
{
OptionType type;
// PAD
union {
bool boolVal;
int intVal;
float floatVal;
StringListVal *strVals;
const char *strVal;
};
};


struct OptionsSet
{
Option *options;
uint32_t num;
};


int CompileHook(void *state, OptionsSet *opts);

decltype(CompileHook) *OrigCompile{};

int CompileHook(void *state, OptionsSet *opts) {
constexpr size_t ReplacementOptsNum = 201;

Option replacementOpts[ReplacementOptsNum];
uint32_t patchedOptNum{};

// Insert your desired args here - results will be written to stderr
const char *argv[] = {"-help", "-help-hidden", "", NULL };
StringListVal listVal{
.len = 3,
.entry = argv,
};

replacementOpts[patchedOptNum++] = {
.type = OptionType::ArgsStringList,
.strVals = &listVal
};

const char *opt ="Verbose=true Quiet=false";
replacementOpts[patchedOptNum++] = {
.type = OptionType::QcArgsString,
.strVal = opt
};

if (opts->num + patchedOptNum > ReplacementOptsNum)
__builtin_trap();

memcpy(&replacementOpts[patchedOptNum], opts->options, sizeof(Option) * opts->num);

OptionsSet optList{
.options = replacementOpts,
.num = opts->num + patchedOptNum
};

return OrigCompile(state, &optList);
}


struct LlvmInterface {
uint32_t unk0[3];
// PAD
void *makeContext;
void *delContext;
decltype(CompileHook) *compile;
void *unk;
};

extern "C" int LoadACC(LlvmInterface *impl);

extern "C" __attribute__((visibility("default"))) int LoadACC(LlvmInterface *impl) {
void *hnd = dlopen("notreal-glnext.so", RTLD_NOW);

auto origLoadACC = reinterpret_cast<decltype(LoadACC) *>(dlsym(hnd, "LoadACC"));
if (!origLoadACC)
__builtin_trap();

int ret = origLoadACC(impl);

OrigCompile = impl->compile;
impl->compile = &CompileHook;

return ret;
}

0 comments on commit b721d83

Please sign in to comment.