mojoshader.h
changeset 1271 5a67d082c55f
parent 1266 285a186ad69d
child 1277 da61410edbc9
equal deleted inserted replaced
1270:fb28ba997299 1271:5a67d082c55f
  3530  * This will NOT clean up shaders you created! Please destroy all shaders
  3530  * This will NOT clean up shaders you created! Please destroy all shaders
  3531  *  before calling this function.
  3531  *  before calling this function.
  3532  */
  3532  */
  3533 DECLSPEC void MOJOSHADER_mtlDestroyContext(void);
  3533 DECLSPEC void MOJOSHADER_mtlDestroyContext(void);
  3534 
  3534 
  3535 
  3535 /* Vulkan interface */
  3536 /* D3D11 interface... */
  3536 
  3537 
  3537 /* Avoid including vulkan.h, don't define handles if it's already included */
  3538 typedef struct MOJOSHADER_d3d11Shader MOJOSHADER_d3d11Shader;
  3538 #ifdef VULKAN_H_
  3539 
  3539 #define NO_MOJOSHADER_VULKAN_TYPEDEFS
  3540 /*
  3540 #endif
  3541  * Prepare MojoShader to manage Direct3D 11 shaders.
  3541 #ifndef NO_MOJOSHADER_VULKAN_TYPEDEFS
  3542  *
  3542 #ifdef _WIN32
  3543  * You do not need to call this if all you want is MOJOSHADER_parse().
  3543 #define VKAPI_CALL __stdcall
  3544  *
  3544 #define VKAPI_PTR VKAPI_CALL
  3545  * You must call this once AFTER you have successfully built your D3D11 context.
  3545 #else
       
  3546 #define VKAPI_CALL
       
  3547 #define VKAPI_PTR
       
  3548 #endif
       
  3549 #define VK_DEFINE_HANDLE(object) typedef struct object##_T* object;
       
  3550 
       
  3551 #if defined(__LP64__) || defined(_WIN64) || defined(__x86_64__) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__)
       
  3552 #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef struct object##_T *object;
       
  3553 #else
       
  3554 #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef unsigned long long object;
       
  3555 #endif
       
  3556 
       
  3557 VK_DEFINE_HANDLE(VkInstance)
       
  3558 VK_DEFINE_HANDLE(VkDevice)
       
  3559 VK_DEFINE_HANDLE(VkPhysicalDevice)
       
  3560 VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkBuffer)
       
  3561 
       
  3562 #endif /* !NO_MOJOSHADER_VULKAN_TYPEDEFS */
       
  3563 
       
  3564 typedef void (VKAPI_PTR *PFN_MOJOSHADER_vkVoidFunction)(void);
       
  3565 typedef PFN_MOJOSHADER_vkVoidFunction (VKAPI_PTR *PFN_MOJOSHADER_vkGetDeviceProcAddr)(
       
  3566 	VkDevice device,
       
  3567 	const char* pName
       
  3568 );
       
  3569 typedef PFN_MOJOSHADER_vkVoidFunction (VKAPI_PTR *PFN_MOJOSHADER_vkGetInstanceProcAddr)(
       
  3570 	VkInstance instance,
       
  3571 	const char* pName
       
  3572 );
       
  3573 
       
  3574 typedef struct MOJOSHADER_vkContext MOJOSHADER_vkContext;
       
  3575 typedef struct MOJOSHADER_vkShader MOJOSHADER_vkShader;
       
  3576 
       
  3577 /*
       
  3578  * Prepares a context to manage Vulkan shaders.
       
  3579  *
       
  3580  * Don't call this unless you know for sure that you need it.
       
  3581  *
       
  3582  * You must call this after creating VkDevice and VkInstance.
       
  3583  *
       
  3584  * (instance) refers to VkInstance, cast to void*.
       
  3585  *
       
  3586  * (device) refers to VkDevice, cast to void*.
       
  3587  *
       
  3588  * (frames_in_flight) refers to the maximum number of frames that can be
       
  3589  * processed simultaneously.
       
  3590  *
       
  3591  * (lookup) refers to PFN_vkGetDeviceProcAddr, a function pointer that
       
  3592  * is used to dynamically link required Vulkan functions.
       
  3593  *
       
  3594  * You must pass in the graphics queue family index and the memory type index
       
  3595  * you will be using with your Vulkan instance.
  3546  *
  3596  *
  3547  * As MojoShader requires some memory to be allocated, you may provide a
  3597  * As MojoShader requires some memory to be allocated, you may provide a
  3548  *  custom allocator to this function, which will be used to allocate/free
  3598  *  custom allocator to this function, which will be used to allocate/free
  3549  *  memory. They function just like malloc() and free(). We do not use
  3599  *  memory. They function just like malloc() and free(). We do not use
  3550  *  realloc(). If you don't care, pass NULL in for the allocator functions.
  3600  *  realloc(). If you don't care, pass NULL in for the allocator functions.
  3551  *  If your allocator needs instance-specific data, you may supply it with the
  3601  *  If your allocator needs instance-specific data, you may supply it with the
  3552  *  (malloc_d) parameter. This pointer is passed as-is to your (m) and (f)
  3602  *  (malloc_d) parameter. This pointer is passed as-is to your (m) and (f)
  3553  *  functions.
  3603  *  functions.
  3554  *
  3604  *
       
  3605  * The context created by this function will automatically become the current
       
  3606  *  context. No further action is needed by the caller.
       
  3607  *
       
  3608  * Returns 0 on success or -1 on failure.
       
  3609  */
       
  3610 
       
  3611 DECLSPEC MOJOSHADER_vkContext *MOJOSHADER_vkCreateContext(VkInstance *instance,
       
  3612                                                           VkPhysicalDevice *physical_device,
       
  3613                                                           VkDevice *logical_device,
       
  3614                                                           int frames_in_flight,
       
  3615                                                           PFN_MOJOSHADER_vkGetInstanceProcAddr instance_lookup,
       
  3616                                                           PFN_MOJOSHADER_vkGetDeviceProcAddr lookup,
       
  3617                                                           unsigned int graphics_queue_family_index,
       
  3618                                                           unsigned int max_uniform_buffer_range,
       
  3619                                                           unsigned int min_uniform_buffer_offset_alignment,
       
  3620                                                           MOJOSHADER_malloc m, MOJOSHADER_free f,
       
  3621                                                           void *malloc_d);
       
  3622 
       
  3623 /*
       
  3624  * You must call this before using the context that you got from
       
  3625  *  MOJOSHADER_vkCreateContext(), and must use it when you switch to a new GL
       
  3626  *  context.
       
  3627  *
       
  3628  * You can only have one MOJOSHADER_vkContext per actual Vulkan context, or
       
  3629  *  undefined behaviour will result.
       
  3630  *
       
  3631  * It is legal to call this with a NULL pointer to make no context current,
       
  3632  *  but you need a valid context to be current to use most of MojoShader.
       
  3633  */
       
  3634 DECLSPEC void MOJOSHADER_vkMakeContextCurrent(MOJOSHADER_vkContext *_ctx);
       
  3635 
       
  3636 /*
       
  3637  * Get any error state we might have picked up.
       
  3638  *
       
  3639  * Returns a human-readable string. This string is for debugging purposes, and
       
  3640  *  not guaranteed to be localized, coherent, or user-friendly in any way.
       
  3641  *  It's for programmers!
       
  3642  *
       
  3643  * The latest error may remain between calls. New errors replace any existing
       
  3644  *  error. Don't check this string for a sign that an error happened, check
       
  3645  *  return codes instead and use this for explanation when debugging.
       
  3646  *
       
  3647  * Do not free the returned string: it's a pointer to a static internal
       
  3648  *  buffer. Do not keep the pointer around, either, as it's likely to become
       
  3649  *  invalid as soon as you call into MojoShader again.
       
  3650  *
       
  3651  * This call does NOT require a valid MOJOSHADER_vkContext to have been made
       
  3652  *  current. The error buffer is shared between contexts, so you can get
       
  3653  *  error results from a failed MOJOSHADER_vkCreateContext().
       
  3654  */
       
  3655 DECLSPEC const char *MOJOSHADER_vkGetError();
       
  3656 
       
  3657 /*
       
  3658  * Deinitialize MojoShader's Vulkan shader management.
       
  3659  *
       
  3660  * You must call this once, while your Vulkan context (not MojoShader context) is
       
  3661  *  still current, if you previously had a successful call to
       
  3662  *  MOJOSHADER_vkCreateContext(). This should be the last MOJOSHADER_vk*
       
  3663  *  function you call until you've prepared a context again.
       
  3664  *
       
  3665  * This will clean up resources previously allocated, and may call into Vulkan.
       
  3666  *
       
  3667  * This will not clean up shaders and programs you created! Please call
       
  3668  *  MOJOSHADER_vkDeleteShader() and MOJOSHADER_vkDeleteProgram() to clean
       
  3669  *  those up before calling this function!
       
  3670  *
       
  3671  * This function destroys the MOJOSHADER_vkContext you pass it. If it's the
       
  3672  *  current context, then no context will be current upon return.
       
  3673  */
       
  3674 DECLSPEC void MOJOSHADER_vkDestroyContext();
       
  3675 
       
  3676 /*
       
  3677  * Compile a buffer of Direct3D shader bytecode into a Vulkan shader module.
       
  3678  *
       
  3679  *   (tokenbuf) is a buffer of Direct3D shader bytecode.
       
  3680  *   (bufsize) is the size, in bytes, of the bytecode buffer.
       
  3681  *   (swiz), (swizcount), (smap), and (smapcount) are passed to
       
  3682  *   MOJOSHADER_parse() unmolested.
       
  3683  *
       
  3684  * Returns NULL on error, or a shader handle on success.
       
  3685  *
       
  3686  * This call requires a valid MOJOSHADER_vkContext to have been made current,
       
  3687  *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
       
  3688  *
       
  3689  * Compiled shaders from this function may not be shared between contexts.
       
  3690  */
       
  3691 DECLSPEC MOJOSHADER_vkShader *MOJOSHADER_vkCompileShader(const char *mainfn,
       
  3692                                                          const unsigned char *tokenbuf,
       
  3693                                                          const unsigned int bufsize,
       
  3694                                                          const MOJOSHADER_swizzle *swiz,
       
  3695                                                          const unsigned int swizcount,
       
  3696                                                          const MOJOSHADER_samplerMap *smap,
       
  3697                                                          const unsigned int smapcount);
       
  3698 
       
  3699 /*
       
  3700  * Increments a shader's internal refcount.
       
  3701  *
       
  3702  * To decrement the refcount, call
       
  3703  *  MOJOSHADER_vkDeleteShader().
       
  3704  *
       
  3705  * This call requires a valid MOJOSHADER_vkContext to have been made current,
       
  3706  *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
       
  3707  */
       
  3708 DECLSPEC void MOJOSHADER_vkShaderAddRef(MOJOSHADER_vkShader *shader);
       
  3709 
       
  3710 /*
       
  3711  * Increments a shader's internal refcount.
       
  3712  *
       
  3713  * To decrement the refcount, call MOJOSHADER_vkDeleteShader().
       
  3714  *
       
  3715  * This call requires a valid MOJOSHADER_vkContext to have been made current,
       
  3716  *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
       
  3717  */
       
  3718 DECLSPEC void MOJOSHADER_vkDeleteShader(MOJOSHADER_vkShader *shader);
       
  3719 
       
  3720 /*
       
  3721  * Get the MOJOSHADER_parseData structure that was produced from the
       
  3722  *  call to MOJOSHADER_vkCompileShader().
       
  3723  *
       
  3724  * This data is read-only, and you should NOT attempt to free it. This
       
  3725  *  pointer remains valid until the shader is deleted.
       
  3726  */
       
  3727 DECLSPEC const MOJOSHADER_parseData *MOJOSHADER_vkGetShaderParseData(
       
  3728                                                 MOJOSHADER_vkShader *shader);
       
  3729 
       
  3730 /*
       
  3731  * This "binds" individual shaders, which effectively means the context
       
  3732  *  will store these shaders for later retrieval. No actual binding or
       
  3733  *  pipeline creation is performed.
       
  3734  *
       
  3735  * This function is only for convenience, specifically for compatibility
       
  3736  *  with the effects API.
       
  3737  *
       
  3738  * This call requires a valid MOJOSHADER_vkContext to have been made current,
       
  3739  *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
       
  3740  */
       
  3741 DECLSPEC void MOJOSHADER_vkBindShaders(MOJOSHADER_vkShader *vshader,
       
  3742                                        MOJOSHADER_vkShader *pshader);
       
  3743 
       
  3744 /*
       
  3745  * This queries for the shaders currently bound to the active context.
       
  3746  *
       
  3747  * This function is only for convenience, specifically for compatibility
       
  3748  *  with the effects API.
       
  3749  *
       
  3750  * This call requires a valid MOJOSHADER_vkContext to have been made current,
       
  3751  *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
       
  3752  */
       
  3753 DECLSPEC void MOJOSHADER_vkGetBoundShaders(MOJOSHADER_vkShader **vshader,
       
  3754                                            MOJOSHADER_vkShader **pshader);
       
  3755 
       
  3756 /*
       
  3757  * Fills register pointers with pointers that are directly used to push uniform
       
  3758  *  data to the Vulkan shader context.
       
  3759  *
       
  3760  * This function is really just for the effects API, you should NOT be using
       
  3761  *  this unless you know every single line of MojoShader from memory.
       
  3762  *
       
  3763  * This call requires a valid MOJOSHADER_vkContext to have been made current,
       
  3764  *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
       
  3765  */
       
  3766 DECLSPEC void MOJOSHADER_vkMapUniformBufferMemory(float **vsf, int **vsi, unsigned char **vsb,
       
  3767                                                   float **psf, int **psi, unsigned char **psb);
       
  3768 
       
  3769 /*
       
  3770  * Tells the context that you are done with the memory mapped by
       
  3771  *  MOJOSHADER_vkMapUniformBufferMemory().
       
  3772  *
       
  3773  * This call requires a valid MOJOSHADER_vkContext to have been made current,
       
  3774  *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
       
  3775  */
       
  3776 DECLSPEC void MOJOSHADER_vkUnmapUniformBufferMemory();
       
  3777 
       
  3778 /*
       
  3779  * This queries for the uniform buffer, byte offset and byte size for each of the
       
  3780  *  currently bound shaders.
       
  3781  *
       
  3782  * This function is only for convenience, specifically for compatibility with
       
  3783  *  the effects API.
       
  3784  *
       
  3785  * This call requires a valid MOJOSHADER_vkContext to have been made current,
       
  3786  *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
       
  3787  */
       
  3788 DECLSPEC void MOJOSHADER_vkGetUniformBuffers(VkBuffer *vbuf,
       
  3789                                              unsigned long long *voff,
       
  3790                                              unsigned long long *vsize,
       
  3791                                              VkBuffer *pbuf,
       
  3792                                              unsigned long long *poff,
       
  3793                                              unsigned long long *psize);
       
  3794 
       
  3795 /*
       
  3796  * Prepares uniform buffers for reuse.
       
  3797  *
       
  3798  * Always call this after submitting the final command buffer for a frame!
       
  3799  */
       
  3800 DECLSPEC void MOJOSHADER_vkEndFrame();
       
  3801 
       
  3802 /*
       
  3803  * Return the location of a vertex attribute for the given shader.
       
  3804  *
       
  3805  * (usage) and (index) map to Direct3D vertex declaration values: COLOR1 would
       
  3806  *  be MOJOSHADER_USAGE_COLOR and 1.
       
  3807  *
       
  3808  * The return value is the index of the attribute to be used to create
       
  3809  *  a VkVertexInputAttributeDescription, or -1 if the stream is not used.
       
  3810  *
       
  3811  *  This call requires a valid MOJOSHADER_vkContext to have been made current,
       
  3812  *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
       
  3813  */
       
  3814 DECLSPEC int MOJOSHADER_vkGetVertexAttribLocation(MOJOSHADER_vkShader *vert,
       
  3815                                                   MOJOSHADER_usage usage,
       
  3816                                                   int index);
       
  3817 
       
  3818 /*
       
  3819  * Get the VkShaderModule from the given MOJOSHADER_vkShader.
       
  3820  */
       
  3821 DECLSPEC unsigned long long MOJOSHADER_vkGetShaderModule(
       
  3822                                                 MOJOSHADER_vkShader *shader);
       
  3823 
       
  3824 /* D3D11 interface... */
       
  3825 
       
  3826 typedef struct MOJOSHADER_d3d11Shader MOJOSHADER_d3d11Shader;
       
  3827 
       
  3828 /*
       
  3829  * Prepare MojoShader to manage Direct3D 11 shaders.
       
  3830  *
       
  3831  * You do not need to call this if all you want is MOJOSHADER_parse().
       
  3832  *
       
  3833  * You must call this once AFTER you have successfully built your D3D11 context.
       
  3834  *
       
  3835  * As MojoShader requires some memory to be allocated, you may provide a
       
  3836  *  custom allocator to this function, which will be used to allocate/free
       
  3837  *  memory. They function just like malloc() and free(). We do not use
       
  3838  *  realloc(). If you don't care, pass NULL in for the allocator functions.
       
  3839  *  If your allocator needs instance-specific data, you may supply it with the
       
  3840  *  (malloc_d) parameter. This pointer is passed as-is to your (m) and (f)
       
  3841  *  functions.
       
  3842  *
  3555  * This call is only as thread safe as your D3D11 context! If you call your
  3843  * This call is only as thread safe as your D3D11 context! If you call your
  3556  *  context from multiple threads, you must protect this call with whatever
  3844  *  context from multiple threads, you must protect this call with whatever
  3557  *  thread synchronization technique you have for your other D3D calls.
  3845  *  thread synchronization technique you have for your other D3D calls.
  3558  */
  3846  */
  3559 DECLSPEC int MOJOSHADER_d3d11CreateContext(void *device, void *deviceContext,
  3847 DECLSPEC int MOJOSHADER_d3d11CreateContext(void *device, void *deviceContext,