main
Allen Webster 2023-09-29 18:02:40 -07:00
commit ed6303d2b9
12 changed files with 2488 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
build/

10
build_examples.bat Normal file
View File

@ -0,0 +1,10 @@
@echo off
if not exist "build\" (mkdir build)
set opts=-nologo -FC -Zi
pushd build
cl %opts% ..\example_texture_extraction.cpp dwrite.lib gdi32.lib /Feextract
cl %opts% ..\example_rasterizer.cpp dwrite.lib gdi32.lib user32.lib opengl32.lib /Ferasterize
popd

1070
dwrite_quick_start.html Normal file

File diff suppressed because it is too large Load Diff

BIN
example-glyph-bow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
example-glyph.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

55
example_gl_defines.h Normal file
View File

@ -0,0 +1,55 @@
// DirectWrite rasterization example: opengl types and constants that we need that are not in GL\gl.h
typedef char GLchar;
typedef ptrdiff_t GLsizeiptr;
typedef ptrdiff_t GLintptr;
typedef void GLDEBUGPROC_Type(GLenum source,GLenum type,GLuint id,GLenum severity,GLsizei length,const GLchar *message,const void *userParam);
typedef GLDEBUGPROC_Type *GLDEBUGPROC;
#define GL_FUNC(N,R,P) typedef R N##_Type P; static N##_Type *N = 0;
#include "example_gl_funcs.h"
#define GL_CONSTANT_COLOR 0x8001
#define GL_CLAMP_TO_EDGE 0x812F
#define GL_FRAMEBUFFER_UNDEFINED 0x8219
#define GL_DEBUG_OUTPUT_SYNCHRONOUS 0x8242
#define GL_DEBUG_SEVERITY_NOTIFICATION 0x826B
#define GL_TEXTURE0 0x84C0
#define GL_ARRAY_BUFFER 0x8892
#define GL_DYNAMIC_DRAW 0x88E8
#define GL_FRAGMENT_SHADER 0x8B30
#define GL_VERTEX_SHADER 0x8B31
#define GL_TEXTURE_2D_ARRAY 0x8C1A
#define GL_SRGB 0x8C40
#define GL_SRGB8 0x8C41
#define GL_READ_FRAMEBUFFER 0x8CA8
#define GL_DRAW_FRAMEBUFFER 0x8CA9
#define GL_FRAMEBUFFER_COMPLETE 0x8CD5
#define GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT 0x8CD6
#define GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT 0x8CD7
#define GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER 0x8CDB
#define GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER 0x8CDC
#define GL_FRAMEBUFFER_UNSUPPORTED 0x8CDD
#define GL_COLOR_ATTACHMENT0 0x8CE0
#define GL_FRAMEBUFFER 0x8D40
#define GL_FRAMEBUFFER_SRGB 0x8DB9
#define GL_DEBUG_SEVERITY_HIGH 0x9146
#define GL_DEBUG_SEVERITY_MEDIUM 0x9147
#define GL_DEBUG_SEVERITY_LOW 0x9148

43
example_gl_funcs.h Normal file
View File

@ -0,0 +1,43 @@
// DirectWrite rasterization example: opengl functions we need to manually load
GL_FUNC(glDebugMessageControl, void, (GLenum source, GLenum type, GLenum severity, GLsizei count, const GLuint *ids, GLboolean enabled))
GL_FUNC(glDebugMessageCallback, void, (GLDEBUGPROC callback, const void *userParam))
GL_FUNC(glGenFramebuffers, void, (GLsizei n, GLuint *framebuffers))
GL_FUNC(glBindFramebuffer, void, (GLenum target, GLuint framebuffer))
GL_FUNC(glFramebufferTexture2D, void, (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level))
GL_FUNC(glCheckFramebufferStatus, GLenum, (GLenum target))
GL_FUNC(glBlitFramebuffer, void, (GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask, GLenum filter))
GL_FUNC(glTexImage3D, void, (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const void *pixels));
GL_FUNC(glCreateProgram, GLuint, (void))
GL_FUNC(glCreateShader, GLuint, (GLenum type))
GL_FUNC(glCompileShader, void, (GLuint shader))
GL_FUNC(glAttachShader, void, (GLuint program, GLuint shader))
GL_FUNC(glLinkProgram, void, (GLuint program))
GL_FUNC(glShaderSource, void, (GLuint shader, GLsizei count, const GLchar *const*string, const GLint *length))
GL_FUNC(glUseProgram, void, (GLuint program))
GL_FUNC(glGetUniformLocation, GLint, (GLuint program, const GLchar *name))
GL_FUNC(glGetAttribLocation, GLint, (GLuint program, const GLchar *name))
GL_FUNC(glGenBuffers, void, (GLsizei n, GLuint *buffers))
GL_FUNC(glBindBuffer, void, (GLenum target, GLuint buffer))
GL_FUNC(glBufferData, void, (GLenum target, GLsizeiptr size, const void *data, GLenum usage))
GL_FUNC(glEnableVertexAttribArray, void, (GLuint index))
GL_FUNC(glVertexAttribPointer, void, (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer))
GL_FUNC(glActiveTexture, void, (GLenum texture))
GL_FUNC(glUniform4f, void, (GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3))
GL_FUNC(glUniform1i, void, (GLint location, GLint v0))
GL_FUNC(glUniform1fv, void, (GLint location, GLsizei count, const GLfloat *value))
GL_FUNC(glUniformMatrix3fv, void, (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value))
GL_FUNC(glGenVertexArrays, void, (GLsizei n, GLuint *arrays))
GL_FUNC(glBindVertexArray, void, (GLuint array))
GL_FUNC(glBlendColor, void, (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha))
#undef GL_FUNC

