Skip to content

Commit e56a34c

Browse files
committed
Initial commit
0 parents  commit e56a34c

20 files changed

+1014
-0
lines changed

.gitignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Project
2+
build/
3+
tmp/
4+
5+
# Compiled Object files
6+
*.slo
7+
*.lo
8+
*.o
9+
*.obj
10+
11+
# Compiled Dynamic libraries
12+
*.so
13+
*.dylib
14+
*.dll
15+
16+
# Compiled Static libraries
17+
*.lai
18+
*.la
19+
*.a
20+
*.lib
21+
22+
# Executables
23+
*.exe
24+
*.out
25+
*.app
26+
27+
# Xcode
28+
xcuserdata/
29+
30+
# VSCode
31+
.vscode/

CMakeLists.txt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
project(graphics)
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
# OPTIONS
7+
8+
if(NOT CMAKE_BUILD_TYPE)
9+
set(CMAKE_BUILD_TYPE Release)
10+
endif()
11+
12+
option(BACKEND_METAL "Build the Metal backend" ON)
13+
option(BACKEND_OPENGL "Build the OpenGL backend" OFF)
14+
15+
# TARGET
16+
17+
add_library(graphics STATIC)
18+
19+
target_include_directories(graphics
20+
PUBLIC
21+
include
22+
PRIVATE
23+
src
24+
)
25+
26+
# BACKENDS
27+
28+
if(BACKEND_METAL)
29+
target_compile_options(graphics
30+
PUBLIC
31+
"-fobjc-arc"
32+
)
33+
34+
target_sources(graphics
35+
PRIVATE
36+
src/graphics/metal/context.mm
37+
# src/graphics/metal/convert.mm
38+
src/graphics/metal/rendertarget.mm
39+
src/graphics/metal/state.mm
40+
src/graphics/metal/staticbuffer.mm
41+
src/graphics/metal/dynamicbuffer.mm
42+
src/graphics/metal/texture.mm
43+
)
44+
endif()
45+
46+
if(BACKEND_OPENGL)
47+
target_sources(graphics
48+
PRIVATE
49+
# src/graphics/opengl/*
50+
)
51+
endif()

include/graphics/descriptors.hpp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#ifndef graphics_descriptors_hpp
2+
#define graphics_descriptors_hpp
3+
4+
#include <cstdint> // uint32_t
5+
6+
namespace graphics {
7+
enum class CompareFunction : uint32_t {
8+
Never = 0,
9+
Less = 1,
10+
Equal = 2,
11+
LessEqual = 3,
12+
Greater = 4,
13+
NotEqual = 5,
14+
GreaterEqual = 6,
15+
Always = 7,
16+
};
17+
18+
enum class CullMode : uint32_t {
19+
None = 0,
20+
Front = 1,
21+
Back = 2,
22+
};
23+
24+
enum class Winding : uint32_t {
25+
Clockwise = 0,
26+
CounterClockwise = 1,
27+
};
28+
29+
enum class VertexFormat : uint32_t {
30+
Invalid = 0,
31+
Float = 1,
32+
Float2 = 2,
33+
Float3 = 3,
34+
Float4 = 4,
35+
};
36+
37+
enum class StepFunction : uint32_t {
38+
Constant = 0,
39+
PerVertex = 1,
40+
PerInstance = 2,
41+
};
42+
43+
enum class PixelFormat : uint32_t {
44+
Invalid = 0,
45+
46+
RGB24u = 1,
47+
RGB24s = 2,
48+
49+
RGBA32u = 3,
50+
RGBA32s = 4,
51+
BGRA32u = 5,
52+
53+
Depth16f = 6,
54+
Depth32f = 7,
55+
// Depth24f_Stencil8u = 8,
56+
};
57+
58+
enum class StorageHint : uint32_t {
59+
None = 0,
60+
MipMap = 1,
61+
Memoryless = 2,
62+
};
63+
64+
struct VertexAttribute {
65+
uint32_t id;
66+
VertexFormat format;
67+
uint32_t offset;
68+
uint32_t bufferIndex;
69+
};
70+
71+
struct BufferLayout {
72+
uint32_t bufferIndex;
73+
uint32_t stride;
74+
uint32_t stepRate;
75+
StepFunction stepFunction;
76+
};
77+
78+
struct VertexDesc {
79+
uint32_t attributeCount;
80+
uint32_t layoutCount;
81+
const VertexAttribute * attributes;
82+
const BufferLayout * layouts;
83+
};
84+
85+
struct Attachment {
86+
uint32_t id;
87+
void * texture;
88+
bool clear;
89+
};
90+
91+
struct RenderTargetDesc {
92+
uint32_t sampleCount;
93+
uint32_t colorAttachmentCount;
94+
const Attachment * colorAttachments;
95+
Attachment depthAttachment;
96+
};
97+
98+
// This is used to create an initialized (opaque) State object
99+
struct StateDesc {
100+
const char * vertexShader;
101+
const char * fragmentShader;
102+
const VertexDesc * vertexDesc;
103+
const RenderTargetDesc * targetDesc;
104+
CullMode cullMode;
105+
Winding winding;
106+
bool depthWriteEnabled;
107+
CompareFunction depthCompareFunction;
108+
float depthBias;
109+
float depthSlopeScale;
110+
float depthBiasClamp;
111+
};
112+
113+
struct TextureDesc {
114+
uint32_t width;
115+
uint32_t height;
116+
PixelFormat format;
117+
StorageHint storage;
118+
};
119+
}
120+
121+
#endif /* graphics_descriptors_hpp */

