Skip to content

Restores the original blend config after a surface render. - #7

Merged
abaire merged 1 commit into
mainfrom
restore_blend_config_after_surface_render
May 7, 2026
Merged

Restores the original blend config after a surface render.#7
abaire merged 1 commit into
mainfrom
restore_blend_config_after_surface_render

Conversation

@abaire

@abaire abaire commented May 7, 2026

Copy link
Copy Markdown
Owner

No description provided.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request implements state tracking for blending configurations in NV2AState to ensure the state is correctly restored after rendering to non-framebuffer surfaces. It also adds environment variable configuration for NXDK_DIR in CMake. The review feedback highlights a redundant and potentially incorrect variable assignment in CMakeLists.txt, suggests using default member initializers for the new BlendConfig struct for better safety, and recommends maintaining the const qualifier on SetBlend methods by marking the tracking members as mutable to avoid breaking the existing API.

Comment thread src/nv2astate.h
Comment on lines +256 to +261
struct BlendConfig {
bool enable;
uint32_t func;
uint32_t sfactor;
uint32_t dfactor;
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

It is safer to provide default member initializers for the BlendConfig struct that match the hardware defaults. This ensures that if RenderToSurfaceStart is called before any explicit call to SetBlend, the state can be correctly restored to the expected defaults rather than zero-initialized values (which may not correspond to the intended hardware state).

Suggested change
struct BlendConfig {
bool enable;
uint32_t func;
uint32_t sfactor;
uint32_t dfactor;
};
struct BlendConfig {
bool enable = false;
uint32_t func = NV097_SET_BLEND_EQUATION_V_FUNC_ADD;
uint32_t sfactor = NV097_SET_BLEND_FUNC_SFACTOR_V_SRC_ALPHA;
uint32_t dfactor = NV097_SET_BLEND_FUNC_DFACTOR_V_ONE_MINUS_SRC_ALPHA;
};

Comment thread CMakeLists.txt Outdated
block(SCOPE_FOR VARIABLES)
get_filename_component(NXDK_ROOT_DIR "${CMAKE_TOOLCHAIN_FILE}/../.." ABSOLUTE)
set(NXDK_DIR "${NXDK_ROOT_DIR}" CACHE PATH "Path to the nxdk root directory.")
set(ENV{NXDK_DIR} "${NXDK_SOURCE_DIR}")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The variable ${NXDK_SOURCE_DIR} is not defined at this point in the script; it is defined within PrebuildNXDK.cmake, which is included later on line 21. Furthermore, PrebuildNXDK.cmake already sets the NXDK_DIR environment variable, making this line redundant and potentially incorrect if it sets the environment variable to an empty string.

Comment thread src/nv2astate.h Outdated
Comment on lines +698 to +700
void SetBlend(bool enable = true, uint32_t func = NV097_SET_BLEND_EQUATION_V_FUNC_ADD,
uint32_t sfactor = NV097_SET_BLEND_FUNC_SFACTOR_V_SRC_ALPHA,
uint32_t dfactor = NV097_SET_BLEND_FUNC_DFACTOR_V_ONE_MINUS_SRC_ALPHA) const;
uint32_t dfactor = NV097_SET_BLEND_FUNC_DFACTOR_V_ONE_MINUS_SRC_ALPHA);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Removing the const qualifier from SetBlend is a breaking change for any code that interacts with a const NV2AState object. Since the internal tracking of the blend configuration is essentially a cache of the hardware state, the tracking members should be marked mutable to allow this method to remain const.

Suggested change
void SetBlend(bool enable = true, uint32_t func = NV097_SET_BLEND_EQUATION_V_FUNC_ADD,
uint32_t sfactor = NV097_SET_BLEND_FUNC_SFACTOR_V_SRC_ALPHA,
uint32_t dfactor = NV097_SET_BLEND_FUNC_DFACTOR_V_ONE_MINUS_SRC_ALPHA) const;
uint32_t dfactor = NV097_SET_BLEND_FUNC_DFACTOR_V_ONE_MINUS_SRC_ALPHA);
void SetBlend(bool enable = true, uint32_t func = NV097_SET_BLEND_EQUATION_V_FUNC_ADD,
uint32_t sfactor = NV097_SET_BLEND_FUNC_SFACTOR_V_SRC_ALPHA,
uint32_t dfactor = NV097_SET_BLEND_FUNC_DFACTOR_V_ONE_MINUS_SRC_ALPHA) const;

Comment thread src/nv2astate.h Outdated
uint32_t dfactor = NV097_SET_BLEND_FUNC_DFACTOR_V_ONE_MINUS_SRC_ALPHA) const;
uint32_t dfactor = NV097_SET_BLEND_FUNC_DFACTOR_V_ONE_MINUS_SRC_ALPHA);

void SetBlend(const BlendConfig &config) { SetBlend(config.enable, config.func, config.sfactor, config.dfactor); }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

This overload should also be const to match the primary SetBlend method.

Suggested change
void SetBlend(const BlendConfig &config) { SetBlend(config.enable, config.func, config.sfactor, config.dfactor); }
void SetBlend(const BlendConfig &config) const { SetBlend(config.enable, config.func, config.sfactor, config.dfactor); }

Comment thread src/nv2astate.h Outdated
kNoStrideOverride, kNoStrideOverride, kNoStrideOverride, kNoStrideOverride, kNoStrideOverride, kNoStrideOverride,
kNoStrideOverride, kNoStrideOverride, kNoStrideOverride, kNoStrideOverride};

BlendConfig active_blend_config_{.enable = false};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Marking active_blend_config_ as mutable allows the SetBlend methods to remain const, preserving the existing API contract. Additionally, if default initializers are added to the BlendConfig struct, the explicit initialization here can be simplified.

Suggested change
BlendConfig active_blend_config_{.enable = false};
mutable BlendConfig active_blend_config_;

Comment thread src/nv2astate.cpp Outdated
}

void NV2AState::SetBlend(bool enable, uint32_t func, uint32_t sfactor, uint32_t dfactor) const {
void NV2AState::SetBlend(bool enable, uint32_t func, uint32_t sfactor, uint32_t dfactor) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The implementation should be restored to const to match the header and maintain API compatibility.

void NV2AState::SetBlend(bool enable, uint32_t func, uint32_t sfactor, uint32_t dfactor) const {

@abaire
abaire force-pushed the restore_blend_config_after_surface_render branch from 486996e to b6ac75a Compare May 7, 2026 16:56
@abaire
abaire merged commit fd79bea into main May 7, 2026
2 checks passed
@abaire
abaire deleted the restore_blend_config_after_surface_render branch May 7, 2026 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant