Rework Presentation Flow (#15)

Removes `Refresh_RenderTarget`, `Refresh_CreateRenderTarget` and `Refresh_QueueDestroyRenderTarget`. Render targets are now managed by the implementation.

Adds `REFRESH_TEXTUREFORMAT_B8G8R8A8`.

Adds `Refresh_AcquireSwapchainTexture`. Returns a swapchain texture for the requested window.

Removes `Refresh_QueuePresent`. It is now assumed that the texture returned by `Refresh_AcquireSwapchainTexture` will be presented. This texture can be manipulated like any other texture.

Adds `Refresh_GetSwapchainFormat`. Returns the swapchain format for the requested window.

Reviewed-on: MoonsideGames/Refresh#15
Co-authored-by: cosmonaut <evan@moonside.games>
Co-committed-by: cosmonaut <evan@moonside.games>
updatetemplate
cosmonaut 2022-03-02 06:33:57 +00:00
parent d22bed8b76
commit cb99489b3c
6 changed files with 965 additions and 653 deletions

View File

@ -58,7 +58,6 @@ typedef struct Refresh_Device Refresh_Device;
typedef struct Refresh_Buffer Refresh_Buffer;
typedef struct Refresh_Texture Refresh_Texture;
typedef struct Refresh_Sampler Refresh_Sampler;
typedef struct Refresh_RenderTarget Refresh_RenderTarget;
typedef struct Refresh_ShaderModule Refresh_ShaderModule;
typedef struct Refresh_ComputePipeline Refresh_ComputePipeline;
typedef struct Refresh_GraphicsPipeline Refresh_GraphicsPipeline;
@ -113,6 +112,7 @@ typedef enum Refresh_TextureFormat
{
/* Color Formats */
REFRESH_TEXTUREFORMAT_R8G8B8A8,
REFRESH_TEXTUREFORMAT_B8G8R8A8,
REFRESH_TEXTUREFORMAT_R5G6B5,
REFRESH_TEXTUREFORMAT_A1R5G5B5,
REFRESH_TEXTUREFORMAT_B4G4R4A4,
@ -585,7 +585,11 @@ typedef struct Refresh_GraphicsPipelineCreateInfo
typedef struct Refresh_ColorAttachmentInfo
{
Refresh_RenderTarget *pRenderTarget;
Refresh_Texture *texture; /* We can't use TextureSlice because render passes take a single rectangle. */
uint32_t depth;
uint32_t layer;
uint32_t level;
Refresh_SampleCount sampleCount;
Refresh_Vec4 clearColor; /* Can be ignored by RenderPass */
Refresh_LoadOp loadOp;
Refresh_StoreOp storeOp;
@ -593,8 +597,11 @@ typedef struct Refresh_ColorAttachmentInfo
typedef struct Refresh_DepthStencilAttachmentInfo
{
Refresh_RenderTarget *pDepthStencilTarget;
Refresh_DepthStencilValue depthStencilValue; /* Can be ignored by RenderPass */
Refresh_Texture *texture; /* We can't use TextureSlice because render passes take a single rectangle. */
uint32_t depth;
uint32_t layer;
uint32_t level;
Refresh_DepthStencilValue depthStencilClearValue; /* Can be ignored by RenderPass */
Refresh_LoadOp loadOp;
Refresh_StoreOp storeOp;
Refresh_LoadOp stencilLoadOp;
@ -831,17 +838,6 @@ REFRESHAPI Refresh_Texture* Refresh_CreateTexture(
Refresh_TextureCreateInfo *textureCreateInfo
);
/* Creates a color target.
*
* textureSlice: The texture slice that the color target will resolve to.
* multisampleCount: The MSAA value for the color target.
*/
REFRESHAPI Refresh_RenderTarget* Refresh_CreateRenderTarget(
Refresh_Device *device,
Refresh_TextureSlice *textureSlice,
Refresh_SampleCount multisampleCount
);
/* Creates a buffer.
*
* usageFlags: Specifies how the buffer will be used.
@ -1053,19 +1049,6 @@ REFRESHAPI void Refresh_QueueDestroyBuffer(
Refresh_Buffer *buffer
);
/* Sends a color target to be destroyed by the renderer. Note that we call it
* "QueueDestroy" because it may not be immediately destroyed by the renderer if
* this is not called from the main thread (for example, if a garbage collector
* deletes the resource instead of the programmer).
*
* renderTarget: The Refresh_ColorTarget to be destroyed.
*/
REFRESHAPI void Refresh_QueueDestroyRenderTarget(
Refresh_Device *device,
Refresh_CommandBuffer *commandBuffer,
Refresh_RenderTarget *renderTarget
);
/* Sends a shader module to be destroyed by the renderer. Note that we call it
* "QueueDestroy" because it may not be immediately destroyed by the renderer if
* this is not called from the main thread (for example, if a garbage collector
@ -1244,23 +1227,24 @@ REFRESHAPI Refresh_CommandBuffer* Refresh_AcquireCommandBuffer(
uint8_t fixed
);
/* Queues an image to be presented to a window.
* The image will be presented upon the next Refresh_Submit call.
/* Acquires a texture to use for presentation.
* May return NULL under certain conditions.
* If NULL, the user must ensure to not present.
* Once a swapchain texture is acquired,
* it will automatically be presented on command buffer submission.
*
* NOTE:
* It is an error to call this function in headless mode.
*
* textureSlice: The texture slice to present.
* destinationRectangle: The region of the window to update. Can be NULL.
* filter: The filter to use if scaling is required.
* windowHandle: The window to present to.
* It is not recommended to hold a reference to this texture long term.
*/
REFRESHAPI void Refresh_QueuePresent(
REFRESHAPI Refresh_Texture* Refresh_AcquireSwapchainTexture(
Refresh_Device *device,
Refresh_CommandBuffer *commandBuffer,
Refresh_TextureSlice *textureSlice,
Refresh_Rect *destinationRectangle,
Refresh_Filter filter,
void *windowHandle
);
/* Returns the format of the swapchain for the given window. */
REFRESHAPI Refresh_TextureFormat Refresh_GetSwapchainFormat(
Refresh_Device *device,
void *windowHandle
);
@ -1271,7 +1255,7 @@ REFRESHAPI void Refresh_Submit(
Refresh_CommandBuffer **pCommandBuffers
);
/* Waits for the previous submission to complete. */
/* Waits for all submissions to complete. */
REFRESHAPI void Refresh_Wait(
Refresh_Device *device
);

View File

@ -68,17 +68,19 @@ REFRESHAPI void Refresh_Image_Free(uint8_t *mem);
/* Image Write API */
/* Encodes RGBA8 image data into PNG data.
/* Encodes 32-bit color data into PNG data.
*
* filename: The filename that the image will be written to.
* w: The width of the PNG data.
* h: The height of the PNG data.
* data: The raw RGBA8 image data.
* bgra: Whether the data is in BGRA8 format. Otherwise will assume RBGA8.
* data: The raw color data.
*/
REFRESHAPI void Refresh_Image_SavePNG(
char const *filename,
int32_t w,
int32_t h,
uint8_t bgra,
uint8_t *data
);

View File

@ -305,19 +305,6 @@ Refresh_Texture* Refresh_CreateTexture(
);
}
Refresh_RenderTarget* Refresh_CreateRenderTarget(
Refresh_Device *device,
Refresh_TextureSlice *textureSlice,
Refresh_SampleCount multisampleCount
) {
NULL_RETURN_NULL(device);
return device->CreateRenderTarget(
device->driverData,
textureSlice,
multisampleCount
);
}
Refresh_Buffer* Refresh_CreateBuffer(
Refresh_Device *device,
Refresh_BufferUsageFlags usageFlags,
@ -557,19 +544,6 @@ void Refresh_QueueDestroyBuffer(
);
}
void Refresh_QueueDestroyRenderTarget(
Refresh_Device *device,
Refresh_CommandBuffer *commandBuffer,
Refresh_RenderTarget *renderTarget
) {
NULL_RETURN(device);
device->QueueDestroyRenderTarget(
device->driverData,
commandBuffer,
renderTarget
);
}
void Refresh_QueueDestroyShaderModule(
Refresh_Device *device,
Refresh_CommandBuffer *commandBuffer,
@ -738,21 +712,26 @@ Refresh_CommandBuffer* Refresh_AcquireCommandBuffer(
);
}
void Refresh_QueuePresent(
Refresh_Texture* Refresh_AcquireSwapchainTexture(
Refresh_Device *device,
Refresh_CommandBuffer *commandBuffer,
Refresh_TextureSlice* textureSlice,
Refresh_Rect *destinationRectangle,
Refresh_Filter filter,
void *windowHandle
) {
NULL_RETURN(device);
device->QueuePresent(
NULL_RETURN_NULL(device);
return device->AcquireSwapchainTexture(
device->driverData,
commandBuffer,
textureSlice,
destinationRectangle,
filter,
windowHandle
);
}
Refresh_TextureFormat Refresh_GetSwapchainFormat(
Refresh_Device *device,
void *windowHandle
) {
if (device == NULL) { return 0; }
return device->GetSwapchainFormat(
device->driverData,
windowHandle
);
}

View File

@ -154,6 +154,7 @@ static inline int32_t BytesPerImage(
#define MAX_BUFFER_BINDINGS 16
#define MAX_COLOR_TARGET_BINDINGS 4
#define MAX_PRESENT_COUNT 16
/* Refresh_Device Definition */
@ -243,12 +244,6 @@ struct Refresh_Device
Refresh_TextureCreateInfo *textureCreateInfo
);
Refresh_RenderTarget* (*CreateRenderTarget)(
Refresh_Renderer *driverData,
Refresh_TextureSlice *textureSlice,
Refresh_SampleCount multisampleCount
);
Refresh_Buffer* (*CreateBuffer)(
Refresh_Renderer *driverData,
Refresh_BufferUsageFlags usageFlags,
@ -367,12 +362,6 @@ struct Refresh_Device
Refresh_Buffer *buffer
);
void(*QueueDestroyRenderTarget)(
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
Refresh_RenderTarget *renderTarget
);
void(*QueueDestroyShaderModule)(
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
@ -453,12 +442,14 @@ struct Refresh_Device
uint8_t fixed
);
void(*QueuePresent)(
Refresh_Texture* (*AcquireSwapchainTexture)(
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
Refresh_TextureSlice *textureSlice,
Refresh_Rect *destinationRectangle,
Refresh_Filter filter,
void *windowHandle
);
Refresh_TextureFormat (*GetSwapchainFormat)(
Refresh_Renderer *driverData,
void *windowHandle
);
@ -490,7 +481,6 @@ struct Refresh_Device
ASSIGN_DRIVER_FUNC(CreateSampler, name) \
ASSIGN_DRIVER_FUNC(CreateShaderModule, name) \
ASSIGN_DRIVER_FUNC(CreateTexture, name) \
ASSIGN_DRIVER_FUNC(CreateRenderTarget, name) \
ASSIGN_DRIVER_FUNC(CreateBuffer, name) \
ASSIGN_DRIVER_FUNC(SetTextureData, name) \
ASSIGN_DRIVER_FUNC(SetTextureDataYUV, name) \
@ -506,7 +496,6 @@ struct Refresh_Device
ASSIGN_DRIVER_FUNC(QueueDestroyTexture, name) \
ASSIGN_DRIVER_FUNC(QueueDestroySampler, name) \
ASSIGN_DRIVER_FUNC(QueueDestroyBuffer, name) \
ASSIGN_DRIVER_FUNC(QueueDestroyRenderTarget, name) \
ASSIGN_DRIVER_FUNC(QueueDestroyShaderModule, name) \
ASSIGN_DRIVER_FUNC(QueueDestroyComputePipeline, name) \
ASSIGN_DRIVER_FUNC(QueueDestroyGraphicsPipeline, name) \
@ -519,7 +508,8 @@ struct Refresh_Device
ASSIGN_DRIVER_FUNC(BindComputeBuffers, name) \
ASSIGN_DRIVER_FUNC(BindComputeTextures, name) \
ASSIGN_DRIVER_FUNC(AcquireCommandBuffer, name) \
ASSIGN_DRIVER_FUNC(QueuePresent, name) \
ASSIGN_DRIVER_FUNC(AcquireSwapchainTexture, name) \
ASSIGN_DRIVER_FUNC(GetSwapchainFormat, name) \
ASSIGN_DRIVER_FUNC(Submit, name) \
ASSIGN_DRIVER_FUNC(Wait, name)

File diff suppressed because it is too large Load Diff

View File

@ -209,9 +209,32 @@ void Refresh_Image_SavePNG(
const char *filename,
int32_t w,
int32_t h,
uint8_t bgra,
uint8_t *data
) {
uint32_t i;
uint8_t *bgraData;
if (bgra)
{
bgraData = SDL_malloc(w * h * 4);
for (i = 0; i < w * h * 4; i += 4)
{
bgraData[i] = data[i + 2];
bgraData[i + 1] = data[i + 1];
bgraData[i + 2] = data[i];
bgraData[i + 3] = data[i + 3];
}
stbi_write_png(filename, w, h, 4, bgraData, w * 4);
SDL_free(bgraData);
}
else
{
stbi_write_png(filename, w, h, 4, data, w * 4);
}
}
/* vim: set noexpandtab shiftwidth=8 tabstop=8: */