Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only render what’s visible #664

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions bridging/android/jni/graphics/NativeRenderObjectInterface.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions bridging/android/jni/graphics/NativeRenderObjectInterface.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions bridging/ios/MCRenderObjectInterface+Private.mm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions bridging/ios/MCRenderObjectInterface.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion djinni/graphics/core.djinni
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ render_object_interface = interface +c +j +o {
has_custom_model_matrix() : bool;
is_screen_space_coords() : bool;
get_custom_model_matrix() : list<f32>;
set_hidden(hidden: bool);
is_hidden(): bool;
}

render_pass_interface = interface +c +j +o {
Expand Down Expand Up @@ -91,4 +93,4 @@ camera_interface = interface +c +j +o {
get_scaling_factor(): f64;
get_origin(): vec_3_d;
viewport_size_changed();
}
}
7 changes: 6 additions & 1 deletion shared/public/RenderObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ class RenderObject : public RenderObjectInterface {
virtual std::vector<float> getCustomModelMatrix() override;

virtual bool isScreenSpaceCoords() override;


virtual void setHidden(bool hidden) override;

virtual bool isHidden() override;

private:
std::shared_ptr<GraphicsObjectInterface> graphicsObject;
bool setCustomModelMatrix = false;
bool screenSpaceCoords = false;
std::vector<float> modelMatrix;
bool hidden = false;
};
4 changes: 4 additions & 0 deletions shared/public/RenderObjectInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ class RenderObjectInterface {
virtual bool isScreenSpaceCoords() = 0;

virtual std::vector<float> getCustomModelMatrix() = 0;

virtual void setHidden(bool hidden) = 0;

virtual bool isHidden() = 0;
Comment on lines +22 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Adding pure virtual methods to an interface can cause breaking changes

Introducing new pure virtual methods virtual void setHidden(bool hidden) = 0; and virtual bool isHidden() = 0; to RenderObjectInterface can be a breaking change. All classes inheriting from this interface must now implement these methods, which can cause compilation errors in existing codebases.

To maintain backward compatibility, consider providing default implementations in the base class or using the "interface segregation" principle to create a new interface for the hidden functionality.

};
8 changes: 8 additions & 0 deletions shared/src/graphics/RenderObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ bool RenderObject::hasCustomModelMatrix() { return setCustomModelMatrix; }
std::vector<float> RenderObject::getCustomModelMatrix() { return modelMatrix; }

bool RenderObject::isScreenSpaceCoords() { return screenSpaceCoords; }

void RenderObject::setHidden(bool _hidden) {
hidden = _hidden;
}

bool RenderObject::isHidden() {
return hidden;
}
Comment on lines +31 to +37
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Based on the search results, I can see that similar properties in the codebase that require thread safety are using std::atomic<bool> for visibility state. This is particularly evident in similar layer classes:

  1. IconLayer: std::atomic<bool> isHidden;
  2. TextLayer: std::atomic<bool> isHidden;
  3. LineLayer: std::atomic<bool> isHidden;
  4. PolygonLayer: std::atomic<bool> isHidden;

Make visibility state thread-safe using std::atomic

The visibility state should be made thread-safe by using std::atomic<bool> instead of a plain bool since other similar layer implementations in the codebase follow this pattern for thread safety.

std::atomic<bool> hidden;  // instead of bool hidden
🔗 Analysis chain

Consider thread safety for visibility state.

Since render objects might be accessed from multiple threads (e.g., render thread and main thread), consider whether the visibility state needs thread protection.

Let's check if other similar properties in the codebase use thread protection:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for mutex/atomic usage in render-related files
rg -A 2 "std::(mutex|atomic)" --type cpp

Length of output: 26972

44 changes: 29 additions & 15 deletions shared/src/graphics/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,32 @@ void Renderer::drawFrame(const std::shared_ptr<RenderingContextInterface> &rende

const auto &renderObjects = pass->getRenderObjects();

bool prepared = false;

auto scissoringRect = pass->getScissoringRect();
if (scissoringRect) {
renderingContext->applyScissorRect(scissoringRect);
}

if (usesStencil) {
renderingContext->preRenderStencilMask();
}
for (const auto &renderObject : renderObjects) {

if (hasMask) {
maskObject->renderAsMask(renderingContext, pass->getRenderPassConfig(), vpMatrixPointer, identityMatrixPointer, origin, factor);
}
if (renderObject->isHidden()) {
continue;
}

if (!prepared) {
if (scissoringRect) {
renderingContext->applyScissorRect(scissoringRect);
}

if (usesStencil) {
renderingContext->preRenderStencilMask();
}

if (hasMask) {
maskObject->renderAsMask(renderingContext, pass->getRenderPassConfig(), vpMatrixPointer, identityMatrixPointer, origin, factor);
}

prepared = true;
}

for (const auto &renderObject : renderObjects) {
const auto &graphicsObject = renderObject->getGraphicsObject();
if (renderObject->isScreenSpaceCoords()) {
graphicsObject->render(renderingContext, pass->getRenderPassConfig(), identityMatrixPointer, identityMatrixPointer, zeroOrigin, hasMask, factor);
Expand All @@ -75,12 +87,14 @@ void Renderer::drawFrame(const std::shared_ptr<RenderingContextInterface> &rende
}
}

if (usesStencil) {
renderingContext->postRenderStencilMask();
}
if (prepared) {
if (usesStencil) {
renderingContext->postRenderStencilMask();
}

if (scissoringRect) {
renderingContext->applyScissorRect(std::nullopt);
if (scissoringRect) {
renderingContext->applyScissorRect(std::nullopt);
}
}
}
}
Expand Down
Loading
Loading