Skip to content

Commit

Permalink
Metal Effect support!
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSpydog committed Jan 12, 2020
1 parent 1450aee commit 7985e19
Show file tree
Hide file tree
Showing 4 changed files with 1,166 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -162,6 +162,7 @@ ADD_LIBRARY(mojoshader ${LIBRARY_FORMAT}
mojoshader.c
mojoshader_common.c
mojoshader_opengl.c
mojoshader_metal.c
profiles/mojoshader_profile_arb1.c
profiles/mojoshader_profile_bytecode.c
profiles/mojoshader_profile_d3d.c
Expand Down
50 changes: 50 additions & 0 deletions mojoshader.h
Expand Up @@ -3252,6 +3252,56 @@ DECLSPEC void MOJOSHADER_glDeleteShader(MOJOSHADER_glShader *shader);
*/
DECLSPEC void MOJOSHADER_glDestroyContext(MOJOSHADER_glContext *ctx);


/* Metal interface... */

typedef struct MOJOSHADER_mtlShader MOJOSHADER_mtlShader;

/*
* Get the MTLFunction* from the given MOJOSHADER_mtlShader.
*
* This function calls [retain] on the MTLFunction* before returning!
* Please call [release] on the result when you no longer need it.
*/
DECLSPEC void *MOJOSHADER_mtlGetFunctionHandle(MOJOSHADER_mtlShader *shader);

/*
* Swaps uniform buffers and resets offsets to prepare for the next frame.
*
* Always call this after submitting the final command buffer for a frame!
*/
DECLSPEC void MOJOSHADER_mtlEndFrame();

/*
* Return the location of a vertex attribute for the given shader.
*
* (usage) and (index) map to Direct3D vertex declaration values: COLOR1 would
* be MOJOSHADER_USAGE_COLOR and 1.
*
* The return value is the index of the attribute to be used to create
* a MTLVertexAttributeDescriptor, or -1 if the stream is not used.
*/
int MOJOSHADER_mtlGetVertexAttribLocation(MOJOSHADER_mtlShader *vert,
MOJOSHADER_usage usage, int index);

/*
* Get any error state we might have picked up, such as failed shader
* compilation.
*
* Returns a human-readable string. This string is for debugging purposes, and
* not guaranteed to be localized, coherent, or user-friendly in any way.
* It's for programmers!
*
* The latest error may remain between calls. New errors replace any existing
* error. Don't check this string for a sign that an error happened, check
* return codes instead and use this for explanation when debugging.
*
* Do not free the returned string: it's a pointer to a static internal
* buffer. Do not keep the pointer around, either, as it's likely to become
* invalid as soon as you call into MojoShader again.
*/
DECLSPEC const char *MOJOSHADER_mtlGetError(void);

#ifdef __cplusplus
}
#endif
Expand Down
113 changes: 113 additions & 0 deletions mojoshader_effects.h
Expand Up @@ -800,6 +800,119 @@ DECLSPEC void MOJOSHADER_glEffectEndPass(MOJOSHADER_glEffect *glEffect);
*/
DECLSPEC void MOJOSHADER_glEffectEnd(MOJOSHADER_glEffect *glEffect);


/* Metal effect interface... */

typedef struct MOJOSHADER_mtlEffect MOJOSHADER_mtlEffect;
typedef struct MOJOSHADER_mtlShader MOJOSHADER_mtlShader;
typedef struct MOJOSHADER_mtlShaderState MOJOSHADER_mtlShaderState;

/* Fully compile/link the shaders found within the effect.
*
* The MOJOSHADER_mtlEffect* is solely for use within the Metal-specific calls.
* In all other cases you will be using the MOJOSHADER_effect* instead.
*
* In a typical use case, you will be calling this immediately after obtaining
* the MOJOSHADER_effect*.
*
* (effect) is a MOJOSHADER_effect* obtained from MOJOSHADER_parseEffect().
* (mtlDevice) is a MTLDevice* obtained from a Metal device creation call,
* such as MTLCreateSystemDefaultDevice().
* (numBackingBuffers) is the number of backing uniform buffers that you
* want to create for each shader. If you are using double-buffering,
* this should be 2; for triple buffering, this should be 3, etc.
*
* This function returns a MOJOSHADER_mtlEffect*, containing Metal-specific
* data for an accompanying MOJOSHADER_effect*.
*/
DECLSPEC MOJOSHADER_mtlEffect *MOJOSHADER_mtlCompileEffect(MOJOSHADER_effect *effect,
void *mtlDevice,
int numBackingBuffers);

