Basic shader cache.
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
#include <xenia/gpu/d3d11/d3d11_graphics_driver.h>
|
||||
|
||||
#include <xenia/gpu/gpu-private.h>
|
||||
#include <xenia/gpu/xenos/ucode_disassembler.h>
|
||||
#include <xenia/gpu/d3d11/d3d11_shader_cache.h>
|
||||
|
||||
|
||||
using namespace xe;
|
||||
@@ -24,9 +24,12 @@ D3D11GraphicsDriver::D3D11GraphicsDriver(
|
||||
GraphicsDriver(memory) {
|
||||
device_ = device;
|
||||
device_->AddRef();
|
||||
|
||||
shader_cache_ = new D3D11ShaderCache(device_);
|
||||
}
|
||||
|
||||
D3D11GraphicsDriver::~D3D11GraphicsDriver() {
|
||||
delete shader_cache_;
|
||||
device_->Release();
|
||||
}
|
||||
|
||||
@@ -51,29 +54,20 @@ void D3D11GraphicsDriver::SetShader(
|
||||
uint32_t address,
|
||||
uint32_t start,
|
||||
uint32_t length) {
|
||||
// Swap shader words.
|
||||
uint32_t dword_count = length / 4;
|
||||
XEASSERT(dword_count <= 512);
|
||||
if (dword_count > 512) {
|
||||
XELOGGPU("D3D11: ignoring shader %d at %0.8X (%db): too long",
|
||||
type, address, length);
|
||||
return;
|
||||
}
|
||||
// Find or create shader in the cache.
|
||||
uint8_t* p = xe_memory_addr(memory_, address);
|
||||
uint32_t dwords[512] = {0};
|
||||
for (uint32_t n = 0; n < dword_count; n++) {
|
||||
dwords[n] = XEGETUINT32BE(p + n * 4);
|
||||
}
|
||||
Shader* shader = shader_cache_->FindOrCreate(
|
||||
type, p, length);
|
||||
|
||||
// Disassemble.
|
||||
const char* source = DisassembleShader(type, dwords, dword_count);
|
||||
char* source = shader->Disassemble();
|
||||
if (!source) {
|
||||
source = "<failed to disassemble>";
|
||||
}
|
||||
XELOGGPU("D3D11: set shader %d at %0.8X (%db):\n%s",
|
||||
type, address, length, source);
|
||||
if (source) {
|
||||
xe_free((void*)source);
|
||||
xe_free(source);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,8 @@ namespace xe {
|
||||
namespace gpu {
|
||||
namespace d3d11 {
|
||||
|
||||
class D3D11ShaderCache;
|
||||
|
||||
|
||||
class D3D11GraphicsDriver : public GraphicsDriver {
|
||||
public:
|
||||
@@ -43,7 +45,9 @@ public:
|
||||
uint32_t index_count);
|
||||
|
||||
private:
|
||||
ID3D11Device* device_;
|
||||
ID3D11Device* device_;
|
||||
|
||||
D3D11ShaderCache* shader_cache_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -13,9 +13,16 @@
|
||||
using namespace xe;
|
||||
using namespace xe::gpu;
|
||||
using namespace xe::gpu::d3d11;
|
||||
using namespace xe::gpu::xenos;
|
||||
|
||||
|
||||
D3D11Shader::D3D11Shader() {
|
||||
D3D11Shader::D3D11Shader(
|
||||
ID3D11Device* device,
|
||||
XE_GPU_SHADER_TYPE type,
|
||||
const uint8_t* src_ptr, size_t length,
|
||||
uint64_t hash) :
|
||||
Shader(type, src_ptr, length, hash) {
|
||||
// TODO(benvanik): create shader/translate/etc.
|
||||
}
|
||||
|
||||
D3D11Shader::~D3D11Shader() {
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
|
||||
#include <xenia/gpu/shader.h>
|
||||
|
||||
#include <d3d11.h>
|
||||
|
||||
|
||||
namespace xe {
|
||||
namespace gpu {
|
||||
@@ -22,7 +24,11 @@ namespace d3d11 {
|
||||
|
||||
class D3D11Shader : public Shader {
|
||||
public:
|
||||
D3D11Shader();
|
||||
D3D11Shader(
|
||||
ID3D11Device* device,
|
||||
xenos::XE_GPU_SHADER_TYPE type,
|
||||
const uint8_t* src_ptr, size_t length,
|
||||
uint64_t hash);
|
||||
virtual ~D3D11Shader();
|
||||
};
|
||||
|
||||
|
||||
@@ -9,14 +9,30 @@
|
||||
|
||||
#include <xenia/gpu/d3d11/d3d11_shader_cache.h>
|
||||
|
||||
#include <xenia/gpu/d3d11/d3d11_shader.h>
|
||||
|
||||
|
||||
using namespace xe;
|
||||
using namespace xe::gpu;
|
||||
using namespace xe::gpu::d3d11;
|
||||
|
||||
|
||||
D3D11ShaderCache::D3D11ShaderCache() {
|
||||
D3D11ShaderCache::D3D11ShaderCache(ID3D11Device* device) {
|
||||
device_ = device;
|
||||
device_->AddRef();
|
||||
}
|
||||
|
||||
D3D11ShaderCache::~D3D11ShaderCache() {
|
||||
device_->Release();
|
||||
}
|
||||
|
||||
Shader* D3D11ShaderCache::CreateCore(
|
||||
xenos::XE_GPU_SHADER_TYPE type,
|
||||
const uint8_t* src_ptr, size_t length,
|
||||
uint32_t hash) {
|
||||
return new D3D11Shader(
|
||||
device_,
|
||||
type,
|
||||
src_ptr, length,
|
||||
hash);
|
||||
}
|
||||
@@ -14,6 +14,8 @@
|
||||
|
||||
#include <xenia/gpu/shader_cache.h>
|
||||
|
||||
#include <D3D11.h>
|
||||
|
||||
|
||||
namespace xe {
|
||||
namespace gpu {
|
||||
@@ -22,8 +24,17 @@ namespace d3d11 {
|
||||
|
||||
class D3D11ShaderCache : public ShaderCache {
|
||||
public:
|
||||
D3D11ShaderCache();
|
||||
D3D11ShaderCache(ID3D11Device* device);
|
||||
virtual ~D3D11ShaderCache();
|
||||
|
||||
protected:
|
||||
virtual Shader* CreateCore(
|
||||
xenos::XE_GPU_SHADER_TYPE type,
|
||||
const uint8_t* src_ptr, size_t length,
|
||||
uint32_t hash);
|
||||
|
||||
protected:
|
||||
ID3D11Device* device_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
#include <xenia/gpu/nop/nop_graphics_driver.h>
|
||||
|
||||
#include <xenia/gpu/gpu-private.h>
|
||||
#include <xenia/gpu/xenos/ucode_disassembler.h>
|
||||
#include <xenia/gpu/shader_cache.h>
|
||||
|
||||
|
||||
using namespace xe;
|
||||
@@ -21,9 +21,11 @@ using namespace xe::gpu::xenos;
|
||||
|
||||
NopGraphicsDriver::NopGraphicsDriver(xe_memory_ref memory) :
|
||||
GraphicsDriver(memory) {
|
||||
shader_cache_ = new ShaderCache();
|
||||
}
|
||||
|
||||
NopGraphicsDriver::~NopGraphicsDriver() {
|
||||
delete shader_cache_;
|
||||
}
|
||||
|
||||
void NopGraphicsDriver::Initialize() {
|
||||
@@ -47,29 +49,20 @@ void NopGraphicsDriver::SetShader(
|
||||
uint32_t address,
|
||||
uint32_t start,
|
||||
uint32_t length) {
|
||||
// Swap shader words.
|
||||
uint32_t dword_count = length / 4;
|
||||
XEASSERT(dword_count <= 512);
|
||||
if (dword_count > 512) {
|
||||
XELOGGPU("NOP: ignoring shader %d at %0.8X (%db): too long",
|
||||
type, address, length);
|
||||
return;
|
||||
}
|
||||
// Find or create shader in the cache.
|
||||
uint8_t* p = xe_memory_addr(memory_, address);
|
||||
uint32_t dwords[512] = {0};
|
||||
for (uint32_t n = 0; n < dword_count; n++) {
|
||||
dwords[n] = XEGETUINT32BE(p + n * 4);
|
||||
}
|
||||
Shader* shader = shader_cache_->FindOrCreate(
|
||||
type, p, length);
|
||||
|
||||
// Disassemble.
|
||||
const char* source = DisassembleShader(type, dwords, dword_count);
|
||||
char* source = shader->Disassemble();
|
||||
if (!source) {
|
||||
source = "<failed to disassemble>";
|
||||
}
|
||||
XELOGGPU("NOP: set shader %d at %0.8X (%db):\n%s",
|
||||
type, address, length, source);
|
||||
if (source) {
|
||||
xe_free((void*)source);
|
||||
xe_free(source);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,9 @@
|
||||
|
||||
namespace xe {
|
||||
namespace gpu {
|
||||
|
||||
class ShaderCache;
|
||||
|
||||
namespace nop {
|
||||
|
||||
|
||||
@@ -41,6 +44,7 @@ public:
|
||||
uint32_t index_count);
|
||||
|
||||
protected:
|
||||
ShaderCache* shader_cache_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -9,13 +9,35 @@
|
||||
|
||||
#include <xenia/gpu/shader.h>
|
||||
|
||||
#include <xenia/gpu/xenos/ucode_disassembler.h>
|
||||
|
||||
|
||||
using namespace xe;
|
||||
using namespace xe::gpu;
|
||||
using namespace xe::gpu::xenos;
|
||||
|
||||
|
||||
Shader::Shader() {
|
||||
Shader::Shader(
|
||||
XE_GPU_SHADER_TYPE type,
|
||||
const uint8_t* src_ptr, size_t length,
|
||||
uint64_t hash) :
|
||||
type_(type), hash_(hash) {
|
||||
// Verify.
|
||||
dword_count_ = length / 4;
|
||||
XEASSERT(dword_count_ <= 512);
|
||||
|
||||
// Copy bytes and swap.
|
||||
size_t byte_size = dword_count_ * sizeof(uint32_t);
|
||||
dwords_ = (uint32_t*)xe_malloc(byte_size);
|
||||
for (uint32_t n = 0; n < dword_count_; n++) {
|
||||
dwords_[n] = XEGETUINT32BE(src_ptr + n * 4);
|
||||
}
|
||||
}
|
||||
|
||||
Shader::~Shader() {
|
||||
xe_free(dwords_);
|
||||
}
|
||||
|
||||
char* Shader::Disassemble() {
|
||||
return DisassembleShader(type_, dwords_, dword_count_);
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#define XENIA_GPU_SHADER_H_
|
||||
|
||||
#include <xenia/core.h>
|
||||
#include <xenia/gpu/xenos/xenos.h>
|
||||
|
||||
|
||||
namespace xe {
|
||||
@@ -19,8 +20,28 @@ namespace gpu {
|
||||
|
||||
class Shader {
|
||||
public:
|
||||
Shader();
|
||||
Shader(xenos::XE_GPU_SHADER_TYPE type,
|
||||
const uint8_t* src_ptr, size_t length,
|
||||
uint64_t hash);
|
||||
virtual ~Shader();
|
||||
|
||||
xenos::XE_GPU_SHADER_TYPE type() const { return type_; }
|
||||
const uint32_t* dwords() const { return dwords_; }
|
||||
size_t dword_count() const { return dword_count_; }
|
||||
uint64_t hash() const { return hash_; }
|
||||
|
||||
// vfetch formats
|
||||
// sampler formats
|
||||
// constants/registers/etc used
|
||||
|
||||
// NOTE: xe_free() the returned string!
|
||||
char* Disassemble();
|
||||
|
||||
protected:
|
||||
xenos::XE_GPU_SHADER_TYPE type_;
|
||||
uint32_t* dwords_;
|
||||
size_t dword_count_;
|
||||
uint64_t hash_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -9,13 +9,72 @@
|
||||
|
||||
#include <xenia/gpu/shader_cache.h>
|
||||
|
||||
#include <xenia/gpu/shader.h>
|
||||
|
||||
|
||||
using namespace std;
|
||||
using namespace xe;
|
||||
using namespace xe::gpu;
|
||||
using namespace xe::gpu::xenos;
|
||||
|
||||
|
||||
ShaderCache::ShaderCache() {
|
||||
}
|
||||
|
||||
ShaderCache::~ShaderCache() {
|
||||
Clear();
|
||||
}
|
||||
|
||||
Shader* ShaderCache::Create(
|
||||
XE_GPU_SHADER_TYPE type,
|
||||
const uint8_t* src_ptr, size_t length) {
|
||||
uint64_t hash = Hash(src_ptr, length);
|
||||
Shader* shader = CreateCore(type, src_ptr, length, hash);
|
||||
map_.insert(pair<uint64_t, Shader*>(hash, shader));
|
||||
return shader;
|
||||
}
|
||||
|
||||
Shader* ShaderCache::CreateCore(
|
||||
XE_GPU_SHADER_TYPE type,
|
||||
const uint8_t* src_ptr, size_t length,
|
||||
uint64_t hash) {
|
||||
return new Shader(type, src_ptr, length, hash);
|
||||
}
|
||||
|
||||
Shader* ShaderCache::Find(
|
||||
XE_GPU_SHADER_TYPE type,
|
||||
const uint8_t* src_ptr, size_t length) {
|
||||
uint64_t hash = Hash(src_ptr, length);
|
||||
unordered_map<uint64_t, Shader*>::iterator it = map_.find(hash);
|
||||
if (it != map_.end()) {
|
||||
return it->second;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Shader* ShaderCache::FindOrCreate(
|
||||
XE_GPU_SHADER_TYPE type,
|
||||
const uint8_t* src_ptr, size_t length) {
|
||||
uint64_t hash = Hash(src_ptr, length);
|
||||
unordered_map<uint64_t, Shader*>::iterator it = map_.find(hash);
|
||||
if (it != map_.end()) {
|
||||
return it->second;
|
||||
}
|
||||
Shader* shader = CreateCore(type, src_ptr, length, hash);
|
||||
map_.insert(pair<uint64_t, Shader*>(hash, shader));
|
||||
return shader;
|
||||
}
|
||||
|
||||
void ShaderCache::Clear() {
|
||||
// TODO(benvanik): clear.
|
||||
for (unordered_map<uint64_t, Shader*>::iterator it = map_.begin();
|
||||
it != map_.end(); ++it) {
|
||||
Shader* shader = it->second;
|
||||
delete shader;
|
||||
}
|
||||
map_.clear();
|
||||
}
|
||||
|
||||
uint64_t ShaderCache::Hash(const uint8_t* src_ptr, size_t length) {
|
||||
return xe_hash64(src_ptr, length, 0);
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
#define XENIA_GPU_SHADER_CACHE_H_
|
||||
|
||||
#include <xenia/core.h>
|
||||
#include <xenia/gpu/shader.h>
|
||||
#include <xenia/gpu/xenos/xenos.h>
|
||||
|
||||
|
||||
namespace xe {
|
||||
@@ -21,6 +23,29 @@ class ShaderCache {
|
||||
public:
|
||||
ShaderCache();
|
||||
virtual ~ShaderCache();
|
||||
|
||||
Shader* Create(
|
||||
xenos::XE_GPU_SHADER_TYPE type,
|
||||
const uint8_t* src_ptr, size_t length);
|
||||
Shader* Find(
|
||||
xenos::XE_GPU_SHADER_TYPE type,
|
||||
const uint8_t* src_ptr, size_t length);
|
||||
Shader* FindOrCreate(
|
||||
xenos::XE_GPU_SHADER_TYPE type,
|
||||
const uint8_t* src_ptr, size_t length);
|
||||
|
||||
void Clear();
|
||||
|
||||
private:
|
||||
uint64_t Hash(const uint8_t* src_ptr, size_t length);
|
||||
|
||||
std::unordered_map<uint64_t, Shader*> map_;
|
||||
|
||||
protected:
|
||||
virtual Shader* CreateCore(
|
||||
xenos::XE_GPU_SHADER_TYPE type,
|
||||
const uint8_t* src_ptr, size_t length,
|
||||
uint64_t hash);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -716,7 +716,7 @@ void disasm_exec(
|
||||
} // anonymous namespace
|
||||
|
||||
|
||||
const char* xenos::DisassembleShader(
|
||||
char* xenos::DisassembleShader(
|
||||
XE_GPU_SHADER_TYPE type,
|
||||
const uint32_t* dwords, size_t dword_count) {
|
||||
Output* output = new Output();
|
||||
@@ -744,7 +744,7 @@ const char* xenos::DisassembleShader(
|
||||
}
|
||||
}
|
||||
|
||||
const char* result = xestrdupa(output->buffer);
|
||||
char* result = xestrdupa(output->buffer);
|
||||
delete output;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace gpu {
|
||||
namespace xenos {
|
||||
|
||||
|
||||
const char* DisassembleShader(
|
||||
char* DisassembleShader(
|
||||
XE_GPU_SHADER_TYPE type,
|
||||
const uint32_t* dwords, size_t dword_count);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user