lovr/src/graphics/graphics.h

259 lines
7.3 KiB
C
Raw Normal View History

#include "graphics/canvas.h"
2017-02-03 23:16:30 +00:00
#include "graphics/font.h"
2017-10-21 21:32:41 +00:00
#include "graphics/material.h"
2017-12-10 04:07:32 +00:00
#include "graphics/mesh.h"
2016-11-19 09:28:01 +00:00
#include "graphics/shader.h"
2016-11-27 02:58:58 +00:00
#include "graphics/texture.h"
2017-01-21 03:55:54 +00:00
#include "math/math.h"
2018-07-04 20:51:35 +00:00
#include "util.h"
2018-11-16 10:26:56 +00:00
#include "platform.h"
2017-10-31 08:14:09 +00:00
#include <stdbool.h>
2018-07-17 06:51:11 +00:00
#include <stdint.h>
2016-09-21 07:55:53 +00:00
2017-01-26 10:20:30 +00:00
#pragma once
2016-09-27 06:48:09 +00:00
2018-07-17 06:51:11 +00:00
#define MAX_TRANSFORMS 64
#define MAX_PIPELINES 16
2018-12-11 21:27:59 +00:00
#define MAX_VERTICES (1 << 16)
#define MAX_INDICES (1 << 16)
#define MAX_BATCH_SIZE 192 // Enough to fit in any UBO
2016-09-29 04:47:36 +00:00
2018-07-16 00:05:06 +00:00
typedef void (*StencilCallback)(void* userdata);
2018-07-17 06:51:11 +00:00
typedef enum {
ARC_MODE_PIE,
ARC_MODE_OPEN,
ARC_MODE_CLOSED
} ArcMode;
2018-07-16 00:05:06 +00:00
typedef enum {
BLEND_ALPHA,
BLEND_ADD,
BLEND_SUBTRACT,
BLEND_MULTIPLY,
BLEND_LIGHTEN,
BLEND_DARKEN,
BLEND_SCREEN,
BLEND_REPLACE
} BlendMode;
typedef enum {
BLEND_ALPHA_MULTIPLY,
BLEND_PREMULTIPLIED
} BlendAlphaMode;
typedef enum {
COMPARE_NONE,
COMPARE_EQUAL,
COMPARE_NEQUAL,
COMPARE_LESS,
COMPARE_LEQUAL,
COMPARE_GREATER,
COMPARE_GEQUAL
} CompareMode;
2018-07-17 06:51:11 +00:00
typedef enum {
STYLE_FILL,
STYLE_LINE
} DrawStyle;
2018-07-17 06:51:11 +00:00
2018-07-16 00:05:06 +00:00
typedef enum {
STENCIL_REPLACE,
STENCIL_INCREMENT,
STENCIL_DECREMENT,
STENCIL_INCREMENT_WRAP,
STENCIL_DECREMENT_WRAP,
STENCIL_INVERT
} StencilAction;
2018-07-17 06:51:11 +00:00
typedef enum {
WINDING_CLOCKWISE,
WINDING_COUNTERCLOCKWISE
} Winding;
2018-08-03 18:43:15 +00:00
typedef struct {
bool computeShaders;
bool singlepass;
2018-08-31 13:03:35 +00:00
} GpuFeatures;
2018-08-03 18:43:15 +00:00
2018-07-16 00:05:06 +00:00
typedef struct {
bool initialized;
float pointSizes[2];
int textureSize;
int textureMSAA;
float textureAnisotropy;
int blockSize;
2018-08-31 13:03:35 +00:00
} GpuLimits;
2018-07-16 00:05:06 +00:00
2018-07-17 06:51:11 +00:00
typedef struct {
int shaderSwitches;
int drawCalls;
2018-08-31 13:03:35 +00:00
} GpuStats;
2018-07-17 06:51:11 +00:00
2018-12-11 05:30:55 +00:00
// Internal
2018-07-17 06:51:11 +00:00
typedef struct {
bool stereo;
2018-07-17 06:51:11 +00:00
Canvas* canvas;
float viewMatrix[2][16];
float projection[2][16];
2018-07-17 06:51:11 +00:00
} Camera;
2018-07-16 00:05:06 +00:00
typedef struct {
BlendMode blendMode : 3;
BlendAlphaMode blendAlphaMode : 1;
CompareMode depthTest : 3;
bool depthWrite : 1;
uint8_t lineWidth : 8;
uint8_t stencilValue: 8;
CompareMode stencilMode : 3;
bool alphaSampling : 1;
bool culling : 1;
Winding winding : 1;
bool wireframe : 1;
2018-07-16 00:05:06 +00:00
} Pipeline;
typedef struct {
2018-07-14 00:12:30 +00:00
Mesh* mesh;
DrawMode mode;
2018-12-11 05:30:55 +00:00
struct { uint32_t count; float* data; } vertex;
struct { uint32_t count; uint16_t* data; } index;
2018-07-17 06:51:11 +00:00
DefaultShader shader;
2018-12-11 05:30:55 +00:00
Texture* diffuseTexture;
Texture* environmentMap;
2018-07-17 06:51:11 +00:00
Material* material;
mat4 transform;
2018-12-05 18:22:12 +00:00
float* pose;
2018-12-11 05:30:55 +00:00
int instances;
bool mono;
2018-12-25 02:44:38 +00:00
} DrawRequest;
2018-07-14 00:12:30 +00:00
2016-09-27 06:48:09 +00:00
typedef struct {
bool initialized;
2018-07-17 06:51:11 +00:00
bool gammaCorrect;
2018-08-29 06:05:09 +00:00
int width;
int height;
2018-07-17 06:51:11 +00:00
Camera camera;
Shader* defaultShaders[MAX_DEFAULT_SHADERS];
2017-10-21 21:32:41 +00:00
Material* defaultMaterial;
2017-02-07 09:13:39 +00:00
Font* defaultFont;
2018-07-17 06:51:11 +00:00
Mesh* defaultMesh;
2017-08-02 07:29:47 +00:00
TextureFilter defaultFilter;
2018-07-17 06:51:11 +00:00
float transforms[MAX_TRANSFORMS][16];
int transform;
2018-07-17 06:51:11 +00:00
Pipeline pipelines[MAX_PIPELINES];
2018-12-11 21:27:59 +00:00
Pipeline* pipeline;
int pipelineIndex;
2018-12-25 02:53:23 +00:00
Color backgroundColor;
2018-12-25 02:56:26 +00:00
Canvas* canvas;
2018-12-25 02:57:27 +00:00
Color color;
2018-12-25 02:58:21 +00:00
Font* font;
2018-12-25 02:59:00 +00:00
float pointSize;
2018-12-25 03:00:23 +00:00
Shader* shader;
2018-12-25 02:44:38 +00:00
DrawRequest batch;
2018-12-11 21:27:59 +00:00
int batchVertex;
int batchIndex;
int batchSize;
2018-12-11 05:30:55 +00:00
int vertexCursor;
2018-12-11 21:27:59 +00:00
int indexCursor;
2018-12-11 05:30:55 +00:00
ShaderBlock* block;
Buffer* vertexMap;
2018-12-12 06:10:29 +00:00
Buffer* identityBuffer;
2016-09-27 06:48:09 +00:00
} GraphicsState;
2016-11-23 05:07:33 +00:00
2016-11-23 05:16:13 +00:00
// Base
bool lovrGraphicsInit(bool gammaCorrect);
void lovrGraphicsDestroy();
2016-08-10 06:28:17 +00:00
void lovrGraphicsPresent();
2018-11-16 15:19:29 +00:00
void lovrGraphicsSetWindow(WindowFlags* flags);
2018-08-29 06:05:09 +00:00
int lovrGraphicsGetWidth();
int lovrGraphicsGetHeight();
2018-07-17 06:51:11 +00:00
void lovrGraphicsSetCamera(Camera* camera, bool clear);
2018-12-12 06:10:29 +00:00
Buffer* lovrGraphicsGetIdentityBuffer();
2018-08-31 13:03:35 +00:00
#define lovrGraphicsGetSupported lovrGpuGetSupported
#define lovrGraphicsGetLimits lovrGpuGetLimits
#define lovrGraphicsGetStats lovrGpuGetStats
2016-11-23 05:16:13 +00:00
// State
2018-07-16 00:05:06 +00:00
void lovrGraphicsReset();
2018-07-17 06:51:11 +00:00
void lovrGraphicsPushPipeline();
void lovrGraphicsPopPipeline();
bool lovrGraphicsGetAlphaSampling();
2018-12-12 06:54:13 +00:00
void lovrGraphicsSetAlphaSampling(bool sample);
2017-08-02 08:25:56 +00:00
Color lovrGraphicsGetBackgroundColor();
void lovrGraphicsSetBackgroundColor(Color color);
2017-03-12 11:03:36 +00:00
void lovrGraphicsGetBlendMode(BlendMode* mode, BlendAlphaMode* alphaMode);
void lovrGraphicsSetBlendMode(BlendMode mode, BlendAlphaMode alphaMode);
Canvas* lovrGraphicsGetCanvas();
void lovrGraphicsSetCanvas(Canvas* canvas);
2017-08-02 08:25:56 +00:00
Color lovrGraphicsGetColor();
void lovrGraphicsSetColor(Color color);
2017-10-31 08:14:09 +00:00
bool lovrGraphicsIsCullingEnabled();
void lovrGraphicsSetCullingEnabled(bool culling);
2017-08-02 07:54:33 +00:00
TextureFilter lovrGraphicsGetDefaultFilter();
void lovrGraphicsSetDefaultFilter(TextureFilter filter);
2018-02-09 05:50:47 +00:00
void lovrGraphicsGetDepthTest(CompareMode* mode, bool* write);
void lovrGraphicsSetDepthTest(CompareMode depthTest, bool write);
2017-08-08 08:36:29 +00:00
Font* lovrGraphicsGetFont();
void lovrGraphicsSetFont(Font* font);
bool lovrGraphicsIsGammaCorrect();
2017-08-02 07:54:33 +00:00
float lovrGraphicsGetLineWidth();
void lovrGraphicsSetLineWidth(uint8_t width);
2017-08-02 07:54:33 +00:00
float lovrGraphicsGetPointSize();
void lovrGraphicsSetPointSize(float size);
2017-08-08 08:36:29 +00:00
Shader* lovrGraphicsGetShader();
void lovrGraphicsSetShader(Shader* shader);
void lovrGraphicsGetStencilTest(CompareMode* mode, int* value);
void lovrGraphicsSetStencilTest(CompareMode mode, int value);
2017-08-02 07:54:33 +00:00
Winding lovrGraphicsGetWinding();
void lovrGraphicsSetWinding(Winding winding);
2017-10-31 08:14:09 +00:00
bool lovrGraphicsIsWireframe();
void lovrGraphicsSetWireframe(bool wireframe);
2016-11-23 05:16:13 +00:00
// Transforms
void lovrGraphicsPush();
void lovrGraphicsPop();
2016-09-21 22:26:05 +00:00
void lovrGraphicsOrigin();
2018-11-27 23:03:05 +00:00
void lovrGraphicsTranslate(vec3 translation);
void lovrGraphicsRotate(quat rotation);
void lovrGraphicsScale(vec3 scale);
2018-03-05 07:06:34 +00:00
void lovrGraphicsMatrixTransform(mat4 transform);
void lovrGraphicsSetProjection(mat4 projection);
2016-11-23 05:16:13 +00:00
// Rendering
2018-12-11 21:27:59 +00:00
float* lovrGraphicsGetVertexPointer(uint32_t count);
uint16_t* lovrGraphicsGetIndexPointer(uint32_t count);
2018-07-16 00:05:06 +00:00
void lovrGraphicsClear(Color* color, float* depth, int* stencil);
2018-10-26 16:14:57 +00:00
void lovrGraphicsDiscard(bool color, bool depth, bool stencil);
2018-12-11 05:30:55 +00:00
void lovrGraphicsFlush();
2018-12-25 02:44:38 +00:00
void lovrGraphicsDraw(DrawRequest* draw);
2018-03-22 05:03:03 +00:00
void lovrGraphicsPoints(uint32_t count);
void lovrGraphicsLine(uint32_t count);
void lovrGraphicsTriangle(DrawStyle style, Material* material, float points[9]);
void lovrGraphicsPlane(DrawStyle style, Material* material, mat4 transform);
void lovrGraphicsBox(DrawStyle style, Material* material, mat4 transform);
void lovrGraphicsArc(DrawStyle style, ArcMode, Material* material, mat4 transform, float theta1, float theta2, int segments);
void lovrGraphicsCircle(DrawStyle style, Material* material, mat4 transform, int segments);
void lovrGraphicsCylinder(Material* material, float x1, float y1, float z1, float x2, float y2, float z2, float r1, float r2, bool capped, int segments);
void lovrGraphicsSphere(Material* material, mat4 transform, int segments);
2017-10-15 23:56:00 +00:00
void lovrGraphicsSkybox(Texture* texture, float angle, float ax, float ay, float az);
2017-08-02 02:58:24 +00:00
void lovrGraphicsPrint(const char* str, mat4 transform, float wrap, HorizontalAlign halign, VerticalAlign valign);
2018-10-01 01:40:51 +00:00
void lovrGraphicsFill(Texture* texture, float u, float v, float w, float h);
2018-08-31 13:03:35 +00:00
#define lovrGraphicsStencil lovrGpuStencil
2018-08-09 00:53:59 +00:00
#define lovrGraphicsCompute lovrGpuCompute
2018-07-17 10:35:01 +00:00
2018-12-11 05:30:55 +00:00
// GPU API
2018-11-16 10:26:56 +00:00
void lovrGpuInit(bool srgb, getProcAddressProc getProcAddress);
2018-07-17 10:35:01 +00:00
void lovrGpuDestroy();
2018-08-31 13:03:35 +00:00
void lovrGpuBindPipeline(Pipeline* pipeline);
void lovrGpuSetViewports(float* viewports, int count);
2018-08-20 03:48:45 +00:00
void lovrGpuClear(Canvas* canvas, Color* color, float* depth, int* stencil);
2018-10-26 16:14:57 +00:00
void lovrGpuDiscard(Canvas* canvas, bool color, bool depth, bool stencil);
2018-08-31 13:03:35 +00:00
void lovrGpuStencil(StencilAction action, int replaceValue, StencilCallback callback, void* userdata);
2018-08-09 00:53:59 +00:00
void lovrGpuCompute(Shader* shader, int x, int y, int z);
2018-07-17 10:35:01 +00:00
void lovrGpuPresent();
2018-09-01 06:24:59 +00:00
void lovrGpuDirtyTexture();
2018-08-31 13:03:35 +00:00
const GpuFeatures* lovrGpuGetSupported();
const GpuLimits* lovrGpuGetLimits();
const GpuStats* lovrGpuGetStats();