/* Delete the shaders that were allocated for an effect.
*
* (mtlEffect) is a MOJOSHADER_mtlEffect* obtained from
* MOJOSHADER_mtlCompileEffect().
*/
DECLSPEC void MOJOSHADER_mtlDeleteEffect(MOJOSHADER_mtlEffect *mtlEffect);

/* Prepare the effect for rendering with the currently applied technique.
*
* This function maps to ID3DXEffect::Begin.
*
* In addition to the expected Begin parameters, we also include a parameter
* to pass in a MOJOSHADER_effectRenderState. Rather than change the render
* state within MojoShader itself we will simply provide what the effect wants
* and allow you to use this information with your own renderer.
* MOJOSHADER_glEffectBeginPass will update with the render state desired by
* the current effect pass.
*
* Note that we only provide the ability to preserve the shader state, but NOT
* the ability to preserve the render/sampler states. You are expected to
* track your own Metal state and restore these states as needed for your
* application.
*
* (mtlEffect) is a MOJOSHADER_mtlEffect* obtained from
* MOJOSHADER_mtlCompileEffect().
* (numPasses) will be filled with the number of passes that this technique
* will need to fully render.
* (saveShaderState) is a boolean value informing the effect whether or not to
* restore the shader bindings after calling MOJOSHADER_mtlEffectEnd.
* (renderState) will be filled by the effect to inform you of the render state
* changes introduced by the technique and its passes.
*/
DECLSPEC void MOJOSHADER_mtlEffectBegin(MOJOSHADER_mtlEffect *mtlEffect,
unsigned int *numPasses,
int saveShaderState,
MOJOSHADER_effectStateChanges *stateChanges);

/* Begin an effect pass from the currently applied technique.
*
* This function maps to ID3DXEffect::BeginPass.
*
* (mtlEffect) is a MOJOSHADER_mtlEffect* obtained from
* MOJOSHADER_mtlCompileEffect().
* (pass) is the index of the effect pass as found in the current technique.
* (state) is a pointer to the current shader state object.
*
* The MOJOSHADER_mtlShaderState pointed to by (shState) must be created
* before calling this function!
*/
DECLSPEC void MOJOSHADER_mtlEffectBeginPass(MOJOSHADER_mtlEffect *mtlEffect,
unsigned int pass,
MOJOSHADER_mtlShaderState *shState);

/* Push render state changes that occurred within an actively rendering pass.
*
* This function maps to ID3DXEffect::CommitChanges.
*
* (mtlEffect) is a MOJOSHADER_mtlEffect* obtained from
* MOJOSHADER_mtlCompileEffect().
* (state) is a pointer to the current shader state object.
*/
DECLSPEC void MOJOSHADER_mtlEffectCommitChanges(MOJOSHADER_mtlEffect *mtlEffect,
MOJOSHADER_mtlShaderState *shState);

/* End an effect pass from the currently applied technique.
*
* This function maps to ID3DXEffect::EndPass.
*
* (mtlEffect) is a MOJOSHADER_mtlEffect* obtained from
* MOJOSHADER_mtlCompileEffect().
*/
DECLSPEC void MOJOSHADER_mtlEffectEndPass(MOJOSHADER_mtlEffect *mtlEffect);

/* Complete rendering the effect technique, and restore the render state.
*
* This function maps to ID3DXEffect::End.
*
* (mtlEffect) is a MOJOSHADER_mtlEffect* obtained from
* MOJOSHADER_glCompileEffect().
* (state) is a pointer to the current shader state object.
*/
DECLSPEC void MOJOSHADER_mtlEffectEnd(MOJOSHADER_mtlEffect *mtlEffect,
MOJOSHADER_mtlShaderState *shState);

#endif /* MOJOSHADER_EFFECT_SUPPORT */

#endif /* MOJOSHADER_EFFECTS_H */

0 comments on commit 7985e19

Please sign in to comment.