1 /**
2 Copyright (c) 2018 Clipsey (clipseypone@gmail.com)
3 
4 Permission is hereby granted, free of charge, to any person or organization
5 obtaining a copy of the software and accompanying documentation covered by
6 this license (the "Software") to use, reproduce, display, distribute,
7 execute, and transmit the Software, and to prepare derivative works of the
8 Software, and to permit third-parties to whom the Software is furnished to
9 do so, all subject to the following:
10 
11 The copyright notices in the Software and this entire statement, including
12 the above license grant, this restriction and the following disclaimer,
13 must be included in all copies of the Software, in whole or in part, and
14 all derivative works of the Software, unless such copies or derivative
15 works are solely in the form of machine-executable object code generated by
16 a source language processor.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
21 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
22 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
23 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
25 */
26 module ppc.backend.loaders.shader.psgl;
27 import ppc.backend.cfile;
28 import ppc.types.shader;
29 import ppc.backend.signatures;
30 /**
31     Polyplex Split GLSL
32     A simple GLSL format combining GLSL shaders in to one file.
33 */
34 
35 ///
36 void loadPSGL(MemFile file, Shader* shader) {
37     import std.stdio;
38     GLSLShader[ShaderType] shaderCode;
39     size_t index;
40     size_t length = file.length-WritableFileSigs.ShaderPSGL.length;
41 
42     // Seek to data beginning
43     file.seek(&file, WritableFileSigs.ShaderPSGL.length, SeekStart);
44     while(index < length) {
45         ShaderType currentShaderType;
46         uint shaderLength;
47         ubyte[] shaderData;
48 
49         // Read data into temp variables
50         index += file.read(&currentShaderType,  ShaderType.sizeof,  1,              &file);
51         index += file.read(&shaderLength,       uint.sizeof,        1,              &file);
52 
53         // Ensure array is big enough
54         shaderData.length = shaderLength;
55         index += file.read(shaderData.ptr,      ubyte.sizeof,       shaderLength,   &file);
56 
57         shaderCode[currentShaderType] = GLSLShader(shaderData);
58     }
59     shader.shaders = shaderCode;
60 }
61 
62 /// 
63 ubyte[] savePSGL(Shader shader) {
64     ubyte[] oArr = new ubyte[1];
65     MemFile mf = MemFile(oArr.ptr, oArr.length);
66     mf.write(WritableFileSigs.ShaderPSGL.ptr, ubyte.sizeof, WritableFileSigs.ShaderPSGL.length, &mf);
67     foreach(typ, shd; shader.shaders) {
68         
69         // Enforce proper EOF.
70         ubyte[] code = shd.code;
71         if (typ != ShaderType.Compiled) {
72             if (code[$-2..$] != [0x0a, 0x00]) {
73                 code ~= [0x0a, 0x00];
74             }
75         }
76 
77         // Write it down.
78         size_t len = code.length;
79         mf.write(&typ, ShaderType.sizeof, 1, &mf);
80         mf.write(&len, uint.sizeof, 1, &mf);
81         mf.write(code.ptr, ubyte.sizeof, len, &mf);
82     }
83     return mf.toArray();
84 }