include/graphics/graphics.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef graphics_graphics_hpp
2+
#define graphics_graphics_hpp
3+
4+
#include "util/os.hpp"
5+
6+
#if !(GRAPHICS_USE_METAL || GRAPHICS_USE_OPENGL)
7+
// Try to determine best backend given the target platform
8+
#if OS(mac) || OS(ios)
9+
#define GRAPHICS_USE_METAL 1
10+
#else
11+
#define GRAPHICS_USE_OPENGL 1
12+
#endif
13+
#endif
14+
15+
#if GRAPHICS_USE_METAL
16+
#include "graphics/metal/context.hpp"
17+
namespace graphics = graphics::metal;
18+
#endif
19+
20+
#if GRAPHICS_USE_OPENGL
21+
#include "graphics/opengl/graphics.hpp"
22+
namespace graphics = graphics::opengl;
23+
#endif
24+
25+
#endif /* graphics_graphics_hpp */

include/graphics/metal/context.hpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#ifndef graphics_metal_context_hpp
2+
#define graphics_metal_context_hpp
3+
4+
#ifdef __OBJC__
5+
#include <Metal/Metal.h>
6+
#include <MetalKit/MetalKit.h>
7+
#endif
8+
9+
#include "graphics/metal/rendertarget.hpp"
10+
#include "graphics/metal/state.hpp"
11+
#include "graphics/metal/staticbuffer.hpp"
12+
#include "graphics/metal/dynamicbuffer.hpp"
13+
#include "graphics/metal/texture.hpp"
14+
15+
namespace graphics::metal {
16+
constexpr uint32_t BUFFER_COUNT = 3;
17+
18+
class Context {
19+
#ifdef __OBJC__
20+
dispatch_semaphore_t _inFlightSemaphore;
21+
MTKView * _view;
22+
id<MTLDevice> _device;
23+
id<MTLCommandQueue> _commandQueue;
24+
id<MTLCommandBuffer> _commandBuffer;
25+
id<MTLRenderCommandEncoder> _renderEncoder;
26+
#else
27+
void * _inFlightSemaphore;
28+
void * _view;
29+
void * _device;
30+
void * _commandQueue;
31+
void * _commandBuffer;
32+
void * _renderEncoder;
33+
#endif
34+
35+
uint32_t _frame;
36+
37+
public:
38+
#ifdef __OBJC__
39+
Context(MTKView * view);
40+
#endif
41+
~Context();
42+
43+
constexpr uint32_t getFrame() const { return _frame; }
44+
constexpr uint32_t getNextFrame() const { return (_frame + 1) % BUFFER_COUNT; }
45+
46+
StaticBuffer * createStaticBuffer(uint32_t size, uint32_t count, const void * data);
47+
DynamicBuffer * createDynamicBuffer(uint32_t size, uint32_t capacity);
48+
49+
Texture * createTexture(const TextureDesc & textureDesc);
50+
Texture * loadTexture(const char * textureName);
51+
52+
RenderTarget * createRenderTarget(const RenderTargetDesc & targetDesc);
53+
void setRenderTarget(RenderTarget * target);
54+
void changeColorAttachment(RenderTarget * target, uint32_t id, Texture * texture);
55+
void changeDepthAttachment(RenderTarget * target, Texture * texture);
56+
57+
State * createState(const StateDesc & desc);
58+
void setState(const State * state);
59+
60+
void startFrame();
61+
void commitFrame();
62+
63+
void setBuffer(uint32_t id, StaticBuffer * buffer, uint32_t offset = 0, bool forFragmentShader = false);
64+
void setBuffer(uint32_t id, DynamicBuffer * buffer, uint32_t offset = 0, bool forFragmentShader = false);
65+
void setTexture(uint32_t id, Texture * texture, bool forFragmentShader = true);
66+
67+
void drawTriangles(uint32_t count);
68+
void drawTriangleStrip(uint32_t count);
69+
void drawIndexedTriangles(uint32_t count, StaticBuffer * indexBuffer);
70+
void drawIndexedTriangleStrip(uint32_t count, StaticBuffer * indexBuffer);
71+
72+
void drawInstancedTriangles(uint32_t instanceCount, uint32_t vertexCount);
73+
void drawInstancedTriangleStrip(uint32_t instanceCount, uint32_t vertexCount);
74+
void drawInstancedIndexedTriangles(uint32_t instanceCount, uint32_t indexCount, StaticBuffer * indexBuffer);
75+
void drawInstancedIndexedTriangleStrip(uint32_t instanceCount, uint32_t indexCount, StaticBuffer * indexBuffer);
76+
};
77+
}
78+
79+
#endif /* graphics_metal_context_hpp */
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#ifndef graphics_metal_dynamicbuffer_hpp
2+
#define graphics_metal_dynamicbuffer_hpp
3+
4+
#ifdef __OBJC__
5+
#include <Metal/Metal.h>
6+
#endif
7+
8+
#include "util/os.hpp"
9+
10+
namespace graphics::metal {
11+
class Context;
12+
13+
class DynamicBuffer {
14+
uint32_t _size;
15+
uint32_t _count;
16+
uint32_t _capacity;
17+
18+
#ifdef __OBJC__
19+
id<MTLBuffer> _buffer;
20+
#else
21+
void * _buffer;
22+
#endif
23+
24+
friend class Context;
25+
26+
static constexpr uint32_t getBufferSize(uint32_t size, uint32_t capacity) {
27+
return (((size * capacity) & ~0xff) + 0x100);
28+
}
29+
30+
public:
31+
#ifdef __OBJC__
32+
DynamicBuffer(uint32_t size, uint32_t capacity, id<MTLDevice> device);
33+
#endif
34+
~DynamicBuffer();
35+
36+
constexpr uint32_t getSize() const { return _size; }
37+
constexpr uint32_t getCount() const { return _count; }
38+
constexpr uint32_t getCapacity() const { return _capacity; }
39+
40+
constexpr uint32_t getOffset(uint32_t frame) const {
41+
return frame * getBufferSize(_size, _capacity);
42+
}
43+
44+
void update(uint32_t frame);
45+
void update(uint32_t frame, uint32_t startByte, uint32_t endByte);
46+
47+
void * getData(uint32_t frame);
48+
};
49+
}
50+
51+
#endif /* graphics_metal_dynamicbuffer_hpp */
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef graphics_metal_rendertarget_hpp
2+
#define graphics_metal_rendertarget_hpp
3+
4+
#ifdef __OBJC__
5+
#include <Metal/Metal.h>
6+
#endif
7+
8+
namespace graphics::metal {
9+
class Context;
10+
11+
class RenderTarget {
12+
#ifdef __OBJC__
13+
MTLRenderPassDescriptor * _renderPassDescriptor;
14+
#else
15+
void * _renderPassDescriptor;
16+
#endif
17+
18+
friend class Context;
19+
20+
public:
21+
#ifdef __OBJC__
22+
RenderTarget(MTLRenderPassDescriptor * renderPassDescriptor);
23+
#endif
24+
~RenderTarget();
25+
};
26+
}
27+
28+
#endif /* graphics_metal_rendertarget_hpp */

0 commit comments

Comments
 (0)