Skip to content

Commit

Permalink
Make Dawntrail geometry rendering work
Browse files Browse the repository at this point in the history
The biggest change is making the model data read from vertex streams,
like how the retail client is doing. The vertex bindings/locations
should be fixed, resilient for the future and work across more shader
packages.
  • Loading branch information
redstrate committed Nov 3, 2024
1 parent a3f3437 commit f63dcbe
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 105 deletions.
2 changes: 1 addition & 1 deletion extern/libphysis
Submodule libphysis updated 2 files
+1 −1 Cargo.lock
+39 −1 src/model.rs
5 changes: 4 additions & 1 deletion renderer/include/drawobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
struct RenderPart {
size_t numIndices;

Buffer vertexBuffer, indexBuffer;
Buffer vertexBuffer; // Only used in the simple renderer
Buffer indexBuffer;
std::vector<Buffer> streamBuffer; // Only used in the game renderer

int materialIndex = 0;
physis_Part originalPart;
};

enum class MaterialType { Object, Skin };
Expand Down
6 changes: 4 additions & 2 deletions renderer/include/gamerenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ class GameRenderer : public BaseRenderer
std::string_view passName,
physis_Shader &vertexShader,
physis_Shader &pixelShader,
std::string_view shaderName);
std::string_view shaderName,
const physis_MDL *mdl,
const physis_Part *part);

void createImageResources();

Expand Down Expand Up @@ -117,4 +119,4 @@ class GameRenderer : public BaseRenderer

// Dawntrail changes part of the rendering system
bool m_dawntrailMode = false;
};
};
5 changes: 3 additions & 2 deletions renderer/include/shadermanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ class ShaderManager
public:
explicit ShaderManager(Device &device);

spirv_cross::CompilerGLSL getShaderModuleResources(const physis_Shader &shader);
spirv_cross::CompilerGLSL getShaderModuleTest(const physis_Shader &shader);
std::string getShaderModuleResources(const physis_Shader &shader, int i);
VkShaderModule convertShaderModule(const physis_Shader &shader, spv::ExecutionModel executionModel);

private:
std::vector<uint32_t> compileGLSL(std::string_view sourceString, ShaderStage stage);

Device &m_device;
};
};
9 changes: 8 additions & 1 deletion renderer/include/shaderstructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@

// Structure definitions from https://github.com/Shaderlayan/Ouroboros
struct CameraParameter {
glm::mat3x4 m_ViewMatrix; // TODO: does it need alignment?
glm::mat3x4 m_InverseViewMatrix;
glm::mat3x4 m_ViewMatrix; // TODO: does it need alignment?
glm::mat4 m_ViewProjectionMatrix;
glm::mat4 m_InverseViewProjectionMatrix;
glm::mat4 m_InverseProjectionMatrix; // used for view position recalc
glm::mat4 m_ProjectionMatrix; // FIXME: ourburos is wrong, this is actually viewProjection
glm::mat4 m_MainViewToProjectionMatrix;
glm::vec4 m_EyePosition;
glm::vec4 m_LookAtVector;
glm::vec4 m_unknown1[12];
glm::mat4 m_unknownMatrix; // used in a vertex shader in characterlegacy.shpk
glm::vec4 m_unknown2[11];
// Dawntrail: something accesses the z and w of this
glm::vec4 m_unknown3;
};

const int JOINT_MATRIX_SIZE_ARR = 64;
Expand All @@ -29,6 +34,8 @@ struct InstanceParameterStruct {
glm::vec4 m_EnvParameter;
CameraLight m_CameraLight;
glm::vec4 m_Wetness;
// New in Dawntrail
glm::vec4 unknown[5];
};

