Shader constants and const buffers

namespace d3d

Functions

bool set_const(unsigned stage, unsigned reg_base, const void *data, unsigned num_regs)

Sets shader constants for the specified stage.

Note

These constants will be ignored, if a constant buffer is explicitly bound to slot 0.

Parameters:
  • stage – The shader stage (STAGE_VS, STAGE_PS, STAGE_CS).

  • reg_base – The base register index.

  • data – Pointer to the data to be set. The size must be a multiple of sizeof(float4).

  • num_regs – The number of registers (float4) to be set.

Returns:

True if the constants were set successfully, false otherwise.

inline bool set_vs_const(unsigned reg_base, const void *data, unsigned num_regs)

Sets vertex shader constants.

Note

These constants will be ignored, if a constant buffer is explicitly bound to slot 0.

Parameters:
  • reg_base – The base register index.

  • data – Pointer to the data to be set. The size must be a multiple of sizeof(float4).

  • num_regs – The number of registers (float4) to be set.

Returns:

True if the constants were set successfully, false otherwise.

inline bool set_ps_const(unsigned reg_base, const void *data, unsigned num_regs)

Sets pixel shader constants.

Note

These constants will be ignored, if a constant buffer is explicitly bound to slot 0.

Parameters:
  • reg_base – The base register index.

  • data – Pointer to the data to be set. The size must be a multiple of sizeof(float4).

  • num_regs – The number of registers (float4) to be set.

Returns:

True if the constants were set successfully, false otherwise.

inline bool set_cs_const(unsigned reg_base, const void *data, unsigned num_regs)

Sets compute shader constants.

Note

These constants will be ignored, if a constant buffer is explicitly bound to slot 0.

Parameters:
  • reg_base – The base register index.

  • data – Pointer to the data to be set. The size must be a multiple of sizeof(float4).

  • num_regs – The number of registers (float4) to be set.

Returns:

True if the constants were set successfully, false otherwise.

inline bool set_vs_const1(unsigned reg, float v0, float v1, float v2, float v3)

Sets a single vertex shader constant register.

Note

This constant will be ignored, if a constant buffer is explicitly bound to slot 0.

Parameters:
  • reg – The register index.

  • v0 – The value for the first component.

  • v1 – The value for the second component.

  • v2 – The value for the third component.

  • v3 – The value for the fourth component.

Returns:

True if the constant was set successfully, false otherwise.

inline bool set_ps_const1(unsigned reg, float v0, float v1, float v2, float v3)

Sets a single pixel shader constant register.

Note

This constant will be ignored, if a constant buffer is explicitly bound to slot 0.

Parameters:
  • reg – The register index.

  • v0 – The value for the first component.

  • v1 – The value for the second component.

  • v2 – The value for the third component.

  • v3 – The value for the fourth component.

Returns:

True if the constant was set successfully, false otherwise.

bool set_immediate_const(unsigned stage, const uint32_t *data, unsigned num_words)

Sets immediate constants for the specified stage.

Immediate constants are supposed to be very cheap to set dwords. It is guaranteed to support up to 4 dwords on each stage. Use as little as possible, ideally one or two (or none). On XB1(PS4), it is implemented as user regs (C|P|V)SSetShaderUserData. On DX11, it is implemented as constant buffers. On VK/DX12, it should be implemented as descriptor/push constants buffers. Calling with data = nullptr || num_words == 0 is benign and currently works as “stop using immediate” (probably have to be replaced with shader system).

Parameters:
  • stage – The shader stage (STAGE_VS, STAGE_PS, STAGE_CS).

  • data – Pointer to the data to be set.

  • num_words – The number of words to be set.

Returns:

True if the constants were set successfully, false otherwise.

bool set_const_buffer(unsigned stage, unsigned slot, const float *data, unsigned num_regs)

Sets a constant buffer for the specified stage. PS4 specific.

Constant buffers are valid until driver acquire call or end of frame. To unbind, use set_const_buffer(stage, 0, nullptr).

Note

When slot is 0, and data is not nullptr, it will also override any constants set via set_const and related calls.

Parameters:
  • stage – The shader stage (STAGE_VS, STAGE_PS, STAGE_CS).

  • slot – The buffer slot.

  • data – Pointer to the data to be set. The size must be a multiple of sizeof(float4).

  • num_regs – The number of registers (float4) to be set.

Returns:

True if the constant buffer was set successfully, false otherwise.

bool set_const_buffer(unsigned stage, unsigned slot, Sbuffer *buffer, uint32_t consts_offset = 0, uint32_t consts_size = 0)

Sets a constant buffer for the specified stage using a buffer object.

Constant buffers are valid until driver acquire call or end of frame. To unbind, use set_const_buffer(stage, 0, nullptr).

Todo:

consts_offset and consts_size are not used. Remove them?

Parameters:
  • stage – The shader stage (STAGE_VS, STAGE_PS, STAGE_CS).

  • slot – The buffer slot.

  • buffer – Pointer to the buffer object. Must be created with d3d::buffers::create_persistent_cb or d3d::buffers::create_one_frame_cb.

  • consts_offset – The offset of the constants in the buffer. (Not used)

  • consts_size – The size of the constants in the buffer. (Not used)

Returns:

True if the constant buffer was set successfully, false otherwise.

int set_vs_constbuffer_size(int required_size)

Sets the size of the vertex shader constant buffer that can be filled with set_const call.

Parameters:

required_size – The required size of the constant buffer. If 0, the default size is set.

Returns:

The actual size of the constant buffer.

int set_cs_constbuffer_size(int required_size)

Sets the size of the compute shader constant buffer that can be filled with set_const call.

Parameters:

required_size – The required size of the constant buffer. If 0, the default size is set.

Returns:

The actual size of the constant buffer.

inline bool set_cb0_data(unsigned stage, const float *data, unsigned num_regs)

Sets a constant buffer at slot 0 for the specified stage. Uses the fastest method available on the platform.

Constant buffers are valid until driver acquire call or end of frame. To unbind, use release_cb0_data(stage).

Parameters:
  • stage – The shader stage (STAGE_VS, STAGE_PS, STAGE_CS).

  • data – Pointer to the data to be set. The size must be a multiple of sizeof(float4).

  • num_regs – The number of registers (float4) to be set.

Returns:

True if the constants were set successfully, false otherwise.

inline void release_cb0_data(unsigned stage)

Releases the constant buffer at slot 0, which should have been previously set by set_cb0_data.

Parameters:

stage – The shader stage (STAGE_VS, STAGE_PS, STAGE_CS).