├── Audio └── playsound.c ├── CUDA ├── TensorCore.cu ├── abstract.cu ├── apollonian.cu ├── bitmap.cu ├── blocks.cu ├── eternity.cu ├── multipass.cu ├── readme.md ├── sphere.cu ├── surface.cu ├── tetrahedron.cu └── uv.cu ├── Compiler ├── Readme.md ├── clear.bat ├── example.pas ├── global.h ├── lexer.l ├── main.c ├── makefile └── parser.y ├── GFX └── tunnel.c ├── GLSL ├── 4klang.dll ├── ASM │ ├── cellular.c │ ├── greenblue.c │ └── redblue.c ├── Audio.c ├── BlueTunnel.c ├── ComputeShader.c ├── ComputeShader.cpp ├── Demo.cpp ├── GL │ ├── inc │ │ ├── eglew.h │ │ ├── glew.h │ │ ├── glxew.h │ │ └── wglew.h │ └── lib │ │ └── glew32.dll ├── GLSL.c ├── GLSL.cpp ├── GLSL4.cpp ├── GenerateTexture.cpp ├── LoadTexture.c ├── PaintWithShadows.c ├── PostProcessing.c ├── RenderingEngine │ ├── compile.cmd │ ├── demo.c │ ├── demo.cpp │ ├── demo1.c │ ├── demo2.c │ └── demo3.c ├── Skybox │ ├── skybox.c │ └── textures │ │ ├── 0.ppm │ │ ├── 1.ppm │ │ ├── 2.ppm │ │ ├── 3.ppm │ │ ├── 4.ppm │ │ ├── 5.ppm │ │ └── plaster.ppm ├── Terrain.c ├── VolumeParticleSystem.c ├── bindless.c ├── blank.c ├── buffer.c ├── clouds.ppm ├── context.c ├── fireball.c ├── fixed.c ├── flames.c ├── geometry.c ├── glext.h ├── grid.c ├── mandelbulb.c ├── menu.c ├── ms_triangle.c ├── multicore.cpp ├── mvp.c ├── nvstream.c ├── pathtracing1.c ├── pathtracing2.c ├── plaster.ppm ├── quad.c ├── smoke.c ├── ssbo.c ├── stain.c ├── strangefluid.c ├── stream.c ├── triangle.c └── vs_quad.c ├── HLSL ├── Asm.cpp ├── Audio.cpp ├── Circle.cpp ├── Helix.cpp ├── Pattern.cpp ├── SM3_HelloWorld.cpp ├── audio.h ├── checkerboard.cpp ├── firesmoke.c ├── hexmap.c ├── mosaic.c ├── neon.c ├── notepad.txt ├── particles.c ├── plasma.ppm ├── tetrahedron.c ├── texture.cpp └── tga.cpp ├── MetalAPI └── Line.metal ├── OpenCL ├── README.md ├── Tools │ ├── clcc.c │ └── clptx.c ├── dawn.cl ├── plasma.cl └── waves.cl ├── README.md ├── Tools ├── BaseConverter.c ├── ImageToArray.c ├── MapToArray.c ├── TriangleArea.c ├── generate_html.cpp ├── list.c ├── number_system_converter.cpp ├── ppm.c └── registry.cpp └── Vulkan ├── RTX └── Triangle │ ├── build-shaders.bat │ ├── hit.rchit │ ├── hit.spv │ ├── miss.rmiss │ ├── miss.spv │ ├── raygen.rgen │ ├── raygen.spv │ └── triangle.c ├── VulkanComputeShader ├── demo.comp ├── demo.cpp └── demo.spv ├── apollonian.c ├── chess.cpp ├── monster.c ├── pattern.c ├── readme.md ├── shader.frag └── vulkan_hello_world.cpp /CUDA/TensorCore.cu: -------------------------------------------------------------------------------- 1 | // Simple CUDA example that demonstrates using Tensor Cores on an NVIDIA RTX GPU. 2 | // This example will use CUDA's Warp Matrix Multiply and Accumulate (WMMA). 3 | // nvcc -arch=sm_75 -o TensorCore TensorCore.cu 4 | #include 5 | #include 6 | #include 7 | 8 | #define M 16 9 | #define N 16 10 | #define K 16 11 | 12 | __global__ void TensorCoreGEMM(half* a, half* b, float* c) 13 | { 14 | nvcuda::wmma::fragment a_frag; 15 | nvcuda::wmma::fragment b_frag; 16 | nvcuda::wmma::fragment c_frag; 17 | nvcuda::wmma::load_matrix_sync(a_frag, a, K); 18 | nvcuda::wmma::load_matrix_sync(b_frag, b, K); 19 | nvcuda::wmma::fill_fragment(c_frag, 0.0f); 20 | nvcuda::wmma::mma_sync(c_frag, a_frag, b_frag, c_frag); 21 | nvcuda::wmma::store_matrix_sync(c, c_frag, N, nvcuda::wmma::mem_row_major); 22 | } 23 | 24 | int main() 25 | { 26 | half host_a[M * K]; 27 | half host_b[K * N]; 28 | float host_c[M * N] = {0}; 29 | for (int i = 0; i < M * K; i++) 30 | { 31 | host_a[i] = __float2half(1.0f); 32 | } 33 | for (int i = 0; i < K * N; i++) 34 | { 35 | host_b[i] = __float2half(1.0f); 36 | } 37 | half *dev_a, *dev_b; 38 | float *dev_c; 39 | cudaMalloc((void**)&dev_a, M * K * sizeof(half)); 40 | cudaMalloc((void**)&dev_b, K * N * sizeof(half)); 41 | cudaMalloc((void**)&dev_c, M * N * sizeof(float)); 42 | cudaMemcpy(dev_a, host_a, M * K * sizeof(half), cudaMemcpyHostToDevice); 43 | cudaMemcpy(dev_b, host_b, K * N * sizeof(half), cudaMemcpyHostToDevice); 44 | TensorCoreGEMM<<<1, 32>>>(dev_a, dev_b, dev_c); 45 | cudaMemcpy(host_c, dev_c, M * N * sizeof(float), cudaMemcpyDeviceToHost); 46 | printf("Result matrix C:\n"); 47 | for (int i = 0; i < M; i++) 48 | { 49 | for (int j = 0; j < N; j++) 50 | { 51 | printf("%.0f ", host_c[i * N + j]); 52 | } 53 | printf("\n"); 54 | } 55 | cudaFree(dev_a); 56 | cudaFree(dev_b); 57 | cudaFree(dev_c); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /CUDA/abstract.cu: -------------------------------------------------------------------------------- 1 | // nvcc -o abstract.exe abstract.cu -arch=sm_30 user32.lib gdi32.lib 2 | #include 3 | 4 | #define width 1280 5 | #define height 720 6 | 7 | static const BITMAPINFO bmi = { {sizeof(BITMAPINFOHEADER),width,height,1,32,BI_RGB,0,0,0,0,0},{0,0,0,0} }; 8 | static unsigned char host[width*height*4]; 9 | 10 | __global__ void mainImage(uchar4 *fragColor, float iTime) 11 | { 12 | unsigned int x = blockIdx.x*blockDim.x + threadIdx.x; 13 | unsigned int y = blockIdx.y*blockDim.y + threadIdx.y; 14 | unsigned int i = (height - y - 1)*width + x; 15 | float2 iResolution = make_float2((float)width, (float)height); 16 | float2 fragCoord = make_float2((float)x, (float)y); 17 | float2 uv = make_float2(fragCoord.x/iResolution.x*8.0f, fragCoord.y/iResolution.y*8.0f); 18 | for(int j=1; j<4; j++) 19 | { 20 | uv.x += sin(iTime + uv.y * float(j)); 21 | uv.y += cos(iTime + uv.x * float(j)); 22 | } 23 | float3 q = make_float3(0.5+0.5*cos(4.0+uv.x), 0.5+0.5*cos(4.0+uv.y+2.0), 0.5+0.5*cos(4.0+uv.x+4.0)); 24 | fragColor[i] = make_uchar4(q.x*255, q.y*255, q.z*255, 1); 25 | } 26 | 27 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 28 | { 29 | if (uMsg == WM_KEYUP && wParam == VK_ESCAPE) 30 | { 31 | PostQuitMessage(0); 32 | return 0; 33 | } 34 | else 35 | { 36 | return DefWindowProc(hWnd, uMsg, wParam, lParam); 37 | } 38 | } 39 | 40 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 41 | { 42 | int exit = 0; 43 | MSG msg; 44 | WNDCLASS win = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, "CUDA Demo"}; 45 | RegisterClass(&win); 46 | HWND hwnd = CreateWindowEx(0, win.lpszClassName, "CUDA Demo", WS_VISIBLE|WS_OVERLAPPEDWINDOW, 0, 0, width, height, 0, 0, 0, 0); 47 | uchar4 *device; 48 | cudaMalloc( (void**)&device, width*height*4 ); 49 | dim3 block(8, 8); 50 | dim3 grid(width/8, height/8); 51 | while (!exit) 52 | { 53 | while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 54 | { 55 | if( msg.message==WM_QUIT ) exit = 1; 56 | TranslateMessage( &msg ); 57 | DispatchMessage( &msg ); 58 | } 59 | mainImage<<>>(device, GetTickCount()*0.001f); 60 | cudaThreadSynchronize(); 61 | cudaMemcpy(host, device, width * height * sizeof(uchar4), cudaMemcpyDeviceToHost); 62 | StretchDIBits(GetDC(hwnd),0,0,width,height,0,0,width,height,host,&bmi,DIB_RGB_COLORS,SRCCOPY); 63 | } 64 | cudaFree( device ); 65 | return 0; 66 | } -------------------------------------------------------------------------------- /CUDA/apollonian.cu: -------------------------------------------------------------------------------- 1 | // nvcc -o apollonian.exe apollonian.cu -arch=sm_30 user32.lib gdi32.lib 2 | #include 3 | 4 | #define width 1920 5 | #define height 1080 6 | 7 | __device__ float clamp (float x) 8 | { 9 | return fmaxf(0.0f, fminf(255.0f, x)); 10 | } 11 | 12 | __device__ float3 mod (float3 s, float3 m) 13 | { 14 | float a = s.x - m.x * floorf(s.x/m.x); 15 | float b = s.y - m.y * floorf(s.y/m.y); 16 | float c = s.z - m.z * floorf(s.z/m.z); 17 | return make_float3(a, b, c); 18 | } 19 | 20 | __device__ float fractal (float3 p, float iTime) 21 | { 22 | float i = 0.0f, s = 1.0f, k = 0.0f; 23 | for(p.y += iTime * 0.3f; i++ < 7.0f; s *= k) 24 | { 25 | p = make_float3(p.x - 1.0f, p.y - 1.0f, p.z - 1.0f); 26 | p = mod(p, make_float3(2.0f,2.0f,2.0f)); 27 | p = make_float3(p.x - 1.0f, p.y - 1.0f, p.z - 1.0f); 28 | k = 1.5f / (p.x*p.x + p.y*p.y + p.z*p.z); 29 | p = make_float3(p.x*k, p.y*k, p.z*k); 30 | } 31 | return sqrtf(p.x*p.x + p.y*p.y + p.z*p.z)/s - 0.01f; 32 | } 33 | 34 | __global__ void mainImage (uchar4 *fragColor, float iTime) 35 | { 36 | unsigned int x = blockIdx.x * blockDim.x + threadIdx.x; 37 | unsigned int y = blockIdx.y * blockDim.y + threadIdx.y; 38 | unsigned int i = (height - y - 1) * width + x; 39 | float2 iResolution = make_float2(width, height); 40 | float2 fragCoord = make_float2(x, y); 41 | float3 d = make_float3((2.0f*fragCoord.x-iResolution.x)/iResolution.y, (2.0f*fragCoord.y-iResolution.y)/iResolution.y, 1.0f); 42 | d = make_float3(d.x / 6.0f, d.y / 6.0f, d.z / 6.0f); 43 | float3 o = make_float3(1.0f, 1.0f, 1.0f); 44 | float4 c = make_float4(0.0f, 0.0f, 0.0f, 0.0f); 45 | for (int i=0; i<100; i++) 46 | { 47 | float f = fractal(o, iTime); 48 | o = make_float3(o.x+f*d.x, o.y+f*d.y, o.z+f*d.z); 49 | } 50 | float m = fractal(make_float3(o.x-d.x,o.y-d.y,o.z-d.z), iTime) / powf(o.z,1.5f) * 2.5f; 51 | c = make_float4(c.x + m, c.y + m, c.z + m, 255.0f); 52 | fragColor[i] = make_uchar4( clamp(c.z * 255.0f), clamp(c.y * 255.0f), clamp(c.x * 255.0f), 255.0f); 53 | } 54 | 55 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 56 | { 57 | if (uMsg == WM_KEYUP && wParam == VK_ESCAPE) 58 | { 59 | PostQuitMessage(0); return 0; 60 | } 61 | else 62 | { 63 | return DefWindowProc(hWnd, uMsg, wParam, lParam); 64 | } 65 | } 66 | 67 | int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 68 | { 69 | ShowCursor(0); 70 | int exit = 0; 71 | MSG msg; 72 | WNDCLASS win = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, "CUDA Demo"}; 73 | RegisterClass(&win); 74 | HDC hdc = GetDC(CreateWindowEx(0, win.lpszClassName, "CUDA Demo", WS_VISIBLE|WS_POPUP, 0, 0, width, height, 0, 0, 0, 0)); 75 | uchar4 *device; 76 | cudaMalloc( (void**)&device, width*height*4 ); 77 | dim3 block(8, 8); 78 | dim3 grid(width/8, height/8); 79 | BITMAPINFO bmi = {{sizeof(BITMAPINFOHEADER),width,height,1,32,BI_RGB,0,0,0,0,0},{0,0,0,0}}; 80 | static unsigned char host[width*height*4]; 81 | while (!exit) 82 | { 83 | while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 84 | { 85 | if( msg.message==WM_QUIT ) exit = 1; 86 | TranslateMessage( &msg ); 87 | DispatchMessage( &msg ); 88 | } 89 | mainImage<<>>(device, GetTickCount()*0.001f); 90 | cudaMemcpy(host, device, width * height * sizeof(uchar4), cudaMemcpyDeviceToHost); 91 | StretchDIBits(hdc,0,0,width,height,0,0,width,height,host,&bmi,DIB_RGB_COLORS,SRCCOPY); 92 | } 93 | cudaFree( device ); 94 | return 0; 95 | } -------------------------------------------------------------------------------- /CUDA/bitmap.cu: -------------------------------------------------------------------------------- 1 | // nvcc -o bitmap.exe bitmap.cu -ID:\CUDA\Samples\common\inc -arch=sm_30 user32.lib gdi32.lib 2 | #include 3 | #include 4 | #include 5 | 6 | #define width 1280 7 | #define height 720 8 | 9 | static const BITMAPINFO bmi = { {sizeof(BITMAPINFOHEADER),width,height,1,32,BI_RGB,0,0,0,0,0},{0,0,0,0} }; 10 | static unsigned char host[width*height*4]; 11 | 12 | __global__ void mainImage(uchar4 *fragColor) 13 | { 14 | unsigned int x = blockIdx.x*blockDim.x + threadIdx.x; 15 | unsigned int y = blockIdx.y*blockDim.y + threadIdx.y; 16 | unsigned int i = (height - y - 1)*width + x; 17 | float2 iResolution = make_float2((float)width, (float)height); 18 | float2 fragCoord = make_float2((float)x, (float)y); 19 | float2 uv = fragCoord / iResolution; 20 | float2 p = floorf(uv*16.0)/16.0; 21 | float3 q = make_float3( dot(p,make_float2(127.1,311.7)), dot(p,make_float2(269.5,183.3)), dot(p,make_float2(419.2,371.9)) ); 22 | float3 k = make_float3(sin(q.x)*43758.5453, sin(q.y)*43758.5453, sin(q.z)*43758.5453); 23 | fragColor[i] = make_uchar4(fracf(k.x)*255, fracf(k.y)*255, fracf(k.z)*255, 1); 24 | } 25 | 26 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 27 | { 28 | if (uMsg == WM_KEYUP && wParam == VK_ESCAPE) 29 | { 30 | PostQuitMessage(0); 31 | return 0; 32 | } 33 | else 34 | { 35 | return DefWindowProc(hWnd, uMsg, wParam, lParam); 36 | } 37 | } 38 | 39 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 40 | { 41 | int exit = 0; 42 | MSG msg; 43 | WNDCLASSEX WndClassEx = { 0 }; 44 | WndClassEx.cbSize = sizeof(WndClassEx); 45 | WndClassEx.lpfnWndProc = WindowProc; 46 | WndClassEx.lpszClassName = "CUDA Demo"; 47 | RegisterClassEx(&WndClassEx); 48 | HWND hwnd = CreateWindowEx(WS_EX_LEFT, WndClassEx.lpszClassName, NULL, WS_POPUP, 0, 0, width, height, 0, 0, NULL, NULL); 49 | HDC hdc = GetDC(hwnd); 50 | ShowWindow(hwnd, SW_SHOW); 51 | uchar4 *device; 52 | cudaMalloc( (void**)&device, width*height*4 ); 53 | dim3 block(8, 8); 54 | dim3 grid(width/8, height/8); 55 | while (!exit) 56 | { 57 | while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 58 | { 59 | if( msg.message==WM_QUIT ) exit = 1; 60 | TranslateMessage( &msg ); 61 | DispatchMessage( &msg ); 62 | } 63 | mainImage<<>>(device); 64 | cudaThreadSynchronize(); 65 | cudaMemcpy(host, device, width * height * sizeof(uchar4), cudaMemcpyDeviceToHost); 66 | StretchDIBits(hdc,0,0,width,height,0,0,width,height,host,&bmi,DIB_RGB_COLORS,SRCCOPY); 67 | } 68 | cudaFree( device ); 69 | return 0; 70 | } -------------------------------------------------------------------------------- /CUDA/eternity.cu: -------------------------------------------------------------------------------- 1 | // nvcc -o eternity.exe eternity.cu -arch=sm_30 d3d11.lib dxguid.lib user32.lib kernel32.lib gdi32.lib d3dcompiler.lib 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define WIDTH 1280 //screen width 9 | #define HEIGHT 720 //screen height 10 | #define SIZE 1024 //texture size 11 | 12 | surface RenderSurface; 13 | 14 | const unsigned char VertexShader[] = 15 | { 16 | "static const float3 vertices[6] = {float3(1,-1,0),float3(-1,-1,0),float3(1,1,0), float3(-1,-1,0),float3(-1,1,0),float3(1,1,0)};" 17 | "static const float2 uvs[6] = {float2(1,0),float2(0,0),float2(1,1), float2(0,0),float2(0,1),float2(1,1)};" 18 | "void VSMain(out float4 vertex:SV_POSITION, out float2 uv:TEXCOORD0, in uint id:SV_VertexID)" 19 | "{" 20 | "uv = uvs[id];" 21 | "vertex = float4(vertices[id], 1);" 22 | "}" 23 | }; 24 | 25 | const unsigned char PixelShader[] = 26 | { 27 | "Texture2D pattern : register(t0);" 28 | "SamplerState state {Filter = MIN_MAG_LINEAR_MIP_POINT;};" 29 | "float4 PSMain(float4 vertex:SV_POSITION, float2 uv:TEXCOORD0) : SV_TARGET" 30 | "{" 31 | "return pattern.Sample( state, uv );" 32 | "}" 33 | }; 34 | 35 | __device__ float smoothstep(float a, float b, float x) 36 | { 37 | float t = fmaxf(0.0f, fminf((x - a)/(b - a), 1.0f)); 38 | return t*t*(3.0f-(2.0f*t)); 39 | } 40 | 41 | __global__ void mainImage (float iTime) 42 | { 43 | int x = blockIdx.x*blockDim.x + threadIdx.x; 44 | int y = blockIdx.y*blockDim.y + threadIdx.y; 45 | float2 iResolution = make_float2((float)SIZE, (float)SIZE); 46 | float2 fragCoord = make_float2((float)x, (float)y); 47 | float2 uv = make_float2((2.0f * fragCoord.x / iResolution.x - 1.0f),(2.0f * fragCoord.y / iResolution.y - 1.0f)); 48 | float L = sqrt(uv.x*uv.x+uv.y*uv.y)*4.0f; 49 | float K = atan2(uv.y, uv.x)+iTime; 50 | float X = fmod(sin(K*3.0f), cos(K*3.0f)); 51 | float Y = fmod(cos(K*3.0f), sin(K*3.0f)); 52 | float3 A = make_float3(X, X, 1.0f-X); 53 | float3 B = make_float3(Y+1.0f, Y+2.0f, Y+5.0f); 54 | float3 T = make_float3(1.0f-sin(iTime)*L, 1.0f-cos(iTime)*L, 1.0f-cos(iTime)*L); 55 | float3 color = make_float3(0.9f-smoothstep(A.x,B.x,T.x), 0.9f-smoothstep(A.y,B.y,T.y), 0.9f-smoothstep(A.z,B.z,T.z)); 56 | uchar4 fragColor = make_uchar4(0.9f*color.x*255, 0.9f*color.y*255, 0.5f*color.z*255, 255); 57 | surf2Dwrite(fragColor, RenderSurface, x*sizeof(uchar4), y, cudaBoundaryModeClamp); 58 | } 59 | 60 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 61 | { 62 | if ((uMsg == WM_KEYUP && wParam == VK_ESCAPE) || uMsg==WM_CLOSE || uMsg==WM_DESTROY) 63 | { 64 | PostQuitMessage(0); return 0; 65 | } 66 | else 67 | { 68 | return DefWindowProc(hWnd, uMsg, wParam, lParam); 69 | } 70 | } 71 | 72 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 73 | { 74 | int exit = 0; 75 | MSG msg; 76 | WNDCLASS win = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, "CUDA DX11"}; 77 | RegisterClass(&win); 78 | HWND hwnd = CreateWindowEx(0, win.lpszClassName, "CUDA DX11", WS_VISIBLE|WS_OVERLAPPEDWINDOW, 0, 0, WIDTH, HEIGHT, 0, 0, 0, 0); 79 | ID3D11Device *device; 80 | IDXGISwapChain *surface; 81 | ID3D11DeviceContext *context; 82 | ID3D11Resource *image; 83 | ID3D11RenderTargetView *target; 84 | ID3D11VertexShader *vs; 85 | ID3D11PixelShader *ps; 86 | ID3DBlob *VSblob; 87 | ID3DBlob *PSblob; 88 | ID3D11Texture2D *texture; 89 | ID3D11ShaderResourceView *srv; 90 | DXGI_SWAP_CHAIN_DESC sd = {{WIDTH, HEIGHT, 0, 0, DXGI_FORMAT_R8G8B8A8_UNORM, (DXGI_MODE_SCANLINE_ORDER)0, (DXGI_MODE_SCALING)0}, {1, 0}, (1L << (1 + 4)) | (1L << (6 + 4)) | (1L << (0 + 4)), 1, hwnd, 1, DXGI_SWAP_EFFECT_SEQUENTIAL, 0}; 91 | D3D11CreateDeviceAndSwapChain(0, D3D_DRIVER_TYPE_HARDWARE, 0, 0, 0, 0, D3D11_SDK_VERSION, &sd, &surface, &device, 0, &context); 92 | surface->GetBuffer(0, __uuidof( ID3D11Resource), (void**)&image ); 93 | device->CreateRenderTargetView(image, 0, &target); 94 | context->OMSetRenderTargets(1, &target, 0); 95 | D3D11_VIEWPORT vp = {0,0,WIDTH,HEIGHT,0.0f,1.0f}; 96 | context->RSSetViewports(1, &vp); 97 | D3DCompile(&VertexShader, sizeof VertexShader, 0, 0, 0, "VSMain", "vs_5_0", 1 << 15, 0, &VSblob, 0); 98 | device->CreateVertexShader(VSblob->GetBufferPointer(), VSblob->GetBufferSize(), 0, &vs); 99 | D3DCompile(&PixelShader, sizeof PixelShader, 0, 0, 0, "PSMain", "ps_5_0", 1 << 15, 0, &PSblob, 0); 100 | device->CreatePixelShader(PSblob->GetBufferPointer(), PSblob->GetBufferSize(), 0, &ps); 101 | context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); 102 | D3D11_TEXTURE2D_DESC tdesc = {SIZE, SIZE, 1, 1, DXGI_FORMAT_R8G8B8A8_UNORM, {1,0}, D3D11_USAGE_DEFAULT,D3D11_BIND_SHADER_RESOURCE, 0, 0}; 103 | device->CreateTexture2D(&tdesc, 0, &texture); 104 | device->CreateShaderResourceView(texture, 0, &srv); 105 | context->PSSetShaderResources(0, 1, &srv); 106 | dim3 block(8, 8, 1); 107 | dim3 grid(SIZE/block.x, SIZE/block.y, 1); 108 | cudaGraphicsResource *resource; 109 | cudaArray *buffer; 110 | cudaGraphicsD3D11RegisterResource(&resource, texture, cudaGraphicsRegisterFlagsSurfaceLoadStore); 111 | cudaGraphicsMapResources(1, &resource, 0); 112 | cudaGraphicsSubResourceGetMappedArray(&buffer, resource, 0, 0); 113 | cudaBindSurfaceToArray(RenderSurface, buffer); 114 | while (!exit) 115 | { 116 | while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 117 | { 118 | if (msg.message == WM_QUIT) exit = 1; 119 | TranslateMessage(&msg); 120 | DispatchMessage(&msg); 121 | } 122 | mainImage <<< grid, block >>> (GetTickCount()*0.001f); 123 | context->VSSetShader(vs, 0, 0); 124 | context->PSSetShader(ps, 0, 0); 125 | context->Draw(6, 0); 126 | surface->Present(0, 0); 127 | } 128 | return 0; 129 | } -------------------------------------------------------------------------------- /CUDA/multipass.cu: -------------------------------------------------------------------------------- 1 | // nvcc -o multipass.exe multipass.cu -arch=sm_30 user32.lib gdi32.lib 2 | #include 3 | 4 | #define width 1920 5 | #define height 1080 6 | 7 | __global__ void mainImage(uchar4 *fragColor, float iTime) 8 | { 9 | unsigned int x = blockIdx.x * blockDim.x + threadIdx.x; 10 | unsigned int y = blockIdx.y * blockDim.y + threadIdx.y; 11 | unsigned int i = (height - y - 1)*width + x; 12 | float2 iResolution = make_float2(width, height); 13 | float2 fragCoord = make_float2(x, y); 14 | float2 uv = make_float2((2.0f*fragCoord.x-iResolution.x)/iResolution.y, (2.0f*fragCoord.y-iResolution.y)/iResolution.y); 15 | float t = floorf(iTime+10.0f); 16 | float hx = (sinf(t*127.1f+t*311.7f)*43758.5453f) - floorf(sinf(t*127.1f+t*311.7f)*43758.5453f); 17 | float hy = (sinf(t*269.5f+t*183.3f)*43758.5453f) - floorf(sinf(t*269.5f+t*183.3f)*43758.5453f); 18 | float s = 1.0f-((sqrtf((uv.x-(hx*2.0f-1.0f))*(uv.x-(hx*2.0f-1.0f))+(uv.y-(hy*2.0f-1.0f))*(uv.y-(hy*2.0f-1.0f)))-0.07f) >= 0.0f); 19 | float buffer = fragColor[i].x; 20 | fragColor[i] = make_uchar4(fminf(s*255.0f + buffer, 255.0f), 0, 0, 255); 21 | } 22 | 23 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 24 | { 25 | if (uMsg == WM_KEYUP && wParam == VK_ESCAPE) 26 | { 27 | PostQuitMessage(0); return 0; 28 | } 29 | else 30 | { 31 | return DefWindowProc(hWnd, uMsg, wParam, lParam); 32 | } 33 | } 34 | 35 | int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 36 | { 37 | ShowCursor(0); 38 | int exit = 0; 39 | MSG msg; 40 | WNDCLASS win = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, "CUDA Demo"}; 41 | RegisterClass(&win); 42 | HDC hdc = GetDC(CreateWindowEx(0, win.lpszClassName, "CUDA Demo", WS_VISIBLE|WS_POPUP, 0, 0, width, height, 0, 0, 0, 0)); 43 | uchar4 *device; 44 | cudaMalloc( (void**)&device, width*height*4 ); 45 | dim3 block(8, 8); 46 | dim3 grid(width/8, height/8); 47 | BITMAPINFO bmi = {{sizeof(BITMAPINFOHEADER),width,height,1,32,BI_RGB,0,0,0,0,0},{0,0,0,0}}; 48 | static unsigned char host[width*height*4]; 49 | while (!exit) 50 | { 51 | while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 52 | { 53 | if( msg.message==WM_QUIT ) exit = 1; 54 | TranslateMessage( &msg ); 55 | DispatchMessage( &msg ); 56 | } 57 | mainImage<<>>(device, GetTickCount()*0.001f); 58 | cudaMemcpy(host, device, width * height * sizeof(uchar4), cudaMemcpyDeviceToHost); 59 | StretchDIBits(hdc,0,0,width,height,0,0,width,height,host,&bmi,DIB_RGB_COLORS,SRCCOPY); 60 | } 61 | cudaFree( device ); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /CUDA/readme.md: -------------------------------------------------------------------------------- 1 | NVIDIA CUDA C++ programs. 2 | -------------------------------------------------------------------------------- /CUDA/sphere.cu: -------------------------------------------------------------------------------- 1 | //nvcc -o sphere.exe sphere.cu 2 | //Program generates image where is visible procedural lit sphere. 3 | //Image is saved to PPM file (then it can be read by GIMP, for example). 4 | //Copy ".\helper_math.h" from CUDA SDK path "Samples\common\inc" 5 | //Code written by Przemyslaw Zaworski, 2017 6 | 7 | #include 8 | #include 9 | #include ".\helper_math.h" 10 | 11 | #define width 1024 //image width 12 | #define height 1024 //image height 13 | 14 | __device__ float sphere (float3 p,float3 c,float r) //sphere distance field function 15 | { 16 | return length (p-c)-r; 17 | } 18 | 19 | __device__ float map (float3 p) //make geometry 20 | { 21 | return sphere (p,make_float3(0.0,0.0,0.0),1.0); 22 | } 23 | 24 | __device__ float3 set_normal (float3 p) //make normal vector 25 | { 26 | float3 x = make_float3 (0.001f,0.000f,0.000f); 27 | float3 y = make_float3 (0.000f,0.001f,0.000f); 28 | float3 z = make_float3 (0.000f,0.000f,0.001f); 29 | return normalize(make_float3(map(p+x)-map(p-x),map(p+y)-map(p-y),map(p+z)-map(p-z))); 30 | } 31 | 32 | __device__ float3 lighting (float3 p) //make Lambert light model 33 | { 34 | float3 AmbientLight = make_float3 (0.1f,0.1f,0.1f); 35 | float3 LightDirection = normalize(make_float3(4.0f,10.0f,-10.0f)); 36 | float3 LightColor = make_float3 (0.9f,0.9f,0.9f); 37 | float3 NormalDirection = set_normal(p); 38 | float3 DiffuseColor = clamp(dot(LightDirection,NormalDirection),0.0f,1.0f)*LightColor+AmbientLight; 39 | return clamp(DiffuseColor,0.0f,1.0f); 40 | } 41 | 42 | __device__ float4 raymarch (float3 ro,float3 rd) //raymarching 43 | { 44 | for (int i=0;i<128;i++) 45 | { 46 | float t = map(ro); 47 | if (t<0.01) return make_float4(lighting(ro),1.0); 48 | ro+=t*rd; 49 | } 50 | return make_float4(0.0,0.0,0.0,1.0); 51 | } 52 | 53 | __global__ void rendering(float3 *color) //scene rendering 54 | { 55 | unsigned int x = blockIdx.x*blockDim.x + threadIdx.x; 56 | unsigned int y = blockIdx.y*blockDim.y + threadIdx.y; 57 | unsigned int i = (height - y - 1)*width + x; 58 | float2 resolution = make_float2 ((float)width,(float)height); 59 | float2 coordinates = make_float2 ((float)x,(float)y); 60 | float2 p = (2.0*coordinates-resolution)/resolution.y; 61 | float3 ro = make_float3 (0.0f,0.0f,-4.0f); 62 | float3 rd = normalize(make_float3(p,2.0f)); 63 | float4 c = raymarch(ro,rd); 64 | color[i] = make_float3(c.x,c.y,c.z); 65 | } 66 | 67 | int to_int(float x) //convert from 0..1 to 0..255 68 | { 69 | return int(x*255+0.5); 70 | } 71 | 72 | int main() //main function 73 | { 74 | float3* host = new float3[width*height]; //pointer to memory on the host (CPU RAM) 75 | float3* device; //pointer to memory on the device (GPU VRAM) 76 | cudaMalloc(&device, width * height * sizeof(float3)); //allocate memory on the GPU VRAM 77 | dim3 block(8, 8, 1); //Manage threads 78 | dim3 grid(width / block.x, height / block.y, 1); ////Manage block array 79 | rendering <<< grid, block >>>(device); //run kernel 80 | cudaMemcpy(host, device, width * height *sizeof(float3), cudaMemcpyDeviceToHost); //copy result from VRAM to system RAM 81 | cudaFree(device); //free GPU VRAM 82 | FILE *file = fopen("sphere.ppm", "w"); //open file to save image 83 | fprintf(file, "P3 %d %d %d ", width, height, 255); //save PPM file header 84 | for (int i = 0; i < width*height; i++) //save pixels 85 | { 86 | fprintf(file, "%d %d %d ", to_int(host[i].x),to_int(host[i].y),to_int(host[i].z)); 87 | } 88 | fclose(file); //close file 89 | delete[] host; //free CPU RAM 90 | } -------------------------------------------------------------------------------- /CUDA/surface.cu: -------------------------------------------------------------------------------- 1 | // nvcc -o surface.exe surface.cu -ID:\CUDA\Samples\common\inc -lopengl32 -arch=sm_30 user32.lib gdi32.lib 2 | // Przemyslaw Zaworski 13.12.2018, fixed 28.04.2019 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define width 1280 10 | #define height 720 11 | typedef int (WINAPI* PFNWGLSWAPINTERVALEXTPROC)(int i); 12 | surface RenderSurface; 13 | 14 | __global__ void mainImage (float k) 15 | { 16 | int x = blockIdx.x*blockDim.x + threadIdx.x; 17 | int y = blockIdx.y*blockDim.y + threadIdx.y; 18 | float2 iResolution = make_float2((float)width, (float)height); 19 | float2 fragCoord = make_float2((float)x, (float)y); 20 | float2 uv = fragCoord / iResolution; 21 | uchar4 fragColor = (uv.x>(sin(k)*0.5f+0.5f)) ? make_uchar4(0,0,255,255) : make_uchar4(255,0,0,255); 22 | surf2Dwrite(fragColor, RenderSurface, x*sizeof(uchar4), y, cudaBoundaryModeClamp); 23 | } 24 | 25 | int main() 26 | { 27 | dim3 block(8, 8, 1); 28 | dim3 grid(width/block.x, height/block.y, 1); 29 | cudaGraphicsResource *resource; 30 | cudaArray *image; 31 | PIXELFORMATDESCRIPTOR pfd = {0,0,PFD_DOUBLEBUFFER}; 32 | HDC hdc = GetDC(CreateWindow("static", 0, WS_VISIBLE|WS_OVERLAPPEDWINDOW, 0, 0, width, height, 0, 0, 0, 0)); 33 | SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd); 34 | wglMakeCurrent(hdc, wglCreateContext(hdc)); 35 | ((PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress ("wglSwapIntervalEXT")) (0); 36 | glOrtho(0, width, 0, height, -1, 1); 37 | unsigned int store; 38 | glGenTextures(1, &store); 39 | glBindTexture(GL_TEXTURE_2D, store); 40 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 41 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 42 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); 43 | glEnable(GL_TEXTURE_2D); 44 | cudaGraphicsGLRegisterImage(&resource, store, GL_TEXTURE_2D, cudaGraphicsRegisterFlagsSurfaceLoadStore); 45 | cudaGraphicsMapResources(1, &resource, 0); 46 | cudaGraphicsSubResourceGetMappedArray(&image, resource, 0, 0); 47 | cudaBindSurfaceToArray(RenderSurface, image); 48 | do 49 | { 50 | mainImage <<< grid, block >>> (GetTickCount()*0.001f); 51 | cudaDeviceSynchronize(); 52 | glBindTexture(GL_TEXTURE_2D, store); 53 | glBegin(GL_QUADS); 54 | glTexCoord2i(0, 0); glVertex2i(0, 0); 55 | glTexCoord2i(0, 1); glVertex2i(0, height); 56 | glTexCoord2i(1, 1); glVertex2i(width, height); 57 | glTexCoord2i(1, 0); glVertex2i(width, 0); 58 | glEnd(); 59 | wglSwapLayerBuffers(hdc,WGL_SWAP_MAIN_PLANE); 60 | } while (!GetAsyncKeyState(VK_ESCAPE)); 61 | cudaGraphicsUnmapResources(1, &resource, 0); 62 | cudaGraphicsUnregisterResource(resource); 63 | return 0; 64 | } -------------------------------------------------------------------------------- /CUDA/tetrahedron.cu: -------------------------------------------------------------------------------- 1 | /* 2 | Code written by Przemyslaw Zaworski https://github.com/przemyslawzaworski 3 | References: https://github.com/straaljager/GPU-path-tracing-tutorial-2 , http://www.iquilezles.org/www/articles/derivative/derivative.htm 4 | Compile from scratch (set *.inc and *lib files manually in new project) or for simplicity, 5 | replace code with simpleGL.cu (http://docs.nvidia.com/cuda/cuda-samples/index.html#simple-opengl) 6 | Visual Studio: built in "Release" mode (x64). 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #define width 1280 //screen width 16 | #define height 720 //screen height 17 | 18 | float t = 0.0f; //timer 19 | float3* device; //pointer to memory on the device (GPU VRAM) 20 | GLuint buffer; //buffer 21 | 22 | __device__ float plane(float3 p, float3 c, float3 n) //plane signed distance field 23 | { 24 | return dot(p - c, n); 25 | } 26 | 27 | __device__ float tetrahedron(float3 p, float e) //tetrahedron signed distance field, created from planes intersection 28 | { 29 | float f = 0.57735; 30 | float a = plane(p, make_float3(e, e, e), make_float3(-f, f, f)); 31 | float b = plane(p, make_float3(e, -e, -e), make_float3(f, -f, f)); 32 | float c = plane(p, make_float3(-e, e, -e), make_float3(f, f, -f)); 33 | float d = plane(p, make_float3(-e, -e, e), make_float3(-f, -f, -f)); 34 | return max(max(a, b), max(c, d)); 35 | } 36 | 37 | __device__ float map(float3 p,float t) //virtual geometry 38 | { 39 | p = make_float3(p.x, cos(t)*p.y + sin(t)*p.z, -sin(t)*p.y + cos(t)*p.z); 40 | p = make_float3(cos(t)*p.x - sin(t)*p.z, p.y, sin(t)*p.x + cos(t)*p.z); 41 | return tetrahedron(p, 2.0f); 42 | } 43 | 44 | __device__ float3 lighting(float3 p, float t, float e) //directional derivative based lighting 45 | { 46 | float3 a = make_float3(0.1f, 0.1f, 0.1f); //ambient light color 47 | float3 ld = normalize(make_float3(6.0f, 15.0f, -7.0f)); //light direction 48 | float3 lc = make_float3(0.95f, 0.95f, 0.0f); //light color 49 | float s = (map(p + ld*e, t) - map(p, t)) / e; 50 | float3 d = clamp(s, 0.0, 1.0)*lc + a; 51 | return clamp(d,0.0f,1.0f); //final color 52 | } 53 | 54 | __device__ float3 raymarch(float3 ro, float3 rd, float k) //raymarching 55 | { 56 | for (int i = 0; i<128; i++) 57 | { 58 | float p = map(ro,k); 59 | if (p < 0.01) return lighting(ro,k,0.01f); 60 | ro += p*rd; 61 | } 62 | return make_float3(0.0f, 0.0f, 0.0f); //background color 63 | } 64 | 65 | __global__ void rendering(float3 *output,float k) 66 | { 67 | unsigned int x = blockIdx.x*blockDim.x + threadIdx.x; 68 | unsigned int y = blockIdx.y*blockDim.y + threadIdx.y; 69 | unsigned int i = (height - y - 1)*width + x; 70 | float2 resolution = make_float2((float)width, (float)height); //screen resolution 71 | float2 coordinates = make_float2((float)x, (float)y); //fragment coordinates 72 | float2 uv = (2.0*coordinates - resolution) / resolution.y; 73 | float3 ro = make_float3(0.0f, 0.0f, -8.0f); //ray origin 74 | float3 rd = normalize(make_float3(uv, 2.0f)); //ray direction 75 | float3 c = raymarch(ro, rd, k); 76 | float colour; 77 | unsigned char bytes[] = {(unsigned char)(c.x*255+0.5), (unsigned char)(c.y*255+0.5), (unsigned char)(c.z*255+0.5), 1}; 78 | memcpy(&colour, &bytes, sizeof(colour)); //convert from 4 bytes to single float 79 | output[i] = make_float3(x, y, colour); 80 | } 81 | 82 | void time(int x) 83 | { 84 | if (glutGetWindow() ) 85 | { 86 | glutPostRedisplay(); 87 | glutTimerFunc(10, time, 0); 88 | t+=0.0166f; 89 | } 90 | } 91 | 92 | void display(void) 93 | { 94 | cudaThreadSynchronize(); 95 | cudaGLMapBufferObject((void**)&device, buffer); //maps the buffer object into the address space of CUDA 96 | glClear(GL_COLOR_BUFFER_BIT); 97 | dim3 block(16, 16, 1); 98 | dim3 grid(width / block.x, height / block.y, 1); 99 | rendering <<< grid, block >>>(device,t); //execute kernel 100 | cudaThreadSynchronize(); 101 | cudaGLUnmapBufferObject(buffer); 102 | glBindBuffer(GL_ARRAY_BUFFER, buffer); 103 | glVertexPointer(2, GL_FLOAT, 12, 0); 104 | glColorPointer(4, GL_UNSIGNED_BYTE, 12, (GLvoid*)8); 105 | glEnableClientState(GL_VERTEX_ARRAY); 106 | glEnableClientState(GL_COLOR_ARRAY); 107 | glDrawArrays(GL_POINTS, 0, width * height); 108 | glDisableClientState(GL_VERTEX_ARRAY); 109 | glutSwapBuffers(); 110 | } 111 | 112 | int main(int argc, char** argv) 113 | { 114 | cudaMalloc(&device, width * height * sizeof(float3)); //allocate memory on the GPU VRAM 115 | glutInit(&argc, argv); //OpenGL initializing 116 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 117 | glutInitWindowPosition(100, 100); 118 | glutInitWindowSize(width, height); 119 | glutCreateWindow("Basic CUDA OpenGL raymarching - tetrahedron"); 120 | glClearColor(0.0, 0.0, 0.0, 0.0); 121 | glMatrixMode(GL_PROJECTION); 122 | gluOrtho2D(0.0, width, 0.0, height); 123 | glutDisplayFunc(display); 124 | time(0); 125 | glewInit(); 126 | glGenBuffers(1, &buffer); 127 | glBindBuffer(GL_ARRAY_BUFFER, buffer); 128 | unsigned int size = width * height * sizeof(float3); 129 | glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW); 130 | glBindBuffer(GL_ARRAY_BUFFER, 0); 131 | cudaGLRegisterBufferObject(buffer); //register the buffer object for access by CUDA 132 | glutMainLoop(); //event processing loop 133 | cudaFree(device); 134 | } -------------------------------------------------------------------------------- /CUDA/uv.cu: -------------------------------------------------------------------------------- 1 | // nvcc -o uv.exe uv.cu -IE:\CUDA\Samples\common\inc -lopengl32 -arch=sm_30 user32.lib gdi32.lib 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define width 1920 9 | #define height 1080 10 | static float timer = 0.0f; 11 | float3* device; 12 | unsigned int buffer; 13 | typedef void (APIENTRY* PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *b); 14 | typedef void (APIENTRY* PFNGLBINDBUFFERPROC) (GLenum t, GLuint b); 15 | typedef void (APIENTRY* PFNGLBUFFERDATAPROC) (GLenum t, ptrdiff_t s, const GLvoid *d, GLenum u); 16 | typedef int (APIENTRY* PFNWGLSWAPINTERVALEXTPROC) (int i); 17 | 18 | __global__ void rendering(float3 *output, float k) 19 | { 20 | unsigned int x = blockIdx.x*blockDim.x + threadIdx.x; 21 | unsigned int y = blockIdx.y*blockDim.y + threadIdx.y; 22 | unsigned int i = (height - y - 1)*width + x; 23 | float2 resolution = make_float2((float)width, (float)height); 24 | float2 coordinates = make_float2((float)x, (float)y); 25 | float2 uv = coordinates / resolution; 26 | float3 c = (uv.x>(sin(k)*0.5f+0.5f) ) ? make_float3(1.0f,0.0f,0.0f) : make_float3(0.0f, 0.0f, 1.0f); 27 | float colour; 28 | unsigned char bytes[] = {(unsigned char)(c.x*255+0.5),(unsigned char)(c.y*255+0.5),(unsigned char)(c.z*255+0.5),1}; 29 | memcpy(&colour, &bytes, sizeof(colour)); 30 | output[i] = make_float3(x, y, colour); 31 | } 32 | 33 | int main() 34 | { 35 | ShowCursor(0); 36 | unsigned int size = width * height * sizeof(float3); 37 | cudaMalloc(&device, size); 38 | dim3 block(8, 8, 1); 39 | dim3 grid(width / block.x, height / block.y, 1); 40 | PIXELFORMATDESCRIPTOR pfd = { 0, 0, PFD_DOUBLEBUFFER }; 41 | HDC hdc = GetDC(CreateWindow("static", 0, WS_POPUP | WS_VISIBLE | WS_MAXIMIZE, 0, 0, 0, 0, 0, 0, 0, 0)); 42 | SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd); 43 | wglMakeCurrent(hdc, wglCreateContext(hdc)); 44 | glOrtho(0.0, width, 0.0, height, -1.0, 1.0); 45 | ((PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT")) (0); 46 | ((PFNGLGENBUFFERSPROC)wglGetProcAddress("glGenBuffers"))(1, &buffer); 47 | ((PFNGLBINDBUFFERPROC)wglGetProcAddress("glBindBuffer"))(0x8892, buffer); 48 | ((PFNGLBUFFERDATAPROC)wglGetProcAddress("glBufferData"))(0x8892, size, 0, 0x88EA); 49 | cudaGLRegisterBufferObject(buffer); 50 | glVertexPointer(2, GL_FLOAT, 12, 0); 51 | glColorPointer(4, GL_UNSIGNED_BYTE, 12, (GLvoid*)8); 52 | glEnableClientState(GL_VERTEX_ARRAY); 53 | glEnableClientState(GL_COLOR_ARRAY); 54 | float s = GetTickCount(); 55 | do 56 | { 57 | timer = (GetTickCount()-s)*0.001f; 58 | cudaGLMapBufferObject((void**)&device, buffer); 59 | rendering <<< grid, block >>>(device, timer); 60 | cudaGLUnmapBufferObject(buffer); 61 | glDrawArrays(GL_POINTS, 0, width * height); 62 | wglSwapLayerBuffers(hdc, WGL_SWAP_MAIN_PLANE); 63 | } while (!GetAsyncKeyState(VK_ESCAPE)); 64 | cudaFree(device); 65 | return 0; 66 | } -------------------------------------------------------------------------------- /Compiler/Readme.md: -------------------------------------------------------------------------------- 1 | Basic template of a compiler for a subset of Pascal programming language. 2 | 3 | Reference: A.V. Aho, R. Sethi, J.D. Ullman - "Compilers - Principles, Techniques, and Tools" Appendix A - A Programming Project 4 | 5 | Implemented lexical and syntax analysis. 6 | -------------------------------------------------------------------------------- /Compiler/clear.bat: -------------------------------------------------------------------------------- 1 | del lexer.c parser.c parser.h compiler.exe -------------------------------------------------------------------------------- /Compiler/example.pas: -------------------------------------------------------------------------------- 1 | program example(input, output); 2 | 3 | var result:integer; 4 | 5 | function factorial(n: integer): integer; 6 | begin 7 | if n = 0 then 8 | factorial := 1 9 | else 10 | factorial := n * factorial(n-1) 11 | end; 12 | 13 | begin 14 | result:=factorial(10); 15 | write(result) 16 | end. 17 | -------------------------------------------------------------------------------- /Compiler/global.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | extern int lineno; 5 | int yylex(); 6 | int yyparse(); 7 | void yyerror(char const* s); 8 | extern FILE* yyin; -------------------------------------------------------------------------------- /Compiler/lexer.l: -------------------------------------------------------------------------------- 1 | %option noyywrap 2 | %{ 3 | #include "parser.h" 4 | #include "global.h" 5 | int lineno = 1; 6 | %} 7 | 8 | letter [a-zA-Z] 9 | digit [0-9] 10 | relop "="|"<>"|"<"|"<="|">="|">" 11 | mulop "*"|"mod"|"/"|"and"|"div" 12 | function "function" 13 | procedure "procedure" 14 | assign ":=" 15 | sign "+"|"-" 16 | array "array" 17 | of "of" 18 | integer "integer" 19 | realword "real" 20 | int {digit}+ 21 | exponent [eE][+-]?{int} 22 | real {int}("."{int}){exponent}? 23 | id {letter}({letter}|{digit})* 24 | 25 | %% 26 | [\t | " "]+ 27 | [ \n ] {lineno++;} 28 | program {return PROGRAM;} 29 | begin {return BEGINN;} 30 | end {return END;} 31 | var {return VAR;} 32 | {integer} {yylval = INTEGER; return INTEGER;} 33 | {realword} {yylval = REAL; return REAL;} 34 | not {return NOT;} 35 | or {return OR;} 36 | if {return IF;} 37 | then {return THEN;} 38 | else {return ELSE;} 39 | do {return DO;} 40 | while {return WHILE;} 41 | {array} {return ARRAY;} 42 | {of} {return OF;} 43 | {procedure} {return PROC;} 44 | {function} {return FUN;} 45 | {relop} {return RELOP;} 46 | {mulop} {return MULOP;} 47 | {sign} {return SIGN;} 48 | {assign} {return ASSIGN;} 49 | {id} {return ID;} 50 | {int} {return NUM;} 51 | {real} {return NUM; } 52 | <> {return DONE;} 53 | . {return *yytext;} 54 | %% -------------------------------------------------------------------------------- /Compiler/main.c: -------------------------------------------------------------------------------- 1 | #include "global.h" 2 | #include "parser.h" 3 | 4 | int main(int argc, char* argv[]) 5 | { 6 | FILE *input_file = fopen(argv[1],"r"); 7 | yyin = input_file; 8 | int result = yyparse(); 9 | fclose(input_file); 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /Compiler/makefile: -------------------------------------------------------------------------------- 1 | SOURCE=main.c parser.c lexer.c 2 | OBJECTS=$(SOURCE:.cpp=.o) 3 | 4 | .cpp.o: 5 | g++ -c -o $@ $< 6 | 7 | compiler.exe: $(OBJECTS) 8 | g++ -o $@ $^ 9 | 10 | ${OBJECTS}: global.h 11 | 12 | parser.c parser.h: parser.y 13 | bison --defines=parser.h -o parser.c parser.y 14 | 15 | lexer.c: lexer.l 16 | flex -t lexer.l > lexer.c -------------------------------------------------------------------------------- /Compiler/parser.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include "global.h" 3 | %} 4 | %token PROGRAM 5 | %token BEGINN 6 | %token END 7 | %token VAR 8 | %token INTEGER 9 | %token REAL 10 | %token ARRAY 11 | %token OF 12 | %token FUN 13 | %token PROC 14 | %token IF 15 | %token THEN 16 | %token ELSE 17 | %token DO 18 | %token WHILE 19 | %token RELOP 20 | %token MULOP 21 | %token SIGN 22 | %token ASSIGN 23 | %token OR 24 | %token NOT 25 | %token ID 26 | %token NUM 27 | %token NONE 28 | %token DONE 29 | %% 30 | start: PROGRAM ID '(' start_params ')' ';' 31 | declarations 32 | subprogram_declarations {cout << "Semantic action 1\n";} 33 | compound_statement '.' {cout << "Semantic action 2\n";} eof 34 | ; 35 | start_params: ID 36 | | start_params ',' ID 37 | ; 38 | identifier_list : ID {cout << "Semantic action 3\n";} 39 | | identifier_list ',' ID {cout << "Semantic action 4\n";} 40 | ; 41 | declarations: declarations VAR identifier_list ':' type ';' 42 | { cout << "Semantic action 5\n"; if($5==INTEGER || $5 == REAL) cout << "Semantic action 6\n";} 43 | | 44 | ; 45 | type: standard_type 46 | | ARRAY '[' NUM '.' '.' NUM ']' OF standard_type { cout << "Semantic action 7\n";} 47 | ; 48 | standard_type: INTEGER | REAL 49 | ; 50 | subprogram_declarations: subprogram_declarations subprogram_declaration ';' 51 | | 52 | ; 53 | subprogram_declaration: subprogram_head declarations compound_statement {cout << "Semantic action 8\n";} 54 | ; 55 | subprogram_head: FUN ID {cout << "Semantic action 9\n";} 56 | arguments {cout << "Semantic action 10\n";} 57 | ':' standard_type {cout << "Semantic action 11\n";} 58 | ';' 59 | | PROC ID {cout << "Semantic action 12\n";} 60 | arguments { cout << "Semantic action 13\n";} ';' 61 | ; 62 | arguments: '(' parameter_list ')' {cout << "Semantic action 14\n";} 63 | | 64 | ; 65 | parameter_list: identifier_list ':' type {cout << "Semantic action 15\n";} 66 | | parameter_list ';' identifier_list ':' type {cout << "Semantic action 16\n";} 67 | ; 68 | compound_statement: BEGINN optional_statement END 69 | ; 70 | optional_statement: statement_list 71 | | 72 | ; 73 | statement_list: statement 74 | | statement_list ';' statement 75 | ; 76 | statement: variable ASSIGN simple_expression {cout << "Semantic action 17\n";} 77 | | procedure_statement 78 | | compound_statement 79 | | IF expression {cout << "Semantic action 18\n";} 80 | THEN statement {cout << "Semantic action 19\n";} 81 | ELSE statement {cout << "Semantic action 20\n";} 82 | | WHILE {cout << "Semantic action 21\n";} 83 | expression DO {cout << "Semantic action 22\n";} 84 | statement {cout << "Semantic action 23\n";} 85 | ; 86 | variable: ID {cout << "Semantic action 24\n";} 87 | | ID '[' simple_expression ']' {cout << "Semantic action 25\n";} 88 | ; 89 | procedure_statement: ID {cout << "Semantic action 26\n";} 90 | | ID '(' expression_list ')' {cout << "Semantic action 27\n";} 91 | ; 92 | expression_list: expression {cout << "Semantic action 28\n";} 93 | | expression_list ',' expression {cout << "Semantic action 29\n";} 94 | ; 95 | expression: simple_expression {cout << "Semantic action 30\n";} 96 | | simple_expression RELOP simple_expression {cout << "Semantic action 31\n";} 97 | ; 98 | simple_expression: term 99 | | SIGN term {cout << "Semantic action 32\n";} 100 | | simple_expression SIGN term {cout << "Semantic action 33\n";} 101 | | simple_expression OR term {cout << "Semantic action 34\n";} 102 | ; 103 | term: factor 104 | | term MULOP factor {cout << "Semantic action 35\n";} 105 | ; 106 | factor: variable {cout << "Semantic action 36\n";} 107 | | ID '(' expression_list ')' {cout << "Semantic action 37\n";} 108 | | NUM 109 | | '(' expression ')' {cout << "Semantic action 38\n";} 110 | | NOT factor {cout << "Semantic action 39\n";} 111 | ; 112 | eof: DONE {return 0; } 113 | ; 114 | %% 115 | void yyerror(char const *s) 116 | { 117 | printf("Line %d: %s \n",lineno,s); 118 | } -------------------------------------------------------------------------------- /GFX/tunnel.c: -------------------------------------------------------------------------------- 1 | // Compile: cl tunnel.c -O2 2 | // Multithreading software rendering demo 3 | // To fix: screen tearing 4 | #include 5 | #include 6 | #include 7 | #pragma comment(lib, "user32.lib") 8 | #pragma comment(lib, "gdi32.lib") 9 | 10 | #define ScreenWidth 1280 11 | #define ScreenHeight 720 12 | #define NumThreads 12 13 | 14 | typedef struct {float x; float y;} Vector2; 15 | typedef struct {float x; float y; float z;} Vector3; 16 | typedef struct {int threadId; float threadTime;} ThreadInfo; 17 | 18 | static int RenderTarget[ScreenWidth*ScreenHeight]; 19 | 20 | int ColorToInt (Vector3 c) 21 | { 22 | return 255 << 24 | (unsigned char)(c.x * 255) << 16 | (unsigned char)(c.y * 255) << 8 | (unsigned char)(c.z * 255); 23 | } 24 | 25 | LPCSTR IntToString (int number) 26 | { 27 | char* chars = malloc(16); 28 | sprintf(chars, "%d", number); 29 | strcat(chars, " FPS"); // Additional line of code 30 | LPCSTR string = chars; 31 | free(chars); 32 | return string; 33 | } 34 | 35 | float Min(float a, float b) 36 | { 37 | return a < b ? a : b; 38 | } 39 | 40 | float Max(float a, float b) 41 | { 42 | return a > b ? a : b; 43 | } 44 | 45 | float Clamp(float x, float a, float b) 46 | { 47 | return Max(a, Min(b, x)); 48 | } 49 | 50 | float Smoothstep(float a, float b, float x) 51 | { 52 | float t = Clamp((x - a)/(b - a), 0.0f, 1.0f); 53 | return t * t * (3.0f - (2.0f * t)); 54 | } 55 | 56 | float Mod(float x, float y) 57 | { 58 | return x - y * floorf(x/y); 59 | } 60 | 61 | Vector3 Normalize(Vector3 p) 62 | { 63 | float q = sqrtf(p.x * p.x + p.y * p.y + p.z * p.z); 64 | Vector3 vector = {p.x / q, p.y / q, p.z / q}; 65 | return vector; 66 | } 67 | 68 | float Length(Vector3 p) 69 | { 70 | return sqrtf(p.x * p.x + p.y * p.y + p.z * p.z); 71 | } 72 | 73 | float Grid(float px, float py) 74 | { 75 | return 0.4 + Smoothstep(0.94f, 1.0f, Max(Mod(3.0f*px, 1.03f), Mod(3.0f*py, 1.03f))); 76 | } 77 | 78 | void MainImage (int fragCoordX, int fragCoordY, float iTime) 79 | { 80 | float ux = ((float)fragCoordX - ScreenWidth * 0.5f) / ScreenHeight + 0.5f * cosf(iTime*0.5f); 81 | float uy = ((float)fragCoordY - ScreenHeight * 0.5f) / ScreenHeight + 0.25f * sinf(iTime*0.5f); 82 | Vector3 uv = {ux, uy, 1.0f}; 83 | Vector3 rd = Normalize(uv); 84 | float sdf = 1.0f / powf(powf(fabs(rd.x)*0.75f, 6.0f) + powf(fabs(rd.y), 6.0f), 1.0f/6.0f); 85 | Vector3 surface = {rd.x * sdf, rd.y * sdf, iTime + rd.z * sdf}; 86 | Vector3 light = {0.0f - surface.x, 0.0f - surface.y, iTime + 3.0f - surface.z}; 87 | float atten = 1.0f / (0.75f + Length(light) * 0.5f); 88 | float f = Clamp((Grid(surface.y,surface.z) + Grid(surface.x,surface.z)) * atten * atten * atten, 0.0f, 1.0f); 89 | Vector3 fragColor = {f, f, f}; 90 | RenderTarget[fragCoordY*ScreenWidth+fragCoordX] = ColorToInt(fragColor); 91 | } 92 | 93 | DWORD WorkerThread (ThreadInfo* info) 94 | { 95 | int x = 0; 96 | int y = (info->threadId * ScreenHeight / NumThreads); 97 | int start = y * ScreenWidth; 98 | int end = ScreenWidth * ((info->threadId + 1) * ScreenHeight / NumThreads); 99 | for (int i = start; i < end ; i++) 100 | { 101 | MainImage(x, y, info->threadTime); 102 | x++; 103 | if (x == ScreenWidth) {x = 0; y++;} 104 | } 105 | return 0; 106 | } 107 | 108 | LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 109 | { 110 | if (uMsg==WM_CLOSE || uMsg==WM_DESTROY || (uMsg==WM_KEYDOWN && wParam==VK_ESCAPE)) 111 | { 112 | PostQuitMessage(0); return 0; 113 | } 114 | else 115 | { 116 | return DefWindowProc(hWnd, uMsg, wParam, lParam); 117 | } 118 | } 119 | 120 | int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 121 | { 122 | int exit = 0; 123 | MSG msg; 124 | WNDCLASS wnd = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, "Demo"}; 125 | RegisterClass(&wnd); 126 | HWND hwnd = CreateWindowEx(0, wnd.lpszClassName, "Demo", WS_VISIBLE|WS_OVERLAPPEDWINDOW, 0, 0, ScreenWidth, ScreenHeight, 0, 0, 0, 0); 127 | HDC hdc = GetDC(hwnd); 128 | BITMAPINFO bmi = {{sizeof(BITMAPINFOHEADER), ScreenWidth, ScreenHeight, 1, 32, BI_RGB, 0, 0, 0, 0, 0}, {0, 0, 0, 0}}; 129 | ThreadInfo info[NumThreads]; 130 | HANDLE* threads = (HANDLE*)malloc(sizeof(HANDLE) * NumThreads); 131 | int frame = 0; 132 | float lastTime = 0.0f; 133 | while (!exit) 134 | { 135 | while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 136 | { 137 | if( msg.message==WM_QUIT ) exit = 1; 138 | TranslateMessage( &msg ); 139 | DispatchMessage( &msg ); 140 | } 141 | float time = (float)GetTickCount() * 0.001f; 142 | for (int i = 0; i < NumThreads; i++) 143 | { 144 | info[i].threadId = i; 145 | info[i].threadTime = time; 146 | threads[i] = CreateThread(0, 0, WorkerThread, &info[i], 0, 0); 147 | } 148 | int result = WaitForMultipleObjects(NumThreads, threads, 0, INFINITE); 149 | for (int i = 0; i < NumThreads; i++) CloseHandle(threads[i]); 150 | StretchDIBits(hdc, 0, 0, ScreenWidth, ScreenHeight, 0, 0, ScreenWidth, ScreenHeight, RenderTarget, &bmi, DIB_RGB_COLORS, SRCCOPY); 151 | frame++; 152 | if (time - lastTime > 1.0f) 153 | { 154 | lastTime = time; 155 | SetWindowTextA(hwnd, IntToString(frame)); 156 | frame = 0; 157 | } 158 | } 159 | free(threads); 160 | return 0; 161 | } 162 | -------------------------------------------------------------------------------- /GLSL/4klang.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/CPP-Programming/9d93e94a00402ac3b19fd1732368a3a4fa1ad18a/GLSL/4klang.dll -------------------------------------------------------------------------------- /GLSL/ASM/cellular.c: -------------------------------------------------------------------------------- 1 | // Compile with Visual Studio: cl cellular.c opengl32.lib user32.lib gdi32.lib 2 | 3 | #include 4 | #include 5 | 6 | typedef void (__stdcall* PFNGLPROGRAMSTRINGARBPROC) (enum target, enum format, int len, const void *string); 7 | typedef void (__stdcall* PFNGLPROGRAMENVPARAMETER4FVARBPROC) (enum target, int index, const float *params); 8 | 9 | static const char* FS = \ 10 | "!!ARBfp1.0\n" 11 | "PARAM time = program.env[0];" 12 | "TEMP R0, R1, R2;" 13 | "MOV R0.x, {0.4};" 14 | "MUL R0.z, R0.x, time.x;" 15 | "MUL R0.xy, fragment.position, {0.005}.x;" 16 | "DP3 R2.z, R0, {1.2, 0.8, 0.4};" 17 | "DP3 R2.x, R0, {-0.4, 0.8, -0.8};" 18 | "DP3 R2.y, R0, {-0.8, 0.4, 1.2};" 19 | "DP3 R1.z, R2, {-0.72, -0.48, -0.24};" 20 | "DP3 R1.x, R2, {0.24, -0.48, 0.48};" 21 | "DP3 R1.y, R2, {0.48, -0.24, -0.72};" 22 | "DP3 R0.z, R1, {1.152, 0.768, 0.384};" 23 | "DP3 R0.x, R1, {-0.384, 0.768, -0.768};" 24 | "DP3 R0.y, R1, {-0.768, 0.384, 1.152};" 25 | "FRC R0.xyz, R0;" 26 | "ADD R0.xyz, R0, {-0.5}.x;" 27 | "DP3 R0.x, R0, R0;" 28 | "RSQ R0.x, R0.x;" 29 | "RCP R0.w, R0.x;" 30 | "FRC R0.xyz, R1;" 31 | "ADD R0.xyz, R0, {-0.5}.x;" 32 | "DP3 R0.x, R0, R0;" 33 | "FRC R1.xyz, R2;" 34 | "ADD R1.xyz, R1, {-0.5}.x;" 35 | "DP3 R0.y, R1, R1;" 36 | "RSQ R0.x, R0.x;" 37 | "RSQ R0.y, R0.y;" 38 | "RCP R0.x, R0.x;" 39 | "RCP R0.y, R0.y;" 40 | "MIN R0.x, R0.y, R0;" 41 | "MIN R0.x, R0, R0.w;" 42 | "MUL result.color, R0.x, {0.86805552, 1.3020833, 2.6041665}.xyzx;" 43 | "END"; 44 | 45 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 46 | { 47 | if (uMsg==WM_CLOSE || uMsg==WM_DESTROY || (uMsg==WM_KEYDOWN && wParam==VK_ESCAPE)) 48 | { 49 | PostQuitMessage(0); return 0; 50 | } 51 | else 52 | { 53 | return DefWindowProc(hWnd, uMsg, wParam, lParam); 54 | } 55 | } 56 | 57 | int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 58 | { 59 | ShowCursor(0); 60 | int exit = 0; 61 | MSG msg; 62 | WNDCLASS win = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, " "}; 63 | RegisterClass(&win); 64 | HDC hdc = GetDC(CreateWindowEx(0, win.lpszClassName, " ", WS_VISIBLE|WS_POPUP, 0, 0, 1920, 1080, 0, 0, 0, 0)); 65 | PIXELFORMATDESCRIPTOR pfd = {0, 0, PFD_DOUBLEBUFFER}; 66 | SetPixelFormat(hdc, ChoosePixelFormat(hdc,&pfd),&pfd); 67 | wglMakeCurrent(hdc, wglCreateContext(hdc)); 68 | glEnable(0x8804); 69 | ((PFNGLPROGRAMSTRINGARBPROC)wglGetProcAddress("glProgramStringARB")) (0x8804, 0x8875, strlen(FS), FS); 70 | while (!exit) 71 | { 72 | while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 73 | { 74 | if( msg.message == WM_QUIT ) exit = 1; 75 | TranslateMessage( &msg ); 76 | DispatchMessage( &msg ); 77 | } 78 | float time = GetTickCount()*0.001f; 79 | ((PFNGLPROGRAMENVPARAMETER4FVARBPROC)wglGetProcAddress("glProgramEnvParameter4fvARB"))(0x8804, 0, &time); 80 | glRects(-1, -1, 1, 1); 81 | wglSwapLayerBuffers(hdc, WGL_SWAP_MAIN_PLANE); 82 | } 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /GLSL/ASM/greenblue.c: -------------------------------------------------------------------------------- 1 | // gcc greenblue.c -lX11 -lGL -o greenblue 2 | // ./greenblue 3 | // Tested in Lubuntu 19 4 | 5 | #include 6 | #include 7 | #define GL_GLEXT_PROTOTYPES 1 8 | #include 9 | #include 10 | #include 11 | 12 | static const char* FS = \ 13 | "!!ARBfp1.0\n" 14 | "PARAM time = program.env[0];" 15 | "TEMP R0, R1, R2, H0;" 16 | "SIN R1.x, time.x;" 17 | "MAD R2.x, R1.x, {0.5}.x, {0.5}.x;" 18 | "MUL R0.x, fragment.position, {0.00078};" 19 | "SGE H0.x, R2.x, R0;" 20 | "MAD result.color, H0.x, {0,.5,-1,0}, {0,0,1,1};" 21 | "END"; 22 | 23 | static long GetTickCount( void ) 24 | { 25 | struct timeval now, res; 26 | gettimeofday(&now, 0); 27 | return (long)((now.tv_sec*1000) + (now.tv_usec/1000)); 28 | } 29 | 30 | int main( void ) 31 | { 32 | Display *hDisplay = XOpenDisplay( NULL ); 33 | int buffer[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None }; 34 | XVisualInfo *info = glXChooseVisual( hDisplay, DefaultScreen(hDisplay), buffer); 35 | XSetWindowAttributes winAttr; 36 | winAttr.override_redirect = 1; 37 | winAttr.colormap = XCreateColormap( hDisplay, RootWindow(hDisplay, info->screen), info->visual, AllocNone ); 38 | Window hWnd = XCreateWindow( hDisplay, RootWindow(hDisplay, info->screen), 0, 0, 1280, 720, 0, info->depth, InputOutput, info->visual, CWColormap|CWOverrideRedirect, &winAttr); 39 | XMapRaised(hDisplay, hWnd); 40 | XGrabKeyboard(hDisplay, hWnd, 1, GrabModeAsync, GrabModeAsync, CurrentTime); 41 | glXMakeCurrent( hDisplay, hWnd, glXCreateContext(hDisplay, info, NULL, 1) ); 42 | glEnable(GL_FRAGMENT_PROGRAM_ARB); 43 | glProgramStringARB (GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(FS), FS); 44 | XEvent event; 45 | long s = GetTickCount(); 46 | while( !XCheckTypedEvent(hDisplay, KeyPress, &event) ) 47 | { 48 | GLfloat t = (GetTickCount()-s) * 0.001f; 49 | glProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0, &t); 50 | glRects( -1, -1, 1, 1 ); 51 | glXSwapBuffers( hDisplay, hWnd ); 52 | } 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /GLSL/ASM/redblue.c: -------------------------------------------------------------------------------- 1 | // Compile with Visual Studio: cl redblue.c opengl32.lib user32.lib gdi32.lib 2 | // GLSL to Asm: cgc -oglsl -profile fp30 test.glsl 3 | // ARB Assembly Language details: http://www.renderguild.com/gpuguide.pdf 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | typedef int(APIENTRY* PFNWGLSWAPINTERVALEXTPROC) (int i); 10 | typedef void(APIENTRY* PFNGLPROGRAMSTRINGARBPROC) (GLenum target, GLenum format, GLsizei len, const void *string); 11 | 12 | static const char* FS = \ 13 | "!!ARBfp1.0\n" 14 | "TEMP R0, H0;" 15 | "MUL R0.x, fragment.position, {0.00052};" 16 | "SGE H0.x, {0.5}, R0;" 17 | "MAD result.color, H0.x, {1,0,-1,0}, {0,0,1,1};" 18 | "END"; 19 | 20 | int main() 21 | { 22 | ShowCursor(0); 23 | PIXELFORMATDESCRIPTOR pfd = { 0,0,PFD_DOUBLEBUFFER }; 24 | HDC hdc = GetDC(CreateWindow("static", 0, WS_POPUP|WS_VISIBLE|WS_MAXIMIZE, 0, 0, 0, 0, 0, 0, 0, 0)); 25 | SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd); 26 | wglMakeCurrent(hdc, wglCreateContext(hdc)); 27 | glEnable(0x8804); 28 | ((PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT")) (0); 29 | ((PFNGLPROGRAMSTRINGARBPROC)wglGetProcAddress("glProgramStringARB")) (0x8804, 0x8875, strlen(FS), FS); 30 | do 31 | { 32 | glRects(-1, -1, 1, 1); 33 | wglSwapLayerBuffers(hdc, WGL_SWAP_MAIN_PLANE); 34 | } while (!GetAsyncKeyState(VK_ESCAPE)); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /GLSL/BlueTunnel.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "gl.h" 3 | 4 | typedef int(APIENTRY* PFNWGLSWAPINTERVALEXTPROC)(int i); 5 | typedef GLuint(APIENTRY* PFNGLCREATEPROGRAMPROC)(); 6 | typedef GLuint(APIENTRY* PFNGLCREATESHADERPROC)(GLenum t); 7 | typedef void(APIENTRY* PFNGLSHADERSOURCEPROC)(GLuint s, GLsizei c, const char *const *n, const GLint *i); 8 | typedef void(APIENTRY* PFNGLCOMPILESHADERPROC)(GLuint s); 9 | typedef void(APIENTRY* PFNGLATTACHSHADERPROC)(GLuint p, GLuint s); 10 | typedef void(APIENTRY* PFNGLLINKPROGRAMPROC)(GLuint p); 11 | typedef void(APIENTRY* PFNGLUSEPROGRAMPROC)(GLuint p); 12 | typedef GLint(APIENTRY* PFNGLGETUNIFORMLOCATIONPROC) (GLuint p, const char *n); 13 | typedef void (APIENTRY* PFNGLUNIFORM1FVPROC) (GLint k, GLsizei c, const GLfloat *v); 14 | 15 | static const char* FragmentShader = \ 16 | "#version 450 \n" 17 | "layout (location=0) out vec4 color;" 18 | "uniform float time;" 19 | "float k = 9.0;" 20 | "float h(vec2 n)" 21 | "{" 22 | "return fract(sin(dot(mod(n,k),vec2(16.7123,9.1414)))*43758.5453);" 23 | "}" 24 | "float n(vec2 p)" 25 | "{" 26 | "vec2 i = floor(p), u = fract(p);" 27 | "u = u*u*(3.-2.*u);" 28 | "return mix(mix(h(i),h(i+vec2(1,0)),u.x),mix(h(i+vec2(0,1)),h(i+vec2(1,1)),u.x),u.y);" 29 | "}" 30 | "float t(vec2 u) " 31 | "{" 32 | "float r = length( u ); " 33 | "u = vec2( 1.0/r+(time*2.0),atan( u.y, u.x )/3.1415927 ); " 34 | "return n(k*u)*r;" 35 | "}" 36 | "void main()" 37 | "{" 38 | "vec2 u = (2.*gl_FragCoord.xy - vec2(1920,1080))/1080;" 39 | "color = vec4(0,0,t(u)*t(u),1);" 40 | "}"; 41 | 42 | int main() 43 | { 44 | ShowCursor(0); 45 | PIXELFORMATDESCRIPTOR pfd = { 0,0,PFD_DOUBLEBUFFER }; 46 | HDC hdc = GetDC(CreateWindow("static", 0, WS_POPUP|WS_VISIBLE|WS_MAXIMIZE, 0, 0, 0, 0, 0, 0, 0, 0)); 47 | SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd); 48 | wglMakeCurrent(hdc, wglCreateContext(hdc)); 49 | ((PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT")) (0); 50 | int p = ((PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram"))(); 51 | int s = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(0x8B30); 52 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(s, 1, &FragmentShader, 0); 53 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(s); 54 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p, s); 55 | ((PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram"))(p); 56 | ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(p); 57 | GLint location = ((PFNGLGETUNIFORMLOCATIONPROC)wglGetProcAddress("glGetUniformLocation"))(p,"time"); 58 | float S = GetTickCount(); 59 | do 60 | { 61 | float t = (GetTickCount()-S)*0.001f; 62 | ((PFNGLUNIFORM1FVPROC)wglGetProcAddress("glUniform1fv"))(location, 1, &t); 63 | glRects(-1, -1, 1, 1); 64 | wglSwapLayerBuffers(hdc, WGL_SWAP_MAIN_PLANE); 65 | } while (!GetAsyncKeyState(VK_ESCAPE)); 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /GLSL/ComputeShader.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef int(APIENTRY* PFNWGLSWAPINTERVALEXTPROC)(int i); 5 | typedef GLuint(APIENTRY* PFNGLCREATEPROGRAMPROC)(); 6 | typedef GLuint(APIENTRY* PFNGLCREATESHADERPROC)(GLenum t); 7 | typedef void(APIENTRY* PFNGLSHADERSOURCEPROC)(GLuint s, GLsizei c, const char*const*string, const GLint* i); 8 | typedef void(APIENTRY* PFNGLCOMPILESHADERPROC)(GLuint s); 9 | typedef void(APIENTRY* PFNGLATTACHSHADERPROC)(GLuint p, GLuint s); 10 | typedef void(APIENTRY* PFNGLLINKPROGRAMPROC)(GLuint p); 11 | typedef void(APIENTRY* PFNGLUSEPROGRAMPROC)(GLuint p); 12 | typedef void (APIENTRY* PFNGLDISPATCHCOMPUTEPROC) (GLuint x, GLuint y, GLuint z); 13 | typedef void (APIENTRY* PFNGLBINDIMAGETEXTUREPROC) (GLuint a, GLuint b, GLint c, GLboolean d, GLint e, GLenum f, GLenum g); 14 | 15 | static const char* ComputeShader = \ 16 | "#version 430 core\n" 17 | "writeonly uniform image2D writer;" 18 | "layout (local_size_x = 16, local_size_y = 16) in;" 19 | "void main()" 20 | "{" 21 | "vec2 coordinates = gl_GlobalInvocationID.xy;" 22 | "vec2 resolution = vec2(1024,1024);" 23 | "vec2 k = sign(cos(coordinates/resolution.yy*32.0));" 24 | "imageStore(writer,ivec2(gl_GlobalInvocationID),vec4(k.x*k.y));" 25 | "}" ; 26 | 27 | static const char* FragmentShader = \ 28 | "#version 430 core\n" 29 | "layout (location=0) out vec4 color;" 30 | "uniform sampler2D reader;" 31 | "void main()" 32 | "{" 33 | "color = texture(reader,gl_FragCoord.xy/vec2(1920,1080));" 34 | "}"; 35 | 36 | void LoadTexture() 37 | { 38 | GLuint h; 39 | glBindTexture(GL_TEXTURE_2D, h); 40 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 41 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 42 | glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,1024,1024,0,GL_RGBA,GL_FLOAT,0); 43 | ((PFNGLBINDIMAGETEXTUREPROC)wglGetProcAddress("glBindImageTexture"))(0,h,0,GL_FALSE,0,0x88B9,GL_RGBA8); 44 | } 45 | 46 | int MakeShader(const char* source, GLenum type) 47 | { 48 | int p = ((PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram"))(); 49 | int s = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(type); 50 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(s,1,&source,0); 51 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(s); 52 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p,s); 53 | ((PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram"))(p); 54 | return p; 55 | } 56 | 57 | int main() 58 | { 59 | ShowCursor(0); 60 | PIXELFORMATDESCRIPTOR pfd = {0,0,PFD_DOUBLEBUFFER}; 61 | HDC hdc = GetDC(CreateWindow("static",0,WS_POPUP|WS_VISIBLE|WS_MAXIMIZE,0,0,0,0,0,0,0,0)); 62 | SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd); 63 | wglMakeCurrent(hdc, wglCreateContext(hdc)); 64 | ((PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress ("wglSwapIntervalEXT")) (0); 65 | int FS = MakeShader(FragmentShader,0x8B30); 66 | int CS = MakeShader(ComputeShader,0x91B9); 67 | LoadTexture(); 68 | ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(CS); 69 | ((PFNGLDISPATCHCOMPUTEPROC)wglGetProcAddress("glDispatchCompute"))(1024/16,1024/16,1); 70 | ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(FS); 71 | do 72 | { 73 | glRects(-1,-1,1,1); 74 | wglSwapLayerBuffers(hdc,WGL_SWAP_MAIN_PLANE); 75 | } while (!GetAsyncKeyState(VK_ESCAPE)); 76 | return 0; 77 | } -------------------------------------------------------------------------------- /GLSL/ComputeShader.cpp: -------------------------------------------------------------------------------- 1 | // g++ -s -o ComputeShader.exe ComputeShader.cpp -mwindows -lopengl32 2 | #define WIN32_LEAN_AND_MEAN 3 | #define WIN32_EXTRA_LEAN 4 | #include 5 | #include 6 | #include "glext.h" 7 | #include //only for debug 8 | #include //only for debug 9 | 10 | typedef bool (APIENTRY *PFNWGLSWAPINTERVALEXTPROC) (int interval); 11 | 12 | static const char* ComputeShader = \ 13 | "#version 430 core\n" 14 | "writeonly uniform image2D writer;" 15 | "layout (local_size_x = 16, local_size_y = 16) in;" 16 | "void main()" 17 | "{" 18 | "vec2 coordinates = gl_GlobalInvocationID.xy;" 19 | "vec2 resolution = vec2(512,512);" 20 | "vec2 k = sign(cos(coordinates/resolution.yy*32.0));" 21 | "imageStore(writer,ivec2(gl_GlobalInvocationID.xy),vec4(k.x*k.y));" 22 | "}" ; 23 | 24 | static const char* FragmentShader = \ 25 | "#version 430 core\n" 26 | "layout (location=0) out vec4 color;" 27 | "uniform float time;" 28 | "uniform sampler2D reader;" 29 | "mat3 rotationX(float x)" 30 | "{" 31 | "return mat3(1.0,0.0,0.0,0.0,cos(x),sin(x),0.0,-sin(x),cos(x));" 32 | "}" 33 | "mat3 rotationY(float y)" 34 | "{" 35 | "return mat3(cos(y),0.0,-sin(y),0.0,1.0,0.0,sin(y),0.0,cos(y));" 36 | "}" 37 | "float Cuboid (vec3 p,vec3 c,vec3 s)" 38 | "{" 39 | "vec3 d = abs(p-c)-s;" 40 | "return max(max(d.x,d.y),d.z);" 41 | "}" 42 | "float Map (vec3 p)" 43 | "{" 44 | "return Cuboid(p,vec3(0.0,0.0,0.0),vec3(2.0,2.0,2.0));" 45 | "}" 46 | "vec4 SetTexture (sampler2D s, vec3 pos, vec3 nor)" 47 | "{" 48 | "vec3 w = nor*nor;" 49 | "return (w.x*texture(s,pos.yz)+w.y*texture(s,pos.zx)+w.z*texture(s,pos.xy))/(w.x+w.y+w.z);" 50 | "}" 51 | "vec3 SetNormal (vec3 p)" 52 | "{" 53 | "vec2 e = vec2(0.01,0.00);" 54 | "return normalize(vec3(Map(p+e.xyy)-Map(p-e.xyy),Map(p+e.yxy)-Map(p-e.yxy),Map(p+e.yyx)-Map(p-e.yyx)));" 55 | "}" 56 | "vec4 Raymarch (vec3 ro, vec3 rd)" 57 | "{" 58 | "for (int i=0; i<128; i++)" 59 | "{" 60 | "float t = Map(ro);" 61 | "if (t<0.001) return SetTexture(reader,ro*0.2,SetNormal(ro));" 62 | "ro+=t*rd;" 63 | "}" 64 | "return vec4(0.0,0.0,0.5,0.0);" 65 | "}" 66 | "void main()" 67 | "{" 68 | "vec2 uv = (2.0*gl_FragCoord.xy-vec2(1366,768))/768;" 69 | "vec3 ro = vec3(0.0,0.0,-10.0);" 70 | "vec3 rd = normalize(vec3(uv,2.0));" 71 | "ro*=rotationY(time)*rotationX(time);" 72 | "rd*=rotationY(time)*rotationX(time);" 73 | "color = Raymarch(ro,rd);" 74 | "}"; 75 | 76 | void Debug(int sh) 77 | { 78 | int isCompiled = 0; 79 | ((PFNGLGETSHADERIVPROC)wglGetProcAddress("glGetShaderiv"))(sh,GL_LINK_STATUS,&isCompiled); 80 | if(isCompiled == GL_FALSE) 81 | { 82 | int length = 0; 83 | ((PFNGLGETSHADERIVPROC)wglGetProcAddress("glGetShaderiv"))(sh,GL_INFO_LOG_LENGTH,&length); 84 | int q = 0; 85 | char* log = (char*)malloc(sizeof(char)*length); 86 | ((PFNGLGETSHADERINFOLOGPROC)wglGetProcAddress("glGetShaderInfoLog"))(sh,length,&q,log); 87 | if (length>1) 88 | { 89 | FILE *file = fopen ("debug.log","a"); 90 | fprintf (file,"%s\n%s\n",(char*)glGetString(GL_SHADING_LANGUAGE_VERSION),log); 91 | fclose (file); 92 | ExitProcess(0); 93 | } 94 | } 95 | } 96 | 97 | void GenerateTexture() 98 | { 99 | GLuint h; 100 | glBindTexture(GL_TEXTURE_2D, h); 101 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 102 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 103 | glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,512,512,0,GL_RGBA,GL_FLOAT,0); 104 | ((PFNGLBINDIMAGETEXTUREPROC)wglGetProcAddress("glBindImageTexture"))(0,h,0,GL_FALSE,0,GL_WRITE_ONLY,GL_RGBA8); 105 | } 106 | 107 | int MakeShader(const char* source, GLenum type) 108 | { 109 | int p = ((PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram"))(); 110 | int s = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(type); 111 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(s,1,&source,0); 112 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(s); 113 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p,s); 114 | ((PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram"))(p); 115 | Debug(s); return p; 116 | } 117 | 118 | int main() 119 | { 120 | ShowCursor(0); 121 | PIXELFORMATDESCRIPTOR pfd = {0,0,PFD_DOUBLEBUFFER}; 122 | HDC hdc = GetDC(CreateWindow("static",0,WS_POPUP|WS_VISIBLE|WS_MAXIMIZE,0,0,0,0,0,0,0,0)); 123 | SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd); 124 | wglMakeCurrent(hdc, wglCreateContext(hdc)); 125 | ((PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress ("wglSwapIntervalEXT")) (0); 126 | int FS = MakeShader(FragmentShader,GL_FRAGMENT_SHADER); 127 | int CS = MakeShader(ComputeShader,GL_COMPUTE_SHADER); 128 | GenerateTexture(); 129 | ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(CS); 130 | ((PFNGLDISPATCHCOMPUTEPROC)wglGetProcAddress("glDispatchCompute"))(512/16,512/16,1); 131 | ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(FS); 132 | GLint location = ((PFNGLGETUNIFORMLOCATIONARBPROC)wglGetProcAddress("glGetUniformLocation"))(FS,"time"); 133 | float S = GetTickCount(); //start timer 134 | do 135 | { 136 | float t = (GetTickCount()-S)*0.001f; //update timer 137 | ((PFNGLUNIFORM1FVPROC)wglGetProcAddress("glUniform1fv"))(location, 1, &t); 138 | glRects(-1,-1,1,1); 139 | wglSwapLayerBuffers(hdc,WGL_SWAP_MAIN_PLANE); 140 | } while (!GetAsyncKeyState(VK_ESCAPE)); 141 | return 0; 142 | } -------------------------------------------------------------------------------- /GLSL/Demo.cpp: -------------------------------------------------------------------------------- 1 | // g++ -s -o Demo.exe Demo.cpp -IGL/inc -LGL/lib -lwinmm -mwindows -lopengl32 -lglew32 2 | #define WIN32_LEAN_AND_MEAN 3 | #define WIN32_EXTRA_LEAN 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include //only for debug 9 | #include //only for debug 10 | 11 | typedef bool (APIENTRY *PFNWGLSWAPINTERVALEXTPROC) (int interval); 12 | static int h[11] = {0x46464952,9172836,0x45564157,0x20746D66,16,WAVE_FORMAT_PCM|131072,44100,176400,1048580,0x61746164,9172800}; 13 | static short m[9172822]; 14 | 15 | static const char* FragmentShader01 = \ 16 | "#version 400\n" 17 | "layout (location=0) out vec4 color;" 18 | "uniform float time;" 19 | "uniform sampler2D hash;" 20 | "void main()" 21 | "{" 22 | "vec2 uv = (2.0*gl_FragCoord.xy-vec2(1366,768))/768;" 23 | "vec3 o = vec3(0.,0.,-3.);" 24 | "for (int i=0;i<64;i++)" 25 | "{" 26 | "float l=length(o)-1.;" 27 | "color=l<.01?dot(vec3(-.6),o)*texture(hash,o.xy+time*.1):color;" 28 | "o+=l*normalize(vec3(uv,2));" 29 | "}" 30 | "}"; 31 | 32 | static const char* FragmentShader02 = \ 33 | "#version 400\n" 34 | "layout (location=0) out vec4 color;" 35 | "uniform float time;" 36 | "uniform sampler2D hash;" 37 | "void main()" 38 | "{" 39 | "vec2 u = gl_FragCoord.xy/37.5;" 40 | "u.x+=time*(int(u.y)%2>0?.8:-2.4);" 41 | "color=texture(hash,floor(u)/256.);" 42 | "}"; 43 | 44 | int f2i(float x) //convert float to int, require C++17 45 | { 46 | if (x>=0x1.0p23) return x; 47 | return (unsigned int) (x+0.49999997f); 48 | } 49 | 50 | float Bass(float t,float n,float e) //time,15.0,18.0 51 | { 52 | float x = fmod(t,0.5f); 53 | return 0.5f * fmax(-1.,fmin(1.,sin(x*pow(2.,((30.-x*5.f-70.)/n))*440.f*6.28f)*10.f*exp(-x*e))); 54 | } 55 | 56 | void SetAudio(short *buffer) 57 | { 58 | for (int i = 0; i<4586400; i++) 59 | { 60 | float t = (float)i / (float)44100; 61 | float f = Bass(t,15.0,18.0); 62 | buffer[2*i+0] = f2i(f*32767.0f); 63 | buffer[2*i+1] = f2i(f*32767.0f); 64 | } 65 | } 66 | 67 | void PlayAudio() 68 | { 69 | SetAudio(m+22); 70 | memcpy(m,h,44); 71 | sndPlaySound((const char*)&m,SND_ASYNC|SND_MEMORY); 72 | } 73 | 74 | void GenerateNoiseTexture() 75 | { 76 | GLubyte Texels [256][256][4]; 77 | for (int i = 0; i < 256; i++) 78 | { 79 | for (int j = 0; j < 256; j++) 80 | { 81 | float h = fmod(sin(j*12.9898f+i*4.1413f)*43758.5453f,1.0f); 82 | float k = fmod(cos(i*19.6534f+j*7.9813f)*51364.8733f,1.0f); 83 | Texels[j][i][0] = h*255; 84 | Texels[j][i][2] = k*255; 85 | } 86 | } 87 | for (int i = 0; i < 256; i++) 88 | { 89 | for (int j = 0; j < 256; j++) 90 | { 91 | int x2 = (j - 37) & 255; 92 | int y2 = (i - 17) & 255; 93 | Texels[j][i][1] = Texels[x2][y2][0]; 94 | Texels[j][i][3] = Texels[x2][y2][2]; 95 | } 96 | } 97 | glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,256,256,0,GL_RGBA,GL_UNSIGNED_BYTE,Texels); 98 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 99 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 100 | } 101 | 102 | void Debug(int sh) 103 | { 104 | GLint isCompiled = 0; 105 | ((PFNGLGETSHADERIVPROC)wglGetProcAddress("glGetShaderiv"))(sh,GL_LINK_STATUS,&isCompiled); 106 | if(isCompiled == GL_FALSE) 107 | { 108 | GLint length = 0; 109 | ((PFNGLGETSHADERIVPROC)wglGetProcAddress("glGetShaderiv"))(sh,GL_INFO_LOG_LENGTH,&length); 110 | GLsizei q = 0; 111 | char* log = (char*)malloc(sizeof(char)*length); 112 | ((PFNGLGETSHADERINFOLOGPROC)wglGetProcAddress("glGetShaderInfoLog"))(sh,length,&q,log); 113 | if (length>1) 114 | { 115 | FILE *file = fopen ("debug.log","a"); 116 | fprintf (file,"%s\n%s\n",(char*)glGetString(GL_SHADING_LANGUAGE_VERSION),log); 117 | fclose (file); 118 | ExitProcess(0); 119 | } 120 | } 121 | } 122 | 123 | int Demo(const char* source) 124 | { 125 | int p = ((PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram"))(); 126 | int s = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(GL_FRAGMENT_SHADER); 127 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(s,1,&source,0); 128 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(s); 129 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p,s); 130 | ((PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram"))(p); 131 | Debug(s); 132 | return p; 133 | } 134 | 135 | int main() 136 | { 137 | ShowCursor(0); 138 | PIXELFORMATDESCRIPTOR pfd = {0,0,PFD_DOUBLEBUFFER}; 139 | HDC hdc = GetDC(CreateWindow("static",0,WS_POPUP|WS_VISIBLE|WS_MAXIMIZE,0,0,0,0,0,0,0,0)); 140 | SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd); 141 | wglMakeCurrent(hdc, wglCreateContext(hdc)); 142 | ((PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress ("wglSwapIntervalEXT")) (0); 143 | int scene1 = Demo(FragmentShader01); 144 | int scene2 = Demo(FragmentShader02); 145 | GLint location1 = ((PFNGLGETUNIFORMLOCATIONARBPROC)wglGetProcAddress("glGetUniformLocation"))(scene1,"time"); 146 | GLint location2 = ((PFNGLGETUNIFORMLOCATIONARBPROC)wglGetProcAddress("glGetUniformLocation"))(scene2,"time"); 147 | GenerateNoiseTexture(); 148 | PlayAudio(); 149 | float S = GetTickCount(); //start timer 150 | do 151 | { 152 | GLfloat t = (GetTickCount()- S) * 0.001f; //update timer 153 | if (t<8.0f) 154 | { 155 | ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(scene1); 156 | ((PFNGLUNIFORM1FVPROC)wglGetProcAddress("glUniform1fv"))(location1, 1, &t); 157 | } 158 | else 159 | { 160 | ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(scene2); 161 | ((PFNGLUNIFORM1FVPROC)wglGetProcAddress("glUniform1fv"))(location2, 1, &t); 162 | } 163 | glRects(-1,-1,1,1); 164 | wglSwapLayerBuffers(hdc,WGL_SWAP_MAIN_PLANE); 165 | } while (!GetAsyncKeyState(VK_ESCAPE)); 166 | return 0; 167 | } 168 | -------------------------------------------------------------------------------- /GLSL/GL/lib/glew32.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/CPP-Programming/9d93e94a00402ac3b19fd1732368a3a4fa1ad18a/GLSL/GL/lib/glew32.dll -------------------------------------------------------------------------------- /GLSL/GLSL.c: -------------------------------------------------------------------------------- 1 | // gcc -x c -s -o GLSL.exe GLSL.c -mwindows -lopengl32 2 | #include 3 | #include 4 | 5 | typedef int(APIENTRY* PFNWGLSWAPINTERVALEXTPROC)(int i); 6 | typedef GLuint(APIENTRY* PFNGLCREATEPROGRAMPROC)(); 7 | typedef GLuint(APIENTRY* PFNGLCREATESHADERPROC)(GLenum t); 8 | typedef void(APIENTRY* PFNGLSHADERSOURCEPROC)(GLuint s, GLsizei c, const char*const*string, const GLint* i); 9 | typedef void(APIENTRY* PFNGLCOMPILESHADERPROC)(GLuint s); 10 | typedef void(APIENTRY* PFNGLATTACHSHADERPROC)(GLuint p, GLuint s); 11 | typedef void(APIENTRY* PFNGLLINKPROGRAMPROC)(GLuint p); 12 | typedef void(APIENTRY* PFNGLUSEPROGRAMPROC)(GLuint p); 13 | 14 | static const char* FragmentShader = \ 15 | "#version 450 \n" 16 | "layout (location=0) out vec4 color;" 17 | "void main()" 18 | "{" 19 | "color = vec4(gl_FragCoord.xy/vec2(1920,1080),0,1);" 20 | "}"; 21 | 22 | int main() 23 | { 24 | ShowCursor(0); 25 | PIXELFORMATDESCRIPTOR pfd = { 0,0,PFD_DOUBLEBUFFER }; 26 | HDC hdc = GetDC(CreateWindow("static", 0, WS_POPUP|WS_VISIBLE|WS_MAXIMIZE, 0, 0, 0, 0, 0, 0, 0, 0)); 27 | SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd); 28 | wglMakeCurrent(hdc, wglCreateContext(hdc)); 29 | ((PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT")) (0); 30 | int p = ((PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram"))(); 31 | int s = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(0x8B30); 32 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(s, 1, &FragmentShader, 0); 33 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(s); 34 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p, s); 35 | ((PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram"))(p); 36 | ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(p); 37 | do 38 | { 39 | glRects(-1, -1, 1, 1); 40 | wglSwapLayerBuffers(hdc, WGL_SWAP_MAIN_PLANE); 41 | } while (!GetAsyncKeyState(VK_ESCAPE)); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /GLSL/GLSL.cpp: -------------------------------------------------------------------------------- 1 | // g++ -s -o GLSL.exe GLSL.cpp -IGL/inc -LGL/lib -mwindows -lopengl32 -lglew32 2 | #define WIN32_LEAN_AND_MEAN 3 | #define WIN32_EXTRA_LEAN 4 | #include 5 | #include 6 | 7 | const static char* FragmentShader = \ 8 | "#version 130\n" 9 | "uniform float time;" 10 | "void main()" 11 | "{" 12 | "vec2 uv = gl_FragCoord.xy / vec2(1920,1080);" 13 | "gl_FragColor = vec4(uv,sin(time)*0.5+0.5,1.0);" 14 | "}" ; 15 | 16 | int main() 17 | { 18 | ShowCursor(0); 19 | PIXELFORMATDESCRIPTOR pfd = {0,0,PFD_DOUBLEBUFFER,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 20 | DEVMODE setup = {{0},0,0,156,0,0x001c0000,{0},0,0,0,0,0,{0},0,32,1920,1080,{0},0,0,0}; 21 | HDC hdc = GetDC(CreateWindow("static",0,WS_POPUP|WS_VISIBLE|WS_MAXIMIZE,0,0,0,0,0,0,0,0)); 22 | SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd); 23 | wglMakeCurrent(hdc, wglCreateContext(hdc)); 24 | const int p = ((PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram"))(); 25 | const int s = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(GL_FRAGMENT_SHADER); 26 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(s,1,&FragmentShader,0); 27 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(s); 28 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p,s); 29 | ((PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram"))(p); 30 | ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(p); 31 | GLint location = ((PFNGLGETUNIFORMLOCATIONARBPROC)wglGetProcAddress("glGetUniformLocation"))(p,"time"); 32 | do 33 | { 34 | glRects(-1,-1,1,1); 35 | GLfloat t = GetTickCount()*0.001f; 36 | ((PFNGLUNIFORM1FVPROC)wglGetProcAddress("glUniform1fv"))(location, 1, &t); 37 | SwapBuffers(hdc); 38 | } while (!GetAsyncKeyState(VK_ESCAPE)); 39 | ExitProcess(0); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /GLSL/GLSL4.cpp: -------------------------------------------------------------------------------- 1 | // g++ -s -o GLSL4.exe GLSL4.cpp -IGL/inc -LGL/lib -mwindows -lopengl32 -lglew32 2 | #define WIN32_LEAN_AND_MEAN 3 | #define WIN32_EXTRA_LEAN 4 | #include 5 | #include 6 | 7 | typedef bool (APIENTRY *PFNWGLSWAPINTERVALEXTPROC) (int interval); 8 | 9 | static const char* VertexShader = \ 10 | "#version 450\n" 11 | "layout (location=0) in vec3 position;" 12 | "void main()" 13 | "{" 14 | "gl_Position=vec4(position,1.0);" 15 | "}"; 16 | 17 | static const char* FragmentShader = \ 18 | "#version 450\n" 19 | "layout (location=0) out vec4 color;" 20 | "layout (location=1) uniform float time;" 21 | "void main()" 22 | "{" 23 | "vec2 uv = gl_FragCoord.xy/vec2(1920,1080);" 24 | "color = mix(vec4(0.0),vec4(uv,0,1),step(uv.x,sin(time)*0.5+0.5));" 25 | "}"; 26 | 27 | int main() 28 | { 29 | ShowCursor(0); 30 | PIXELFORMATDESCRIPTOR pfd = {0,0,PFD_DOUBLEBUFFER}; 31 | HDC hdc = GetDC(CreateWindow((LPCSTR)0xC018,0,WS_POPUP|WS_VISIBLE|WS_MAXIMIZE,0,0,1920,1080,0,0,0,0)); 32 | SetPixelFormat(hdc,ChoosePixelFormat(hdc,&pfd),&pfd); 33 | wglMakeCurrent(hdc,wglCreateContext(hdc)); 34 | int vs = ((PFNGLCREATESHADERPROGRAMVPROC)wglGetProcAddress("glCreateShaderProgramv")) (GL_VERTEX_SHADER, 1, &VertexShader); 35 | int fs = ((PFNGLCREATESHADERPROGRAMVPROC)wglGetProcAddress("glCreateShaderProgramv")) (GL_FRAGMENT_SHADER, 1, &FragmentShader); 36 | unsigned int p; 37 | ((PFNGLGENPROGRAMPIPELINESPROC)wglGetProcAddress("glGenProgramPipelines")) (1, &p); 38 | ((PFNGLBINDPROGRAMPIPELINEPROC)wglGetProcAddress("glBindProgramPipeline")) (p); 39 | ((PFNGLUSEPROGRAMSTAGESPROC)wglGetProcAddress("glUseProgramStages"))(p, GL_VERTEX_SHADER_BIT, vs); 40 | ((PFNGLUSEPROGRAMSTAGESPROC)wglGetProcAddress("glUseProgramStages"))(p, GL_FRAGMENT_SHADER_BIT, fs); 41 | ((PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress ("wglSwapIntervalEXT")) (0); 42 | do 43 | { 44 | GLfloat t = 0.5f * GetTickCount()*0.001f; 45 | ((PFNGLPROGRAMUNIFORM1FVPROC)wglGetProcAddress("glProgramUniform1fv"))( fs, 1, 1, &t); 46 | glRects(-1,-1,1,1); 47 | wglSwapLayerBuffers(hdc,WGL_SWAP_MAIN_PLANE); 48 | } while (!GetAsyncKeyState(VK_ESCAPE)); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /GLSL/GenerateTexture.cpp: -------------------------------------------------------------------------------- 1 | // g++ -s -o GenerateTexture.exe GenerateTexture.cpp -IGL/inc -LGL/lib -mwindows -lopengl32 -lglew32 2 | #define WIN32_LEAN_AND_MEAN 3 | #define WIN32_EXTRA_LEAN 4 | #include 5 | #include 6 | #include 7 | 8 | typedef bool (APIENTRY *PFNWGLSWAPINTERVALEXTPROC) (int interval); 9 | 10 | static const char* VertexShader = \ 11 | "#version 450\n" 12 | "layout (location=0) in vec3 position;" 13 | "void main()" 14 | "{" 15 | "gl_Position=vec4(position,1.0);" 16 | "}"; 17 | 18 | static const char* FragmentShader = \ 19 | "#version 450\n" 20 | "layout (location=0) out vec4 color;" 21 | "layout (location=1) uniform float time;" 22 | "uniform sampler2D hash;" 23 | "void main()" 24 | "{" 25 | "vec2 uv = gl_FragCoord.xy/vec2(1920,1080);" 26 | "color = textureLod(hash,uv,0)*(sin(time)*0.5+0.5);" 27 | "}"; 28 | 29 | void GenerateNoiseTexture(void) 30 | { 31 | GLubyte Texels [512][512][4]; 32 | for (int i=0; i<512; i++) 33 | { 34 | for (int j=0; j<512; j++) 35 | { 36 | float h = fmod(sin(j*12.9898f+i*4.1413f)*43758.5453f,1.0f); 37 | Texels[i][j][0] = h*255; 38 | Texels[i][j][1] = h*255; 39 | Texels[i][j][2] = h*255; 40 | Texels[i][j][3] = 255; 41 | } 42 | } 43 | glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,512,512,0,GL_RGBA,GL_UNSIGNED_BYTE,Texels); 44 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); 45 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); 46 | } 47 | 48 | int main() 49 | { 50 | ShowCursor(0); 51 | PIXELFORMATDESCRIPTOR pfd = {0,0,PFD_DOUBLEBUFFER}; 52 | HDC hdc = GetDC(CreateWindow((LPCSTR)0xC018,0,WS_POPUP|WS_VISIBLE|WS_MAXIMIZE,0,0,1920,1080,0,0,0,0)); 53 | SetPixelFormat(hdc,ChoosePixelFormat(hdc,&pfd),&pfd); 54 | wglMakeCurrent(hdc,wglCreateContext(hdc)); 55 | int vs = ((PFNGLCREATESHADERPROGRAMVPROC)wglGetProcAddress("glCreateShaderProgramv")) (GL_VERTEX_SHADER, 1, &VertexShader); 56 | int fs = ((PFNGLCREATESHADERPROGRAMVPROC)wglGetProcAddress("glCreateShaderProgramv")) (GL_FRAGMENT_SHADER, 1, &FragmentShader); 57 | unsigned int p; 58 | ((PFNGLGENPROGRAMPIPELINESPROC)wglGetProcAddress("glGenProgramPipelines")) (1, &p); 59 | ((PFNGLBINDPROGRAMPIPELINEPROC)wglGetProcAddress("glBindProgramPipeline")) (p); 60 | ((PFNGLUSEPROGRAMSTAGESPROC)wglGetProcAddress("glUseProgramStages"))(p, GL_VERTEX_SHADER_BIT, vs); 61 | ((PFNGLUSEPROGRAMSTAGESPROC)wglGetProcAddress("glUseProgramStages"))(p, GL_FRAGMENT_SHADER_BIT, fs); 62 | ((PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress ("wglSwapIntervalEXT")) (0); 63 | GenerateNoiseTexture(); 64 | do 65 | { 66 | GLfloat t = GetTickCount()*0.001f; 67 | ((PFNGLPROGRAMUNIFORM1FVPROC)wglGetProcAddress("glProgramUniform1fv"))( fs, 1, 1, &t); 68 | glRects(-1,-1,1,1); 69 | wglSwapLayerBuffers(hdc,WGL_SWAP_MAIN_PLANE); 70 | } while (!GetAsyncKeyState(VK_ESCAPE)); 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /GLSL/LoadTexture.c: -------------------------------------------------------------------------------- 1 | // gcc -x c -s -o LoadTexture.exe LoadTexture.c -mwindows -lopengl32 2 | // Texture loader is compatible with GIMP PPM (PNM) binary file format. 3 | #define WIN32_LEAN_AND_MEAN 4 | #define WIN32_EXTRA_LEAN 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | typedef int(APIENTRY *PFNWGLSWAPINTERVALEXTPROC)(int i); 11 | typedef GLuint(APIENTRY *PFNGLCREATEPROGRAMPROC)(); 12 | typedef GLuint(APIENTRY *PFNGLCREATESHADERPROC)(GLenum t); 13 | typedef void(APIENTRY *PFNGLSHADERSOURCEPROC)(GLuint s, GLsizei c, const char *const *n, const GLint *i); 14 | typedef void(APIENTRY *PFNGLCOMPILESHADERPROC)(GLuint s); 15 | typedef void(APIENTRY *PFNGLATTACHSHADERPROC)(GLuint p, GLuint s); 16 | typedef void(APIENTRY *PFNGLLINKPROGRAMPROC)(GLuint p); 17 | typedef void(APIENTRY *PFNGLUSEPROGRAMPROC)(GLuint p); 18 | typedef GLint(APIENTRY *PFNGLGETUNIFORMLOCATIONPROC) (GLuint p, const char *n); 19 | typedef void (APIENTRY *PFNGLUNIFORM1FVPROC) (GLint k, GLsizei c, const GLfloat *v); 20 | typedef void (APIENTRY *PFNGLUNIFORM1IPROC) (GLint l, GLint v); 21 | typedef void (APIENTRY *PFNGLACTIVETEXTUREPROC) (GLenum t); 22 | 23 | static const char* FragmentShader = \ 24 | "#version 400 \n" 25 | "layout (location=0) out vec4 color;" 26 | "uniform float time;" 27 | "uniform sampler2D clouds;" 28 | "uniform sampler2D ground;" 29 | "void main()" 30 | "{" 31 | "vec3 d = normalize(vec3((2.0*gl_FragCoord.xy-vec2(1366,768))/768,2.0));" 32 | "vec3 p = d*(-0.2/d.y);" 33 | "p.z+=time*0.1;" 34 | "color = max(texture(clouds,p.xz)*(3.0*d.y),texture(ground,p.xz)*(-3.0*d.y));" 35 | "}"; 36 | 37 | void LoadTexture(char *filename, int unit, int id, int shader, char *name) 38 | { 39 | int width, height; 40 | char buffer[128]; 41 | FILE *file = fopen(filename, "rb"); 42 | fgets(buffer, sizeof(buffer), file); 43 | do fgets(buffer, sizeof (buffer), file); while (buffer[0] == '#'); 44 | sscanf (buffer, "%d %d", &width, &height); 45 | do fgets (buffer, sizeof (buffer), file); while (buffer[0] == '#'); 46 | int size = width * height * 4 * sizeof(GLubyte); 47 | GLubyte *Texels = (GLubyte *)malloc(size); 48 | for (int i = 0; i < size; i++) 49 | { 50 | Texels[i] = ((i % 4) < 3 ) ? (GLubyte) fgetc(file) : (GLubyte) 255 ; 51 | } 52 | fclose(file); 53 | ((PFNGLACTIVETEXTUREPROC)wglGetProcAddress("glActiveTexture"))(unit); 54 | glBindTexture(GL_TEXTURE_2D, id); 55 | glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,Texels); 56 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 57 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 58 | int num = ((PFNGLGETUNIFORMLOCATIONPROC)wglGetProcAddress("glGetUniformLocation"))(shader, name); 59 | ((PFNGLUNIFORM1IPROC)wglGetProcAddress("glUniform1i"))(num, id); 60 | } 61 | 62 | int main() 63 | { 64 | ShowCursor(0); 65 | PIXELFORMATDESCRIPTOR pfd = { 0,0,PFD_DOUBLEBUFFER }; 66 | HDC hdc = GetDC(CreateWindow("static", 0, WS_POPUP|WS_VISIBLE|WS_MAXIMIZE, 0, 0, 0, 0, 0, 0, 0, 0)); 67 | SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd); 68 | wglMakeCurrent(hdc, wglCreateContext(hdc)); 69 | ((PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT")) (0); 70 | int p = ((PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram"))(); 71 | int s = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(0x8B30); 72 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(s, 1, &FragmentShader, 0); 73 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(s); 74 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p, s); 75 | ((PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram"))(p); 76 | ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(p); 77 | LoadTexture("clouds.ppm", 0x84C0, 0, p, "clouds"); 78 | LoadTexture("plaster.ppm", 0x84C1, 1, p, "ground"); 79 | int location = ((PFNGLGETUNIFORMLOCATIONPROC)wglGetProcAddress("glGetUniformLocation"))(p,"time"); 80 | DWORD S = GetTickCount(); 81 | do 82 | { 83 | float t = (GetTickCount()-S)*0.001f; 84 | ((PFNGLUNIFORM1FVPROC)wglGetProcAddress("glUniform1fv"))(location, 1, &t); 85 | glRects(-1, -1, 1, 1); 86 | wglSwapLayerBuffers(hdc, WGL_SWAP_MAIN_PLANE); 87 | } while (!GetAsyncKeyState(VK_ESCAPE)); 88 | return 0; 89 | } -------------------------------------------------------------------------------- /GLSL/PostProcessing.c: -------------------------------------------------------------------------------- 1 | // gcc -x c -s -o PostProcessing.exe PostProcessing.c -mwindows -lopengl32 2 | #include 3 | #include 4 | 5 | typedef int (APIENTRY *PFNWGLSWAPINTERVALEXTPROC)(int i); 6 | typedef GLuint (APIENTRY *PFNGLCREATEPROGRAMPROC)(); 7 | typedef GLuint (APIENTRY *PFNGLCREATESHADERPROC)(GLenum t); 8 | typedef void (APIENTRY *PFNGLSHADERSOURCEPROC)(GLuint s, GLsizei c, const char*const*string, const GLint* i); 9 | typedef void (APIENTRY *PFNGLCOMPILESHADERPROC)(GLuint s); 10 | typedef void (APIENTRY *PFNGLATTACHSHADERPROC)(GLuint p, GLuint s); 11 | typedef void (APIENTRY *PFNGLLINKPROGRAMPROC)(GLuint p); 12 | typedef void (APIENTRY *PFNGLUSEPROGRAMPROC)(GLuint p); 13 | typedef void (APIENTRY *PFNGLGENFRAMEBUFFERSPROC) (GLsizei n, GLuint *f); 14 | typedef void (APIENTRY *PFNGLBINDFRAMEBUFFEREXTPROC) (GLenum t, GLuint f); 15 | typedef void (APIENTRY *PFNGLTEXSTORAGE2DPROC) (GLenum t, GLsizei l, GLenum i, GLsizei w, GLsizei h); 16 | typedef void (APIENTRY *PFNGLFRAMEBUFFERTEXTUREPROC) (GLenum t, GLenum a, GLuint s, GLint l); 17 | typedef GLint(APIENTRY *PFNGLGETUNIFORMLOCATIONPROC) (GLuint p, const char *n); 18 | typedef void (APIENTRY *PFNGLUNIFORM1FVPROC) (GLint k, GLsizei c, const GLfloat *v); 19 | 20 | static const char *PostProcessingShader = \ 21 | "#version 430 \n" 22 | "layout (location=0) out vec4 color;" 23 | "uniform sampler2D reader;" 24 | "void main()" 25 | "{" 26 | "vec3 s = texture(reader,gl_FragCoord.xy/vec2(1920,1080)).rgb;" 27 | "float grayscale = dot(s, vec3(0.2126, 0.7152, 0.0722));" 28 | "color = vec4(grayscale,grayscale,grayscale,1.0);" 29 | "}"; 30 | 31 | static const char *MainShader = \ 32 | "#version 430 \n" 33 | "layout (location=0) out vec4 color;" 34 | "uniform float time;" 35 | "vec3 map( vec3 p )" 36 | "{" 37 | "for (int i = 0; i < 30; ++i)" 38 | "{" 39 | "p = vec3(1.25,1.07,1.29)*abs(p/dot(p,p)-vec3(0.95,0.91,0.67));" 40 | "}" 41 | "return p/30.;" 42 | "}" 43 | "vec4 raymarch( vec3 ro, vec3 rd )" 44 | "{" 45 | "float T = 3.0;" 46 | "vec3 c = vec3(0,0,0);" 47 | "for(int i=0; i<30; ++i)" 48 | "{" 49 | "T+=0.1;" 50 | "c+=map(ro+T*rd);" 51 | "}" 52 | "return vec4(c,1.0);" 53 | "}" 54 | "void main()" 55 | "{" 56 | "vec2 uv = (2.0*gl_FragCoord.xy-vec2(1920,1080))/1080;" 57 | "vec3 ro = vec3(0,sin(time*0.2)*5.0,0);" 58 | "vec3 rd = normalize(vec3(uv,2.0));" 59 | "color = raymarch(ro,rd);" 60 | "}"; 61 | 62 | int MakeShader(const char* source, GLenum type) 63 | { 64 | int p = ((PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram"))(); 65 | int s = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(type); 66 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(s,1,&source,0); 67 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(s); 68 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p,s); 69 | ((PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram"))(p); 70 | return p; 71 | } 72 | 73 | int main() 74 | { 75 | ShowCursor(0); 76 | GLuint framebuffer, texture; 77 | PIXELFORMATDESCRIPTOR pfd = { 0,0,PFD_DOUBLEBUFFER }; 78 | HDC hdc = GetDC(CreateWindow("static", 0, WS_POPUP|WS_VISIBLE|WS_MAXIMIZE, 0, 0, 0, 0, 0, 0, 0, 0)); 79 | SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd); 80 | wglMakeCurrent(hdc, wglCreateContext(hdc)); 81 | ((PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT")) (0); 82 | int MS = MakeShader(MainShader,0x8B30); 83 | int PS = MakeShader(PostProcessingShader,0x8B30); 84 | ((PFNGLGENFRAMEBUFFERSPROC)wglGetProcAddress("glGenFramebuffers"))(1, &framebuffer); 85 | ((PFNGLBINDFRAMEBUFFEREXTPROC)wglGetProcAddress("glBindFramebuffer"))(0x8D40, framebuffer); 86 | glGenTextures(1, &texture); 87 | glBindTexture(GL_TEXTURE_2D, texture); 88 | ((PFNGLTEXSTORAGE2DPROC)wglGetProcAddress("glTexStorage2D"))(GL_TEXTURE_2D, 1, GL_RGBA8, 1920, 1080); 89 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 90 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 91 | ((PFNGLFRAMEBUFFERTEXTUREPROC)wglGetProcAddress("glFramebufferTexture"))(0x8D40, 0x8CE0, texture, 0); 92 | GLint location = ((PFNGLGETUNIFORMLOCATIONPROC)wglGetProcAddress("glGetUniformLocation"))(MS,"time"); 93 | DWORD S = GetTickCount(); 94 | do 95 | { 96 | ((PFNGLBINDFRAMEBUFFEREXTPROC)wglGetProcAddress("glBindFramebuffer"))(0x8D40, framebuffer); 97 | ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(MS); 98 | float t = (GetTickCount()-S)*0.001f; 99 | ((PFNGLUNIFORM1FVPROC)wglGetProcAddress("glUniform1fv"))(location, 1, &t); 100 | glRects(-1, -1, 1, 1); 101 | ((PFNGLBINDFRAMEBUFFEREXTPROC)wglGetProcAddress("glBindFramebuffer"))(0x8D40, 0); 102 | ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(PS); 103 | glRects(-1, -1, 1, 1); 104 | wglSwapLayerBuffers(hdc, WGL_SWAP_MAIN_PLANE); 105 | } while (!GetAsyncKeyState(VK_ESCAPE)); 106 | return 0; 107 | } -------------------------------------------------------------------------------- /GLSL/RenderingEngine/compile.cmd: -------------------------------------------------------------------------------- 1 | gcc -x c -s -o demo.exe demo.c -mwindows -lopengl32 2 | pause 3 | -------------------------------------------------------------------------------- /GLSL/Skybox/textures/0.ppm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/CPP-Programming/9d93e94a00402ac3b19fd1732368a3a4fa1ad18a/GLSL/Skybox/textures/0.ppm -------------------------------------------------------------------------------- /GLSL/Skybox/textures/1.ppm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/CPP-Programming/9d93e94a00402ac3b19fd1732368a3a4fa1ad18a/GLSL/Skybox/textures/1.ppm -------------------------------------------------------------------------------- /GLSL/Skybox/textures/2.ppm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/CPP-Programming/9d93e94a00402ac3b19fd1732368a3a4fa1ad18a/GLSL/Skybox/textures/2.ppm -------------------------------------------------------------------------------- /GLSL/Skybox/textures/4.ppm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/CPP-Programming/9d93e94a00402ac3b19fd1732368a3a4fa1ad18a/GLSL/Skybox/textures/4.ppm -------------------------------------------------------------------------------- /GLSL/Skybox/textures/5.ppm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/CPP-Programming/9d93e94a00402ac3b19fd1732368a3a4fa1ad18a/GLSL/Skybox/textures/5.ppm -------------------------------------------------------------------------------- /GLSL/Skybox/textures/plaster.ppm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/CPP-Programming/9d93e94a00402ac3b19fd1732368a3a4fa1ad18a/GLSL/Skybox/textures/plaster.ppm -------------------------------------------------------------------------------- /GLSL/Terrain.c: -------------------------------------------------------------------------------- 1 | // gcc -x c -s -o Terrain.exe Terrain.c -mwindows -lopengl32 2 | #define WIN32_LEAN_AND_MEAN 3 | #define WIN32_EXTRA_LEAN 4 | #include 5 | #include 6 | #include 7 | 8 | typedef int(APIENTRY* PFNWGLSWAPINTERVALEXTPROC)(int i); 9 | typedef GLuint(APIENTRY* PFNGLCREATEPROGRAMPROC)(); 10 | typedef GLuint(APIENTRY* PFNGLCREATESHADERPROC)(GLenum t); 11 | typedef void(APIENTRY* PFNGLSHADERSOURCEPROC)(GLuint s, GLsizei c, const char *const *n, const GLint *i); 12 | typedef void(APIENTRY* PFNGLCOMPILESHADERPROC)(GLuint s); 13 | typedef void(APIENTRY* PFNGLATTACHSHADERPROC)(GLuint p, GLuint s); 14 | typedef void(APIENTRY* PFNGLLINKPROGRAMPROC)(GLuint p); 15 | typedef void(APIENTRY* PFNGLUSEPROGRAMPROC)(GLuint p); 16 | typedef GLint(APIENTRY* PFNGLGETUNIFORMLOCATIONPROC) (GLuint p, const char *n); 17 | typedef void (APIENTRY* PFNGLUNIFORM1FVPROC) (GLint k, GLsizei c, const GLfloat *v); 18 | 19 | static const char* FragmentShader = \ 20 | "#version 430 \n" 21 | "layout (location=0) out vec4 color;" 22 | "uniform float time;" 23 | "uniform sampler2D hash;" 24 | "float FBM( vec2 p )" 25 | "{" 26 | "float a = 0.5, b = 0.0, t = 0.0;" 27 | "for (int i=0; i<7; i++)" 28 | "{" 29 | "b *= a; t *= a;" 30 | "b += texture(hash,p).r;" 31 | "t += 1.0; p /= 2.0;" 32 | "}" 33 | "return b /= t;" 34 | "}" 35 | "float Map( vec3 p )" 36 | "{" 37 | "if (p.y>2.0) return 1.0;" 38 | "return ((p.y-2.*FBM(p.xz*0.25))*0.4);" 39 | "}" 40 | "bool Raymarch( inout vec3 ro, vec3 rd)" 41 | "{" 42 | "float t = 0.0;" 43 | "for (int i=0; i<256; i++)" 44 | "{" 45 | "float d = Map(ro+rd*t);" 46 | "t+=d;" 47 | "if (d 3 | #include 4 | 5 | #define ScreenWidth 1920.0f 6 | #define ScreenHeight 1080.0f 7 | #define GL_VERTEX_SHADER 0x8B31 8 | #define GL_FRAGMENT_SHADER 0x8B30 9 | #define GL_VERTEX_SHADER_BIT 0x00000001 10 | #define GL_FRAGMENT_SHADER_BIT 0x00000002 11 | 12 | typedef GLuint (__stdcall *PFNGLCREATESHADERPROGRAMVPROC) (GLenum type, GLsizei count, const char *const*strings); 13 | typedef void (__stdcall *PFNGLGENPROGRAMPIPELINESPROC) (GLsizei n, GLuint *pipelines); 14 | typedef void (__stdcall *PFNGLBINDPROGRAMPIPELINEPROC) (GLuint pipeline); 15 | typedef void (__stdcall *PFNGLUSEPROGRAMSTAGESPROC) (GLuint pipeline, GLbitfield stages, GLuint program); 16 | typedef void (__stdcall *PFNGLPROGRAMUNIFORM1FPROC) (GLuint program, GLint location, GLfloat v0); 17 | typedef void (__stdcall *PFNGLPROGRAMUNIFORM2FPROC) (GLuint program, GLint location, GLfloat v0, GLfloat v1); 18 | 19 | PFNGLCREATESHADERPROGRAMVPROC glCreateShaderProgramv; 20 | PFNGLGENPROGRAMPIPELINESPROC glGenProgramPipelines; 21 | PFNGLBINDPROGRAMPIPELINEPROC glBindProgramPipeline; 22 | PFNGLUSEPROGRAMSTAGESPROC glUseProgramStages; 23 | PFNGLPROGRAMUNIFORM1FPROC glProgramUniform1f; 24 | PFNGLPROGRAMUNIFORM2FPROC glProgramUniform2f; 25 | 26 | void glInit() 27 | { 28 | glCreateShaderProgramv = (PFNGLCREATESHADERPROGRAMVPROC)wglGetProcAddress ("glCreateShaderProgramv"); 29 | glGenProgramPipelines = (PFNGLGENPROGRAMPIPELINESPROC)wglGetProcAddress ("glGenProgramPipelines"); 30 | glBindProgramPipeline = (PFNGLBINDPROGRAMPIPELINEPROC)wglGetProcAddress ("glBindProgramPipeline"); 31 | glUseProgramStages = (PFNGLUSEPROGRAMSTAGESPROC)wglGetProcAddress ("glUseProgramStages"); 32 | glProgramUniform1f = (PFNGLPROGRAMUNIFORM1FPROC)wglGetProcAddress ("glProgramUniform1f"); 33 | glProgramUniform2f = (PFNGLPROGRAMUNIFORM2FPROC)wglGetProcAddress ("glProgramUniform2f"); 34 | } 35 | 36 | static const char* VertexShader = \ 37 | "#version 450\n" 38 | "layout (location=0) in vec3 position;" 39 | "void main()" 40 | "{" 41 | "gl_Position = vec4(position, 1.0);" 42 | "}"; 43 | 44 | static const char* FragmentShader = \ 45 | "#version 450\n" 46 | "layout (location=0) out vec4 fragColor;" 47 | "layout (location=1) uniform float iTime;" 48 | "layout (location=2) uniform vec2 iResolution;" 49 | "void main()" 50 | "{" 51 | "vec2 fragCoord = gl_FragCoord.xy;" 52 | "vec2 uv = fragCoord/iResolution.xy;" 53 | "vec3 col = 0.5 + 0.5*cos(iTime+uv.xyx+vec3(0,2,4));" 54 | "fragColor = vec4(col,1.0);" 55 | "}"; 56 | 57 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 58 | { 59 | if (uMsg==WM_CLOSE || uMsg==WM_DESTROY || (uMsg==WM_KEYDOWN && wParam==VK_ESCAPE)) 60 | { 61 | PostQuitMessage(0); return 0; 62 | } 63 | else { return DefWindowProc(hWnd, uMsg, wParam, lParam); } 64 | } 65 | 66 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 67 | { 68 | int exit = 0; 69 | MSG msg; 70 | WNDCLASS win = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, "Demo"}; 71 | RegisterClass(&win); 72 | HWND hwnd = CreateWindowEx(0, win.lpszClassName, "Demo", WS_VISIBLE|WS_POPUP, 0, 0, ScreenWidth, ScreenHeight, 0, 0, 0, 0); 73 | HDC hdc = GetDC(hwnd); 74 | PIXELFORMATDESCRIPTOR pfd = { 0, 0, PFD_DOUBLEBUFFER }; 75 | SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd); 76 | wglMakeCurrent(hdc, wglCreateContext(hdc)); 77 | ShowCursor(0); 78 | glInit(); 79 | int vs = glCreateShaderProgramv (GL_VERTEX_SHADER, 1, &VertexShader); 80 | int fs = glCreateShaderProgramv (GL_FRAGMENT_SHADER, 1, &FragmentShader); 81 | unsigned int p; 82 | glGenProgramPipelines (1, &p); 83 | glBindProgramPipeline (p); 84 | glUseProgramStages(p, GL_VERTEX_SHADER_BIT, vs); 85 | glUseProgramStages(p, GL_FRAGMENT_SHADER_BIT, fs); 86 | while (!exit) 87 | { 88 | while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 89 | { 90 | if( msg.message==WM_QUIT ) exit = 1; 91 | TranslateMessage( &msg ); 92 | DispatchMessage( &msg ); 93 | } 94 | float time = GetTickCount() * 0.001f; 95 | glProgramUniform1f (fs, 1, time); 96 | glProgramUniform2f (fs, 2, ScreenWidth, ScreenHeight); 97 | glRects (-1, -1, 1, 1); 98 | wglSwapLayerBuffers (hdc, WGL_SWAP_MAIN_PLANE); 99 | } 100 | return 0; 101 | } -------------------------------------------------------------------------------- /GLSL/buffer.c: -------------------------------------------------------------------------------- 1 | // cl buffer.c opengl32.lib user32.lib gdi32.lib 2 | #include 3 | #include 4 | 5 | #define GL_SHADER_STORAGE_BUFFER 0x90D2 6 | #define GL_READ_ONLY 0x88B8 7 | #define GL_DYNAMIC_DRAW 0x88E8 8 | #define GL_SHADER_STORAGE_BARRIER_BIT 0x00002000 9 | #define GL_COMPUTE_SHADER 0x91B9 10 | 11 | typedef GLuint(__stdcall *PFNGLCREATEPROGRAMPROC)(); 12 | typedef GLuint(__stdcall *PFNGLCREATESHADERPROC)(GLenum t); 13 | typedef void(__stdcall *PFNGLSHADERSOURCEPROC)(GLuint s, GLsizei c, const char*const*string, const GLint* i); 14 | typedef void(__stdcall *PFNGLCOMPILESHADERPROC)(GLuint s); 15 | typedef void(__stdcall *PFNGLATTACHSHADERPROC)(GLuint p, GLuint s); 16 | typedef void(__stdcall *PFNGLLINKPROGRAMPROC)(GLuint p); 17 | typedef void(__stdcall *PFNGLUSEPROGRAMPROC)(GLuint p); 18 | typedef void (__stdcall *PFNGLDISPATCHCOMPUTEPROC) (GLuint x, GLuint y, GLuint z); 19 | typedef void (__stdcall *PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers); 20 | typedef void (__stdcall *PFNGLBINDBUFFERBASEPROC) (GLenum target, GLuint index, GLuint buffer); 21 | typedef void (__stdcall *PFNGLBUFFERDATAPROC) (GLenum target, signed long int size, const void *data, GLenum usage); 22 | typedef void (__stdcall *PFNGLMEMORYBARRIERPROC) (GLbitfield barriers); 23 | typedef void *(__stdcall *PFNGLMAPBUFFERPROC) (GLenum target, GLenum access); 24 | typedef GLboolean (__stdcall *PFNGLUNMAPBUFFERPROC) (GLenum target); 25 | typedef void (__stdcall *PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer); 26 | 27 | PFNGLCREATEPROGRAMPROC glCreateProgram; 28 | PFNGLCREATESHADERPROC glCreateShader; 29 | PFNGLSHADERSOURCEPROC glShaderSource; 30 | PFNGLCOMPILESHADERPROC glCompileShader; 31 | PFNGLATTACHSHADERPROC glAttachShader; 32 | PFNGLLINKPROGRAMPROC glLinkProgram; 33 | PFNGLUSEPROGRAMPROC glUseProgram; 34 | PFNGLDISPATCHCOMPUTEPROC glDispatchCompute; 35 | PFNGLGENBUFFERSPROC glGenBuffers; 36 | PFNGLBINDBUFFERBASEPROC glBindBufferBase; 37 | PFNGLBUFFERDATAPROC glBufferData; 38 | PFNGLMEMORYBARRIERPROC glMemoryBarrier; 39 | PFNGLMAPBUFFERPROC glMapBuffer; 40 | PFNGLUNMAPBUFFERPROC glUnmapBuffer; 41 | PFNGLBINDBUFFERPROC glBindBuffer; 42 | 43 | void glInit() 44 | { 45 | glCreateProgram = (PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram"); 46 | glCreateShader = (PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"); 47 | glShaderSource = (PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"); 48 | glCompileShader = (PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"); 49 | glAttachShader = (PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"); 50 | glLinkProgram = (PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram"); 51 | glUseProgram = (PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"); 52 | glDispatchCompute = (PFNGLDISPATCHCOMPUTEPROC)wglGetProcAddress("glDispatchCompute"); 53 | glGenBuffers = (PFNGLGENBUFFERSPROC)wglGetProcAddress("glGenBuffers"); 54 | glBindBufferBase = (PFNGLBINDBUFFERBASEPROC)wglGetProcAddress("glBindBufferBase"); 55 | glBufferData = (PFNGLBUFFERDATAPROC)wglGetProcAddress("glBufferData"); 56 | glMemoryBarrier = (PFNGLMEMORYBARRIERPROC)wglGetProcAddress("glMemoryBarrier"); 57 | glMapBuffer = (PFNGLMAPBUFFERPROC)wglGetProcAddress("glMapBuffer"); 58 | glUnmapBuffer = (PFNGLUNMAPBUFFERPROC)wglGetProcAddress("glUnmapBuffer"); 59 | glBindBuffer = (PFNGLBINDBUFFERPROC)wglGetProcAddress("glBindBuffer"); 60 | } 61 | 62 | static const char* ComputeShader = \ 63 | "#version 430 core\n" 64 | "layout (local_size_x = 1) in;" 65 | "layout(std430, binding=0) writeonly buffer Pixels {vec4 fragColor[];};" 66 | "void main()" 67 | "{" 68 | "fragColor[gl_GlobalInvocationID.x] = vec4(1.0, 2.0, 3.0, 4.0);" 69 | "}" ; 70 | 71 | int main() 72 | { 73 | PIXELFORMATDESCRIPTOR pfd = {0, 0, PFD_DOUBLEBUFFER}; 74 | HDC hdc = GetDC(CreateWindow("static", 0, WS_POPUP, 0, 0, 0, 0, 0, 0, 0, 0)); 75 | SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd); 76 | wglMakeCurrent(hdc, wglCreateContext(hdc)); 77 | glInit(); 78 | int P = glCreateProgram(); 79 | int CS = glCreateShader(GL_COMPUTE_SHADER); 80 | glShaderSource(CS, 1, &ComputeShader, 0); 81 | glCompileShader(CS); 82 | glAttachShader(P, CS); 83 | glLinkProgram(P); 84 | glUseProgram(P); 85 | unsigned int SSBO = 0; 86 | glGenBuffers(1, &SSBO); 87 | glBindBufferBase (GL_SHADER_STORAGE_BUFFER, 0, SSBO); 88 | glBufferData (GL_SHADER_STORAGE_BUFFER, 4 * sizeof(float), 0, GL_DYNAMIC_DRAW); 89 | glDispatchCompute(1, 1, 1); 90 | glMemoryBarrier (GL_SHADER_STORAGE_BARRIER_BIT); 91 | glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO); 92 | float *buffer = (float *) glMapBuffer (GL_SHADER_STORAGE_BUFFER, GL_READ_ONLY); 93 | glUnmapBuffer (GL_SHADER_STORAGE_BUFFER); 94 | printf("%f\n%f\n%f\n%f\n", buffer[0], buffer[1], buffer[2], buffer[3]); 95 | return 0; 96 | } -------------------------------------------------------------------------------- /GLSL/clouds.ppm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/CPP-Programming/9d93e94a00402ac3b19fd1732368a3a4fa1ad18a/GLSL/clouds.ppm -------------------------------------------------------------------------------- /GLSL/fixed.c: -------------------------------------------------------------------------------- 1 | // cl fixed.c opengl32.lib user32.lib gdi32.lib 2 | #include 3 | #include 4 | #include 5 | 6 | #define width 1920 7 | #define height 1080 8 | 9 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 10 | { 11 | if (uMsg==WM_CLOSE || uMsg==WM_DESTROY || (uMsg==WM_KEYDOWN && wParam==VK_ESCAPE)) 12 | { 13 | PostQuitMessage(0); return 0; 14 | } 15 | else 16 | { 17 | return DefWindowProc(hWnd, uMsg, wParam, lParam); 18 | } 19 | } 20 | 21 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 22 | { 23 | ShowCursor(0); 24 | int exit = 0; 25 | MSG msg; 26 | WNDCLASS win = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, "Demo"}; 27 | RegisterClass(&win); 28 | HDC hdc = GetDC(CreateWindowEx(0, win.lpszClassName, "Demo", WS_VISIBLE|WS_POPUP, 0, 0, width, height, 0, 0, 0, 0)); 29 | PIXELFORMATDESCRIPTOR pfd = { 0,0,PFD_DOUBLEBUFFER }; 30 | SetPixelFormat(hdc,ChoosePixelFormat(hdc,&pfd),&pfd); 31 | wglMakeCurrent(hdc,wglCreateContext(hdc)); 32 | glOrtho(0.0, width, 0.0, height, -1.0, 1.0); 33 | int x = 0; 34 | int y = 0; 35 | float *vertices = (float*) malloc(width * height * sizeof(float) * 3); 36 | float *colors = (float*) malloc(width * height * sizeof(float) * 3); 37 | for(int i=0; i<(width*height*3); i++) 38 | { 39 | float uvx = (2.0f * x - (float)width) / (float)height; 40 | float uvy = (2.0f * y - (float)height) / (float)height; 41 | float length = 0.1f / sqrtf(uvx*uvx + uvy*uvy); 42 | if ((i % 3) == 0) 43 | { 44 | vertices[i] = x; 45 | colors[i] = length; 46 | } 47 | if ((i % 3) == 1) 48 | { 49 | vertices[i] = y; 50 | colors[i] = length; 51 | } 52 | if ((i % 3) == 2) 53 | { 54 | vertices[i] = 0.0f; 55 | colors[i] = 0.0f; 56 | if (x == width) x = 0; 57 | if (x % width == 0) y++; 58 | x++; 59 | } 60 | } 61 | glEnableClientState (GL_VERTEX_ARRAY); 62 | glEnableClientState (GL_COLOR_ARRAY); 63 | glVertexPointer (3, GL_FLOAT, 0, vertices); 64 | glColorPointer (3, GL_FLOAT, 0, colors); 65 | while (!exit) 66 | { 67 | while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 68 | { 69 | if( msg.message==WM_QUIT ) exit = 1; 70 | TranslateMessage( &msg ); 71 | DispatchMessage( &msg ); 72 | } 73 | glDrawArrays(GL_POINTS, 0, width * height); 74 | wglSwapLayerBuffers(hdc, WGL_SWAP_MAIN_PLANE); 75 | }; 76 | return 0; 77 | } -------------------------------------------------------------------------------- /GLSL/flames.c: -------------------------------------------------------------------------------- 1 | // cl flames.c opengl32.lib user32.lib gdi32.lib 2 | // Reference: https://www.shadertoy.com/view/4ttGWM 3 | #include 4 | #include 5 | 6 | #define width 1680 7 | #define height 1050 8 | 9 | typedef int (WINAPI *PFNWGLSWAPINTERVALEXTPROC)(int i); 10 | typedef void (WINAPI *PFNGLUSEPROGRAMPROC)(GLuint p); 11 | typedef void (WINAPI *PFNGLDISPATCHCOMPUTEPROC) (GLuint x, GLuint y, GLuint z); 12 | typedef void (WINAPI *PFNGLBINDIMAGETEXTUREPROC) (GLuint a, GLuint b, GLint c, GLboolean d, GLint e, GLenum f, GLenum g); 13 | typedef GLint (WINAPI *PFNGLGETUNIFORMLOCATIONPROC) (GLuint p, const char *n); 14 | typedef void (WINAPI *PFNGLUNIFORM1FPROC) (GLint s, GLfloat v0); 15 | typedef void (WINAPI *PFNGLUNIFORM2IPROC) (GLint s, GLint v0, GLint v1); 16 | typedef GLuint (WINAPI *PFNGLCREATESHADERPROGRAMVPROC) (GLenum t, GLsizei c, const char*const*s); 17 | 18 | static const char* ComputeShader = \ 19 | "#version 430 \n" 20 | "writeonly uniform image2D writer;" 21 | "uniform float iTime;" 22 | "uniform ivec2 iResolution;" 23 | 24 | "float hash(vec2 n)" 25 | "{" 26 | "return fract(sin(cos(dot(n, vec2(12.9898,12.1414)))) * 83758.5453);" 27 | "}" 28 | 29 | "float noise(vec2 n)" 30 | "{" 31 | "vec2 d = vec2(0.0, 1.0);" 32 | "vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));" 33 | "return mix(mix(hash(b), hash(b + d.yx), f.x), mix(hash(b + d.xy), hash(b + d.yy), f.x), f.y);" 34 | "}" 35 | 36 | "float fbm(vec2 n)" 37 | "{" 38 | "float total = 0.0, amplitude = 1.0;" 39 | "for (int i = 0; i <5; i++)" 40 | "{" 41 | "total += noise(n) * amplitude;" 42 | "n += n*1.7;" 43 | "amplitude *= 0.47;" 44 | "}" 45 | "return total;" 46 | "}" 47 | 48 | "layout (local_size_x = 8, local_size_y = 8) in;" 49 | "void main()" 50 | "{" 51 | "vec2 fragCoord = gl_GlobalInvocationID.xy;" 52 | "float shift = 1.327+sin(iTime*2.0)/2.4;" 53 | "vec2 uv = fragCoord.xy / iResolution.xy;" 54 | "vec2 p = fragCoord.xy * (3.5-sin(iTime*0.4)/1.89) / iResolution.xx;" 55 | "p += sin(p.yx*4.0+vec2(.2,-.3)*iTime)*0.04;" 56 | "p += sin(p.yx*8.0+vec2(.6,+.1)*iTime)*0.01; " 57 | "p.x -= iTime/1.1;" 58 | "float q = fbm(p - iTime * 0.3+1.0*sin(iTime+0.5)/2.0);" 59 | "float qb = fbm(p - iTime * 0.4+0.1*cos(iTime)/2.0);" 60 | "float q2 = fbm(p - iTime * 0.44 - 5.0*cos(iTime)/2.0) - 6.0;" 61 | "float q3 = fbm(p - iTime * 0.9 - 10.0*cos(iTime)/15.0)-4.0;" 62 | "float q4 = fbm(p - iTime * 1.4 - 20.0*sin(iTime)/14.0)+2.0;" 63 | "q = (q + qb - .4 * q2 -2.0*q3 + .6*q4)/3.8;" 64 | "vec2 r = vec2(fbm(p + q /2.0 + iTime * 0.1 - p.x - p.y), fbm(p + q - iTime * 0.9));" 65 | "vec3 color = vec3(1.0,.2,.05)/(pow((r.y+r.y)* max(.0,p.y)+0.1, 4.0));" 66 | "color = color/(1.0+max(vec3(0),color));" 67 | "vec4 fragColor = vec4(color, 1.0);" 68 | "imageStore(writer,ivec2(gl_GlobalInvocationID),fragColor);" 69 | "}"; 70 | 71 | static LRESULT CALLBACK WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) 72 | { 73 | if( uMsg==WM_CLOSE || uMsg==WM_DESTROY || (uMsg==WM_KEYDOWN && wParam==VK_ESCAPE) ) 74 | { 75 | PostQuitMessage(0); return 0; 76 | } 77 | if( uMsg==WM_SIZE ) 78 | { 79 | glViewport( 0, 0, lParam&65535, lParam>>16 ); 80 | } 81 | return(DefWindowProc(hWnd,uMsg,wParam,lParam)); 82 | } 83 | 84 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 85 | { 86 | int exit = 0; 87 | MSG msg; 88 | WNDCLASS win = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, "Compute Shader"}; 89 | RegisterClass(&win); 90 | HDC hdc = GetDC(CreateWindowEx(0, win.lpszClassName, "Compute Shader", WS_VISIBLE|WS_OVERLAPPEDWINDOW, 0, 0, width, height, 0, 0, 0, 0)); 91 | PIXELFORMATDESCRIPTOR pfd = { 0,0,PFD_DOUBLEBUFFER }; 92 | SetPixelFormat(hdc,ChoosePixelFormat(hdc,&pfd),&pfd); 93 | wglMakeCurrent(hdc,wglCreateContext(hdc)); 94 | ((PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress ("wglSwapIntervalEXT")) (0); 95 | glOrtho(0, width, 0, height, -1, 1); 96 | int CS = ((PFNGLCREATESHADERPROGRAMVPROC)wglGetProcAddress("glCreateShaderProgramv")) (0x91B9, 1, &ComputeShader); 97 | glBindTexture(GL_TEXTURE_2D, 1); 98 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 99 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 100 | glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,width,height,0,GL_RGBA,GL_FLOAT,0); 101 | ((PFNGLBINDIMAGETEXTUREPROC)wglGetProcAddress("glBindImageTexture"))(0,1,0,GL_FALSE,0,0x88B9,GL_RGBA8); 102 | glEnable(GL_TEXTURE_2D); 103 | ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(CS); 104 | int iTime = ((PFNGLGETUNIFORMLOCATIONPROC)wglGetProcAddress("glGetUniformLocation"))(CS,"iTime"); 105 | GLint iResolution = ((PFNGLGETUNIFORMLOCATIONPROC)wglGetProcAddress("glGetUniformLocation"))(CS,"iResolution"); 106 | ((PFNGLUNIFORM2IPROC)wglGetProcAddress("glUniform2i"))(iResolution, width, height); 107 | while( !exit ) 108 | { 109 | while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE) ) 110 | { 111 | if( msg.message==WM_QUIT ) exit = 1; 112 | TranslateMessage( &msg ); 113 | DispatchMessage( &msg ); 114 | } 115 | glBindTexture(GL_TEXTURE_2D, 1); 116 | ((PFNGLUNIFORM1FPROC)wglGetProcAddress("glUniform1f"))(iTime, GetTickCount()*0.001f); 117 | ((PFNGLDISPATCHCOMPUTEPROC)wglGetProcAddress("glDispatchCompute"))(width/8, height/8, 1); 118 | glBegin(GL_QUADS); 119 | glTexCoord2i(0, 0); glVertex2i(0, 0); 120 | glTexCoord2i(0, 1); glVertex2i(0, height); 121 | glTexCoord2i(1, 1); glVertex2i(width, height); 122 | glTexCoord2i(1, 0); glVertex2i(width, 0); 123 | glEnd(); 124 | wglSwapLayerBuffers(hdc,WGL_SWAP_MAIN_PLANE); 125 | } 126 | return 0; 127 | } -------------------------------------------------------------------------------- /GLSL/geometry.c: -------------------------------------------------------------------------------- 1 | // cl geometry.c opengl32.lib user32.lib gdi32.lib 2 | #include 3 | #include 4 | #include 5 | 6 | #define width 1280 7 | #define height 720 8 | 9 | typedef GLuint(WINAPI *PFNGLCREATEPROGRAMPROC) (); 10 | typedef GLuint(WINAPI *PFNGLCREATESHADERPROC) (GLenum t); 11 | typedef void(WINAPI *PFNGLSHADERSOURCEPROC) (GLuint s, GLsizei c, const char*const*string, const GLint* i); 12 | typedef void(WINAPI *PFNGLCOMPILESHADERPROC) (GLuint s); 13 | typedef void(WINAPI *PFNGLATTACHSHADERPROC) (GLuint p, GLuint s); 14 | typedef void(WINAPI *PFNGLLINKPROGRAMPROC) (GLuint p); 15 | typedef void(WINAPI *PFNGLUSEPROGRAMPROC) (GLuint p); 16 | typedef int(WINAPI *PFNWGLSWAPINTERVALEXTPROC) (int i); 17 | typedef void(WINAPI *PFNGLGETSHADERIVPROC) (GLuint s, GLenum v, GLint *p); 18 | typedef void(WINAPI *PFNGLGETSHADERINFOLOGPROC) (GLuint s, GLsizei b, GLsizei *l, char *i); 19 | 20 | static const char* VertexShader = \ 21 | "#version 450 \n" 22 | "const vec3 vertices[4] = {vec3(-1,-1,0), vec3(1,-1,0), vec3(-1,1,0), vec3(1,1,0)};" 23 | "void main()" 24 | "{" 25 | "gl_Position = vec4(vertices[gl_VertexID], 2);" 26 | "}"; 27 | 28 | static const char* GeometryShader = \ 29 | "#version 450 \n" 30 | "#define num 8 \n" 31 | "layout(points) in;" 32 | "layout(line_strip, max_vertices = num + 1) out;" 33 | "const vec3 colors[4] = {vec3(1,0,0), vec3(0,1,0), vec3(0,0,1), vec3(1,1,1)};" 34 | "out vec3 hue;" 35 | "void main()" 36 | "{" 37 | "hue = colors[gl_PrimitiveIDIn];" 38 | "for (int i = 0; i <= num; i++)" 39 | "{" 40 | "float angle = 3.14159265 * 2.0 / num * i;" 41 | "vec4 offset = vec4(cos(angle)*0.3 , -sin(angle)*0.5 , 0.0, 0.0);" 42 | "gl_Position = gl_in[0].gl_Position + offset;" 43 | "EmitVertex();" 44 | "}" 45 | "EndPrimitive();" 46 | "}"; 47 | 48 | static const char* FragmentShader = \ 49 | "#version 450 \n" 50 | "out vec4 fragColor;" 51 | "in vec3 hue;" 52 | "void main()" 53 | "{" 54 | "fragColor = vec4(hue, 1.0);" 55 | "}"; 56 | 57 | void Debug(int sh) 58 | { 59 | GLint isCompiled = 0; 60 | ((PFNGLGETSHADERIVPROC)wglGetProcAddress("glGetShaderiv"))(sh,0x8B82,&isCompiled); 61 | if(isCompiled == GL_FALSE) 62 | { 63 | GLint length = 0; 64 | ((PFNGLGETSHADERIVPROC)wglGetProcAddress("glGetShaderiv"))(sh,0x8B84,&length); 65 | GLsizei q = 0; 66 | char* log = (char*)malloc(sizeof(char)*length); 67 | ((PFNGLGETSHADERINFOLOGPROC)wglGetProcAddress("glGetShaderInfoLog"))(sh,length,&q,log); 68 | if (length>1) 69 | { 70 | FILE *file = fopen ("debug.log","a"); 71 | fprintf (file,"%s\n%s\n",(char*)glGetString(0x8B8C),log); 72 | fclose (file); 73 | ExitProcess(0); 74 | } 75 | } 76 | } 77 | 78 | int LoadShaders(const char* VS, const char* GS, const char* FS) 79 | { 80 | int p = ((PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram"))(); 81 | int sv = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(0x8B31); 82 | int sg = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(0x8DD9); 83 | int sf = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(0x8B30); 84 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(sv,1,&VS,0); 85 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(sg,1,&GS,0); 86 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(sf,1,&FS,0); 87 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(sv); 88 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(sg); 89 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(sf); 90 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p,sv); 91 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p,sg); 92 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p,sf); 93 | ((PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram"))(p); 94 | Debug(sv); 95 | Debug(sg); 96 | Debug(sf); 97 | return p; 98 | } 99 | 100 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 101 | { 102 | if (uMsg==WM_CLOSE || uMsg==WM_DESTROY || (uMsg==WM_KEYDOWN && wParam==VK_ESCAPE)) 103 | { 104 | PostQuitMessage(0); return 0; 105 | } 106 | else 107 | { 108 | return DefWindowProc(hWnd, uMsg, wParam, lParam); 109 | } 110 | } 111 | 112 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 113 | { 114 | int exit = 0; 115 | MSG msg; 116 | WNDCLASS win = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, "Geometry Shader"}; 117 | RegisterClass(&win); 118 | HDC hdc = GetDC(CreateWindowEx(0, win.lpszClassName, "Geometry Shader", WS_VISIBLE|WS_OVERLAPPEDWINDOW, 0, 0, width, height, 0, 0, 0, 0)); 119 | PIXELFORMATDESCRIPTOR pfd = { 0,0,PFD_DOUBLEBUFFER }; 120 | SetPixelFormat(hdc,ChoosePixelFormat(hdc,&pfd),&pfd); 121 | wglMakeCurrent(hdc,wglCreateContext(hdc)); 122 | ((PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT")) (0); 123 | int SH = LoadShaders(VertexShader,GeometryShader, FragmentShader); 124 | ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(SH); 125 | while (!exit) 126 | { 127 | while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 128 | { 129 | if( msg.message==WM_QUIT ) exit = 1; 130 | TranslateMessage( &msg ); 131 | DispatchMessage( &msg ); 132 | } 133 | glClear(GL_COLOR_BUFFER_BIT); 134 | glDrawArrays(GL_POINTS, 0, 4); 135 | wglSwapLayerBuffers(hdc, WGL_SWAP_MAIN_PLANE); 136 | } 137 | return 0; 138 | } -------------------------------------------------------------------------------- /GLSL/mandelbulb.c: -------------------------------------------------------------------------------- 1 | // cl mandelbulb.c opengl32.lib user32.lib gdi32.lib 2 | #include 3 | #include 4 | 5 | typedef int(APIENTRY* PFNWGLSWAPINTERVALEXTPROC)(int i); 6 | typedef GLuint(APIENTRY* PFNGLCREATEPROGRAMPROC)(); 7 | typedef GLuint(APIENTRY* PFNGLCREATESHADERPROC)(GLenum t); 8 | typedef void(APIENTRY* PFNGLSHADERSOURCEPROC)(GLuint s, GLsizei c, const char *const *n, const GLint *i); 9 | typedef void(APIENTRY* PFNGLCOMPILESHADERPROC)(GLuint s); 10 | typedef void(APIENTRY* PFNGLATTACHSHADERPROC)(GLuint p, GLuint s); 11 | typedef void(APIENTRY* PFNGLLINKPROGRAMPROC)(GLuint p); 12 | typedef void(APIENTRY* PFNGLUSEPROGRAMPROC)(GLuint p); 13 | 14 | static const char* FragmentShader = \ 15 | "#version 450 \n" 16 | "layout (location=0) out vec4 color;" 17 | "float map( in vec3 p )" 18 | "{" 19 | "vec3 w = p;" 20 | "float m = dot(w,w);" 21 | "float dz = 1.0;" 22 | "for( int i=0; i<5; i++ )" 23 | "{" 24 | "float m2 = m*m;" 25 | "float m4 = m2*m2;" 26 | "dz = 8.0*sqrt(m4*m2*m)*dz + 1.0;" 27 | "float x = w.x; float x2 = x*x; float x4 = x2*x2;" 28 | "float y = w.y; float y2 = y*y; float y4 = y2*y2;" 29 | "float z = w.z; float z2 = z*z; float z4 = z2*z2;" 30 | "float k3 = x2 + z2;" 31 | "float k2 = inversesqrt( k3*k3*k3*k3*k3*k3*k3 );" 32 | "float k1 = x4 + y4 + z4 - 6.0*y2*z2 - 6.0*x2*y2 + 2.0*z2*x2;" 33 | "float k4 = x2 - y2 + z2;" 34 | "w.x = p.x + 64.0*x*y*z*(x2-z2)*k4*(x4-6.0*x2*z2+z4)*k1*k2;" 35 | "w.y = p.y + -16.0*y2*k3*k4*k4 + k1*k1;" 36 | "w.z = p.z + -8.0*y*k4*(x4*x4 - 28.0*x4*x2*z2+70.0*x4*z4-28.0*x2*z2*z4+z4*z4)*k1*k2;" 37 | "m = dot(w,w);" 38 | "if( m > 256.0 ) break;" 39 | "}" 40 | "return 0.25*log(m)*sqrt(m)/dz;" 41 | "}" 42 | "vec4 raymarch (vec3 ro, vec3 rd)" 43 | "{" 44 | "for (int i=0;i<128;i++)" 45 | "{" 46 | "float t = map(ro);" 47 | "if (t<0.001)" 48 | "{" 49 | "float c = pow(1.0-float(i)/float(128),2.0);" 50 | "return vec4(c,c,c,1.0);" 51 | "}" 52 | "ro+=t*rd;" 53 | "}" 54 | "return vec4(0,0,0,1);" 55 | "}" 56 | "void main()" 57 | "{" 58 | "vec2 uv = (2.0*gl_FragCoord.xy - vec2(1920,1080))/1080;" 59 | "vec3 ro = vec3(0,0.0,-2.5);" 60 | "vec3 rd = normalize(vec3(uv,2.0));" 61 | "color = raymarch(ro,rd);" 62 | "}"; 63 | 64 | int main() 65 | { 66 | ShowCursor(0); 67 | PIXELFORMATDESCRIPTOR pfd = { 0,0,PFD_DOUBLEBUFFER }; 68 | HDC hdc = GetDC(CreateWindow("static", 0, WS_POPUP|WS_VISIBLE|WS_MAXIMIZE, 0, 0, 1920, 1080, 0, 0, 0, 0)); 69 | SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd); 70 | wglMakeCurrent(hdc, wglCreateContext(hdc)); 71 | ((PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT")) (0); 72 | int p = ((PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram"))(); 73 | int s = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(0x8B30); 74 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(s, 1, &FragmentShader, 0); 75 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(s); 76 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p, s); 77 | ((PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram"))(p); 78 | ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(p); 79 | do 80 | { 81 | glRects(-1, -1, 1, 1); 82 | wglSwapLayerBuffers(hdc, WGL_SWAP_MAIN_PLANE); 83 | } while (!GetAsyncKeyState(VK_ESCAPE)); 84 | return 0; 85 | } -------------------------------------------------------------------------------- /GLSL/ms_triangle.c: -------------------------------------------------------------------------------- 1 | // cl ms_triangle.c opengl32.lib user32.lib gdi32.lib 2 | #include 3 | #include 4 | #include 5 | 6 | #define width 1280 7 | #define height 720 8 | 9 | typedef GLuint(WINAPI *PFNGLCREATEPROGRAMPROC) (); 10 | typedef GLuint(WINAPI *PFNGLCREATESHADERPROC) (GLenum t); 11 | typedef void(WINAPI *PFNGLSHADERSOURCEPROC) (GLuint s, GLsizei c, const char*const*string, const GLint* i); 12 | typedef void(WINAPI *PFNGLCOMPILESHADERPROC) (GLuint s); 13 | typedef void(WINAPI *PFNGLATTACHSHADERPROC) (GLuint p, GLuint s); 14 | typedef void(WINAPI *PFNGLLINKPROGRAMPROC) (GLuint p); 15 | typedef void(WINAPI *PFNGLUSEPROGRAMPROC) (GLuint p); 16 | typedef void(WINAPI *PFNGLGETSHADERIVPROC) (GLuint s, GLenum v, GLint *p); 17 | typedef void(WINAPI *PFNGLGETSHADERINFOLOGPROC) (GLuint s, GLsizei b, GLsizei *l, char *i); 18 | typedef void(WINAPI *PFNGLDRAWMESHTASKSNVPROC) (GLuint f, GLuint c); 19 | 20 | static const char* MeshShader = \ 21 | "#version 450 \n" 22 | "#extension GL_NV_mesh_shader : enable\n" 23 | "layout(local_size_x = 3) in;" 24 | "layout(max_vertices = 64) out;" 25 | "layout(max_primitives = 126) out;" 26 | "layout(triangles) out;" 27 | "const vec3 vertices[3] = {vec3(-1,-1,0), vec3(1,-1,0), vec3(0,1,0)};" 28 | "void main()" 29 | "{" 30 | "uint id = gl_LocalInvocationID.x;" 31 | "gl_MeshVerticesNV[id].gl_Position = vec4(vertices[id], 2);" 32 | "gl_PrimitiveIndicesNV[id] = id;" 33 | "gl_PrimitiveCountNV = 1;" 34 | "}"; 35 | 36 | static const char* FragmentShader = \ 37 | "#version 450 \n" 38 | "#extension GL_NV_fragment_shader_barycentric : enable\n" 39 | "out vec4 color;" 40 | "void main()" 41 | "{" 42 | "color = vec4(gl_BaryCoordNV, 1.0);" 43 | "}"; 44 | 45 | void Debug(int sh) 46 | { 47 | GLint isCompiled = 0; 48 | ((PFNGLGETSHADERIVPROC)wglGetProcAddress("glGetShaderiv"))(sh,0x8B82,&isCompiled); 49 | if(isCompiled == GL_FALSE) 50 | { 51 | GLint length = 0; 52 | ((PFNGLGETSHADERIVPROC)wglGetProcAddress("glGetShaderiv"))(sh,0x8B84,&length); 53 | GLsizei q = 0; 54 | char* log = (char*)malloc(sizeof(char)*length); 55 | ((PFNGLGETSHADERINFOLOGPROC)wglGetProcAddress("glGetShaderInfoLog"))(sh,length,&q,log); 56 | if (length>1) 57 | { 58 | FILE *file = fopen ("debug.log","a"); 59 | fprintf (file,"%s\n%s\n",(char*)glGetString(0x8B8C),log); 60 | fclose (file); 61 | ExitProcess(0); 62 | } 63 | } 64 | } 65 | 66 | int MakeShaders(const char* MS, const char* FS) 67 | { 68 | int p = ((PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram"))(); 69 | int sm = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(0x9559); 70 | int sf = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(0x8B30); 71 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(sm,1,&MS,0); 72 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(sf,1,&FS,0); 73 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(sm); 74 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(sf); 75 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p,sm); 76 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p,sf); 77 | ((PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram"))(p); 78 | Debug(sm); 79 | Debug(sf); 80 | return p; 81 | } 82 | 83 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 84 | { 85 | if (uMsg==WM_CLOSE || uMsg==WM_DESTROY || (uMsg==WM_KEYDOWN && wParam==VK_ESCAPE)) 86 | { 87 | PostQuitMessage(0); return 0; 88 | } 89 | else 90 | { 91 | return DefWindowProc(hWnd, uMsg, wParam, lParam); 92 | } 93 | } 94 | 95 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 96 | { 97 | int exit = 0; 98 | MSG msg; 99 | WNDCLASS win = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, "Mesh Shader Demo"}; 100 | RegisterClass(&win); 101 | HWND hwnd = CreateWindowEx(0, win.lpszClassName, "Mesh Shader Demo", WS_VISIBLE|WS_OVERLAPPEDWINDOW, 0, 0, width, height, 0, 0, 0, 0); 102 | HDC hdc = GetDC(hwnd); 103 | PIXELFORMATDESCRIPTOR pfd = { 0,0,PFD_DOUBLEBUFFER }; 104 | SetPixelFormat(hdc,ChoosePixelFormat(hdc,&pfd),&pfd); 105 | wglMakeCurrent(hdc,wglCreateContext(hdc)); 106 | int PS = MakeShaders(MeshShader,FragmentShader); 107 | ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(PS); 108 | while (!exit) 109 | { 110 | while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 111 | { 112 | if( msg.message==WM_QUIT ) exit = 1; 113 | TranslateMessage( &msg ); 114 | DispatchMessage( &msg ); 115 | } 116 | glClear(GL_COLOR_BUFFER_BIT); 117 | ((PFNGLDRAWMESHTASKSNVPROC)wglGetProcAddress("glDrawMeshTasksNV"))(0,1); 118 | wglSwapLayerBuffers(hdc, WGL_SWAP_MAIN_PLANE); 119 | } 120 | return 0; 121 | } -------------------------------------------------------------------------------- /GLSL/multicore.cpp: -------------------------------------------------------------------------------- 1 | // cl multicore.cpp opengl32.lib user32.lib gdi32.lib /EHsc 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | const int width = 1280; 9 | const int height = 720; 10 | float buffer[width][height][3] = {}; 11 | const int NUM_THREADS = 12; 12 | 13 | void mainImage(std::vector rows) 14 | { 15 | for( int i = 0; i < rows.size(); ++i ) 16 | { 17 | for( int x = 0; x < width; ++x ) 18 | { 19 | int y = rows[i]; 20 | float iTime = GetTickCount()*0.001f; 21 | float dx = (2.0f*(float)x-(float)width)/(float)height; 22 | float dy = (2.0f*(float)y-(float)height)/(float)height; 23 | float dz = 2.0f; 24 | float length = sqrtf(dx*dx+dy*dy+dz*dz); 25 | dx = dx / length; 26 | dy = dy / length; 27 | dz = dz / length; 28 | float px = dx*(-1.0f/dy); 29 | float py = dy*(-1.0f/dy); 30 | float pz = dz*(-1.0f/dy); 31 | for(int j=1; j<24; j++) 32 | { 33 | px += 0.5f*(sinf(iTime + 0.5f*pz * float(j))); 34 | pz += 0.5f*(cosf(iTime + 0.5f*px * float(j))); 35 | } 36 | buffer[x][y][0] = fmin(0.5f+0.5f*cosf(4.0f+px),1.0f) * -dy * 3.0f; 37 | buffer[x][y][1] = fmin(0.5f+0.5f*cosf(4.0f+pz+2.0f),1.0f) * -dy * 3.0f; 38 | buffer[x][y][2] = fmin(0.5f+0.5f*cosf(4.0f+px+4.0f),1.0f) * -dy * 3.0f; 39 | } 40 | } 41 | } 42 | 43 | void GenerateData() 44 | { 45 | std::thread th[NUM_THREADS]; 46 | int N = 0, rows = height / NUM_THREADS; 47 | std::vector> buckets( NUM_THREADS ); 48 | for (int y = 0; y < height; ++y) 49 | { 50 | buckets[N].push_back(y); 51 | if ( (buckets[N].size() == rows) && (N < (NUM_THREADS - 1)) ) {N++;} 52 | } 53 | for( int i = 0; i < NUM_THREADS; ++i ) 54 | { 55 | th[i] = std::thread(mainImage, buckets[i]); 56 | } 57 | for( int i = 0; i < NUM_THREADS; ++i ) 58 | { 59 | th[i].join(); 60 | } 61 | } 62 | 63 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 64 | { 65 | if (uMsg==WM_CLOSE || uMsg==WM_DESTROY || (uMsg==WM_KEYDOWN && wParam==VK_ESCAPE)) 66 | { 67 | PostQuitMessage(0); return 0; 68 | } 69 | else 70 | { 71 | return DefWindowProc(hWnd, uMsg, wParam, lParam); 72 | } 73 | } 74 | 75 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 76 | { 77 | int exit = 0; 78 | MSG msg; 79 | WNDCLASS win = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, "Demo"}; 80 | RegisterClass(&win); 81 | HDC hdc = GetDC(CreateWindowEx(0, win.lpszClassName, "Demo", WS_VISIBLE|WS_OVERLAPPEDWINDOW, 0, 0, width, height, 0, 0, 0, 0)); 82 | PIXELFORMATDESCRIPTOR pfd = { 0,0,PFD_DOUBLEBUFFER }; 83 | SetPixelFormat(hdc,ChoosePixelFormat(hdc,&pfd),&pfd); 84 | wglMakeCurrent(hdc,wglCreateContext(hdc)); 85 | glOrtho(0.0, width, 0.0, height, -1.0, 1.0); 86 | while (!exit) 87 | { 88 | while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 89 | { 90 | if( msg.message==WM_QUIT ) exit = 1; 91 | TranslateMessage( &msg ); 92 | DispatchMessage( &msg ); 93 | } 94 | GenerateData(); 95 | glBegin( GL_POINTS ); 96 | for (int y = 0; y < height; ++y) 97 | { 98 | for (int x = 0; x < width; ++x) 99 | { 100 | glVertex2i( x, y ); 101 | glColor3f(buffer[x][y][0], buffer[x][y][1], buffer[x][y][2]); 102 | } 103 | } 104 | glEnd(); 105 | wglSwapLayerBuffers(hdc, WGL_SWAP_MAIN_PLANE); 106 | }; 107 | return 0; 108 | } -------------------------------------------------------------------------------- /GLSL/plaster.ppm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/CPP-Programming/9d93e94a00402ac3b19fd1732368a3a4fa1ad18a/GLSL/plaster.ppm -------------------------------------------------------------------------------- /GLSL/stain.c: -------------------------------------------------------------------------------- 1 | // cl stain.c opengl32.lib user32.lib gdi32.lib 2 | 3 | #include 4 | #include 5 | 6 | #define width 1280 7 | #define height 720 8 | 9 | typedef int (WINAPI *PFNWGLSWAPINTERVALEXTPROC)(int i); 10 | typedef GLuint (WINAPI *PFNGLCREATEPROGRAMPROC)(); 11 | typedef GLuint (WINAPI *PFNGLCREATESHADERPROC)(GLenum t); 12 | typedef void (WINAPI *PFNGLSHADERSOURCEPROC)(GLuint s, GLsizei c, const char*const*string, const GLint* i); 13 | typedef void (WINAPI *PFNGLCOMPILESHADERPROC)(GLuint s); 14 | typedef void (WINAPI *PFNGLATTACHSHADERPROC)(GLuint p, GLuint s); 15 | typedef void (WINAPI *PFNGLLINKPROGRAMPROC)(GLuint p); 16 | typedef void (WINAPI *PFNGLUSEPROGRAMPROC)(GLuint p); 17 | typedef void (WINAPI *PFNGLDISPATCHCOMPUTEPROC) (GLuint x, GLuint y, GLuint z); 18 | typedef void (WINAPI *PFNGLBINDIMAGETEXTUREPROC) (GLuint a, GLuint b, GLint c, GLboolean d, GLint e, GLenum f, GLenum g); 19 | typedef GLint (WINAPI *PFNGLGETUNIFORMLOCATIONPROC) (GLuint p, const char *n); 20 | typedef void (WINAPI *PFNGLUNIFORM1FPROC) (GLint s, GLfloat v0); 21 | typedef void (WINAPI *PFNGLUNIFORM2IPROC) (GLint s, GLint v0, GLint v1); 22 | 23 | static const char* ComputeShader = \ 24 | "#version 430 \n" 25 | "writeonly uniform image2D writer;" 26 | "uniform float iTime;" 27 | "uniform ivec2 iResolution;" 28 | "layout (local_size_x = 8, local_size_y = 8) in;" 29 | "void main()" 30 | "{" 31 | "vec2 fragCoord = gl_GlobalInvocationID.xy;" 32 | "vec2 uv = (2.0 * fragCoord.xy - iResolution.xy) / iResolution.y;" 33 | "vec3 k = sin(vec3(iTime*.08, iTime*.03, iTime*.007));" 34 | "for (int i = 0; i < 30; i++) " 35 | "{" 36 | "vec3 p = vec3(uv*float(i),float(i));" 37 | "k += vec3( cos(k.y+sin(p.x)), sin(k.z+cos(p.z)), -sin(k.x+sin(p.y)) );" 38 | "}" 39 | "vec4 fragColor = vec4(k*0.06,1.0);" 40 | "imageStore(writer,ivec2(gl_GlobalInvocationID),fragColor);" 41 | "}" ; 42 | 43 | void MakeBuffer() 44 | { 45 | glBindTexture(GL_TEXTURE_2D, 1); 46 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 47 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 48 | glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,width,height,0,GL_RGBA,GL_FLOAT,0); 49 | ((PFNGLBINDIMAGETEXTUREPROC)wglGetProcAddress("glBindImageTexture"))(0,1,0,GL_FALSE,0,0x88B9,GL_RGBA8); 50 | glEnable(GL_TEXTURE_2D); 51 | } 52 | 53 | int MakeShader(const char* source, GLenum type) 54 | { 55 | int p = ((PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram"))(); 56 | int s = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(type); 57 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(s,1,&source,0); 58 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(s); 59 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p,s); 60 | ((PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram"))(p); 61 | return p; 62 | } 63 | 64 | static LRESULT CALLBACK WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) 65 | { 66 | if( uMsg==WM_CLOSE || uMsg==WM_DESTROY || (uMsg==WM_KEYDOWN && wParam==VK_ESCAPE) ) 67 | { 68 | PostQuitMessage(0); 69 | return 0; 70 | } 71 | if( uMsg==WM_SIZE ) 72 | { 73 | glViewport( 0, 0, lParam&65535, lParam>>16 ); 74 | } 75 | return(DefWindowProc(hWnd,uMsg,wParam,lParam)); 76 | } 77 | 78 | int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) 79 | { 80 | MSG msg; 81 | int exit = 0; 82 | PIXELFORMATDESCRIPTOR pfd = { 0,0,PFD_DOUBLEBUFFER }; 83 | WNDCLASS win; 84 | ZeroMemory( &win, sizeof(WNDCLASS) ); 85 | win.style = CS_OWNDC|CS_HREDRAW|CS_VREDRAW; 86 | win.lpfnWndProc = WindowProc; 87 | win.hInstance = 0; 88 | win.lpszClassName = "noname"; 89 | win.hbrBackground =(HBRUSH)(COLOR_WINDOW+1); 90 | RegisterClass(&win); 91 | HDC hdc = GetDC(CreateWindowEx(0, win.lpszClassName, "Compute Shader", WS_VISIBLE|WS_OVERLAPPEDWINDOW, 0, 0, width, height, 0, 0, 0, 0)); 92 | SetPixelFormat(hdc,ChoosePixelFormat(hdc,&pfd),&pfd); 93 | wglMakeCurrent(hdc,wglCreateContext(hdc)); 94 | ((PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress ("wglSwapIntervalEXT")) (0); 95 | glOrtho(0, width, 0, height, -1, 1); 96 | int CS = MakeShader(ComputeShader,0x91B9); 97 | MakeBuffer(); 98 | ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(CS); 99 | int iTime = ((PFNGLGETUNIFORMLOCATIONPROC)wglGetProcAddress("glGetUniformLocation"))(CS,"iTime"); 100 | GLint iResolution = ((PFNGLGETUNIFORMLOCATIONPROC)wglGetProcAddress("glGetUniformLocation"))(CS,"iResolution"); 101 | ((PFNGLUNIFORM2IPROC)wglGetProcAddress("glUniform2i"))(iResolution, width, height); 102 | while( !exit ) 103 | { 104 | while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE) ) 105 | { 106 | if( msg.message==WM_QUIT ) exit = 1; 107 | TranslateMessage( &msg ); 108 | DispatchMessage( &msg ); 109 | } 110 | glBindTexture(GL_TEXTURE_2D, 1); 111 | ((PFNGLUNIFORM1FPROC)wglGetProcAddress("glUniform1f"))(iTime, GetTickCount()*0.001f); 112 | ((PFNGLDISPATCHCOMPUTEPROC)wglGetProcAddress("glDispatchCompute"))(width/8, height/8, 1); 113 | glBegin(GL_QUADS); 114 | glTexCoord2i(0, 0); glVertex2i(0, 0); 115 | glTexCoord2i(0, 1); glVertex2i(0, height); 116 | glTexCoord2i(1, 1); glVertex2i(width, height); 117 | glTexCoord2i(1, 0); glVertex2i(width, 0); 118 | glEnd(); 119 | wglSwapLayerBuffers(hdc,WGL_SWAP_MAIN_PLANE); 120 | } 121 | return 0; 122 | } -------------------------------------------------------------------------------- /GLSL/stream.c: -------------------------------------------------------------------------------- 1 | // cl stream.c opengl32.lib user32.lib gdi32.lib 2 | // reference: https://www.vertexshaderart.com/art/ZSksx2deRsDocFDKT 3 | #include 4 | #include 5 | #include 6 | 7 | #define width 1280 8 | #define height 720 9 | 10 | typedef GLuint (WINAPI *PFNGLCREATEPROGRAMPROC) (); 11 | typedef GLuint (WINAPI *PFNGLCREATESHADERPROC) (GLenum t); 12 | typedef void (WINAPI *PFNGLSHADERSOURCEPROC) (GLuint s, GLsizei c, const char*const*string, const GLint* i); 13 | typedef void (WINAPI *PFNGLCOMPILESHADERPROC) (GLuint s); 14 | typedef void (WINAPI *PFNGLATTACHSHADERPROC) (GLuint p, GLuint s); 15 | typedef void (WINAPI *PFNGLLINKPROGRAMPROC) (GLuint p); 16 | typedef void (WINAPI *PFNGLUSEPROGRAMPROC) (GLuint p); 17 | typedef int (WINAPI *PFNWGLSWAPINTERVALEXTPROC) (int i); 18 | typedef void (WINAPI *PFNGLGETSHADERIVPROC) (GLuint s, GLenum v, GLint *p); 19 | typedef void (WINAPI *PFNGLGETSHADERINFOLOGPROC) (GLuint s, GLsizei b, GLsizei *l, char *i); 20 | typedef GLint (WINAPI *PFNGLGETUNIFORMLOCATIONPROC) (GLuint p, const char *n); 21 | typedef void (WINAPI *PFNGLUNIFORM1FPROC) (GLint s, GLfloat v0); 22 | 23 | static const char* VertexShader = \ 24 | "#version 450 \n" 25 | "out vec3 v_color; \n" 26 | "uniform float time;" 27 | 28 | "vec3 rgb(vec3 c)" 29 | "{" 30 | "c = vec3(c.x, clamp(c.yz, 0.0, 1.0));" 31 | "vec4 K = vec4(1.0, 0.666, 0.333, 3.0);" 32 | "vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);" 33 | "return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);" 34 | "}" 35 | 36 | "void main()" 37 | "{" 38 | "vec2 resolution = vec2(1280, 720);" 39 | "int vertexId = gl_VertexID;" 40 | "float point = mod(floor(vertexId / 2.0) + mod(float(vertexId), 2.0) * 5.0, 4.0);" 41 | "float count = floor(vertexId / 8.0);" 42 | "float offset = count * 0.02;" 43 | "float angle = point * radians(180.) * 2.0 / 4.0 + offset;" 44 | "float radius = 0.2 * pow(1.0, .0);" 45 | "float c = cos(angle + time) * radius;" 46 | "float s = sin(angle + time) * radius;" 47 | "float orbitAngle = count * 0.0;" 48 | "float innerRadius = count * 0.001;" 49 | "float oC = sin(orbitAngle + time * 0.3 + count * 0.1) * innerRadius;" 50 | "float oS = tan(orbitAngle + time + count * 0.1) * innerRadius;" 51 | "vec2 aspect = vec2(1, resolution.x / resolution.y);" 52 | "vec2 xy = vec2(oC + c,oS + s);" 53 | "gl_Position = vec4(xy * aspect, 0, 1);" 54 | "float hue = (time * 0.01 + count * 1.001);" 55 | "v_color = vec3(rgb(vec3(hue, 1, 1)));" 56 | "}"; 57 | 58 | static const char* FragmentShader = \ 59 | "#version 450 \n" 60 | "in vec3 v_color;" 61 | "out vec4 color;" 62 | "void main()" 63 | "{" 64 | "color = vec4(v_color,1.0);" 65 | "}"; 66 | 67 | void Debug(int sh) 68 | { 69 | GLint isCompiled = 0; 70 | ((PFNGLGETSHADERIVPROC)wglGetProcAddress("glGetShaderiv"))(sh,0x8B82,&isCompiled); 71 | if(isCompiled == GL_FALSE) 72 | { 73 | GLint length = 0; 74 | ((PFNGLGETSHADERIVPROC)wglGetProcAddress("glGetShaderiv"))(sh,0x8B84,&length); 75 | GLsizei q = 0; 76 | char* log = (char*)malloc(sizeof(char)*length); 77 | ((PFNGLGETSHADERINFOLOGPROC)wglGetProcAddress("glGetShaderInfoLog"))(sh,length,&q,log); 78 | if (length>1) 79 | { 80 | FILE *file = fopen ("debug.log","a"); 81 | fprintf (file,"%s\n%s\n",(char*)glGetString(0x8B8C),log); 82 | fclose (file); 83 | ExitProcess(0); 84 | } 85 | } 86 | } 87 | 88 | int MakeShaders(const char* VS, const char* FS) 89 | { 90 | int p = ((PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram"))(); 91 | int sv = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(0x8B31); 92 | int sf = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(0x8B30); 93 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(sv,1,&VS,0); 94 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(sf,1,&FS,0); 95 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(sv); 96 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(sf); 97 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p,sv); 98 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p,sf); 99 | ((PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram"))(p); 100 | Debug(sv); 101 | Debug(sf); 102 | return p; 103 | } 104 | 105 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 106 | { 107 | if (uMsg==WM_CLOSE || uMsg==WM_DESTROY || (uMsg==WM_KEYDOWN && wParam==VK_ESCAPE)) 108 | { 109 | PostQuitMessage(0); return 0; 110 | } 111 | else 112 | { 113 | return DefWindowProc(hWnd, uMsg, wParam, lParam); 114 | } 115 | } 116 | 117 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 118 | { 119 | int exit = 0; 120 | MSG msg; 121 | WNDCLASS win = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, "Demo"}; 122 | RegisterClass(&win); 123 | HDC hdc = GetDC(CreateWindowEx(0, win.lpszClassName, "Demo", WS_VISIBLE|WS_OVERLAPPEDWINDOW, 0, 0, width, height, 0, 0, 0, 0)); 124 | PIXELFORMATDESCRIPTOR pfd = {0, 0, PFD_DOUBLEBUFFER}; 125 | SetPixelFormat(hdc, ChoosePixelFormat(hdc,&pfd), &pfd); 126 | wglMakeCurrent(hdc, wglCreateContext(hdc)); 127 | ((PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT")) (0); 128 | int SH = MakeShaders(VertexShader, FragmentShader); 129 | ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(SH); 130 | int time = ((PFNGLGETUNIFORMLOCATIONPROC)wglGetProcAddress("glGetUniformLocation"))(SH, "time"); 131 | while (!exit) 132 | { 133 | while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 134 | { 135 | if( msg.message==WM_QUIT ) exit = 1; 136 | TranslateMessage( &msg ); 137 | DispatchMessage( &msg ); 138 | } 139 | glClear(GL_COLOR_BUFFER_BIT); 140 | glEnable(0x8642); 141 | ((PFNGLUNIFORM1FPROC)wglGetProcAddress("glUniform1f"))(time, GetTickCount()*0.001f); 142 | glDrawArrays(GL_LINES, 0, 9623); 143 | wglSwapLayerBuffers(hdc, WGL_SWAP_MAIN_PLANE); 144 | } 145 | return 0; 146 | } -------------------------------------------------------------------------------- /GLSL/triangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef GLuint(APIENTRY* PFNGLCREATEPROGRAMPROC)(); 6 | typedef GLuint(APIENTRY* PFNGLCREATESHADERPROC)(GLenum t); 7 | typedef void(APIENTRY* PFNGLSHADERSOURCEPROC)(GLuint s, GLsizei c, const char*const*string, const GLint* i); 8 | typedef void(APIENTRY* PFNGLCOMPILESHADERPROC)(GLuint s); 9 | typedef void(APIENTRY* PFNGLATTACHSHADERPROC)(GLuint p, GLuint s); 10 | typedef void(APIENTRY* PFNGLLINKPROGRAMPROC)(GLuint p); 11 | typedef void(APIENTRY* PFNGLUSEPROGRAMPROC)(GLuint p); 12 | typedef void (APIENTRY* PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *b); 13 | typedef void (APIENTRY* PFNGLBINDBUFFERPROC) (GLenum t, GLuint b); 14 | typedef void (APIENTRY* PFNGLBUFFERDATAPROC) (GLenum t, ptrdiff_t s, const GLvoid *d, GLenum u); 15 | typedef void (APIENTRY* PFNGLBINDVERTEXARRAYPROC) (GLuint a); 16 | typedef void (APIENTRY* PFNGLENABLEVERTEXATTRIBARRAYPROC) (GLuint i); 17 | typedef void (APIENTRY* PFNGLVERTEXATTRIBPOINTERPROC) (GLuint i, GLint s, GLenum t, GLboolean n, GLsizei k, const void *p); 18 | typedef void (APIENTRY* PFNGLDISABLEVERTEXATTRIBARRAYPROC) (GLuint i); 19 | typedef int (APIENTRY* PFNWGLSWAPINTERVALEXTPROC) (int i); 20 | typedef GLint(APIENTRY *PFNGLGETUNIFORMLOCATIONPROC) (GLuint p, const char *n); 21 | typedef void (APIENTRY *PFNGLUNIFORM1FVPROC) (GLint k, GLsizei c, const GLfloat *v); 22 | typedef GLint (APIENTRY* PFNGLGETATTRIBLOCATIONPROC) (GLuint p, const char *n); 23 | typedef void (APIENTRY* PFNGLGENVERTEXARRAYSPROC) (GLsizei n, GLuint *a); 24 | 25 | static const GLfloat vertices[] = {-1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f}; 26 | static const GLfloat colors[] = {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f}; 27 | unsigned int VertexBuffer, ColorBuffer, VertexArrayID; 28 | 29 | static const char* VertexShader = \ 30 | "#version 430 core\n" 31 | "layout (location=0) in vec3 vertex;" 32 | "layout (location=1) in vec3 vertexColor;" 33 | "out vec3 fragmentColor;" 34 | "uniform float time;" 35 | "void main()" 36 | "{" 37 | "vec3 position = vertex * sin(time);" 38 | "fragmentColor = vertexColor;" 39 | "gl_Position = vec4(position,1.0);" 40 | 41 | "}"; 42 | 43 | static const char* FragmentShader = \ 44 | "#version 430 core\n" 45 | "layout (location=0) out vec4 color;" 46 | "layout (location=1) in vec3 fragmentColor;" 47 | "void main()" 48 | "{" 49 | "color = vec4(fragmentColor,1);" 50 | "}"; 51 | 52 | int MakeShader(const char* VS, const char* FS) 53 | { 54 | int p = ((PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram"))(); 55 | int s1 = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(0x8B31); 56 | int s2 = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(0x8B30); 57 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(s1,1,&VS,0); 58 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(s2,1,&FS,0); 59 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(s1); 60 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(s2); 61 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p,s1); 62 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p,s2); 63 | ((PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram"))(p); 64 | return p; 65 | } 66 | 67 | int main() 68 | { 69 | PIXELFORMATDESCRIPTOR pfd = { 0, 0, PFD_DOUBLEBUFFER }; 70 | HDC hdc = GetDC(CreateWindow("static", 0, WS_POPUP | WS_VISIBLE | WS_MAXIMIZE, 0, 0, 0, 0, 0, 0, 0, 0)); 71 | SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd); 72 | wglMakeCurrent(hdc, wglCreateContext(hdc)); 73 | ShowCursor(0); 74 | ((PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT")) (0); 75 | ((PFNGLGENVERTEXARRAYSPROC)wglGetProcAddress("glGenVertexArrays")) (1, &VertexArrayID); 76 | ((PFNGLBINDVERTEXARRAYPROC)wglGetProcAddress("glBindVertexArray")) (VertexArrayID); 77 | ((PFNGLGENBUFFERSPROC)wglGetProcAddress("glGenBuffers"))(1, &VertexBuffer); 78 | ((PFNGLBINDBUFFERPROC)wglGetProcAddress("glBindBuffer"))(0x8892, VertexBuffer); 79 | ((PFNGLBUFFERDATAPROC)wglGetProcAddress("glBufferData"))(0x8892, sizeof(vertices), vertices, 0x88E4); 80 | ((PFNGLGENBUFFERSPROC)wglGetProcAddress("glGenBuffers"))(1, &ColorBuffer); 81 | ((PFNGLBINDBUFFERPROC)wglGetProcAddress("glBindBuffer"))(0x8892, ColorBuffer); 82 | ((PFNGLBUFFERDATAPROC)wglGetProcAddress("glBufferData"))(0x8892, sizeof(colors), colors, 0x88E4); 83 | int PS = MakeShader(VertexShader,FragmentShader); 84 | ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(PS); 85 | int location = ((PFNGLGETUNIFORMLOCATIONPROC)wglGetProcAddress("glGetUniformLocation"))(PS,"time"); 86 | DWORD S = GetTickCount(); 87 | do 88 | { 89 | glClear(GL_COLOR_BUFFER_BIT); 90 | float t = (GetTickCount()-S)*0.001f; 91 | ((PFNGLUNIFORM1FVPROC)wglGetProcAddress("glUniform1fv"))(location, 1, &t); 92 | ((PFNGLENABLEVERTEXATTRIBARRAYPROC)wglGetProcAddress("glEnableVertexAttribArray"))(0); 93 | ((PFNGLBINDBUFFERPROC)wglGetProcAddress("glBindBuffer"))(0x8892, VertexBuffer); 94 | ((PFNGLVERTEXATTRIBPOINTERPROC)wglGetProcAddress("glVertexAttribPointer"))(0,3, GL_FLOAT, GL_FALSE, 0,(void*)0 ); 95 | ((PFNGLENABLEVERTEXATTRIBARRAYPROC)wglGetProcAddress("glEnableVertexAttribArray"))(1); 96 | ((PFNGLBINDBUFFERPROC)wglGetProcAddress("glBindBuffer"))(0x8892, ColorBuffer); 97 | ((PFNGLVERTEXATTRIBPOINTERPROC)wglGetProcAddress("glVertexAttribPointer"))(1,3, GL_FLOAT, GL_FALSE, 0,(void*)0 ); 98 | glDrawArrays(GL_TRIANGLES, 0, 3); 99 | ((PFNGLDISABLEVERTEXATTRIBARRAYPROC)wglGetProcAddress("glDisableVertexAttribArray"))(0); 100 | ((PFNGLDISABLEVERTEXATTRIBARRAYPROC)wglGetProcAddress("glDisableVertexAttribArray"))(1); 101 | wglSwapLayerBuffers(hdc, WGL_SWAP_MAIN_PLANE); 102 | } while (!GetAsyncKeyState(VK_ESCAPE)); 103 | return 0; 104 | } -------------------------------------------------------------------------------- /GLSL/vs_quad.c: -------------------------------------------------------------------------------- 1 | // cl vs_quad.c opengl32.lib user32.lib gdi32.lib 2 | #include 3 | #include 4 | #include 5 | 6 | #define width 1280.0f 7 | #define height 720.0f 8 | 9 | typedef GLuint(APIENTRY *PFNGLCREATEPROGRAMPROC) (); 10 | typedef GLuint(APIENTRY *PFNGLCREATESHADERPROC) (GLenum t); 11 | typedef void(APIENTRY *PFNGLSHADERSOURCEPROC) (GLuint s, GLsizei c, const char*const*string, const GLint* i); 12 | typedef void(APIENTRY *PFNGLCOMPILESHADERPROC) (GLuint s); 13 | typedef void(APIENTRY *PFNGLATTACHSHADERPROC) (GLuint p, GLuint s); 14 | typedef void(APIENTRY *PFNGLLINKPROGRAMPROC) (GLuint p); 15 | typedef void(APIENTRY *PFNGLUSEPROGRAMPROC) (GLuint p); 16 | typedef int(APIENTRY *PFNWGLSWAPINTERVALEXTPROC) (int i); 17 | typedef void(APIENTRY *PFNGLGETSHADERIVPROC) (GLuint s, GLenum v, GLint *p); 18 | typedef void(APIENTRY *PFNGLGETSHADERINFOLOGPROC) (GLuint s, GLsizei b, GLsizei *l, char *i); 19 | 20 | static const char* VertexShader = \ 21 | "#version 450 \n" 22 | "out vec2 UV;" 23 | "const vec3 vertices[6] = {vec3(-1,-1,0), vec3(1,-1,0), vec3(-1,1,0), vec3(1,-1,0), vec3(1,1,0), vec3(-1,1,0)};" 24 | "const vec2 uv[6] = {vec2(0,0), vec2(1,0), vec2(0,1), vec2(1,0), vec2(1,1), vec2(0,1)};" 25 | "void main()" 26 | "{" 27 | "uint id = gl_VertexID;" 28 | "UV = uv[id];" 29 | "gl_Position = vec4(vertices[id], 2);" 30 | "}"; 31 | 32 | static const char* FragmentShader = \ 33 | "#version 450 \n" 34 | "out vec4 color;" 35 | "in vec2 UV;" 36 | "void main()" 37 | "{" 38 | "color = vec4(UV,0.0,1.0);" 39 | "}"; 40 | 41 | void Debug(int sh) 42 | { 43 | GLint isCompiled = 0; 44 | ((PFNGLGETSHADERIVPROC)wglGetProcAddress("glGetShaderiv"))(sh,0x8B82,&isCompiled); 45 | if(isCompiled == GL_FALSE) 46 | { 47 | GLint length = 0; 48 | ((PFNGLGETSHADERIVPROC)wglGetProcAddress("glGetShaderiv"))(sh,0x8B84,&length); 49 | GLsizei q = 0; 50 | char* log = (char*)malloc(sizeof(char)*length); 51 | ((PFNGLGETSHADERINFOLOGPROC)wglGetProcAddress("glGetShaderInfoLog"))(sh,length,&q,log); 52 | if (length>1) 53 | { 54 | FILE *file = fopen ("debug.log","a"); 55 | fprintf (file,"%s\n%s\n",(char*)glGetString(0x8B8C),log); 56 | fclose (file); 57 | ExitProcess(0); 58 | } 59 | } 60 | } 61 | 62 | int MakeShader(const char* VS, const char* FS) 63 | { 64 | int p = ((PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram"))(); 65 | int sv = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(0x8B31); 66 | int sf = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(0x8B30); 67 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(sv,1,&VS,0); 68 | ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource"))(sf,1,&FS,0); 69 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(sv); 70 | ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(sf); 71 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p,sv); 72 | ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader"))(p,sf); 73 | ((PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram"))(p); 74 | Debug(sv); 75 | Debug(sf); 76 | return p; 77 | } 78 | 79 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 80 | { 81 | if (uMsg==WM_CLOSE || uMsg==WM_DESTROY || (uMsg==WM_KEYDOWN && wParam==VK_ESCAPE)) 82 | { 83 | PostQuitMessage(0); return 0; 84 | } 85 | else 86 | { 87 | return DefWindowProc(hWnd, uMsg, wParam, lParam); 88 | } 89 | } 90 | 91 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 92 | { 93 | int exit = 0; 94 | MSG msg; 95 | WNDCLASS win = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, "Demo"}; 96 | RegisterClass(&win); 97 | HWND hwnd = CreateWindowEx(0, win.lpszClassName, "Demo", WS_VISIBLE|WS_OVERLAPPEDWINDOW, 0, 0, width, height, 0, 0, 0, 0); 98 | HDC hdc = GetDC(hwnd); 99 | PIXELFORMATDESCRIPTOR pfd = { 0,0,PFD_DOUBLEBUFFER }; 100 | SetPixelFormat(hdc,ChoosePixelFormat(hdc,&pfd),&pfd); 101 | wglMakeCurrent(hdc,wglCreateContext(hdc)); 102 | ((PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT")) (0); 103 | int PS = MakeShader(VertexShader,FragmentShader); 104 | ((PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram"))(PS); 105 | while (!exit) 106 | { 107 | while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 108 | { 109 | if( msg.message==WM_QUIT ) exit = 1; 110 | TranslateMessage( &msg ); 111 | DispatchMessage( &msg ); 112 | } 113 | glClear(GL_COLOR_BUFFER_BIT); 114 | glDrawArrays(GL_TRIANGLES, 0, 6); 115 | wglSwapLayerBuffers(hdc, WGL_SWAP_MAIN_PLANE); 116 | } 117 | return 0; 118 | } -------------------------------------------------------------------------------- /HLSL/Asm.cpp: -------------------------------------------------------------------------------- 1 | // g++ -s -o Asm.exe Asm.cpp "-IC:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Include" "-LC:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Lib\x86" -ld3d9 -ld3dx9 2 | // fxc /T ps_2_0 /Fc C:\pixel.asm C:\pixel.hlsl 3 | #define WIN32_LEAN_AND_MEAN 4 | #define WIN32_EXTRA_LEAN 5 | #define DLL D3DXSHADER_USE_LEGACY_D3DX9_31_DLL 6 | #define MSAA D3DMULTISAMPLE_NONE 7 | #define VSYNC D3DPRESENT_INTERVAL_IMMEDIATE 8 | #define SWAP D3DSWAPEFFECT_DISCARD 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | static const char VertexShaderCode[] = \ 15 | "vs.2.0\n" 16 | "dcl_position v0\n" 17 | "mov oPos, v0\n" ; 18 | 19 | static const char PixelShaderCode[] = \ 20 | "ps.2.0\n" 21 | "def c0, 0, 0, 1, 1\n" 22 | "mov oC0, c0\n"; 23 | 24 | int main() 25 | { 26 | LPDIRECT3DVERTEXSHADER9 VertexShader; 27 | LPDIRECT3DPIXELSHADER9 PixelShader; 28 | LPD3DXBUFFER VSBuffer, PSBuffer, PSDebug; 29 | LPDIRECT3DDEVICE9 d3dDevice; 30 | LPDIRECT3D9 d3d = Direct3DCreate9( D3D_SDK_VERSION ); 31 | D3DPRESENT_PARAMETERS W = {1920,1080,D3DFMT_A8R8G8B8,1,MSAA,0,SWAP,0,0,1,D3DFMT_D24S8,0,0,VSYNC}; 32 | W.hDeviceWindow = CreateWindow("static",0,WS_POPUP|WS_VISIBLE,0,0,1920,1080,0,0,0,0); 33 | d3d->CreateDevice(0,D3DDEVTYPE_HAL,W.hDeviceWindow,D3DCREATE_HARDWARE_VERTEXPROCESSING,&W,&d3dDevice); 34 | ShowCursor(0); 35 | D3DXAssembleShader(VertexShaderCode,sizeof(VertexShaderCode), 0,0, DLL, &VSBuffer, 0); 36 | D3DXAssembleShader(PixelShaderCode,sizeof(PixelShaderCode), 0,0, DLL, &PSBuffer, &PSDebug); 37 | if (PSDebug) 38 | { 39 | FILE *file; 40 | file = fopen ("debug.log","a"); 41 | char *p = (char*)PSDebug->GetBufferPointer(); 42 | fprintf (file,"\n%s error : %s \n", p ); 43 | fclose (file); 44 | return 0; 45 | } 46 | d3dDevice->CreateVertexShader((DWORD*)VSBuffer->GetBufferPointer(), &VertexShader); 47 | d3dDevice->CreatePixelShader((DWORD*)PSBuffer->GetBufferPointer(), &PixelShader); 48 | d3dDevice->SetVertexShader(VertexShader); 49 | d3dDevice->SetPixelShader(PixelShader); 50 | d3dDevice->SetFVF(D3DFVF_XYZ); 51 | float quad[20] = {1,-1,0,1,0,-1,-1,0,0,0,1,1,0,1,1,-1,1,0,0,1}; 52 | do 53 | { 54 | d3dDevice->BeginScene(); 55 | d3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, 5*sizeof(float)); 56 | d3dDevice->EndScene(); 57 | d3dDevice->Present(NULL, NULL, NULL, NULL); 58 | } 59 | while ( !GetAsyncKeyState(VK_ESCAPE) ); 60 | VertexShader->Release(); 61 | PixelShader->Release(); 62 | VSBuffer->Release(); 63 | PSBuffer->Release(); 64 | d3dDevice->Release(); 65 | d3d->Release(); 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /HLSL/Audio.cpp: -------------------------------------------------------------------------------- 1 | // g++ -s -o Audio.exe Audio.cpp "-IC:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Include" "-LC:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Lib\x86" -lwinmm -ld3d9 -ld3dx9 2 | #define WIN32_LEAN_AND_MEAN 3 | #define WIN32_EXTRA_LEAN 4 | #define DLL D3DXSHADER_OPTIMIZATION_LEVEL3|D3DXSHADER_PREFER_FLOW_CONTROL 5 | #define MSAA D3DMULTISAMPLE_NONE 6 | #define VSYNC D3DPRESENT_INTERVAL_IMMEDIATE 7 | #define SWAP D3DSWAPEFFECT_DISCARD 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | static const char VertexShaderCode[] = \ 15 | "float4 VSMain(float4 P:POSITION):POSITION {return P;};" ; 16 | 17 | static const char PixelShaderCode[] = \ 18 | "float T : register(c0);" 19 | "void PSMain(float2 U:VPOS, out float4 S:COLOR) " 20 | "{" 21 | "U = U/float2(1920,1080);" 22 | "S = lerp(float4(0,sin(T)*0.5+0.5,0,1),0.5,step(U.x,0.5));" 23 | "}" ; 24 | 25 | int h[11] = {0x46464952,9172836,0x45564157,0x20746D66,16,WAVE_FORMAT_PCM|131072,44100,176400,1048580,0x61746164,9172800}; 26 | short m[9172822]; 27 | 28 | int f2i(float x) 29 | { 30 | if (x>=0x1.0p23) return x; 31 | return (unsigned int) (x+0.49999997f); 32 | } 33 | 34 | void audio(short *buffer) 35 | { 36 | for (int i = 0; i<4586400; i++) 37 | { 38 | float t = (float)i / (float)44100; 39 | float f = fmod(sinf(t*fmod(t,1.0f))*10.0f,1.0f); 40 | buffer[2*i+0] = f2i(f*32767.0f); 41 | buffer[2*i+1] = f2i(f*32767.0f); 42 | } 43 | } 44 | 45 | int main() 46 | { 47 | LPDIRECT3DVERTEXSHADER9 VertexShader; 48 | LPDIRECT3DPIXELSHADER9 PixelShader; 49 | LPD3DXBUFFER VSBuffer, PSBuffer; 50 | LPDIRECT3DDEVICE9 d3dDevice; 51 | LPDIRECT3D9 d3d = Direct3DCreate9( D3D_SDK_VERSION ); 52 | D3DPRESENT_PARAMETERS W = {1920,1080,D3DFMT_A8R8G8B8,1,MSAA,0,SWAP,0,0,1,D3DFMT_D24S8,0,0,VSYNC}; 53 | W.hDeviceWindow = CreateWindow("static",0,WS_POPUP|WS_VISIBLE,0,0,1920,1080,0,0,0,0); 54 | d3d->CreateDevice(0,D3DDEVTYPE_HAL,W.hDeviceWindow,D3DCREATE_HARDWARE_VERTEXPROCESSING,&W,&d3dDevice); 55 | ShowCursor(0); 56 | D3DXCompileShader(VertexShaderCode,sizeof(VertexShaderCode),0,0,"VSMain","vs_3_0",DLL,&VSBuffer,0,0); 57 | D3DXCompileShader(PixelShaderCode,sizeof(PixelShaderCode),0,0,"PSMain","ps_3_0",DLL,&PSBuffer,0,0); 58 | d3dDevice->CreateVertexShader((DWORD*)VSBuffer->GetBufferPointer(), &VertexShader); 59 | d3dDevice->CreatePixelShader((DWORD*)PSBuffer->GetBufferPointer(), &PixelShader); 60 | d3dDevice->SetVertexShader(VertexShader); 61 | d3dDevice->SetPixelShader(PixelShader); 62 | d3dDevice->SetFVF(D3DFVF_XYZ); 63 | float quad[20] = {1,-1,0,1,0,-1,-1,0,0,0,1,1,0,1,1,-1,1,0,0,1}, S = GetTickCount()*0.001f; 64 | audio(m+22); 65 | memcpy(m,h,44); 66 | sndPlaySound((const char*)&m,SND_ASYNC|SND_MEMORY); 67 | do 68 | { 69 | d3dDevice->BeginScene(); 70 | float timer[1] = {GetTickCount()*0.001f-S}; 71 | d3dDevice->SetPixelShaderConstantF(0, timer, 1); 72 | d3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, 5*sizeof(float)); 73 | d3dDevice->EndScene(); 74 | d3dDevice->Present(NULL, NULL, NULL, NULL); 75 | } 76 | while ( !GetAsyncKeyState(VK_ESCAPE) ); 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /HLSL/Circle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | static const char PixelShaderCode[] = \ 4 | "ps_3_0\n" 5 | "def c0, 2, -1366, -768, 0.00130208337\n" 6 | "def c1, 0.7, 0, 0, 0\n" 7 | "def c2, 0, 0.5, -1, 0\n" 8 | "dcl vPos.xy\n" 9 | "mad r0.xy, vPos, c0.x, c0.yzzw\n" 10 | "mul r0.xy, r0, c0.w\n" 11 | "dp2add r0.x, r0, r0, c2.x\n" 12 | "rsq r0.x, r0.x\n" 13 | "rcp r0.x, r0.x\n" 14 | "add r0.y, -r0.x, c2.y\n" 15 | "cmp r0.y, r0.y, c2.z, c2.w\n" 16 | "add r0.x, r0.y, -r0.x\n" 17 | "add r0.x, r0.x, c1.x\n" 18 | "cmp oC0.z, r0.x, -c2.z, -c2.w\n" 19 | "mov oC0.xyw, -c2.wwzz\n"; 20 | 21 | int main() 22 | { 23 | LPDIRECT3DPIXELSHADER9 PixelShader; 24 | LPD3DXBUFFER PSBuffer; 25 | LPDIRECT3DDEVICE9 d3dDevice; 26 | LPDIRECT3D9 d3d = Direct3DCreate9( D3D_SDK_VERSION ); 27 | D3DPRESENT_PARAMETERS W = {1366,768,D3DFMT_A8R8G8B8,1,D3DMULTISAMPLE_NONE,0,_D3DSWAPEFFECT(1),0,0,1,D3DFMT_D24S8,0,0,0x80000000L}; 28 | W.hDeviceWindow = CreateWindow("static",0,WS_POPUP|WS_VISIBLE,0,0,1366,768,0,0,0,0); 29 | d3d->CreateDevice(0,D3DDEVTYPE_HAL,W.hDeviceWindow,0x00000040L,&W,&d3dDevice); 30 | ShowCursor(0); 31 | D3DXAssembleShader(PixelShaderCode,sizeof(PixelShaderCode), 0,0, (1 << 16), &PSBuffer, 0); 32 | d3dDevice->CreatePixelShader((DWORD*)PSBuffer->GetBufferPointer(), &PixelShader); 33 | d3dDevice->SetPixelShader(PixelShader); 34 | d3dDevice->SetFVF(D3DFVF_XYZ); 35 | float quad[20] = {1,-1,0,1,0,-1,-1,0,0,0,1,1,0,1,1,-1,1,0,0,1}; 36 | do 37 | { 38 | d3dDevice->BeginScene(); 39 | d3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, 5*sizeof(float)); 40 | d3dDevice->EndScene(); 41 | d3dDevice->Present(NULL, NULL, NULL, NULL); 42 | } 43 | while ( !GetAsyncKeyState(VK_ESCAPE) ); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /HLSL/Helix.cpp: -------------------------------------------------------------------------------- 1 | //g++ -s -o Helix.exe Helix.cpp "-IC:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Include" "-LC:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Lib\x86" -ld3d9 -ld3dx9 2 | #define WIN32_LEAN_AND_MEAN 3 | #define WIN32_EXTRA_LEAN 4 | #define DLL D3DXSHADER_OPTIMIZATION_LEVEL3|D3DXSHADER_PREFER_FLOW_CONTROL 5 | #define MSAA D3DMULTISAMPLE_NONE 6 | #define VSYNC D3DPRESENT_INTERVAL_IMMEDIATE 7 | #define SWAP D3DSWAPEFFECT_DISCARD 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | static const char VertexShaderCode[] = \ 14 | "float4 VSMain(float4 P:POSITION):POSITION {return P;};" ; 15 | 16 | static const char PixelShaderCode[] = \ 17 | "uniform extern float T : register(c0);" 18 | "float mod(float x, float y)" 19 | "{" 20 | "return x - y * floor(x/y);" 21 | "}" 22 | "float map (float3 q)" 23 | "{" 24 | "float r = 20.0;" 25 | "float a = atan2(q.z,q.x); " 26 | "q.x = length(q.xz)-r; " 27 | "q.y = mod(q.y-a*r/6.28,r)-r*0.5;" 28 | "q.z = r*a;" 29 | "float l = length(q.xy);" 30 | "float d = sin(atan2(q.y,q.x)-q.z);" 31 | "return length(float2(l-4.0,d)) - 0.5;" 32 | "}" 33 | "float4 raymarch (float3 ro, float3 rd)" 34 | "{" 35 | "float4 n = float4(0,0,0,1); " 36 | "for (int i=0;i<128;i++)" 37 | "{" 38 | "float t = map (ro);" 39 | "if (t<0.001)" 40 | "{" 41 | "float c = pow(1.0-float(i)/float(128),2.0);" 42 | "n = float4(c,c,c,1.0); break;" 43 | "}" 44 | "ro+=t*rd;" 45 | "}" 46 | "return n;" 47 | "}" 48 | "void PSMain(float2 U:VPOS, out float4 S:COLOR) " 49 | "{" 50 | "float2 UV = (2.0*U-float2(1920,1080))/1080.0;" 51 | "float3 ro = float3(0.0,T*3.0,-40.0);" 52 | "float3 rd = normalize(float3(UV,2.0)); " 53 | "S = raymarch(ro,rd);" 54 | "}" ; 55 | 56 | int main() 57 | { 58 | LPDIRECT3DVERTEXSHADER9 VertexShader; 59 | LPDIRECT3DPIXELSHADER9 PixelShader; 60 | LPD3DXBUFFER VSBuffer, PSBuffer, PSDebug; 61 | LPDIRECT3DDEVICE9 d3dDevice; 62 | LPDIRECT3D9 d3d = Direct3DCreate9( D3D_SDK_VERSION ); 63 | D3DPRESENT_PARAMETERS W = {1920,1080,D3DFMT_A8R8G8B8,1,MSAA,0,SWAP,0,0,1,D3DFMT_D24S8,0,0,VSYNC}; 64 | W.hDeviceWindow = CreateWindow("static",0,WS_POPUP|WS_VISIBLE,0,0,1920,1080,0,0,0,0); 65 | d3d->CreateDevice(0,D3DDEVTYPE_HAL,W.hDeviceWindow,D3DCREATE_HARDWARE_VERTEXPROCESSING,&W,&d3dDevice); 66 | ShowCursor(0); 67 | D3DXCompileShader(VertexShaderCode,sizeof(VertexShaderCode),0,0,"VSMain","vs_3_0",DLL,&VSBuffer,0,0); 68 | D3DXCompileShader(PixelShaderCode,sizeof(PixelShaderCode),0,0,"PSMain","ps_3_0",DLL,&PSBuffer,&PSDebug,0); 69 | if (PSDebug) 70 | { 71 | FILE *file = fopen ("debug.log","a"); 72 | char *p = (char*)PSDebug->GetBufferPointer(); 73 | fprintf (file,"\n%s error : %s \n", p ); 74 | fclose (file); 75 | return 0; 76 | } 77 | d3dDevice->CreateVertexShader((DWORD*)VSBuffer->GetBufferPointer(), &VertexShader); 78 | d3dDevice->CreatePixelShader((DWORD*)PSBuffer->GetBufferPointer(), &PixelShader); 79 | d3dDevice->SetVertexShader(VertexShader); 80 | d3dDevice->SetPixelShader(PixelShader); 81 | d3dDevice->SetFVF(D3DFVF_XYZ); 82 | float quad[20] = {1,-1,0,1,0,-1,-1,0,0,0,1,1,0,1,1,-1,1,0,0,1}, S = GetTickCount()*0.001f; 83 | do 84 | { 85 | d3dDevice->BeginScene(); 86 | float timer[1] = {GetTickCount()*0.001f-S}; 87 | d3dDevice->SetPixelShaderConstantF(0, timer, 1); 88 | d3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, 5*sizeof(float)); 89 | d3dDevice->EndScene(); 90 | d3dDevice->Present(NULL, NULL, NULL, NULL); 91 | } 92 | while ( !GetAsyncKeyState(VK_ESCAPE) ); 93 | VertexShader->Release(); 94 | PixelShader->Release(); 95 | VSBuffer->Release(); 96 | PSBuffer->Release(); 97 | d3dDevice->Release(); 98 | d3d->Release(); 99 | return 0; 100 | } 101 | -------------------------------------------------------------------------------- /HLSL/Pattern.cpp: -------------------------------------------------------------------------------- 1 | #define WIN32_LEAN_AND_MEAN 2 | #define WIN32_EXTRA_LEAN 3 | #define DLL D3DXSHADER_USE_LEGACY_D3DX9_31_DLL 4 | #define MSAA D3DMULTISAMPLE_NONE 5 | #define VSYNC D3DPRESENT_INTERVAL_IMMEDIATE 6 | #define SWAP D3DSWAPEFFECT_DISCARD 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | static const char VertexShaderCode[] = \ 13 | "vs.3.0\n" 14 | "dcl_position v0\n" 15 | "dcl_position o0\n" 16 | "mov o0, v0\n"; 17 | 18 | static const char PixelShaderCode[] = \ 19 | "ps.3.0\n" 20 | "def c0, 2, -1366, -768, 0.00520833349\n" 21 | "def c1, 10, 0, 1, 0\n" 22 | "def c2, 0.159154937, 0.5, 6.28318548, -3.14159274\n" 23 | "dcl vPos.xy\n" 24 | "mad r0.xy, vPos, c0.x, c0.yzzw\n" 25 | "mul r0.xy, r0, c0.w\n" 26 | "mul r0.z, r0_abs.y, r0_abs.x\n" 27 | "mad r0.z, r0.z, c2.x, c2.y\n" 28 | "frc r0.z, r0.z\n" 29 | "mad r0.z, r0.z, c2.z, c2.w\n" 30 | "sincos r1.x, r0.z\n" 31 | "mul r0.z, r1.x, c1.x\n" 32 | "lrp r1.x, r0.z, r0_abs.y, r0_abs.x\n" 33 | "add oC0.x, -r1_abs.x, c1.x\n" 34 | "mov oC0.yzw, c1.xyyz\n"; 35 | 36 | int main() 37 | { 38 | LPDIRECT3DVERTEXSHADER9 VertexShader; 39 | LPDIRECT3DPIXELSHADER9 PixelShader; 40 | LPD3DXBUFFER VSBuffer, PSBuffer, PSDebug; 41 | LPDIRECT3DDEVICE9 d3dDevice; 42 | LPDIRECT3D9 d3d = Direct3DCreate9( D3D_SDK_VERSION ); 43 | D3DPRESENT_PARAMETERS W = {1366,768,D3DFMT_A8R8G8B8,1,MSAA,0,SWAP,0,0,1,D3DFMT_D24S8,0,0,VSYNC}; 44 | W.hDeviceWindow = CreateWindow("static",0,WS_POPUP|WS_VISIBLE,0,0,1366,768,0,0,0,0); 45 | d3d->CreateDevice(0,D3DDEVTYPE_HAL,W.hDeviceWindow,D3DCREATE_HARDWARE_VERTEXPROCESSING,&W,&d3dDevice); 46 | ShowCursor(0); 47 | D3DXAssembleShader(VertexShaderCode,sizeof(VertexShaderCode), 0,0, DLL, &VSBuffer, 0); 48 | D3DXAssembleShader(PixelShaderCode,sizeof(PixelShaderCode), 0,0, DLL, &PSBuffer, &PSDebug); 49 | if (PSDebug) 50 | { 51 | FILE *file; 52 | file = fopen ("debug.log","a"); 53 | char *p = (char*)PSDebug->GetBufferPointer(); 54 | fprintf (file,"\n%s error : %s \n", p ); 55 | fclose (file); 56 | return 0; 57 | } 58 | d3dDevice->CreateVertexShader((DWORD*)VSBuffer->GetBufferPointer(), &VertexShader); 59 | d3dDevice->CreatePixelShader((DWORD*)PSBuffer->GetBufferPointer(), &PixelShader); 60 | d3dDevice->SetVertexShader(VertexShader); 61 | d3dDevice->SetPixelShader(PixelShader); 62 | d3dDevice->SetFVF(D3DFVF_XYZ); 63 | float quad[20] = {1,-1,0,1,0,-1,-1,0,0,0,1,1,0,1,1,-1,1,0,0,1}; 64 | do 65 | { 66 | d3dDevice->BeginScene(); 67 | d3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, 5*sizeof(float)); 68 | d3dDevice->EndScene(); 69 | d3dDevice->Present(NULL, NULL, NULL, NULL); 70 | } 71 | while ( !GetAsyncKeyState(VK_ESCAPE) ); 72 | VertexShader->Release(); 73 | PixelShader->Release(); 74 | VSBuffer->Release(); 75 | PSBuffer->Release(); 76 | d3dDevice->Release(); 77 | d3d->Release(); 78 | return 0; 79 | } -------------------------------------------------------------------------------- /HLSL/SM3_HelloWorld.cpp: -------------------------------------------------------------------------------- 1 | //g++ -s -o SM3_HelloWorld.exe SM3_HelloWorld.cpp "-IC:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Include" "-LC:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Lib\x86" -ld3d9 -ld3dx9 2 | #define WIN32_LEAN_AND_MEAN 3 | #define WIN32_EXTRA_LEAN 4 | #define DLL D3DXSHADER_OPTIMIZATION_LEVEL3|D3DXSHADER_PREFER_FLOW_CONTROL 5 | #define MSAA D3DMULTISAMPLE_NONE 6 | #define VSYNC D3DPRESENT_INTERVAL_IMMEDIATE 7 | #define SWAP D3DSWAPEFFECT_DISCARD 8 | #include 9 | #include 10 | #include 11 | 12 | static const char VertexShaderCode[] = \ 13 | "float4 VSMain(float4 P:POSITION):POSITION {return P;};" ; 14 | 15 | static const char PixelShaderCode[] = \ 16 | "float T : register(c0);" 17 | "void PSMain(float2 U:VPOS, out float4 S:COLOR) " 18 | "{" 19 | "U = U/float2(1920,1080);" 20 | "S = lerp(float4(0,sin(T)*0.5+0.5,0,1),0.5,step(U.x,0.5));" 21 | "}" ; 22 | 23 | int main() 24 | { 25 | LPDIRECT3DVERTEXSHADER9 VertexShader; 26 | LPDIRECT3DPIXELSHADER9 PixelShader; 27 | LPD3DXBUFFER VSBuffer, PSBuffer; 28 | LPDIRECT3DDEVICE9 d3dDevice; 29 | LPDIRECT3D9 d3d = Direct3DCreate9( D3D_SDK_VERSION ); 30 | D3DPRESENT_PARAMETERS W = {1920,1080,D3DFMT_A8R8G8B8,1,MSAA,0,SWAP,0,0,1,D3DFMT_D24S8,0,0,VSYNC}; 31 | W.hDeviceWindow = CreateWindow("static",0,WS_POPUP|WS_VISIBLE,0,0,1920,1080,0,0,0,0); 32 | d3d->CreateDevice(0,D3DDEVTYPE_HAL,W.hDeviceWindow,D3DCREATE_HARDWARE_VERTEXPROCESSING,&W,&d3dDevice); 33 | ShowCursor(0); 34 | D3DXCompileShader(VertexShaderCode,sizeof(VertexShaderCode),0,0,"VSMain","vs_3_0",DLL,&VSBuffer,0,0); 35 | D3DXCompileShader(PixelShaderCode,sizeof(PixelShaderCode),0,0,"PSMain","ps_3_0",DLL,&PSBuffer,0,0); 36 | d3dDevice->CreateVertexShader((DWORD*)VSBuffer->GetBufferPointer(), &VertexShader); 37 | d3dDevice->CreatePixelShader((DWORD*)PSBuffer->GetBufferPointer(), &PixelShader); 38 | d3dDevice->SetVertexShader(VertexShader); 39 | d3dDevice->SetPixelShader(PixelShader); 40 | d3dDevice->SetFVF(D3DFVF_XYZ); 41 | float quad[20] = {1,-1,0,1,0,-1,-1,0,0,0,1,1,0,1,1,-1,1,0,0,1}, S = GetTickCount()*0.001f; 42 | do 43 | { 44 | d3dDevice->BeginScene(); 45 | float timer[1] = {GetTickCount()*0.001f-S}; 46 | d3dDevice->SetPixelShaderConstantF(0, timer, 1); 47 | d3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, 5*sizeof(float)); 48 | d3dDevice->EndScene(); 49 | d3dDevice->Present(NULL, NULL, NULL, NULL); 50 | } 51 | while ( !GetAsyncKeyState(VK_ESCAPE) ); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /HLSL/audio.h: -------------------------------------------------------------------------------- 1 | // Audio functions. 2 | 3 | float bass(float t,float n,float e) //time,15.0,18.0 4 | { 5 | float x = fmod(t,0.5f); 6 | return 0.5f * max(-1.,min(1.,sin(x*pow(2.,((30.-x*5.f-70.)/n))*440.f*6.28f)*10.f*exp(-x*e))); 7 | } 8 | -------------------------------------------------------------------------------- /HLSL/checkerboard.cpp: -------------------------------------------------------------------------------- 1 | // cl.exe checkerboard.cpp d3d11.lib dxguid.lib user32.lib kernel32.lib gdi32.lib d3dcompiler.lib 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #define WIDTH 1280 8 | #define HEIGHT 720 9 | 10 | const unsigned char VertexShader[] = 11 | { 12 | "static const float3 vertices[6] = {float3(1,-1,0),float3(-1,-1,0),float3(1,1,0), float3(-1,-1,0),float3(-1,1,0),float3(1,1,0)};" 13 | "static const float2 uvs[6] = {float2(1,0),float2(0,0),float2(1,1), float2(0,0),float2(0,1),float2(1,1)};" 14 | "void VSMain(out float4 vertex:SV_POSITION, out float2 uv:TEXCOORD0, in uint id:SV_VertexID)" 15 | "{" 16 | "uv = uvs[id];" 17 | "vertex = float4(vertices[id], 1);" 18 | "}" 19 | }; 20 | 21 | const unsigned char PixelShader[] = 22 | { 23 | "Texture2D pattern : register(t0);" 24 | "SamplerState state {Filter = MIN_MAG_LINEAR_MIP_POINT;};" 25 | "float4 PSMain(float4 vertex:SV_POSITION, float2 uv:TEXCOORD0) : SV_TARGET" 26 | "{" 27 | "return pattern.Sample( state, uv );" 28 | "}" 29 | }; 30 | 31 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 32 | { 33 | if ((uMsg == WM_KEYUP && wParam == VK_ESCAPE) || uMsg==WM_CLOSE || uMsg==WM_DESTROY) 34 | { 35 | PostQuitMessage(0); return 0; 36 | } 37 | else 38 | { 39 | return DefWindowProc(hWnd, uMsg, wParam, lParam); 40 | } 41 | } 42 | 43 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 44 | { 45 | int exit = 0; 46 | MSG msg; 47 | WNDCLASS win = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, "DirectX 11"}; 48 | RegisterClass(&win); 49 | HWND hwnd = CreateWindowEx(0, win.lpszClassName, "DirectX 11", WS_VISIBLE|WS_OVERLAPPEDWINDOW, 0, 0, WIDTH, HEIGHT, 0, 0, 0, 0); 50 | ID3D11Device *device; 51 | IDXGISwapChain *surface; 52 | ID3D11DeviceContext *context; 53 | ID3D11Resource *image; 54 | ID3D11RenderTargetView *target; 55 | ID3D11VertexShader *vs; 56 | ID3D11PixelShader *ps; 57 | ID3DBlob *VSblob; 58 | ID3DBlob *PSblob; 59 | ID3D11Texture2D *texture; 60 | ID3D11ShaderResourceView *srv; 61 | DXGI_SWAP_CHAIN_DESC sd = {{WIDTH, HEIGHT, 0, 0, DXGI_FORMAT_R8G8B8A8_UNORM, (DXGI_MODE_SCANLINE_ORDER)0, (DXGI_MODE_SCALING)0 }, {1, 0}, (1L << (1 + 4)) | (1L << (6 + 4)) | (1L << (0 + 4)), 1, hwnd, 1, DXGI_SWAP_EFFECT_SEQUENTIAL, 0}; 62 | D3D11CreateDeviceAndSwapChain(0, D3D_DRIVER_TYPE_HARDWARE, 0, 0, 0, 0, D3D11_SDK_VERSION, &sd, &surface, &device, 0, &context); 63 | surface->GetBuffer(0, __uuidof( ID3D11Resource), (void**)&image ); 64 | device->CreateRenderTargetView(image, 0, &target); 65 | context->OMSetRenderTargets(1, &target, 0); 66 | D3D11_VIEWPORT vp = {0,0,WIDTH,HEIGHT,0.0f,1.0f}; 67 | context->RSSetViewports(1, &vp); 68 | D3DCompile(&VertexShader, sizeof VertexShader, 0, 0, 0, "VSMain", "vs_5_0", 1 << 15, 0, &VSblob, 0); 69 | device->CreateVertexShader(VSblob->GetBufferPointer(), VSblob->GetBufferSize(), 0, &vs); 70 | D3DCompile(&PixelShader, sizeof PixelShader, 0, 0, 0, "PSMain", "ps_5_0", 1 << 15, 0, &PSblob, 0); 71 | device->CreatePixelShader(PSblob->GetBufferPointer(), PSblob->GetBufferSize(), 0, &ps); 72 | context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); 73 | int *pixels = new int[512*512]; 74 | for(int i=0; i<512; i++) 75 | { 76 | for(int j=0; j<512; j++) 77 | { 78 | pixels[i*512+j] = ((i&32)==(j&32)) ? 0x00000000 : 0xffffffff; 79 | } 80 | } 81 | D3D11_SUBRESOURCE_DATA tsd = {(void *)pixels, 512*4, 512*512*4}; 82 | D3D11_TEXTURE2D_DESC tdesc = {512, 512, 1, 1, DXGI_FORMAT_R8G8B8A8_UNORM, {1,0}, D3D11_USAGE_DEFAULT,D3D11_BIND_SHADER_RESOURCE, 0, 0}; 83 | device->CreateTexture2D(&tdesc, &tsd, &texture); 84 | device->CreateShaderResourceView(texture, 0, &srv); 85 | context->PSSetShaderResources(0, 1, &srv); 86 | while (!exit) 87 | { 88 | while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 89 | { 90 | if (msg.message == WM_QUIT) exit = 1; 91 | TranslateMessage(&msg); 92 | DispatchMessage(&msg); 93 | } 94 | context->VSSetShader(vs, 0, 0); 95 | context->PSSetShader(ps, 0, 0); 96 | context->Draw(6, 0); 97 | surface->Present(0, 0); 98 | } 99 | return 0; 100 | } -------------------------------------------------------------------------------- /HLSL/hexmap.c: -------------------------------------------------------------------------------- 1 | // cl.exe hexmap.c /I"C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Include" /link /LIBPATH:"C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Lib\x64" d3d9.lib d3dx9.lib user32.lib kernel32.lib gdi32.lib 2 | #include 3 | 4 | static const char PixelShader[] = \ 5 | "ps_3_0 \n" 6 | "def c1, 0.00925925933, 1.15470064, 0.577350318, 0.333333343 \n" 7 | "def c2, -0.5, 0.5, 4.47034836e-007, 0 \n" 8 | "def c3, 0.159154937, 0.25, 6.28318548, -3.14159274 \n" 9 | "def c4, -2.52398507e-007, 2.47609005e-005, -0.00138883968, 0.0416666418 \n" 10 | "def c5, 3, -1, -2, -0 \n" 11 | "dcl vPos.xy \n" 12 | "mov r0.x, c1.x \n" 13 | "mad r0.xy, vPos, r0.x, c0.x \n" 14 | "mul r1.x, r0.x, c1.y \n" 15 | "mad r1.z, r0.x, c1.z, r0.y \n" 16 | "frc r0.xy, r1.zxzw \n" 17 | "add r0.zw, -r0.xyyx, r1.xyxz \n" 18 | "add r0.xy, -r0.yxzw, r0 \n" 19 | "cmp r0.xy, r0, c5.y, c5.w \n" 20 | "add r1.x, r0.w, r0.z \n" 21 | "mul r1.y, r1.x, c1.w \n" 22 | "frc r1.z, r1.y \n" 23 | "add r1.y, -r1.z, r1.y \n" 24 | "mad r1.x, r1.y, -c5.x, r1.x \n" 25 | "add r1.xy, r1.x, c5.yzzw \n" 26 | "cmp r0.xy, r1.y, r0, -c5.w \n" 27 | "cmp r1.x, r1.x, -c5.y, -c5.w \n" 28 | "add r0.zw, r0, r1.x \n" 29 | "add r0.xy, r0, r0.zwzw \n" 30 | "mad r0.xy, r0, c3.x, c3.y \n" 31 | "frc r0.xy, r0 \n" 32 | "mad r0.xy, r0, c3.z, c3.w \n" 33 | "mul r0.xy, r0, r0 \n" 34 | "mad r0.zw, r0.xyxy, c4.x, c4.y \n" 35 | "mad r0.zw, r0.xyxy, r0, c4.z \n" 36 | "mad r0.zw, r0.xyxy, r0, c4.w \n" 37 | "mad r0.zw, r0.xyxy, r0, c2.x \n" 38 | "mad r0.xy, r0, r0.zwzw, -c5.y \n" 39 | "mul r0.xy, r0, c2.y \n" 40 | "mov r0.zw, c2.z \n" 41 | "add oC0, r0, c2.y \n"; 42 | 43 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 44 | { 45 | if (uMsg==WM_CLOSE || uMsg==WM_DESTROY || (uMsg==WM_KEYDOWN && wParam==VK_ESCAPE)) 46 | { 47 | PostQuitMessage(0); return 0; 48 | } 49 | else 50 | { 51 | return DefWindowProc(hWnd, uMsg, wParam, lParam); 52 | } 53 | } 54 | 55 | int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 56 | { 57 | ShowCursor(0); 58 | int exit = 0; 59 | MSG msg; 60 | WNDCLASS win = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, " "}; 61 | RegisterClass(&win); 62 | HWND hwnd = CreateWindowEx(0, win.lpszClassName, " ", WS_VISIBLE|WS_POPUP, 0, 0, 1920, 1080, 0, 0, 0, 0); 63 | LPDIRECT3DPIXELSHADER9 ps; 64 | LPD3DXBUFFER buffer; 65 | LPDIRECT3DDEVICE9 device; 66 | LPDIRECT3D9 d3d = Direct3DCreate9( D3D_SDK_VERSION ); 67 | D3DPRESENT_PARAMETERS d3p = {1920, 1080, D3DFMT_A8R8G8B8, 1, 0, 0, 1, hwnd, 0, 1, D3DFMT_D24S8, 0, 0, 0x80000000L}; 68 | d3d->lpVtbl->CreateDevice(d3d, 0, D3DDEVTYPE_HAL, hwnd, 0x00000040L, &d3p, &device); 69 | D3DXAssembleShader(PixelShader, sizeof(PixelShader), 0, 0, (1 << 16), &buffer, 0); 70 | device->lpVtbl->CreatePixelShader(device, (DWORD*)buffer->lpVtbl->GetBufferPointer(buffer), &ps); 71 | device->lpVtbl->SetPixelShader(device, ps); 72 | device->lpVtbl->SetFVF(device, D3DFVF_XYZ); 73 | float quad[20] = {1,-1,0,1,0,-1,-1,0,0,0,1,1,0,1,1,-1,1,0,0,1}, S = GetTickCount()*0.001f; 74 | while (!exit) 75 | { 76 | while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 77 | { 78 | if( msg.message == WM_QUIT ) exit = 1; 79 | TranslateMessage( &msg ); 80 | DispatchMessage( &msg ); 81 | } 82 | device->lpVtbl->BeginScene(device); 83 | float timer[1] = {GetTickCount()*0.001f-S}; 84 | device->lpVtbl->SetPixelShaderConstantF(device,0, timer, 1); 85 | device->lpVtbl->DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, 20); 86 | device->lpVtbl->EndScene(device); 87 | device->lpVtbl->Present(device, 0, 0, 0, 0); 88 | } 89 | return 0; 90 | } -------------------------------------------------------------------------------- /HLSL/notepad.txt: -------------------------------------------------------------------------------- 1 | Useful functions for DirectX 12 programming. 2 | 3 | ************************************************** 4 | // Dump amount of video memory used by application in megabytes. 5 | void VideoMemoryUsage (IDXGIFactory4* factory) 6 | { 7 | IDXGIAdapter3* adapter; 8 | factory->EnumAdapters(0, (IDXGIAdapter**)(&adapter)); 9 | DXGI_QUERY_VIDEO_MEMORY_INFO videoMemoryInfo; 10 | adapter->QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_LOCAL, &videoMemoryInfo); 11 | size_t usedVRAM = videoMemoryInfo.CurrentUsage / 1024 / 1024; 12 | FILE *f = fopen("vram.txt", "w"); 13 | fprintf(f, "%zu\n", usedVRAM); 14 | fclose(f); 15 | } 16 | 17 | ************************************************** -------------------------------------------------------------------------------- /HLSL/particles.c: -------------------------------------------------------------------------------- 1 | // cl.exe particles.c d3d11.lib dxguid.lib user32.lib kernel32.lib gdi32.lib d3dcompiler.lib 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #define WIDTH 1280 8 | #define HEIGHT 720 9 | 10 | const unsigned char VertexShader[] = 11 | { 12 | "static const float3 vertices[6] = {float3(1,-1,0),float3(-1,-1,0),float3(1,1,0), float3(-1,-1,0),float3(-1,1,0),float3(1,1,0)};" 13 | "static const float2 uvs[6] = {float2(1,0),float2(0,0),float2(1,1), float2(0,0),float2(0,1),float2(1,1)};" 14 | "void VSMain(out float4 vertex:SV_POSITION, out float2 uv:TEXCOORD0, in uint id:SV_VertexID)" 15 | "{" 16 | "uv = uvs[id];" 17 | "vertex = float4(vertices[id], 1);" 18 | "}" 19 | }; 20 | 21 | const unsigned char PixelShader[] = 22 | { 23 | "cbuffer Constants : register(b0)" 24 | "{" 25 | "float iTime;" 26 | "};" 27 | "float3 surface (float2 uv)" 28 | "{" 29 | "float2 k = 0;" 30 | "for (float i=0.0;i<64.0;i++)" 31 | "{" 32 | "float2 q = float2(i*127.1+i*311.7,i*269.5+i*183.3);" 33 | "float2 h = frac(sin(q)*43758.5453);" 34 | "float2 p = cos(h*iTime);" 35 | "float d = length(uv-p);" 36 | "k+=(1.0-step(0.06,d))*h;" 37 | "}" 38 | "return float3(0.0,k);" 39 | "}" 40 | "float4 PSMain(float4 vertex:SV_POSITION, float2 uv:TEXCOORD0) : SV_TARGET" 41 | "{" 42 | "float2 p = float2(2.0*uv-1.0)/(1.0-uv.y);" 43 | "float3 c = 0;" 44 | "float2 d = (float2(0.0,-1.0)-p)/float(80);" 45 | "float w = 1.0;" 46 | "float2 s = p;" 47 | "for( int i=0; i<80; i++ )" 48 | "{" 49 | "float3 res = surface(s);" 50 | "c += w*smoothstep(0.0, 1.0, res);" 51 | "w *= 0.97;" 52 | "s += d;" 53 | "}" 54 | "c = c * 8.0 / float(80);" 55 | "return float4(c, 1.0);" 56 | "}" 57 | }; 58 | 59 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 60 | { 61 | if ((uMsg == WM_KEYUP && wParam == VK_ESCAPE) || uMsg==WM_CLOSE || uMsg==WM_DESTROY) 62 | { 63 | PostQuitMessage(0); return 0; 64 | } 65 | else 66 | { 67 | return DefWindowProc(hWnd, uMsg, wParam, lParam); 68 | } 69 | } 70 | 71 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 72 | { 73 | int exit = 0; 74 | MSG msg; 75 | WNDCLASS win = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, "DirectX 11"}; 76 | RegisterClass(&win); 77 | HWND hwnd = CreateWindowEx(0, win.lpszClassName, "DirectX 11", WS_VISIBLE|WS_OVERLAPPEDWINDOW, 0, 0, WIDTH, HEIGHT, 0, 0, 0, 0); 78 | ID3D11Device *device; 79 | IDXGISwapChain *surface; 80 | ID3D11DeviceContext *context; 81 | ID3D11Resource *image; 82 | ID3D11RenderTargetView *target; 83 | ID3D11VertexShader *vs; 84 | ID3D11PixelShader *ps; 85 | ID3DBlob* VSblob; 86 | ID3DBlob* PSblob; 87 | ID3D11Buffer *buffer; 88 | D3D11_MAPPED_SUBRESOURCE resource; 89 | DXGI_SWAP_CHAIN_DESC sd = {{WIDTH, HEIGHT, 0, 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 0 }, {1, 0}, (1L << (1 + 4)) | (1L << (6 + 4)) | (1L << (0 + 4)), 1, hwnd, 1, 1, 0}; 90 | D3D11CreateDeviceAndSwapChain(0, D3D_DRIVER_TYPE_HARDWARE, 0, 0, 0, 0, D3D11_SDK_VERSION, &sd, &surface, &device, 0, &context); 91 | surface->lpVtbl->GetBuffer(surface, 0, (REFIID) &IID_ID3D11Resource, ( LPVOID* )&image ); 92 | D3D11_BUFFER_DESC desc = {16, D3D11_USAGE_DYNAMIC, D3D11_BIND_CONSTANT_BUFFER, D3D11_CPU_ACCESS_WRITE, 0, 0}; 93 | device->lpVtbl->CreateBuffer(device, &desc, 0, &buffer); 94 | device->lpVtbl->CreateRenderTargetView(device, image, 0, &target); 95 | context->lpVtbl->OMSetRenderTargets(context,1, &target, 0); 96 | D3D11_VIEWPORT vp = {0,0,WIDTH,HEIGHT,0.0f,1.0f}; 97 | context->lpVtbl->RSSetViewports(context,1, &vp); 98 | D3DCompile(&VertexShader, sizeof VertexShader, 0, 0, 0, "VSMain", "vs_5_0", 1 << 15, 0, &VSblob, 0); 99 | device->lpVtbl->CreateVertexShader(device, VSblob->lpVtbl->GetBufferPointer(VSblob), VSblob->lpVtbl->GetBufferSize(VSblob), 0, &vs); 100 | D3DCompile(&PixelShader, sizeof PixelShader, 0, 0, 0, "PSMain", "ps_5_0", 1 << 15, 0, &PSblob, 0); 101 | device->lpVtbl->CreatePixelShader(device, PSblob->lpVtbl->GetBufferPointer(PSblob), PSblob->lpVtbl->GetBufferSize(PSblob), 0, &ps); 102 | context->lpVtbl->IASetPrimitiveTopology(context,D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); 103 | while (!exit) 104 | { 105 | while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 106 | { 107 | if (msg.message == WM_QUIT) exit = 1; 108 | TranslateMessage(&msg); 109 | DispatchMessage(&msg); 110 | } 111 | context->lpVtbl->Map(context,(ID3D11Resource*)buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource); 112 | float time[] = {GetTickCount() * 0.001f}; 113 | memcpy(resource.pData, time, sizeof(float)); 114 | context->lpVtbl->Unmap(context, (ID3D11Resource *)buffer, 0); 115 | context->lpVtbl->VSSetShader(context, vs, 0, 0 ); 116 | context->lpVtbl->PSSetShader(context, ps, 0, 0 ); 117 | context->lpVtbl->PSSetConstantBuffers(context, 0, 1, &buffer ); 118 | context->lpVtbl->Draw(context, 6, 0); 119 | surface->lpVtbl->Present(surface, 0, 0 ); 120 | } 121 | return 0; 122 | } -------------------------------------------------------------------------------- /HLSL/plasma.ppm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/przemyslawzaworski/CPP-Programming/9d93e94a00402ac3b19fd1732368a3a4fa1ad18a/HLSL/plasma.ppm -------------------------------------------------------------------------------- /HLSL/tetrahedron.c: -------------------------------------------------------------------------------- 1 | // cl.exe tetrahedron.c d3d11.lib dxguid.lib user32.lib kernel32.lib gdi32.lib d3dcompiler.lib 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #define WIDTH 1280 8 | #define HEIGHT 720 9 | 10 | const unsigned char ComputeShader[] = 11 | { 12 | "RWTexture2D image : register (u0);" 13 | 14 | "cbuffer Constants : register(b0)" 15 | "{" 16 | "float iTime;" 17 | "};" 18 | 19 | "float3x3 rotationX(float x) " 20 | "{" 21 | "return float3x3(1.0,0.0,0.0,0.0,cos(x),sin(x),0.0,-sin(x),cos(x));" 22 | "}" 23 | 24 | "float3x3 rotationY(float y) " 25 | "{" 26 | "return float3x3(cos(y),0.0,-sin(y),0.0,1.0,0.0,sin(y),0.0,cos(y));" 27 | "}" 28 | 29 | "bool InsideTetrahedron(float3 a, float3 b, float3 c, float3 d, float3 p, out float3 color)" 30 | "{" 31 | "p=mul(rotationY(iTime),mul(rotationX(iTime),p));" 32 | "float3 vap = p - a;" 33 | "float3 vbp = p - b;" 34 | "float3 vab = b - a;" 35 | "float3 vac = c - a;" 36 | "float3 vad = d - a;" 37 | "float3 vbc = c - b;" 38 | "float3 vbd = d - b;" 39 | "float va6 = dot(vbp, cross(vbd, vbc));" 40 | "float vb6 = dot(vap, cross(vac, vad));" 41 | "float vc6 = dot(vap, cross(vad, vab));" 42 | "float vd6 = dot(vap, cross(vab, vac));" 43 | "float v6 = 1.0 / dot(vab, cross(vac, vad));" 44 | "float4 k = float4(va6*v6, vb6*v6, vc6*v6, vd6*v6);" 45 | "if ((k.x >= 0.0) && (k.x <= 1.0) && (k.y >= 0.0) && (k.y <= 1.0) && (k.z >= 0.0) && (k.z <= 1.0) && (k.w >= 0.0) && (k.w <= 1.0))" 46 | "{" 47 | "color = k.rgb;" 48 | "return true;" 49 | "}" 50 | "else" 51 | "{" 52 | "color = float3(0.0,0.0,0.0);" 53 | "return false;" 54 | "}" 55 | "}" 56 | 57 | "float4 raymarch (float3 ro, float3 rd)" 58 | "{" 59 | "float3 color = 0..xxx;" 60 | "for (int i = 0; i < 512; i++)" 61 | "{" 62 | "bool hit = InsideTetrahedron(float3(0.943, 0, -0.333 ),float3( -0.471, 0.816, -0.333), float3( -0.471, -0.816, -0.333), float3(0, 0, 1 ),ro,color);" 63 | "if (hit) return float4(color,1.0);" 64 | "ro += rd * 0.01;" 65 | "}" 66 | "return float4(0,0,0,1);" 67 | "}" 68 | 69 | "[numthreads(8, 8, 1)]" 70 | "void main (uint3 id : SV_DispatchThreadID)" 71 | "{" 72 | "float2 uv = (2.*id.xy - float2(1280, 720)) / 720.0;" 73 | "image[id.xy] = raymarch(float3(0,0.0,-2.5), normalize(float3(uv,2.)));" 74 | "}" 75 | }; 76 | 77 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 78 | { 79 | if ((uMsg == WM_KEYUP && wParam == VK_ESCAPE) || uMsg==WM_CLOSE || uMsg==WM_DESTROY) 80 | { 81 | PostQuitMessage(0); return 0; 82 | } 83 | else 84 | { 85 | return DefWindowProc(hWnd, uMsg, wParam, lParam); 86 | } 87 | } 88 | 89 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 90 | { 91 | int exit = 0; 92 | MSG msg; 93 | WNDCLASS win = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, "DirectX 11"}; 94 | RegisterClass(&win); 95 | HWND hwnd = CreateWindowEx(0, win.lpszClassName, "DirectX 11", WS_VISIBLE|WS_OVERLAPPEDWINDOW, 0, 0, WIDTH, HEIGHT, 0, 0, 0, 0); 96 | ID3D11Device *device; 97 | IDXGISwapChain *surface; 98 | ID3D11DeviceContext *context; 99 | ID3D11Buffer *buffer; 100 | ID3D11UnorderedAccessView *uav; 101 | ID3D11Texture2D *image; 102 | ID3D11ComputeShader *shader; 103 | D3D11_MAPPED_SUBRESOURCE resource; 104 | DXGI_SWAP_CHAIN_DESC sd = {{WIDTH, HEIGHT, 0, 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 0 }, {1, 0}, (1L << (1 + 4)) | (1L << (6 + 4)) | (1L << (0 + 4)), 1, hwnd, 1, 1, 0}; 105 | D3D11CreateDeviceAndSwapChain(0, D3D_DRIVER_TYPE_HARDWARE, 0, 0, 0, 0, D3D11_SDK_VERSION, &sd, &surface, &device, 0, &context); 106 | surface->lpVtbl->GetDesc(surface, &sd); 107 | surface->lpVtbl->GetBuffer(surface, 0, (REFIID) &IID_ID3D11Texture2D, ( LPVOID* )&image ); 108 | D3D11_BUFFER_DESC desc = {16, D3D11_USAGE_DYNAMIC, D3D11_BIND_CONSTANT_BUFFER, D3D11_CPU_ACCESS_WRITE, 0, 0}; 109 | device->lpVtbl->CreateBuffer(device, &desc, NULL, &buffer); 110 | device->lpVtbl->CreateUnorderedAccessView(device,(ID3D11Resource*)image, NULL, &uav ); 111 | ID3DBlob* blob; 112 | D3DCompile(&ComputeShader, sizeof ComputeShader, 0, 0, 0, "main", "cs_5_0", 1 << 15, 0, &blob, 0); 113 | device->lpVtbl->CreateComputeShader(device, blob->lpVtbl->GetBufferPointer(blob), blob->lpVtbl->GetBufferSize(blob), NULL, &shader); 114 | while (!exit) 115 | { 116 | while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 117 | { 118 | if (msg.message == WM_QUIT) exit = 1; 119 | TranslateMessage(&msg); 120 | DispatchMessage(&msg); 121 | } 122 | context->lpVtbl->Map(context,(ID3D11Resource*)buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource); 123 | float time[] = {GetTickCount() * 0.001f}; 124 | memcpy(resource.pData, time, sizeof(float)); 125 | context->lpVtbl->Unmap(context, (ID3D11Resource *)buffer, 0); 126 | context->lpVtbl->CSSetShader(context, shader, NULL, 0 ); 127 | context->lpVtbl->CSSetUnorderedAccessViews(context, 0, 1, &uav, NULL ); 128 | context->lpVtbl->CSSetConstantBuffers(context, 0, 1, &buffer ); 129 | context->lpVtbl->Dispatch(context, WIDTH / 8, HEIGHT / 8, 1 ); 130 | surface->lpVtbl->Present( surface, 0, 0 ); 131 | } 132 | context->lpVtbl->ClearState(context); 133 | device->lpVtbl->Release(device); 134 | surface->lpVtbl->Release(surface); 135 | image->lpVtbl->Release(image); 136 | buffer->lpVtbl->Release(buffer); 137 | uav->lpVtbl->Release(uav); 138 | return 0; 139 | } -------------------------------------------------------------------------------- /MetalAPI/Line.metal: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace metal; 5 | 6 | template 7 | struct spvUnsafeArray 8 | { 9 | T elements[Num ? Num : 1]; 10 | 11 | thread T& operator [] (size_t pos) thread 12 | { 13 | return elements[pos]; 14 | } 15 | constexpr const thread T& operator [] (size_t pos) const thread 16 | { 17 | return elements[pos]; 18 | } 19 | 20 | device T& operator [] (size_t pos) device 21 | { 22 | return elements[pos]; 23 | } 24 | constexpr const device T& operator [] (size_t pos) const device 25 | { 26 | return elements[pos]; 27 | } 28 | 29 | constexpr const constant T& operator [] (size_t pos) const constant 30 | { 31 | return elements[pos]; 32 | } 33 | 34 | threadgroup T& operator [] (size_t pos) threadgroup 35 | { 36 | return elements[pos]; 37 | } 38 | constexpr const threadgroup T& operator [] (size_t pos) const threadgroup 39 | { 40 | return elements[pos]; 41 | } 42 | }; 43 | 44 | constant spvUnsafeArray _TexCoords = spvUnsafeArray({ float2(0.0), float2(1.0, 0.0), float2(0.0, 1.0), float2(1.0, 0.0), float2(1.0), float2(0.0, 1.0) }); 45 | constant spvUnsafeArray _Vertices = spvUnsafeArray({ float3(-1.0, -1.0, 0.0), float3(1.0, -1.0, 0.0), float3(-1.0, 1.0, 0.0), float3(1.0, -1.0, 0.0), float3(1.0, 1.0, 0.0), float3(-1.0, 1.0, 0.0) }); 46 | 47 | struct Vertex 48 | { 49 | float2 uv [[user(locn1)]]; 50 | float4 position [[position]]; 51 | }; 52 | 53 | vertex Vertex VSMain (uint id [[vertex_id]]) 54 | { 55 | Vertex out = {}; 56 | out.uv = _TexCoords[id]; 57 | out.position = float4(_Vertices[id], 1.0); 58 | return out; 59 | } 60 | 61 | static inline __attribute__((always_inline)) 62 | float Line(thread const float2& p, thread const float2& a, thread const float2& b) 63 | { 64 | float2 pa = p - a; 65 | float2 ba = b - a; 66 | float h = fast::max(0.0, fast::min(1.0, dot(pa, ba) / dot(ba, ba))); 67 | float2 d = pa - (ba * h); 68 | return dot(d, d); 69 | } 70 | 71 | fragment float4 PSMain (Vertex in [[stage_in]]) 72 | { 73 | float k = Line(in.uv, float2(0.3, 0.1), float2(0.8, 0.5)); 74 | return mix(float4(1.0), float4(0.0, 0.0, 0.0, 1.0), float4(smoothstep(0.0, 0.00001, k))); 75 | } -------------------------------------------------------------------------------- /OpenCL/README.md: -------------------------------------------------------------------------------- 1 | OpenCL (Open Computing Language) programs. 2 | -------------------------------------------------------------------------------- /OpenCL/Tools/clcc.c: -------------------------------------------------------------------------------- 1 | // Compile: nvcc -o clcc.exe clcc.c -lOpenCL 2 | // Tool for debug OpenCL kernels. Example usage: clcc test.cl 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | cl_int err; 11 | cl_platform_id platform; 12 | if (argc < 2) 13 | { 14 | printf("Usage: ./compile source [DEVICE]\n"); 15 | return 1; 16 | } 17 | char *filename = argv[1]; 18 | int index = 0; 19 | if (argc > 2) 20 | index = atoi(argv[2]); 21 | clGetPlatformIDs(1, &platform, NULL); 22 | cl_uint num; 23 | cl_device_id devices[3]; 24 | clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 3, devices, &num); 25 | if (index >= num) 26 | { 27 | printf("Invalid device index (%d).\n", index); 28 | return 1; 29 | } 30 | cl_device_id device = devices[index]; 31 | char name[256]; 32 | clGetDeviceInfo(device, CL_DEVICE_NAME, 256, name, NULL); 33 | printf("Using device: %s\n", name); 34 | cl_device_fp_config cfg; 35 | clGetDeviceInfo(device, CL_DEVICE_DOUBLE_FP_CONFIG, sizeof(cfg), &cfg, NULL); 36 | printf("Double FP config = %llu\n", cfg); 37 | cl_context context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL); 38 | FILE *file = fopen(filename, "r"); 39 | fseek(file, 0, SEEK_END); 40 | size_t size = ftell(file); 41 | rewind(file); 42 | char *source = (char*) malloc(size + 1); 43 | source[size] = '\0'; 44 | fread(source, sizeof(char), size, file); 45 | fclose(file); 46 | cl_program program = clCreateProgramWithSource(context, 1, (const char**)&source, &size, NULL); 47 | err = clBuildProgram(program, 1, &device, "", NULL, NULL); 48 | if (err == CL_SUCCESS) 49 | { 50 | printf("Program built successfully.\n"); 51 | } 52 | else 53 | { 54 | if (err == CL_BUILD_PROGRAM_FAILURE) 55 | { 56 | size_t sz; 57 | clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &sz); 58 | char *log = malloc(++sz); 59 | clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sz, log, NULL); 60 | printf("%s\n", log); 61 | free(log); 62 | } 63 | else 64 | { 65 | printf("Other build error\n"); 66 | } 67 | } 68 | clReleaseProgram(program); 69 | clReleaseContext(context); 70 | return 0; 71 | } -------------------------------------------------------------------------------- /OpenCL/Tools/clptx.c: -------------------------------------------------------------------------------- 1 | // Compile: nvcc -o clptx.exe clptx.c -lOpenCL 2 | // Tool for generation of PTX assembly from OpenCL C. Example usage: clptx test.cl 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main(int argc, char **argv) 9 | { 10 | cl_device_id device; 11 | cl_platform_id platform; 12 | char *filename = argv[1]; 13 | clGetPlatformIDs(1, &platform, NULL); 14 | clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, &device, NULL); 15 | cl_context context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL); 16 | FILE *file = fopen(filename, "r"); 17 | fseek(file, 0, SEEK_END); 18 | size_t size = ftell(file); 19 | rewind(file); 20 | char *source = (char*) malloc(size + 1); 21 | source[size] = '\0'; 22 | fread(source, sizeof(char), size, file); 23 | fclose(file); 24 | cl_program program = clCreateProgramWithSource(context, 1, (const char**)&source, NULL, NULL); 25 | clBuildProgram(program, 1, &device, "", NULL, NULL); 26 | size_t bsize; 27 | clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t), &bsize, NULL); 28 | char *binary = (char*)malloc(bsize); 29 | clGetProgramInfo(program, CL_PROGRAM_BINARIES, bsize, &binary, NULL); 30 | FILE *f = fopen("output.ptx", "w"); 31 | fwrite(binary, bsize, 1, f); 32 | fclose(f); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /OpenCL/plasma.cl: -------------------------------------------------------------------------------- 1 | // Author: Przemyslaw Zaworski 2 | // nvcc -x cu -o plasma.exe plasma.cl -luser32 -lgdi32 -lOpenCL 3 | 4 | #include 5 | #include 6 | 7 | #define width 1280 8 | #define height 720 9 | 10 | static const char* ComputeKernel = 11 | 12 | "float3 lerp(float3 a, float3 b, float w)" 13 | "{" 14 | "return (float3)(mad(w,b-a,a));" 15 | "}" 16 | 17 | "float3 hash(float2 p)" 18 | "{" 19 | "float3 d = (float3) (p.x * .1031f, p.y * .1030f, p.x * .0973f);" 20 | "float3 p3 = (float3) (d - floor(d));" 21 | "float k = p3.x * (p3.y+19.19f) + p3.y * (p3.x+19.19f) + p3.z * (p3.z+19.19f);" 22 | "p3 = (float3)(p3.x+k, p3.y+k, p3.z+k);" 23 | "float3 q = (float3)((p3.x + p3.y) * p3.z, (p3.x + p3.z) * p3.y, (p3.y + p3.z) * p3.x);" 24 | "return (float3) (q - floor(q));" 25 | "}" 26 | 27 | "float3 noise(float2 p)" 28 | "{" 29 | "float2 ip = floor(p);" 30 | "float2 u = (float2)(p - floor(p));" 31 | "u = (float2)(u.x*u.x*(3.0f-2.0f*u.x), u.y*u.y*(3.0f-2.0f*u.y));" 32 | "float3 res = lerp(lerp(hash(ip),hash((float2)(ip.x+1.0f,ip.y)),u.x), lerp(hash((float2)(ip.x,ip.y+1.0f)),hash((float2)(ip.x+1.0f,ip.y+1.0f)),u.x),u.y);" 33 | "return (float3)(res * res);" 34 | "}" 35 | 36 | "float3 fbm(float2 p) " 37 | "{" 38 | "float3 v = (float3)(0.0f, 0.0f, 0.0f);" 39 | "float3 a = (float3)(0.5f, 0.5f, 0.5f);" 40 | "for (int i = 0; i < 4; ++i)" 41 | "{" 42 | "v = (float3)(v + a * noise(p));" 43 | "p = (float2)((0.87f * p.x -0.48f * p.y) * 2.0f, (0.48f * p.x + 0.87f * p.y) * 2.0f);" 44 | "a = (float3)(a * (float3)(0.5f, 0.5f, 0.5f));" 45 | "}" 46 | "return v;" 47 | "}" 48 | 49 | "float3 pattern (float2 p, float iTime)" 50 | "{" 51 | "float3 q = fbm((float2)(p.x+5.2f, p.y+1.3f));" 52 | "float3 r = fbm((float2)(p.x+4.0f*q.x -iTime*0.5f, p.y+4.0f*q.y+iTime*0.3f));" 53 | "return fbm((float2)(p.x+8.0f*r.x, p.y+8.0f*r.z));" 54 | "}" 55 | 56 | "float2 gradient(float2 uv, float delta, float iTime)" 57 | "{" 58 | "float3 a = pattern((float2)(uv.x + delta, uv.y), iTime);" 59 | "float3 b = pattern((float2)(uv.x - delta, uv.y), iTime);" 60 | "float3 c = pattern((float2)(uv.x, uv.y + delta), iTime);" 61 | "float3 d = pattern((float2)(uv.x, uv.y - delta), iTime);" 62 | "return (float2)(length(a)-length(b), length(c)-length(d))/delta;" 63 | "}" 64 | 65 | "__kernel void mainImage(__global uchar4 *fragColor, float iTime)" 66 | "{" 67 | "unsigned int id = get_global_id(0);" 68 | "int2 iResolution = (int2)(1280, 720);" 69 | "int2 fragCoord = (int2)(id % iResolution.x, id / iResolution.x);" 70 | "float2 uv = (float2)(fragCoord.x / (float)iResolution.x * 3.0f, fragCoord.y / (float)iResolution.y * 3.0f);" 71 | "float3 n = normalize((float3)(gradient(uv,1.0f/(float)iResolution.y,iTime),100.0f));" 72 | "float3 l = normalize((float3)(1.0f,1.0f,2.0f));" 73 | "float s = pow(clamp(-(l.z - 2.0f * n.z * dot(n,l)), 0.0f, 1.0f), 36.0f) * 2.5f;" 74 | "float3 q = (float3)(clamp(pattern(uv,iTime) + (float3)(s, s, s), (float3)(0.0f,0.0f,0.0f), (float3)(1.0f,1.0f,1.0f)));" 75 | "fragColor[id] = (uchar4)(q.z * 255, q.y * 255, q.x * 255, 255);" 76 | "}"; 77 | 78 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 79 | { 80 | if( uMsg==WM_CLOSE || uMsg==WM_DESTROY || (uMsg==WM_KEYDOWN && wParam==VK_ESCAPE) ) 81 | { 82 | PostQuitMessage(0); return 0; 83 | } 84 | else 85 | { 86 | return DefWindowProc(hWnd, uMsg, wParam, lParam); 87 | } 88 | } 89 | 90 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 91 | { 92 | int exit = 0; 93 | MSG msg; 94 | WNDCLASS win = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, "OpenCL Demo"}; 95 | RegisterClass(&win); 96 | HDC hdc = GetDC(CreateWindowEx(0, win.lpszClassName, "OpenCL Demo", WS_VISIBLE|WS_OVERLAPPEDWINDOW, 0, 0, width, height, 0, 0, 0, 0)); 97 | const BITMAPINFO bmi = { {sizeof(BITMAPINFOHEADER),width,height,1,32,BI_RGB,0,0,0,0,0},{0,0,0,0} }; 98 | unsigned char* host = (unsigned char*) malloc(width*height*sizeof(uchar4)); 99 | size_t bytes; 100 | cl_uint platformCount; 101 | clGetPlatformIDs(0, 0, &platformCount); 102 | cl_platform_id* platforms = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformCount); 103 | clGetPlatformIDs(platformCount, platforms, 0); 104 | cl_context_properties properties[] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[0], 0}; 105 | cl_context context = clCreateContextFromType (properties, CL_DEVICE_TYPE_GPU, 0, 0, 0); 106 | clGetContextInfo (context, CL_CONTEXT_DEVICES, 0, 0, &bytes); 107 | cl_device_id* info = (cl_device_id*) malloc (bytes); 108 | clGetContextInfo (context, CL_CONTEXT_DEVICES, bytes, info, 0); 109 | cl_program program = clCreateProgramWithSource (context, 1, &ComputeKernel, 0, 0); 110 | clBuildProgram (program, 0, 0, 0, 0, 0); 111 | cl_kernel kernel = clCreateKernel (program, "mainImage", 0); 112 | cl_command_queue queue = clCreateCommandQueue (context, info[0], 0, 0); 113 | cl_mem buffer = clCreateBuffer (context, CL_MEM_WRITE_ONLY, width*height*sizeof(uchar4), 0, 0); 114 | clSetKernelArg (kernel, 0, sizeof(cl_mem), (void*)&buffer); 115 | size_t size = width * height; 116 | while (!exit) 117 | { 118 | while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 119 | { 120 | if( msg.message==WM_QUIT ) exit = 1; 121 | TranslateMessage( &msg ); 122 | DispatchMessage( &msg ); 123 | } 124 | float time = GetTickCount()*0.001f; 125 | clSetKernelArg (kernel, 1, sizeof(cl_float), (void*)&time); 126 | clEnqueueNDRangeKernel(queue,kernel, 1, 0, &size, 0, 0, 0, 0); 127 | clEnqueueReadBuffer (queue, buffer, 0, 0, width*height*sizeof(uchar4), host, 0, 0, 0); 128 | StretchDIBits(hdc,0,0,width,height,0,0,width,height,host,&bmi,DIB_RGB_COLORS,SRCCOPY); 129 | } 130 | return 0; 131 | } -------------------------------------------------------------------------------- /OpenCL/waves.cl: -------------------------------------------------------------------------------- 1 | // Author: Przemyslaw Zaworski 2 | // nvcc -x cu -o waves.exe waves.cl -luser32 -lgdi32 -lOpenCL 3 | // ToDo: Correct order in RGBA components. To test, check: "fragColor[id] = (uchar4)(255, 0, 0, 255);" (should be red, not blue) 4 | 5 | #include 6 | #include 7 | 8 | #define width 1280 9 | #define height 720 10 | 11 | static const char* ComputeKernel = 12 | "__kernel void mainImage(__global uchar4 *fragColor, float iTime)" 13 | "{" 14 | "unsigned int id = get_global_id(0);" 15 | "int2 iResolution = (int2)(1280, 720);" 16 | "int2 fragCoord = (int2)(id % iResolution.x, id / iResolution.x);" 17 | "float2 uv = (float2)(fragCoord.x / (float)iResolution.x * 8.0f, fragCoord.y / (float)iResolution.y * 8.0f);" 18 | "for(int j=1; j<4; j++)" 19 | "{" 20 | "uv.x += sin(iTime + uv.y * (float)j);" 21 | "uv.y += cos(iTime + uv.x * (float)j);" 22 | "}" 23 | "float3 q = (float3)(0.5+0.5*cos(4.0+uv.x), 0.5+0.5*cos(4.0+uv.y+2.0), 0.5+0.5*cos(4.0+uv.x+4.0));" 24 | "fragColor[id] = (uchar4)(q.x * 255, q.y * 255, q.z * 255, 255);" 25 | "}"; 26 | 27 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 28 | { 29 | if (uMsg == WM_KEYUP && wParam == VK_ESCAPE) 30 | { 31 | PostQuitMessage(0); 32 | return 0; 33 | } 34 | else 35 | { 36 | return DefWindowProc(hWnd, uMsg, wParam, lParam); 37 | } 38 | } 39 | 40 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 41 | { 42 | int exit = 0; 43 | MSG msg; 44 | WNDCLASS win = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, "OpenCL Demo"}; 45 | RegisterClass(&win); 46 | HWND hwnd = CreateWindowEx(0, win.lpszClassName, "OpenCL Demo", WS_VISIBLE|WS_OVERLAPPEDWINDOW, 0, 0, width, height, 0, 0, 0, 0); 47 | const BITMAPINFO bmi = { {sizeof(BITMAPINFOHEADER),width,height,1,32,BI_RGB,0,0,0,0,0},{0,0,0,0} }; 48 | unsigned char* host = (unsigned char*) malloc(width*height*sizeof(uchar4)); 49 | size_t bytes; 50 | cl_uint platformCount; 51 | clGetPlatformIDs(0, 0, &platformCount); 52 | cl_platform_id* platforms = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformCount); 53 | clGetPlatformIDs(platformCount, platforms, 0); 54 | cl_context_properties properties[] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[0], 0}; 55 | cl_context context = clCreateContextFromType (properties, CL_DEVICE_TYPE_GPU, 0, 0, 0); 56 | clGetContextInfo (context, CL_CONTEXT_DEVICES, 0, 0, &bytes); 57 | cl_device_id* info = (cl_device_id*) malloc (bytes); 58 | clGetContextInfo (context, CL_CONTEXT_DEVICES, bytes, info, 0); 59 | cl_program program = clCreateProgramWithSource (context, 1, &ComputeKernel, 0, 0); 60 | clBuildProgram (program, 0, 0, 0, 0, 0); 61 | cl_kernel kernel = clCreateKernel (program, "mainImage", 0); 62 | cl_command_queue queue = clCreateCommandQueue (context, info[0], 0, 0); 63 | cl_mem buffer = clCreateBuffer (context, CL_MEM_WRITE_ONLY, width*height*sizeof(uchar4), 0, 0); 64 | clSetKernelArg (kernel, 0, sizeof(cl_mem), (void*)&buffer); 65 | size_t size = width * height; 66 | while (!exit) 67 | { 68 | while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 69 | { 70 | if( msg.message==WM_QUIT ) exit = 1; 71 | TranslateMessage( &msg ); 72 | DispatchMessage( &msg ); 73 | } 74 | float time = GetTickCount()*0.001f; 75 | clSetKernelArg (kernel, 1, sizeof(cl_float), (void*)&time); 76 | clEnqueueNDRangeKernel(queue,kernel, 1, 0, &size, 0, 0, 0, 0); 77 | clEnqueueReadBuffer (queue, buffer, CL_TRUE, 0, width*height*sizeof(uchar4), host, 0, 0, 0); 78 | StretchDIBits(GetDC(hwnd),0,0,width,height,0,0,width,height,host,&bmi,DIB_RGB_COLORS,SRCCOPY); 79 | } 80 | return 0; 81 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Various C/C++ examples. DirectX, OpenGL, CUDA, Vulkan, OpenCL. 2 | -------------------------------------------------------------------------------- /Tools/BaseConverter.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | char* BaseConverter(int number, int baseFrom, int baseTo) 5 | { 6 | int length = 32; 7 | char* result = (char*)malloc(length * sizeof(char)); 8 | int decimal = 0; 9 | int power = 1; 10 | while (number > 0) 11 | { 12 | int digit = number % 10; 13 | decimal += digit * power; 14 | power *= baseFrom; 15 | number /= 10; 16 | } 17 | int index = 0; 18 | do 19 | { 20 | if (index >= length - 1) 21 | { 22 | length *= 2; 23 | result = (char*)realloc(result, length * sizeof(char)); 24 | } 25 | int remainder = decimal % baseTo; 26 | result[index++] = (remainder < 10) ? remainder + '0' : (remainder - 10) + 'A'; 27 | decimal /= baseTo; 28 | } while (decimal > 0); 29 | result[index] = '\0'; 30 | int start = 0; 31 | int end = index - 1; 32 | while (start < end) 33 | { 34 | char temp = result[start]; 35 | result[start] = result[end]; 36 | result[end] = temp; 37 | start++; 38 | end--; 39 | } 40 | return result; 41 | } 42 | 43 | int main() 44 | { 45 | int number, baseFrom, baseTo; 46 | printf("Enter the number to convert: "); 47 | scanf("%d", &number); 48 | printf("Enter the base of the number: "); 49 | scanf("%d", &baseFrom); 50 | printf("Enter the base to convert to: "); 51 | scanf("%d", &baseTo); 52 | char* output = BaseConverter(number, baseFrom, baseTo); 53 | printf("Converted number: %s\n", output); 54 | free(output); 55 | return 0; 56 | } -------------------------------------------------------------------------------- /Tools/ImageToArray.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void ImageToArray (char *ppm, char *header, char *name) 5 | { 6 | FILE *fileWrite = fopen(header, "w"); 7 | int width, height; 8 | char buffer[128]; 9 | FILE *fileRead = fopen(ppm, "rb"); 10 | fgets(buffer, sizeof(buffer), fileRead); 11 | do fgets(buffer, sizeof (buffer), fileRead); while (buffer[0] == '#'); 12 | sscanf (buffer, "%d %d", &width, &height); 13 | do fgets (buffer, sizeof (buffer), fileRead); while (buffer[0] == '#'); 14 | int size = width * height * 4 * sizeof(unsigned char); 15 | unsigned char *Texels = (unsigned char*)malloc(size); 16 | fprintf(fileWrite, "unsigned char %s[] = {", name); 17 | for (int i = 0; i < size; i++) 18 | { 19 | Texels[i] = ((i % 4) < 3 ) ? (unsigned char) fgetc(fileRead) : (unsigned char) 255 ; 20 | fprintf(fileWrite, "%d,", Texels[i]); 21 | if ( (i+1) % 50 == 0) fprintf(fileWrite, "\n", Texels[i]); 22 | } 23 | fprintf(fileWrite, "};"); 24 | printf("Image resolution: %d x %d", width, height); 25 | free(Texels); 26 | fclose(fileRead); 27 | fclose(fileWrite); 28 | } 29 | 30 | int main(int argc, char **argv) 31 | { 32 | ImageToArray ("heightmap.ppm", "heightmap.h", "heightmap"); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Tools/MapToArray.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | float* LoadMapFromFile (const char* filename, int size) 5 | { 6 | FILE* binFile = fopen(filename,"rb"); 7 | float* source = (float*) malloc(size*sizeof(float)); 8 | fread(source, sizeof(float), size, binFile); 9 | fclose(binFile); 10 | return source; 11 | } 12 | 13 | void MapToArray (char *bin, char *header, char *name, int trianglecount) 14 | { 15 | FILE *fileWrite = fopen(header, "w"); 16 | float* source = LoadMapFromFile ("uvmap.bin", trianglecount * 2); 17 | fprintf(fileWrite, "float %s[] = {", name); 18 | for (int i = 0; i < (trianglecount * 2); i++) 19 | { 20 | fprintf(fileWrite, "%.3f,", source[i]); 21 | if ( (i+1) % 20 == 0) fprintf(fileWrite, "\n", source[i]); 22 | } 23 | fprintf(fileWrite, "};"); 24 | free (source); 25 | fclose(fileWrite); 26 | } 27 | 28 | int main(int argc, char **argv) 29 | { 30 | MapToArray("uvmap.bin", "uv.h", "uvarray", 1572867); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Tools/TriangleArea.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | typedef struct 7 | { 8 | float* vertices; 9 | float area; 10 | } Triangle; 11 | 12 | float TriangleArea(const float* vertices) 13 | { 14 | float abx = vertices[3] - vertices[0]; 15 | float aby = vertices[4] - vertices[1]; 16 | float abz = vertices[5] - vertices[2]; 17 | float acx = vertices[6] - vertices[0]; 18 | float acy = vertices[7] - vertices[1]; 19 | float acz = vertices[8] - vertices[2]; 20 | float crossx = aby * acz - abz * acy; 21 | float crossy = abz * acx - abx * acz; 22 | float crossz = abx * acy - aby * acx; 23 | return sqrt(crossx * crossx + crossy * crossy + crossz * crossz) * 0.5f; 24 | } 25 | 26 | void WriteToFile(const char* filename, const Triangle* triangle) 27 | { 28 | FILE* file = fopen(filename, "wb"); 29 | fwrite(triangle->vertices, sizeof(float), 9, file); 30 | fwrite(&triangle->area, sizeof(float), 1, file); 31 | fclose(file); 32 | } 33 | 34 | void ReadFromFile(const char* filename, Triangle* triangle) 35 | { 36 | FILE* file = fopen(filename, "rb"); 37 | triangle->vertices = (float*)malloc(9 * sizeof(float)); 38 | fread(triangle->vertices, sizeof(float), 9, file); 39 | fread(&triangle->area, sizeof(float), 1, file); 40 | fclose(file); 41 | } 42 | 43 | void WriteData() 44 | { 45 | Triangle triangle; 46 | triangle.vertices = (float*)malloc(9 * sizeof(float)); 47 | float vertices[9] = {0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f}; 48 | memcpy(triangle.vertices, vertices, 9 * sizeof(float)); 49 | triangle.area = TriangleArea(triangle.vertices); 50 | printf("The area of the triangle is: %f\n", triangle.area); 51 | WriteToFile("triangle.dat", &triangle); 52 | free(triangle.vertices); 53 | } 54 | 55 | void ReadData() 56 | { 57 | Triangle triangle; 58 | ReadFromFile("triangle.dat", &triangle); 59 | printf("The area of the triangle is: %f\n", triangle.area); 60 | free(triangle.vertices); 61 | } 62 | 63 | int main() 64 | { 65 | WriteData(); 66 | ReadData(); 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /Tools/generate_html.cpp: -------------------------------------------------------------------------------- 1 | //Windows: g++ -o generate_html.exe generate_html.cpp 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | fstream html_file("index.html",std::ios::out); 10 | string content = "Hello World!"; 11 | html_file.write(&content[0],content.length() ); 12 | html_file.close(); 13 | system("index.html"); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Tools/list.c: -------------------------------------------------------------------------------- 1 | /* Output: 2 | 4.000000 3 | 11.000000 4 | -123.500000 5 | ---------------------------------------------- 6 | 11.000000 7 | -123.500000 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | struct List 14 | { 15 | size_t size; 16 | float* elements; 17 | }; 18 | 19 | void Add(struct List* list, float value) 20 | { 21 | list->size++; 22 | list->elements = realloc(list->elements, list->size * sizeof(float)); 23 | list->elements[list->size - 1] = value; 24 | } 25 | 26 | void RemoveAt(struct List* list, size_t index) 27 | { 28 | if (index >= list->size) return; 29 | for (int i=index; isize; i++) list->elements[i] = list->elements[i+1]; 30 | list->size--; 31 | list->elements = realloc(list->elements, list->size * sizeof(float)); 32 | } 33 | 34 | int main(void) 35 | { 36 | struct List list = {0, NULL}; 37 | Add(&list, 4.0f); 38 | Add(&list, 11.0f); 39 | Add(&list, -123.5f); 40 | for (int i=0; i 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | static string conversion (int n, int p, int w) 11 | { 12 | string number = to_string(n); 13 | int d = 1; 14 | int k = 0; 15 | for (int i=number.length(); i>0; --i) 16 | { 17 | int x = number[i-1]-'0'; 18 | k += (x*d); 19 | d *= p; 20 | } 21 | string r=""; 22 | int t,z; 23 | while (true) 24 | { 25 | r = to_string(k%w)+r; 26 | k = int(trunc(k/w)); 27 | if (k==0) break; 28 | } 29 | return r; 30 | } 31 | 32 | int main() 33 | { 34 | string output = conversion(21013,5,9); 35 | cout << output; 36 | } 37 | -------------------------------------------------------------------------------- /Tools/ppm.c: -------------------------------------------------------------------------------- 1 | // cl.exe ppm.c user32.lib gdi32.lib shell32.lib 2 | #include 3 | #include 4 | #include 5 | 6 | static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 7 | { 8 | if ((uMsg == WM_KEYUP && wParam == VK_ESCAPE) || uMsg==WM_CLOSE || uMsg==WM_DESTROY) 9 | { 10 | PostQuitMessage(0); return 0; 11 | } 12 | else 13 | { 14 | return DefWindowProc(hWnd, uMsg, wParam, lParam); 15 | } 16 | } 17 | 18 | int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 19 | { 20 | AllocConsole(); 21 | freopen("CONOUT$", "w+", stdout); 22 | int argc; 23 | char** argv; 24 | LPWSTR* lpArgv = CommandLineToArgvW( GetCommandLineW(), &argc ); 25 | argv = (char**)malloc( argc*sizeof(char*) ); 26 | int s = 0; 27 | for( int i=0; i < argc; ++i ) 28 | { 29 | s = wcslen( lpArgv[i] ) + 1; 30 | argv[i] = (char*)malloc( s ); 31 | wcstombs( argv[i], lpArgv[i], s ); 32 | } 33 | if (argc != 2) 34 | { 35 | ExitProcess(0); 36 | } 37 | LocalFree(lpArgv); 38 | ShowWindow(GetConsoleWindow(), SW_HIDE); 39 | FreeConsole(); 40 | 41 | int width, height; 42 | char buffer[128]; 43 | FILE *file = fopen(argv[1], "rb"); 44 | fgets(buffer, sizeof(buffer), file); 45 | do fgets(buffer, sizeof (buffer), file); while (buffer[0] == '#'); 46 | sscanf (buffer, "%d %d", &width, &height); 47 | do fgets (buffer, sizeof (buffer), file); while (buffer[0] == '#'); 48 | int size = width * height * 4 * sizeof(unsigned char); 49 | unsigned char *pixels = (unsigned char *)malloc(size); 50 | int counter = 0; 51 | for (int i = 0; i < (size/4); i++) 52 | { 53 | unsigned char r = (unsigned char) fgetc(file); 54 | unsigned char g = (unsigned char) fgetc(file); 55 | unsigned char b = (unsigned char) fgetc(file); 56 | unsigned char a = (unsigned char) 255; 57 | pixels[counter] = b; 58 | pixels[counter+1] = g; 59 | pixels[counter+2] = r; 60 | pixels[counter+3] = a; 61 | counter += 4; 62 | } 63 | fclose(file); 64 | 65 | int exit = 0; 66 | MSG msg; 67 | WNDCLASS win = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW, WindowProc, 0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW+1), 0, "PPM Viewer"}; 68 | RegisterClass(&win); 69 | HWND hwnd = CreateWindowEx(0, win.lpszClassName, "PPM Viewer", WS_VISIBLE|WS_OVERLAPPEDWINDOW, 0, 0, width, height+40, 0, 0, 0, 0); 70 | HDC hdc = GetDC(hwnd); 71 | BITMAPINFO bmi = {{sizeof(BITMAPINFOHEADER),width,height,1,32,BI_RGB,0,0,0,0,0},{0,0,0,0}}; 72 | while (!exit) 73 | { 74 | while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 75 | { 76 | if( msg.message==WM_QUIT ) exit = 1; 77 | TranslateMessage( &msg ); 78 | DispatchMessage( &msg ); 79 | } 80 | if (hwnd == GetActiveWindow()) 81 | StretchDIBits(hdc,0,height,width,-height,0,0,width,height,pixels,&bmi,DIB_RGB_COLORS,SRCCOPY); 82 | } 83 | free(pixels); 84 | free(argv); 85 | return 0; 86 | } -------------------------------------------------------------------------------- /Tools/registry.cpp: -------------------------------------------------------------------------------- 1 | //Basic read and write from Windows Registry. Be careful ! You modify the registry at your own risk ! 2 | //Przemyslaw Zaworski, 2017 3 | 4 | #include 5 | #include 6 | 7 | void registry_read (LPCTSTR subkey,LPCTSTR name,DWORD type) 8 | { 9 | HKEY key; 10 | TCHAR value[255]; 11 | DWORD value_length = 255; 12 | RegOpenKey(HKEY_LOCAL_MACHINE,subkey,&key); 13 | RegQueryValueEx(key,name, NULL, &type, (LPBYTE)&value, &value_length); 14 | RegCloseKey(key); 15 | std::cout <