struct InstanceParameter {
Expand Down
245 changes: 171 additions & 74 deletions renderer/src/gamerenderer.cpp

Large diffs are not rendered by default.

20 changes: 17 additions & 3 deletions renderer/src/rendermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "simplerenderer.h"
#include "swapchain.h"

#include <magic_enum/include/magic_enum.hpp>

VkResult CreateDebugUtilsMessengerEXT(VkInstance instance,
const VkDebugUtilsMessengerCreateInfoEXT *pCreateInfo,
const VkAllocationCallbacks *pAllocator,
Expand Down Expand Up @@ -516,11 +518,23 @@ void RenderManager::reloadDrawObject(DrawObject &DrawObject, uint32_t lod)

const physis_Part part = DrawObject.model.lods[lod].parts[i];

renderPart.originalPart = part;
renderPart.materialIndex = part.material_index;

size_t vertexSize = part.num_vertices * sizeof(Vertex);
renderPart.vertexBuffer = m_device->createBuffer(vertexSize, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
m_device->copyToBuffer(renderPart.vertexBuffer, (void *)part.vertices, vertexSize);
if (qgetenv("NOVUS_USE_NEW_RENDERER") == QByteArrayLiteral("1")) {
renderPart.streamBuffer.resize(DrawObject.model.lods[lod].num_vertex_elements);
for (uint32_t j = 0; j < part.num_streams; j++) {
size_t size = part.stream_sizes[j];
auto buffer = m_device->createBuffer(size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
m_device->copyToBuffer(buffer, (void *)part.streams[j], size);

renderPart.streamBuffer[j] = buffer;
}
} else {
size_t vertexSize = part.num_vertices * sizeof(Vertex);
renderPart.vertexBuffer = m_device->createBuffer(vertexSize, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
m_device->copyToBuffer(renderPart.vertexBuffer, (void *)part.vertices, vertexSize);
}

size_t indexSize = part.num_indices * sizeof(uint16_t);
renderPart.indexBuffer = m_device->createBuffer(indexSize, VK_BUFFER_USAGE_INDEX_BUFFER_BIT);
Expand Down
45 changes: 24 additions & 21 deletions renderer/src/shadermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

#include "device.h"

#include <ranges>

ShaderManager::ShaderManager(Device &device)
: m_device(device)
{
}

spirv_cross::CompilerGLSL ShaderManager::getShaderModuleResources(const physis_Shader &shader)
spirv_cross::CompilerGLSL ShaderManager::getShaderModuleTest(const physis_Shader &shader)
{
dxvk::DxbcReader reader(reinterpret_cast<const char *>(shader.bytecode), shader.len);

Expand All @@ -34,6 +36,20 @@ spirv_cross::CompilerGLSL ShaderManager::getShaderModuleResources(const physis_S
return {result.code.data(), result.code.dwords()};
}

std::string ShaderManager::getShaderModuleResources(const physis_Shader &shader, int i)
{
dxvk::DxbcReader reader(reinterpret_cast<const char *>(shader.bytecode), shader.len);

dxvk::DxbcModule module(reader);

dxvk::DxbcModuleInfo info;
auto result = module.compile(info, "test");

std::ranges::subrange entries(module.isgn()->begin(), module.isgn()->end());

return module.isgn()->findByRegister(i)->semanticName;
}

VkShaderModule ShaderManager::convertShaderModule(const physis_Shader &shader, spv::ExecutionModel executionModel)
{
dxvk::DxbcReader reader(reinterpret_cast<const char *>(shader.bytecode), shader.len);
Expand All @@ -52,25 +68,12 @@ VkShaderModule ShaderManager::convertShaderModule(const physis_Shader &shader, s

auto resources = glsl.get_shader_resources();

std::ranges::subrange entries(module.isgn()->begin(), module.isgn()->end());

int i = 0;
for (auto texture : resources.stage_inputs) {
if (texture.name == "v0") {
glsl.set_name(texture.id, "Position");
} else if (texture.name == "v1") {
glsl.set_name(texture.id, "Color");
} else if (texture.name == "v2") {
glsl.set_name(texture.id, "Normal");
} else if (texture.name == "v3") {
glsl.set_name(texture.id, "TexCoord");
} else if (texture.name == "v4") {
glsl.set_name(texture.id, "Tangent");
} else if (texture.name == "v5") {
glsl.set_name(texture.id, "Bitangent");
} else if (texture.name == "v6") {
glsl.set_name(texture.id, "BoneWeight");
} else if (texture.name == "v7") {
glsl.set_name(texture.id, "BoneId");
}
unsigned binding = glsl.get_decoration(texture.id, spv::DecorationLocation);
glsl.set_name(texture.id, module.isgn()->findByRegister(binding)->semanticName);
i++;
}

Expand Down Expand Up @@ -141,14 +144,14 @@ std::vector<uint32_t> ShaderManager::compileGLSL(const std::string_view sourceSt
shader.setEnvClient(glslang::EShClientVulkan, glslang::EShTargetVulkan_1_3);
shader.setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_6);

if (!shader.parse(GetDefaultResources(), 100, false, EShMsgDefault)) {
if (!shader.parse(GetDefaultResources(), 100, false, EShMsgDebugInfo)) {
return {};
}

glslang::TProgram Program;
Program.addShader(&shader);

if (!Program.link(EShMsgDefault)) {
if (!Program.link(EShMsgDebugInfo)) {
return {};
}

Expand All @@ -168,4 +171,4 @@ std::vector<uint32_t> ShaderManager::compileGLSL(const std::string_view sourceSt
#else
return {};
#endif
}
}

0 comments on commit f63dcbe

Please sign in to comment.