1034
example_rasterizer.cpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,21 @@
#version 330
// DirectWrite rasterization example: fragment shader
// This file is only included for reference, GLSL code is stuffed into the rasterizer inline.
smooth in vec3 uv;
uniform sampler2DArray tex;
uniform float M_value_table[7];
layout(location = 0) out vec4 mask;
void main(){
vec3 S = texture(tex, uv).rgb;
int C0 = int(S.r*6 + 0.1); // + 0.1 just incase we have some small rounding taking us below the integer we should be hitting.
int C1 = int(S.g*6 + 0.1);
int C2 = int(S.b*6 + 0.1);
mask.rgb = vec3(M_value_table[C0],
M_value_table[C1],
M_value_table[C2]);
mask.a = 1;
}

View File

@ -0,0 +1,15 @@
#version 330
// DirectWrite rasterization example: vertex shader
// This file is only included for reference, GLSL code is stuffed into the rasterizer inline.
uniform mat3 pixel_to_normal;
in vec2 position;
in vec3 tex_position;
smooth out vec3 uv;
void main(){
gl_Position.xy = (pixel_to_normal*vec3(position, 1.f)).xy;
gl_Position.z = 0.f;
gl_Position.w = 1.f;
uv = tex_position;
}

View File

@ -0,0 +1,208 @@
/*
** Win32 Direct Write Example Program
** v1.0.0 - June 16th 2021
** by Allen Webster allenwebster@4coder.net
**
** public domain example program
** NO WARRANTY IMPLIED; USE AT YOUR OWN RISK
**
** *WARNING* this example has not yet been curated and refined to save
** your time if you are trying to use it for learning. It lacks detailed
** commentary and is probably sloppy in places.
**
*/
// DirectWrite texture extraction example
#include <windows.h>
#include <dwrite.h>
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
// Bitmap structs
#pragma pack(push, 1)
struct Header{
char sig[2];
uint32_t file_size;
uint32_t reserved;
uint32_t data_offset;
};
struct Info_Header{
uint32_t size;
uint32_t width;
uint32_t height;
uint16_t planes;
uint16_t bits_per_pixel;
uint32_t compression;
uint32_t image_size;
uint32_t x_pixels_per_meter;
uint32_t y_pixels_per_meter;
uint32_t colors_used;
uint32_t important_colors;
};
struct Color_Table{
uint8_t R;
uint8_t G;
uint8_t B;
uint8_t A;
};
#pragma pack(pop)
int main(){
COLORREF back_color = RGB(0,0,0);
COLORREF fore_color = RGB(255,0,0);
int32_t raster_target_w = 200;
int32_t raster_target_h = 200;
float raster_target_x = 100.f;
float raster_target_y = 100.f;
wchar_t font_path[] = L"C:\\Windows\\Fonts\\arial.ttf";
float point_size = 12.f;
uint32_t codepoint = '?';
char *test_output_file_name = "test.bmp";
HRESULT error = 0;
// DWrite Factory
IDWriteFactory *dwrite_factory = 0;
error = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), (IUnknown**)&dwrite_factory);
assert(error == S_OK);
// DWrite Font File Reference
IDWriteFontFile *font_file = 0;
error = dwrite_factory->CreateFontFileReference(font_path, 0, &font_file);
assert(error == S_OK);
// DWrite Font Face
IDWriteFontFace *font_face = 0;
error = dwrite_factory->CreateFontFace(DWRITE_FONT_FACE_TYPE_TRUETYPE, 1, &font_file, 0, DWRITE_FONT_SIMULATIONS_NONE, &font_face);
assert(error == S_OK);
// DWrite Rendering Params
IDWriteRenderingParams *base_rendering_params = 0;
error = dwrite_factory->CreateRenderingParams(&base_rendering_params);
assert(error == S_OK);
IDWriteRenderingParams *rendering_params = 0;
{
FLOAT gamma = 1.f;
//FLOAT gamma = base_rendering_params->GetGamma();
FLOAT enhanced_contrast = base_rendering_params->GetEnhancedContrast();
FLOAT clear_type_level = base_rendering_params->GetClearTypeLevel();
error = dwrite_factory->CreateCustomRenderingParams(
gamma,
enhanced_contrast,
clear_type_level,
DWRITE_PIXEL_GEOMETRY_RGB,
DWRITE_RENDERING_MODE_NATURAL,
&rendering_params);
assert(error == S_OK);
}
// DWrite GDI Interop
IDWriteGdiInterop *dwrite_gdi_interop = 0;
error = dwrite_factory->GetGdiInterop(&dwrite_gdi_interop);
assert(error == S_OK);
// DWrite Bitmap Render Target
IDWriteBitmapRenderTarget *render_target = 0;
error = dwrite_gdi_interop->CreateBitmapRenderTarget(0, raster_target_w, raster_target_h, &render_target);
assert(error == S_OK);
// Clear the Render Target
HDC dc = render_target->GetMemoryDC();
{
HGDIOBJ original = SelectObject(dc, GetStockObject(DC_PEN));
SetDCPenColor(dc, back_color);
SelectObject(dc, GetStockObject(DC_BRUSH));
SetDCBrushColor(dc, back_color);
Rectangle(dc, 0, 0, raster_target_w, raster_target_h);
SelectObject(dc, original);
}
// Find the glyph index for the codepoint we want to render
uint16_t index = 0;
error = font_face->GetGlyphIndices(&codepoint, 1, &index);
assert(error == S_OK);
// Render the glyph
DWRITE_GLYPH_RUN glyph_run = {0};
glyph_run.fontFace = font_face;
glyph_run.fontEmSize = point_size*96.f/72.f;
glyph_run.glyphCount = 1;
glyph_run.glyphIndices = &index;
RECT bounding_box = {0};
error = render_target->DrawGlyphRun(raster_target_x, raster_target_y, DWRITE_MEASURING_MODE_NATURAL, &glyph_run, rendering_params, fore_color, &bounding_box);
assert(error == S_OK);
// Get the Bitmap
HBITMAP bitmap = (HBITMAP)GetCurrentObject(dc, OBJ_BITMAP);
DIBSECTION dib = {0};
GetObject(bitmap, sizeof(dib), &dib);
// Save the Bitmap
{
uint8_t *in_data = (uint8_t*)dib.dsBm.bmBits;
int32_t in_pitch = dib.dsBm.bmWidthBytes;
int32_t width = raster_target_w;
int32_t height = raster_target_h;
char *file_name = test_output_file_name;
void *memory = (void*)malloc(1 << 20);
assert(memory != 0);
void *ptr = memory;
int32_t out_pitch = (width*3) + 3;
out_pitch = out_pitch - (out_pitch%4);
Header *header = (Header*)ptr;
ptr = header + 1;
Info_Header *info_header = (Info_Header*)ptr;
ptr = info_header + 1;
uint8_t *out_data = (uint8_t*)ptr;
ptr = out_data + out_pitch*height;
header->sig[0] = 'B';
header->sig[1] = 'M';
header->file_size = (uint8_t*)ptr - (uint8_t*)memory;
header->reserved = 0;
header->data_offset = out_data - (uint8_t*)memory;
info_header->size = sizeof(*info_header);
info_header->width = width;
info_header->height = height;
info_header->planes = 1;
info_header->bits_per_pixel = 24;
info_header->compression = 0;
info_header->image_size = 0;
info_header->x_pixels_per_meter = 0;
info_header->y_pixels_per_meter = 0;
info_header->colors_used = 0;
info_header->important_colors = 0;
uint8_t *in_line = (uint8_t*)in_data;
uint8_t *out_line = out_data + out_pitch*(height - 1);
for (int32_t y = 0; y < height; y += 1){
uint8_t *in_pixel = in_line;
uint8_t *out_pixel = out_line;
for (int32_t x = 0; x < width; x += 1){
out_pixel[0] = in_pixel[0];
out_pixel[1] = in_pixel[1];
out_pixel[2] = in_pixel[2];
in_pixel += 4;
out_pixel += 3;
}
in_line += in_pitch;
out_line -= out_pitch;
}
FILE *out = fopen(file_name, "wb");
assert(out != 0);
fwrite(memory, 1, header->file_size, out);
fclose(out);
}
return(0);
}

31
project.4coder Normal file
View File

@ -0,0 +1,31 @@
version(1);
project_name = "direct_write";
patterns = {
"*.c",
"*.cpp",
"*.h",
"*.m",
"*.bat",
"*.sh",
"*.4coder",
};
blacklist_patterns = {
".*",
};
load_paths_base = {
{ ".", .relative = true, .recursive = true, },
};
load_paths = {
{ load_paths_base, .os = "win", },
};
command_list = {
{ .name = "build",
.out = "*compilation*", .footer_panel = true, .save_dirty_files = true,
.cmd = { { "build_examples.bat" , .os = "win" }, }, },
{ .name = "run",
.out = "*run*", .footer_panel = false, .save_dirty_files = false,
.cmd = { { "build\\rasterize", .os = "win" }, }, },
};
fkey_command[1] = "build";
fkey_command[2] = "run";