Shader first pass
This commit is contained in:
@@ -192,7 +192,7 @@ function sceneRender()
|
|||||||
spriteBatchPush(
|
spriteBatchPush(
|
||||||
nil,
|
nil,
|
||||||
x, y, x + 32, y + 32,
|
x, y, x + 32, y + 32,
|
||||||
colorBlue()
|
colorWhite()
|
||||||
)
|
)
|
||||||
|
|
||||||
-- Update mouse position
|
-- Update mouse position
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
|||||||
DUSK_OPENGL
|
DUSK_OPENGL
|
||||||
DUSK_LINUX
|
DUSK_LINUX
|
||||||
DUSK_DISPLAY_SIZE_DYNAMIC
|
DUSK_DISPLAY_SIZE_DYNAMIC
|
||||||
|
DUSK_OPENGL_SHADER
|
||||||
DUSK_DISPLAY_WIDTH_DEFAULT=640
|
DUSK_DISPLAY_WIDTH_DEFAULT=640
|
||||||
DUSK_DISPLAY_HEIGHT_DEFAULT=480
|
DUSK_DISPLAY_HEIGHT_DEFAULT=480
|
||||||
DUSK_DISPLAY_SCREEN_HEIGHT=240
|
DUSK_DISPLAY_SCREEN_HEIGHT=240
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ add_subdirectory(camera)
|
|||||||
add_subdirectory(framebuffer)
|
add_subdirectory(framebuffer)
|
||||||
add_subdirectory(mesh)
|
add_subdirectory(mesh)
|
||||||
add_subdirectory(screen)
|
add_subdirectory(screen)
|
||||||
|
add_subdirectory(shader)
|
||||||
add_subdirectory(spritebatch)
|
add_subdirectory(spritebatch)
|
||||||
add_subdirectory(text)
|
add_subdirectory(text)
|
||||||
add_subdirectory(texture)
|
add_subdirectory(texture)
|
||||||
|
|||||||
@@ -46,16 +46,16 @@ errorret_t displayUpdate(void) {
|
|||||||
errorChain(frameBufferBind(NULL));
|
errorChain(frameBufferBind(NULL));
|
||||||
|
|
||||||
// Bind screen and render scene
|
// Bind screen and render scene
|
||||||
screenBind();
|
errorChain(screenBind());
|
||||||
frameBufferClear(
|
frameBufferClear(
|
||||||
FRAMEBUFFER_CLEAR_COLOR | FRAMEBUFFER_CLEAR_DEPTH,
|
FRAMEBUFFER_CLEAR_COLOR | FRAMEBUFFER_CLEAR_DEPTH,
|
||||||
SCREEN.background
|
SCREEN.background
|
||||||
);
|
);
|
||||||
|
|
||||||
errorChain(sceneRender());
|
errorCatch(errorPrint(sceneRender()));
|
||||||
|
|
||||||
// Render UI
|
// Render UI
|
||||||
uiRender();
|
// uiRender();
|
||||||
|
|
||||||
// Finish up
|
// Finish up
|
||||||
screenUnbind();
|
screenUnbind();
|
||||||
|
|||||||
@@ -44,11 +44,11 @@ errorret_t screenInit() {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Init screen to backbuffer mode by default
|
// Init screen to backbuffer mode by default
|
||||||
screenBind();
|
errorChain(screenBind());
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
void screenBind() {
|
errorret_t screenBind() {
|
||||||
// Assume backbuffer is currently bound.
|
// Assume backbuffer is currently bound.
|
||||||
switch(SCREEN.mode) {
|
switch(SCREEN.mode) {
|
||||||
case SCREEN_MODE_BACKBUFFER: {
|
case SCREEN_MODE_BACKBUFFER: {
|
||||||
@@ -59,7 +59,7 @@ void screenBind() {
|
|||||||
// No needd for a framebuffer.
|
// No needd for a framebuffer.
|
||||||
#ifdef DUSK_DISPLAY_SIZE_DYNAMIC
|
#ifdef DUSK_DISPLAY_SIZE_DYNAMIC
|
||||||
if(SCREEN.framebufferReady) {
|
if(SCREEN.framebufferReady) {
|
||||||
frameBufferDispose(&SCREEN.framebuffer);
|
errorChain(frameBufferDispose(&SCREEN.framebuffer));
|
||||||
SCREEN.framebufferReady = false;
|
SCREEN.framebufferReady = false;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -79,19 +79,21 @@ void screenBind() {
|
|||||||
curFbHeight = frameBufferGetHeight(&SCREEN.framebuffer);
|
curFbHeight = frameBufferGetHeight(&SCREEN.framebuffer);
|
||||||
if(curFbWidth == SCREEN.width && curFbHeight == SCREEN.height) {
|
if(curFbWidth == SCREEN.width && curFbHeight == SCREEN.height) {
|
||||||
// Correct size, nothing to do.
|
// Correct size, nothing to do.
|
||||||
frameBufferBind(&SCREEN.framebuffer);
|
errorChain(frameBufferBind(&SCREEN.framebuffer));
|
||||||
return;
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Need a new framebuffer.
|
// Need a new framebuffer.
|
||||||
frameBufferDispose(&SCREEN.framebuffer);
|
errorChain(frameBufferDispose(&SCREEN.framebuffer));
|
||||||
SCREEN.framebufferReady = false;
|
SCREEN.framebufferReady = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new framebuffer
|
// Create new framebuffer
|
||||||
frameBufferInit(&SCREEN.framebuffer, SCREEN.width, SCREEN.height);
|
errorChain(frameBufferInit(
|
||||||
|
&SCREEN.framebuffer, SCREEN.width, SCREEN.height
|
||||||
|
));
|
||||||
SCREEN.framebufferReady = true;
|
SCREEN.framebufferReady = true;
|
||||||
frameBufferBind(&SCREEN.framebuffer);
|
errorChain(frameBufferBind(&SCREEN.framebuffer));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,10 +111,10 @@ void screenBind() {
|
|||||||
SCREEN.aspect = (float_t)SCREEN.width / (float_t)SCREEN.height;
|
SCREEN.aspect = (float_t)SCREEN.width / (float_t)SCREEN.height;
|
||||||
|
|
||||||
if(SCREEN.framebufferReady) {
|
if(SCREEN.framebufferReady) {
|
||||||
frameBufferDispose(&SCREEN.framebuffer);
|
errorChain(frameBufferDispose(&SCREEN.framebuffer));
|
||||||
SCREEN.framebufferReady = false;
|
SCREEN.framebufferReady = false;
|
||||||
}
|
}
|
||||||
return;
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t newFbWidth, newFbHeight;
|
int32_t newFbWidth, newFbHeight;
|
||||||
@@ -136,24 +138,26 @@ void screenBind() {
|
|||||||
SCREEN.width = newFbWidth;
|
SCREEN.width = newFbWidth;
|
||||||
SCREEN.height = newFbHeight;
|
SCREEN.height = newFbHeight;
|
||||||
SCREEN.aspect = (float_t)SCREEN.width / (float_t)SCREEN.height;
|
SCREEN.aspect = (float_t)SCREEN.width / (float_t)SCREEN.height;
|
||||||
frameBufferBind(&SCREEN.framebuffer);
|
errorChain(frameBufferBind(&SCREEN.framebuffer));
|
||||||
return;
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Need a new framebuffer.
|
// Need a new framebuffer.
|
||||||
frameBufferDispose(&SCREEN.framebuffer);
|
errorChain(frameBufferDispose(&SCREEN.framebuffer));
|
||||||
SCREEN.framebufferReady = false;
|
SCREEN.framebufferReady = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new framebuffer
|
// Create new framebuffer
|
||||||
frameBufferInit(&SCREEN.framebuffer, newFbWidth, newFbHeight);
|
errorChain(frameBufferInit(
|
||||||
|
&SCREEN.framebuffer, newFbWidth, newFbHeight
|
||||||
|
));
|
||||||
SCREEN.width = newFbWidth;
|
SCREEN.width = newFbWidth;
|
||||||
SCREEN.height = newFbHeight;
|
SCREEN.height = newFbHeight;
|
||||||
SCREEN.aspect = (float_t)SCREEN.width / (float_t)SCREEN.height;
|
SCREEN.aspect = (float_t)SCREEN.width / (float_t)SCREEN.height;
|
||||||
SCREEN.framebufferReady = true;
|
SCREEN.framebufferReady = true;
|
||||||
|
|
||||||
// Bind FB
|
// Bind FB
|
||||||
frameBufferBind(&SCREEN.framebuffer);
|
errorChain(frameBufferBind(&SCREEN.framebuffer));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,10 +177,10 @@ void screenBind() {
|
|||||||
if(fbWidth == newFbWidth && fbHeight == newFbHeight) {
|
if(fbWidth == newFbWidth && fbHeight == newFbHeight) {
|
||||||
// No need to use framebuffer.
|
// No need to use framebuffer.
|
||||||
if(SCREEN.framebufferReady) {
|
if(SCREEN.framebufferReady) {
|
||||||
frameBufferDispose(&SCREEN.framebuffer);
|
errorChain(frameBufferDispose(&SCREEN.framebuffer));
|
||||||
SCREEN.framebufferReady = false;
|
SCREEN.framebufferReady = false;
|
||||||
}
|
}
|
||||||
return;
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(SCREEN.framebufferReady) {
|
if(SCREEN.framebufferReady) {
|
||||||
@@ -185,19 +189,21 @@ void screenBind() {
|
|||||||
curFbWidth = frameBufferGetWidth(&SCREEN.framebuffer);
|
curFbWidth = frameBufferGetWidth(&SCREEN.framebuffer);
|
||||||
curFbHeight = frameBufferGetHeight(&SCREEN.framebuffer);
|
curFbHeight = frameBufferGetHeight(&SCREEN.framebuffer);
|
||||||
if(curFbWidth == newFbWidth && curFbHeight == newFbHeight) {
|
if(curFbWidth == newFbWidth && curFbHeight == newFbHeight) {
|
||||||
frameBufferBind(&SCREEN.framebuffer);
|
errorChain(frameBufferBind(&SCREEN.framebuffer));
|
||||||
return;
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Need a new framebuffer.
|
// Need a new framebuffer.
|
||||||
frameBufferDispose(&SCREEN.framebuffer);
|
errorChain(frameBufferDispose(&SCREEN.framebuffer));
|
||||||
SCREEN.framebufferReady = false;
|
SCREEN.framebufferReady = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new framebuffer.
|
// Create a new framebuffer.
|
||||||
frameBufferInit(&SCREEN.framebuffer, newFbWidth, newFbHeight);
|
errorChain(frameBufferInit(
|
||||||
|
&SCREEN.framebuffer, newFbWidth, newFbHeight
|
||||||
|
));
|
||||||
SCREEN.framebufferReady = true;
|
SCREEN.framebufferReady = true;
|
||||||
frameBufferBind(&SCREEN.framebuffer);
|
errorChain(frameBufferBind(&SCREEN.framebuffer));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -217,10 +223,10 @@ void screenBind() {
|
|||||||
if(fbWidth == newFbWidth && fbHeight == newFbHeight) {
|
if(fbWidth == newFbWidth && fbHeight == newFbHeight) {
|
||||||
// No need to use framebuffer.
|
// No need to use framebuffer.
|
||||||
if(SCREEN.framebufferReady) {
|
if(SCREEN.framebufferReady) {
|
||||||
frameBufferDispose(&SCREEN.framebuffer);
|
errorChain(frameBufferDispose(&SCREEN.framebuffer));
|
||||||
SCREEN.framebufferReady = false;
|
SCREEN.framebufferReady = false;
|
||||||
}
|
}
|
||||||
return;
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(SCREEN.framebufferReady) {
|
if(SCREEN.framebufferReady) {
|
||||||
@@ -229,19 +235,21 @@ void screenBind() {
|
|||||||
curFbWidth = frameBufferGetWidth(&SCREEN.framebuffer);
|
curFbWidth = frameBufferGetWidth(&SCREEN.framebuffer);
|
||||||
curFbHeight = frameBufferGetHeight(&SCREEN.framebuffer);
|
curFbHeight = frameBufferGetHeight(&SCREEN.framebuffer);
|
||||||
if(curFbWidth == newFbWidth && curFbHeight == newFbHeight) {
|
if(curFbWidth == newFbWidth && curFbHeight == newFbHeight) {
|
||||||
frameBufferBind(&SCREEN.framebuffer);
|
errorChain(frameBufferBind(&SCREEN.framebuffer));
|
||||||
return;
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Need a new framebuffer.
|
// Need a new framebuffer.
|
||||||
frameBufferDispose(&SCREEN.framebuffer);
|
errorChain(frameBufferDispose(&SCREEN.framebuffer));
|
||||||
SCREEN.framebufferReady = false;
|
SCREEN.framebufferReady = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new framebuffer.
|
// Create a new framebuffer.
|
||||||
frameBufferInit(&SCREEN.framebuffer, newFbWidth, newFbHeight);
|
errorChain(frameBufferInit(
|
||||||
|
&SCREEN.framebuffer, newFbWidth, newFbHeight
|
||||||
|
));
|
||||||
SCREEN.framebufferReady = true;
|
SCREEN.framebufferReady = true;
|
||||||
frameBufferBind(&SCREEN.framebuffer);
|
errorChain(frameBufferBind(&SCREEN.framebuffer));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -262,9 +270,11 @@ void screenBind() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
void screenUnbind() {
|
errorret_t screenUnbind() {
|
||||||
switch(SCREEN.mode) {
|
switch(SCREEN.mode) {
|
||||||
// Nothing to do here.
|
// Nothing to do here.
|
||||||
case SCREEN_MODE_BACKBUFFER:
|
case SCREEN_MODE_BACKBUFFER:
|
||||||
@@ -275,7 +285,9 @@ void screenUnbind() {
|
|||||||
case SCREEN_MODE_FIXED_HEIGHT:
|
case SCREEN_MODE_FIXED_HEIGHT:
|
||||||
case SCREEN_MODE_FIXED_SIZE:
|
case SCREEN_MODE_FIXED_SIZE:
|
||||||
case SCREEN_MODE_FIXED_WIDTH:
|
case SCREEN_MODE_FIXED_WIDTH:
|
||||||
if(SCREEN.framebufferReady) frameBufferBind(NULL);
|
if(SCREEN.framebufferReady) {
|
||||||
|
errorChain(frameBufferBind(NULL));
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SCREEN_MODE_FIXED_VIEWPORT_HEIGHT:
|
case SCREEN_MODE_FIXED_VIEWPORT_HEIGHT:
|
||||||
@@ -286,17 +298,19 @@ void screenUnbind() {
|
|||||||
assertUnreachable("Invalid screen mode.");
|
assertUnreachable("Invalid screen mode.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
void screenRender() {
|
errorret_t screenRender() {
|
||||||
if(SCREEN.mode == SCREEN_MODE_BACKBUFFER) {
|
if(SCREEN.mode == SCREEN_MODE_BACKBUFFER) {
|
||||||
return;
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DUSK_DISPLAY_SIZE_DYNAMIC
|
#ifdef DUSK_DISPLAY_SIZE_DYNAMIC
|
||||||
if(SCREEN.mode == SCREEN_MODE_FIXED_VIEWPORT_HEIGHT) {
|
if(SCREEN.mode == SCREEN_MODE_FIXED_VIEWPORT_HEIGHT) {
|
||||||
glViewport(0, 0, SCREEN.width, SCREEN.height);
|
glViewport(0, 0, SCREEN.width, SCREEN.height);
|
||||||
return;
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(
|
if(
|
||||||
@@ -307,7 +321,7 @@ void screenRender() {
|
|||||||
) {
|
) {
|
||||||
if(!SCREEN.framebufferReady) {
|
if(!SCREEN.framebufferReady) {
|
||||||
// Nothing to do here.
|
// Nothing to do here.
|
||||||
return;
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
float_t bbWidth, bbHeight;
|
float_t bbWidth, bbHeight;
|
||||||
@@ -357,22 +371,25 @@ void screenRender() {
|
|||||||
COLOR_BLACK
|
COLOR_BLACK
|
||||||
);
|
);
|
||||||
cameraPushMatrix(&SCREEN.framebufferCamera);
|
cameraPushMatrix(&SCREEN.framebufferCamera);
|
||||||
textureBind(&SCREEN.framebuffer.texture);
|
errorChain(textureBind(&SCREEN.framebuffer.texture));
|
||||||
meshDraw(&SCREEN.frameBufferMesh, 0, -1);
|
errorChain(meshDraw(&SCREEN.frameBufferMesh, 0, -1));
|
||||||
cameraPopMatrix();
|
cameraPopMatrix();
|
||||||
|
|
||||||
return;
|
errorOk();
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
assertUnreachable("Invalid screen mode.");
|
assertUnreachable("Invalid screen mode.");
|
||||||
|
errorThrow("Invalid screen mode.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void screenDispose() {
|
errorret_t screenDispose() {
|
||||||
#ifdef DUSK_DISPLAY_SIZE_DYNAMIC
|
#ifdef DUSK_DISPLAY_SIZE_DYNAMIC
|
||||||
if(SCREEN.framebufferReady) {
|
if(SCREEN.framebufferReady) {
|
||||||
frameBufferDispose(&SCREEN.framebuffer);
|
errorChain(frameBufferDispose(&SCREEN.framebuffer));
|
||||||
SCREEN.framebufferReady = false;
|
SCREEN.framebufferReady = false;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
errorOk();
|
||||||
}
|
}
|
||||||
@@ -90,20 +90,28 @@ errorret_t screenInit();
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Binds the screen, this is done before rendering game content.
|
* Binds the screen, this is done before rendering game content.
|
||||||
|
*
|
||||||
|
* @return Error code and state, if error occurs.
|
||||||
*/
|
*/
|
||||||
void screenBind();
|
errorret_t screenBind();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unbinds the screen, does nothing for now.
|
* Unbinds the screen, does nothing for now.
|
||||||
|
*
|
||||||
|
* @return Error code and state, if error occurs.
|
||||||
*/
|
*/
|
||||||
void screenUnbind();
|
errorret_t screenUnbind();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renders the screen to the current framebuffer.
|
* Renders the screen to the current framebuffer.
|
||||||
|
*
|
||||||
|
* @return Error code and state, if error occurs.
|
||||||
*/
|
*/
|
||||||
void screenRender();
|
errorret_t screenRender();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disposes the screen system.
|
* Disposes the screen system.
|
||||||
|
*
|
||||||
|
* @return Error code and state, if error occurs.
|
||||||
*/
|
*/
|
||||||
void screenDispose();
|
errorret_t screenDispose();
|
||||||
10
src/dusk/display/shader/CMakeLists.txt
Normal file
10
src/dusk/display/shader/CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# Copyright (c) 2026 Dominic Masters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
# Sources
|
||||||
|
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||||
|
PUBLIC
|
||||||
|
shader.c
|
||||||
|
)
|
||||||
39
src/dusk/display/shader/shader.c
Normal file
39
src/dusk/display/shader/shader.c
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "shader.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
|
||||||
|
errorret_t shaderInit(shader_t *shader, const shaderdefinition_t *def) {
|
||||||
|
assertNotNull(shader, "Shader cannot be null");
|
||||||
|
errorChain(shaderInitPlatform(shader, def));
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t shaderBind(shader_t *shader) {
|
||||||
|
assertNotNull(shader, "Shader cannot be null");
|
||||||
|
errorChain(shaderBindPlatform(shader));
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t shaderSetMatrix(
|
||||||
|
shader_t *shader,
|
||||||
|
const char_t *name,
|
||||||
|
const mat4 matrix
|
||||||
|
) {
|
||||||
|
assertNotNull(shader, "Shader cannot be null");
|
||||||
|
assertStrLenMin(name, 1, "Uniform name cannot be empty");
|
||||||
|
assertNotNull(matrix, "Matrix cannot be null");
|
||||||
|
errorChain(shaderSetMatrixPlatform(shader, name, matrix));
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t shaderDispose(shader_t *shader) {
|
||||||
|
assertNotNull(shader, "Shader cannot be null");
|
||||||
|
errorChain(shaderDisposePlatform(shader));
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
80
src/dusk/display/shader/shader.h
Normal file
80
src/dusk/display/shader/shader.h
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "error/error.h"
|
||||||
|
#include "display/texture/texture.h"
|
||||||
|
#include "display/shader/shaderplatform.h"
|
||||||
|
|
||||||
|
#ifndef shaderInitPlatform
|
||||||
|
#error "shaderInitPlatform must be defined to use shader.h"
|
||||||
|
#endif
|
||||||
|
#ifndef shaderBindPlatform
|
||||||
|
#error "shaderBindPlatform must be defined to use shader.h"
|
||||||
|
#endif
|
||||||
|
#ifndef shaderSetMatrixPlatform
|
||||||
|
#error "shaderSetMatrixPlatform must be defined to use shader.h"
|
||||||
|
#endif
|
||||||
|
#ifndef shaderDisposePlatform
|
||||||
|
#error "shaderDisposePlatform must be defined to use shader.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef shaderplatform_t shader_t;
|
||||||
|
typedef shaderdefinitionplatform_t shaderdefinition_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a shader. This is platform dependant.
|
||||||
|
*
|
||||||
|
* @param shader Shader to initialize
|
||||||
|
* @param def Definition of the shader to initialize with.
|
||||||
|
* @return Error if failure, otherwise errorOk
|
||||||
|
*/
|
||||||
|
errorret_t shaderInit(shader_t *shader, const shaderdefinition_t *def);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Binds a shader. This is platform dependant.
|
||||||
|
*
|
||||||
|
* @param shader Shader to bind
|
||||||
|
* @return Error if failure, otherwise errorOk
|
||||||
|
*/
|
||||||
|
errorret_t shaderBind(shader_t *shader);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a matrix uniform in the shader. This is platform dependant.
|
||||||
|
*
|
||||||
|
* @param shader Shader to set the matrix in
|
||||||
|
* @param name Name of the uniform to set
|
||||||
|
* @param matrix Matrix to set
|
||||||
|
* @return Error if failure, otherwise errorOk
|
||||||
|
*/
|
||||||
|
errorret_t shaderSetMatrix(
|
||||||
|
shader_t *shader,
|
||||||
|
const char_t *name,
|
||||||
|
const mat4 matrix
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a texture uniform in the shader. This is platform dependant.
|
||||||
|
*
|
||||||
|
* @param shader Shader to set the texture in
|
||||||
|
* @param name Name of the uniform to set
|
||||||
|
* @param texture Texture to set
|
||||||
|
* @return Error if failure, otherwise errorOk
|
||||||
|
*/
|
||||||
|
errorret_t shaderSetTexture(
|
||||||
|
shader_t *shader,
|
||||||
|
const char_t *name,
|
||||||
|
texture_t *texture
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disposes of a shader. This is platform dependant.
|
||||||
|
*
|
||||||
|
* @param shader Shader to dispose
|
||||||
|
* @return Error if failure, otherwise errorOk
|
||||||
|
*/
|
||||||
|
errorret_t shaderDispose(shader_t *shader);
|
||||||
15
src/dusk/display/shader/shaderunlit.h
Normal file
15
src/dusk/display/shader/shaderunlit.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "shader.h"
|
||||||
|
|
||||||
|
#define SHADER_UNLIT_PROJECTION "u_Proj"
|
||||||
|
#define SHADER_UNLIT_VIEW "u_View"
|
||||||
|
#define SHADER_UNLIT_MODEL "u_Model"
|
||||||
|
|
||||||
|
extern shaderdefinition_t SHADER_UNLIT_DEFINITION;
|
||||||
@@ -91,7 +91,7 @@ int moduleSpriteBatchPush(lua_State *L) {
|
|||||||
float_t maxX = (float_t)lua_tonumber(L, 4);
|
float_t maxX = (float_t)lua_tonumber(L, 4);
|
||||||
float_t maxY = (float_t)lua_tonumber(L, 5);
|
float_t maxY = (float_t)lua_tonumber(L, 5);
|
||||||
|
|
||||||
spriteBatchPush(
|
errorret_t ret = spriteBatchPush(
|
||||||
tex,
|
tex,
|
||||||
minX,
|
minX,
|
||||||
minY,
|
minY,
|
||||||
@@ -103,6 +103,14 @@ int moduleSpriteBatchPush(lua_State *L) {
|
|||||||
u1,
|
u1,
|
||||||
v1
|
v1
|
||||||
);
|
);
|
||||||
|
if(ret.code != ERROR_OK) {
|
||||||
|
int err = luaL_error(L,
|
||||||
|
"Failed to push sprite to batch: %s",
|
||||||
|
ret.state->message
|
||||||
|
);
|
||||||
|
errorCatch(errorPrint(ret));
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
15
src/duskgl/assert/assertgl.h
Normal file
15
src/duskgl/assert/assertgl.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "assert/assert.h"
|
||||||
|
#include "error/errorgl.h"
|
||||||
|
|
||||||
|
#define assertNoGLError(message) \
|
||||||
|
assertTrue(errorGLCheck().code == ERROR_OK, message)
|
||||||
|
|
||||||
|
// EOF
|
||||||
@@ -14,3 +14,4 @@ add_subdirectory(camera)
|
|||||||
add_subdirectory(framebuffer)
|
add_subdirectory(framebuffer)
|
||||||
add_subdirectory(texture)
|
add_subdirectory(texture)
|
||||||
add_subdirectory(mesh)
|
add_subdirectory(mesh)
|
||||||
|
add_subdirectory(shader)
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
#include "display/camera/camera.h"
|
#include "display/camera/camera.h"
|
||||||
#include "display/framebuffer/framebuffer.h"
|
#include "display/framebuffer/framebuffer.h"
|
||||||
#include "display/screen/screen.h"
|
#include "display/screen/screen.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assertgl.h"
|
||||||
|
|
||||||
void cameraPushMatrixGL(camera_t *camera) {
|
void cameraPushMatrixGL(camera_t *camera) {
|
||||||
assertNotNull(camera, "Not a camera component");
|
assertNotNull(camera, "Not a camera component");
|
||||||
@@ -117,16 +117,19 @@ void cameraPushMatrixGL(camera_t *camera) {
|
|||||||
assertUnreachable("Invalid camera view type");
|
assertUnreachable("Invalid camera view type");
|
||||||
}
|
}
|
||||||
|
|
||||||
glPushMatrix();
|
// glPushMatrix();
|
||||||
glMatrixMode(GL_PROJECTION);
|
// glMatrixMode(GL_PROJECTION);
|
||||||
glLoadIdentity();
|
// glLoadIdentity();
|
||||||
glLoadMatrixf((const GLfloat*)projection);
|
// glLoadMatrixf((const GLfloat*)projection);
|
||||||
|
assertNoGLError("Failed to set projection matrix");
|
||||||
|
|
||||||
glMatrixMode(GL_MODELVIEW);
|
// glMatrixMode(GL_MODELVIEW);
|
||||||
glLoadIdentity();
|
// glLoadIdentity();
|
||||||
glLoadMatrixf((const GLfloat*)view);
|
// glLoadMatrixf((const GLfloat*)view);
|
||||||
|
assertNoGLError("Failed to set view matrix");
|
||||||
}
|
}
|
||||||
|
|
||||||
void cameraPopMatrixGL(void) {
|
void cameraPopMatrixGL(void) {
|
||||||
glPopMatrix();
|
// glPopMatrix();
|
||||||
|
assertNoGLError("Failed to pop camera matrix");
|
||||||
}
|
}
|
||||||
@@ -9,8 +9,8 @@
|
|||||||
|
|
||||||
errorret_t displayOpenGLInit(void) {
|
errorret_t displayOpenGLInit(void) {
|
||||||
glDisable(GL_CULL_FACE);
|
glDisable(GL_CULL_FACE);
|
||||||
glDisable(GL_LIGHTING);// PSP defaults this on?
|
// glDisable(GL_LIGHTING);// PSP defaults this on?
|
||||||
glShadeModel(GL_SMOOTH); // Fixes color on PSP?
|
// glShadeModel(GL_SMOOTH); // Fixes color on PSP?
|
||||||
errorChain(errorGLCheck());
|
errorChain(errorGLCheck());
|
||||||
|
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
@@ -24,9 +24,12 @@ errorret_t displayOpenGLInit(void) {
|
|||||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||||
errorChain(errorGLCheck());
|
errorChain(errorGLCheck());
|
||||||
|
|
||||||
glEnableClientState(GL_COLOR_ARRAY);// To confirm: every frame on PSP?
|
// glEnableClientState(GL_COLOR_ARRAY);// To confirm: every frame on PSP?
|
||||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
// glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
glEnableClientState(GL_VERTEX_ARRAY);
|
// glEnableClientState(GL_VERTEX_ARRAY);
|
||||||
errorChain(errorGLCheck());
|
// errorChain(errorGLCheck());
|
||||||
|
|
||||||
|
errorChain(shaderInit(&testShader, &SHADER_UNLIT_DEFINITION));
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
@@ -7,8 +7,13 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "error/errorgl.h"
|
#include "error/errorgl.h"
|
||||||
|
#include "display/shader/shaderunlit.h"
|
||||||
|
|
||||||
|
static shadergl_t testShader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the OpenGL specific contexts for rendering.
|
* Initializes the OpenGL specific contexts for rendering.
|
||||||
|
*
|
||||||
|
* @return An errorret_t indicating success or failure of the initialization.
|
||||||
*/
|
*/
|
||||||
errorret_t displayOpenGLInit(void);
|
errorret_t displayOpenGLInit(void);
|
||||||
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
#include "display/display.h"
|
#include "display/display.h"
|
||||||
#include "display/framebuffer/framebuffer.h"
|
#include "display/framebuffer/framebuffer.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assertgl.h"
|
||||||
#include "util/memory.h"
|
#include "util/memory.h"
|
||||||
|
|
||||||
errorret_t frameBufferGLInitBackBuffer(void) {
|
errorret_t frameBufferGLInitBackBuffer(void) {
|
||||||
@@ -86,6 +86,7 @@ void frameBufferGLClear(const uint8_t flags, const color_t color) {
|
|||||||
color.b / 255.0f,
|
color.b / 255.0f,
|
||||||
color.a / 255.0f
|
color.a / 255.0f
|
||||||
);
|
);
|
||||||
|
assertNoGLError("Failed to set clear color");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(flags & FRAMEBUFFER_CLEAR_DEPTH) {
|
if(flags & FRAMEBUFFER_CLEAR_DEPTH) {
|
||||||
@@ -93,6 +94,7 @@ void frameBufferGLClear(const uint8_t flags, const color_t color) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
glClear(glFlags);
|
glClear(glFlags);
|
||||||
|
assertNoGLError("Failed to clear framebuffer");
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DUSK_DISPLAY_SIZE_DYNAMIC
|
#ifdef DUSK_DISPLAY_SIZE_DYNAMIC
|
||||||
|
|||||||
@@ -6,7 +6,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "display/mesh/mesh.h"
|
#include "display/mesh/mesh.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assertgl.h"
|
||||||
|
#include "error/errorgl.h"
|
||||||
|
|
||||||
errorret_t meshInitGL(
|
errorret_t meshInitGL(
|
||||||
meshgl_t *mesh,
|
meshgl_t *mesh,
|
||||||
@@ -33,26 +34,27 @@ errorret_t meshDrawGL(
|
|||||||
// PSP style pointer legacy OpenGL
|
// PSP style pointer legacy OpenGL
|
||||||
const GLsizei stride = sizeof(meshvertex_t);
|
const GLsizei stride = sizeof(meshvertex_t);
|
||||||
|
|
||||||
glColorPointer(
|
// glColorPointer(
|
||||||
sizeof(color4b_t),
|
// sizeof(color4b_t),
|
||||||
GL_UNSIGNED_BYTE,
|
// GL_UNSIGNED_BYTE,
|
||||||
stride,
|
// stride,
|
||||||
(const GLvoid*)&mesh->vertices[offset].color
|
// (const GLvoid*)&mesh->vertices[offset].color
|
||||||
);
|
// );
|
||||||
glTexCoordPointer(
|
// glTexCoordPointer(
|
||||||
MESH_VERTEX_UV_SIZE,
|
// MESH_VERTEX_UV_SIZE,
|
||||||
GL_FLOAT,
|
// GL_FLOAT,
|
||||||
stride,
|
// stride,
|
||||||
(const GLvoid*)&mesh->vertices[offset].uv
|
// (const GLvoid*)&mesh->vertices[offset].uv
|
||||||
);
|
// );
|
||||||
glVertexPointer(
|
// glVertexPointer(
|
||||||
MESH_VERTEX_POS_SIZE,
|
// MESH_VERTEX_POS_SIZE,
|
||||||
GL_FLOAT,
|
// GL_FLOAT,
|
||||||
stride,
|
// stride,
|
||||||
(const GLvoid*)&mesh->vertices[offset].pos
|
// (const GLvoid*)&mesh->vertices[offset].pos
|
||||||
);
|
// );
|
||||||
|
|
||||||
glDrawArrays(mesh->primitiveType, offset, count);
|
// glDrawArrays(mesh->primitiveType, offset, count);
|
||||||
|
// errorChain(errorGLCheck());
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|||||||
11
src/duskgl/display/shader/CMakeLists.txt
Normal file
11
src/duskgl/display/shader/CMakeLists.txt
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
# Copyright (c) 2026 Dominic Masters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
# Sources
|
||||||
|
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||||
|
PUBLIC
|
||||||
|
shadergl.c
|
||||||
|
shaderunlitgl.c
|
||||||
|
)
|
||||||
197
src/duskgl/display/shader/shadergl.c
Normal file
197
src/duskgl/display/shader/shadergl.c
Normal file
@@ -0,0 +1,197 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "shadergl.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
#include "assert/assertgl.h"
|
||||||
|
|
||||||
|
errorret_t shaderInitGL(shadergl_t *shader, const shaderdefinitiongl_t *def) {
|
||||||
|
assertNotNull(shader, "Shader cannot be null");
|
||||||
|
assertNotNull(def, "Shader definition cannot be null");
|
||||||
|
|
||||||
|
memoryZero(shader, sizeof(shadergl_t));
|
||||||
|
|
||||||
|
// Create vertex shader
|
||||||
|
shader->vertexShaderId = glCreateShader(GL_VERTEX_SHADER);
|
||||||
|
errorret_t err = errorGLCheck();
|
||||||
|
errorChain(err);
|
||||||
|
|
||||||
|
glShaderSource(shader->vertexShaderId, 1, &def->vert, NULL);
|
||||||
|
err = errorGLCheck();
|
||||||
|
if(err.code != ERROR_OK) {
|
||||||
|
glDeleteShader(shader->vertexShaderId);
|
||||||
|
errorChain(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
glCompileShader(shader->vertexShaderId);
|
||||||
|
err = errorGLCheck();
|
||||||
|
if(err.code != ERROR_OK) {
|
||||||
|
glDeleteShader(shader->vertexShaderId);
|
||||||
|
errorChain(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
GLint ok = 0;
|
||||||
|
glGetShaderiv(shader->vertexShaderId, GL_COMPILE_STATUS, &ok);
|
||||||
|
if(!ok) {
|
||||||
|
GLchar log[1024];
|
||||||
|
glGetShaderInfoLog(shader->vertexShaderId, sizeof(log), NULL, log);
|
||||||
|
glDeleteShader(shader->vertexShaderId);
|
||||||
|
errorThrow("Vertex shader compilation failed: %s", log);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create fragment shader
|
||||||
|
shader->fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
|
err = errorGLCheck();
|
||||||
|
if(err.code != ERROR_OK) {
|
||||||
|
glDeleteShader(shader->vertexShaderId);
|
||||||
|
errorChain(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
glShaderSource(shader->fragmentShaderId, 1, &def->frag, NULL);
|
||||||
|
err = errorGLCheck();
|
||||||
|
if(err.code != ERROR_OK) {
|
||||||
|
glDeleteShader(shader->vertexShaderId);
|
||||||
|
glDeleteShader(shader->fragmentShaderId);
|
||||||
|
errorChain(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
glCompileShader(shader->fragmentShaderId);
|
||||||
|
err = errorGLCheck();
|
||||||
|
if(err.code != ERROR_OK) {
|
||||||
|
glDeleteShader(shader->vertexShaderId);
|
||||||
|
glDeleteShader(shader->fragmentShaderId);
|
||||||
|
errorChain(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
glGetShaderiv(shader->fragmentShaderId, GL_COMPILE_STATUS, &ok);
|
||||||
|
if(!ok) {
|
||||||
|
GLchar log[1024];
|
||||||
|
glGetShaderInfoLog(shader->fragmentShaderId, sizeof(log), NULL, log);
|
||||||
|
glDeleteShader(shader->vertexShaderId);
|
||||||
|
glDeleteShader(shader->fragmentShaderId);
|
||||||
|
errorThrow("Fragment shader compilation failed: %s", log);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create shader program
|
||||||
|
shader->shaderProgramId = glCreateProgram();
|
||||||
|
err = errorGLCheck();
|
||||||
|
if(err.code != ERROR_OK) {
|
||||||
|
glDeleteShader(shader->vertexShaderId);
|
||||||
|
glDeleteShader(shader->fragmentShaderId);
|
||||||
|
errorChain(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
glAttachShader(shader->shaderProgramId, shader->vertexShaderId);
|
||||||
|
err = errorGLCheck();
|
||||||
|
if(err.code != ERROR_OK) {
|
||||||
|
glDeleteProgram(shader->shaderProgramId);
|
||||||
|
glDeleteShader(shader->vertexShaderId);
|
||||||
|
glDeleteShader(shader->fragmentShaderId);
|
||||||
|
errorChain(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
glAttachShader(shader->shaderProgramId, shader->fragmentShaderId);
|
||||||
|
err = errorGLCheck();
|
||||||
|
if(err.code != ERROR_OK) {
|
||||||
|
glDeleteProgram(shader->shaderProgramId);
|
||||||
|
glDeleteShader(shader->vertexShaderId);
|
||||||
|
glDeleteShader(shader->fragmentShaderId);
|
||||||
|
errorChain(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
glLinkProgram(shader->shaderProgramId);
|
||||||
|
err = errorGLCheck();
|
||||||
|
if(err.code != ERROR_OK) {
|
||||||
|
glDeleteProgram(shader->shaderProgramId);
|
||||||
|
glDeleteShader(shader->vertexShaderId);
|
||||||
|
glDeleteShader(shader->fragmentShaderId);
|
||||||
|
errorChain(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
ok = 0;
|
||||||
|
glGetProgramiv(shader->shaderProgramId, GL_LINK_STATUS, &ok);
|
||||||
|
if(!ok) {
|
||||||
|
GLchar log[1024];
|
||||||
|
glGetProgramInfoLog(shader->shaderProgramId, sizeof(log), NULL, log);
|
||||||
|
glDeleteProgram(shader->shaderProgramId);
|
||||||
|
glDeleteShader(shader->vertexShaderId);
|
||||||
|
glDeleteShader(shader->fragmentShaderId);
|
||||||
|
errorThrow("Shader program linking failed: %s", log);
|
||||||
|
}
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t shaderParamGetLocationGL(
|
||||||
|
shadergl_t *shader,
|
||||||
|
const char_t *name,
|
||||||
|
GLint *location
|
||||||
|
) {
|
||||||
|
assertNotNull(shader, "Shader cannot be null");
|
||||||
|
assertStrLenMin(name, 1, "Uniform name cannot be empty");
|
||||||
|
assertNotNull(location, "Location cannot be null");
|
||||||
|
|
||||||
|
shadergl_t *shaderGL = (shadergl_t *)shader;
|
||||||
|
*location = glGetUniformLocation(shaderGL->shaderProgramId, name);
|
||||||
|
errorret_t err = errorGLCheck();
|
||||||
|
if(err.code != ERROR_OK) {
|
||||||
|
errorChain(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t shaderSetMatrixGL(
|
||||||
|
shadergl_t *shader,
|
||||||
|
const char_t *name,
|
||||||
|
const mat4 mat
|
||||||
|
) {
|
||||||
|
assertNotNull(shader, "Shader cannot be null");
|
||||||
|
assertNotNull(mat, "Matrix data cannot be null");
|
||||||
|
assertStrLenMin(name, 1, "Uniform name cannot be empty");
|
||||||
|
|
||||||
|
GLint location;
|
||||||
|
errorChain(shaderParamGetLocationGL(shader, name, &location));
|
||||||
|
|
||||||
|
glUniformMatrix4fv(location, 1, GL_FALSE, (const GLfloat *)mat);
|
||||||
|
errorChain(errorGLCheck());
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
errorret_t shaderBindGL(shadergl_t *shader) {
|
||||||
|
assertNotNull(shader, "Shader cannot be null");
|
||||||
|
|
||||||
|
glUseProgram(shader->shaderProgramId);
|
||||||
|
errorChain(errorGLCheck());
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t shaderDisposeGL(shadergl_t *shader) {
|
||||||
|
assertNotNull(shader, "Shader cannot be null");
|
||||||
|
|
||||||
|
if(shader->shaderProgramId != 0) {
|
||||||
|
glDeleteProgram(shader->shaderProgramId);
|
||||||
|
errorChain(errorGLCheck());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(shader->vertexShaderId != 0) {
|
||||||
|
glDeleteShader(shader->vertexShaderId);
|
||||||
|
errorChain(errorGLCheck());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(shader->fragmentShaderId != 0) {
|
||||||
|
glDeleteShader(shader->fragmentShaderId);
|
||||||
|
errorChain(errorGLCheck());
|
||||||
|
}
|
||||||
|
|
||||||
|
assertNoGLError("Failed disposing shader");
|
||||||
|
memoryZero(shader, sizeof(shadergl_t));
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
72
src/duskgl/display/shader/shadergl.h
Normal file
72
src/duskgl/display/shader/shadergl.h
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "error/errorgl.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
GLuint shaderProgramId;
|
||||||
|
GLuint vertexShaderId;
|
||||||
|
GLuint fragmentShaderId;
|
||||||
|
} shadergl_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const char_t *vert;
|
||||||
|
const char_t *frag;
|
||||||
|
} shaderdefinitiongl_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a shader.
|
||||||
|
*
|
||||||
|
* @param shader The shader to initialize.
|
||||||
|
* @param def The definition of the shader to initialize with.
|
||||||
|
* @return An errorret_t indicating success or failure.
|
||||||
|
*/
|
||||||
|
errorret_t shaderInitGL(shadergl_t *shader, const shaderdefinitiongl_t *def);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Binds a shader for use in rendering.
|
||||||
|
*
|
||||||
|
* @param shader The shader to bind.
|
||||||
|
* @return An errorret_t indicating success or failure.
|
||||||
|
*/
|
||||||
|
errorret_t shaderBindGL(shadergl_t *shader);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the location of a shader uniform parameter.
|
||||||
|
*
|
||||||
|
* @param shader The shader to query.
|
||||||
|
* @param name The name of the uniform parameter.
|
||||||
|
* @param location Output parameter to receive the location of the uniform.
|
||||||
|
* @return An errorret_t indicating success or failure.
|
||||||
|
*/
|
||||||
|
errorret_t shaderParamGetLocationGL(
|
||||||
|
shadergl_t *shader,
|
||||||
|
const char_t *name,
|
||||||
|
GLint *location
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a mat4 uniform parameter in the shader.
|
||||||
|
*
|
||||||
|
* @param shader The shader to update.
|
||||||
|
* @param name The name of the uniform parameter.
|
||||||
|
* @param mat The 4x4 matrix data to set.
|
||||||
|
* @return An errorret_t indicating success or failure.
|
||||||
|
*/
|
||||||
|
errorret_t shaderSetMatrixGL(
|
||||||
|
shadergl_t *shader,
|
||||||
|
const char_t *name,
|
||||||
|
const mat4 matrix
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disposes of a shader, freeing any associated resources.
|
||||||
|
*
|
||||||
|
* @param shader The shader to dispose.
|
||||||
|
*/
|
||||||
|
errorret_t shaderDisposeGL(shadergl_t *shader);
|
||||||
18
src/duskgl/display/shader/shaderplatform.h
Normal file
18
src/duskgl/display/shader/shaderplatform.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "shadergl.h"
|
||||||
|
|
||||||
|
typedef shadergl_t shaderplatform_t;
|
||||||
|
typedef shaderdefinitiongl_t shaderdefinitionplatform_t;
|
||||||
|
|
||||||
|
#define shaderInitPlatform shaderInitGL
|
||||||
|
#define shaderBindPlatform shaderBindGL
|
||||||
|
#define shaderSetMatrixPlatform shaderSetMatrixGL
|
||||||
|
// #define shaderSetTexturePlatform shaderSetTextureGL
|
||||||
|
#define shaderDisposePlatform shaderDisposeGL
|
||||||
32
src/duskgl/display/shader/shaderunlitgl.c
Normal file
32
src/duskgl/display/shader/shaderunlitgl.c
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "display/shader/shaderunlit.h"
|
||||||
|
|
||||||
|
shaderdefinition_t SHADER_UNLIT_DEFINITION = {
|
||||||
|
.vert =
|
||||||
|
"#version 330 core\n"
|
||||||
|
"layout(location = 0) in vec3 aPos;\n"
|
||||||
|
"layout(location = 1) in vec2 aTexCoord;\n"
|
||||||
|
"uniform mat4 u_Proj;\n"
|
||||||
|
"uniform mat4 u_View;\n"
|
||||||
|
"uniform mat4 u_Model;\n"
|
||||||
|
"out vec2 v_TexCoord;\n"
|
||||||
|
"void main() {\n"
|
||||||
|
" gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n"
|
||||||
|
" v_TexCoord = aTexCoord;\n"
|
||||||
|
"}\n",
|
||||||
|
|
||||||
|
.frag =
|
||||||
|
"#version 330 core\n"
|
||||||
|
"in vec2 v_TexCoord;\n"
|
||||||
|
"out vec4 FragColor;\n"
|
||||||
|
"uniform sampler2D u_Texture;\n"
|
||||||
|
"void main() {\n"
|
||||||
|
" FragColor = texture(u_Texture, v_TexCoord);\n"
|
||||||
|
"}\n"
|
||||||
|
};
|
||||||
@@ -21,6 +21,11 @@ errorret_t displaySDL2Init(void) {
|
|||||||
|
|
||||||
// Set OpenGL attributes (Needs to be done now or later?)
|
// Set OpenGL attributes (Needs to be done now or later?)
|
||||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||||
|
// SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
||||||
|
// SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
|
||||||
|
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
|
||||||
|
|
||||||
// Create window with OpenGL flag.
|
// Create window with OpenGL flag.
|
||||||
DISPLAY.window = SDL_CreateWindow(
|
DISPLAY.window = SDL_CreateWindow(
|
||||||
@@ -44,7 +49,6 @@ errorret_t displaySDL2Init(void) {
|
|||||||
errorChain(errorGLCheck());
|
errorChain(errorGLCheck());
|
||||||
|
|
||||||
SDL_GL_SetSwapInterval(1);
|
SDL_GL_SetSwapInterval(1);
|
||||||
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
|
||||||
errorChain(errorGLCheck());
|
errorChain(errorGLCheck());
|
||||||
|
|
||||||
errorChain(displayOpenGLInit());
|
errorChain(displayOpenGLInit());
|
||||||
@@ -82,6 +86,9 @@ errorret_t displaySDL2Update(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SDL_GL_MakeCurrent(DISPLAY.window, DISPLAY.glContext);
|
SDL_GL_MakeCurrent(DISPLAY.window, DISPLAY.glContext);
|
||||||
|
|
||||||
|
// errorChain(shaderPaletteTextureBindGL(&testShader));
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user