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
|
#include "RenderCommand.h"
#include "GL/GLShaderProgram.h"
#include "GL/GLScissorTest.h"
#include "RenderResources.h"
#include "Camera.h"
#include "DrawableNode.h"
#include "../tracy.h"
namespace nCine
{
RenderCommand::RenderCommand(Type type)
: materialSortKey_(0), layer_(0), numInstances_(0), batchSize_(0), transformationCommitted_(false), modelMatrix_(Matrix4x4f::Identity)
#if defined(NCINE_PROFILING)
, type_(type)
#endif
{
}
RenderCommand::RenderCommand()
: RenderCommand(Type::Unspecified)
{
}
void RenderCommand::CalculateMaterialSortKey()
{
const std::uint64_t upper = std::uint64_t(GetLayerSortKey()) << 32;
const std::uint32_t lower = material_.GetSortKey();
materialSortKey_ = upper | lower;
}
void RenderCommand::Issue()
{
ZoneScopedC(0x81A861);
if (geometry_.numVertices_ == 0 && geometry_.numIndices_ == 0) {
return;
}
material_.Bind();
material_.CommitUniforms();
GLScissorTest::State scissorTestState = GLScissorTest::GetState();
if (scissorRect_.W > 0 && scissorRect_.H > 0) {
GLScissorTest::Enable(scissorRect_);
}
std::uint32_t offset = 0;
#if (defined(WITH_OPENGLES) && !GL_ES_VERSION_3_2) || defined(DEATH_TARGET_EMSCRIPTEN)
// Simulating missing `glDrawElementsBaseVertex()` on OpenGL ES 3.0
if (geometry_.numIndices_ > 0) {
offset = geometry_.GetVboParams().offset + (geometry_.firstVertex_ * geometry_.numElementsPerVertex_ * sizeof(GLfloat));
}
#endif
material_.DefineVertexFormat(geometry_.GetVboParams().object, geometry_.GetIboParams().object, offset);
geometry_.Bind();
geometry_.Draw(numInstances_);
GLScissorTest::SetState(scissorTestState);
}
void RenderCommand::SetScissor(GLint x, GLint y, GLsizei width, GLsizei height)
{
scissorRect_.Set(x, y, width, height);
}
void RenderCommand::SetTransformation(const Matrix4x4f& modelMatrix)
{
modelMatrix_ = modelMatrix;
transformationCommitted_ = false;
}
void RenderCommand::CommitNodeTransformation()
{
if (transformationCommitted_) {
return;
}
ZoneScopedC(0x81A861);
const Camera::ProjectionValues cameraValues = RenderResources::GetCurrentCamera()->GetProjectionValues();
modelMatrix_[3][2] = CalculateDepth(layer_, cameraValues.nearClip, cameraValues.farClip);
if (material_.shaderProgram_ && material_.shaderProgram_->GetStatus() == GLShaderProgram::Status::LinkedWithIntrospection) {
GLUniformBlockCache* instanceBlock = material_.UniformBlock(Material::InstanceBlockName);
GLUniformCache* matrixUniform = instanceBlock
? instanceBlock->GetUniform(Material::ModelMatrixUniformName)
: material_.Uniform(Material::ModelMatrixUniformName);
if (matrixUniform) {
//ZoneScopedNC("Set model matrix", 0x81A861);
matrixUniform->SetFloatVector(modelMatrix_.Data());
}
}
transformationCommitted_ = true;
}
void RenderCommand::CommitCameraTransformation()
{
ZoneScopedC(0x81A861);
RenderResources::CameraUniformData* cameraUniformData = RenderResources::FindCameraUniformData(material_.shaderProgram_);
if (cameraUniformData == nullptr) {
RenderResources::CameraUniformData newCameraUniformData;
newCameraUniformData.shaderUniforms.SetProgram(material_.shaderProgram_, Material::ProjectionViewMatrixExcludeString, nullptr);
if (newCameraUniformData.shaderUniforms.GetUniformCount() == 2) {
newCameraUniformData.shaderUniforms.SetUniformsDataPointer(RenderResources::GetCameraUniformsBuffer());
newCameraUniformData.shaderUniforms.GetUniform(Material::ProjectionMatrixUniformName)->SetDirty(true);
newCameraUniformData.shaderUniforms.GetUniform(Material::ViewMatrixUniformName)->SetDirty(true);
newCameraUniformData.shaderUniforms.CommitUniforms();
RenderResources::InsertCameraUniformData(material_.shaderProgram_, std::move(newCameraUniformData));
}
} else {
cameraUniformData->shaderUniforms.CommitUniforms();
}
}
void RenderCommand::CommitAll()
{
// Copy the vertices and indices stored in host memory to video memory
// This step is not needed if the command uses a custom VBO or IBO or directly writes into the common one
geometry_.CommitVertices();
geometry_.CommitIndices();
// The model matrix should always be updated before committing uniform blocks
CommitNodeTransformation();
// Commits all the uniform blocks of command's shader program
material_.CommitUniformBlocks();
}
float RenderCommand::CalculateDepth(std::uint16_t layer, float nearClip, float farClip)
{
// The layer translates to depth, from near to far
return nearClip + LayerStep + (farClip - nearClip - LayerStep) * layer * LayerStep;
}
}
|