Przeglądaj źródła

gpu: allow to load a .lolfx file instead of all those shaders.

legacy
Sam Hocevar sam 12 lat temu
rodzic
commit
a606290ed2
7 zmienionych plików z 111 dodań i 24 usunięć
  1. +1
    -1
      src/Makefile.am
  2. +59
    -0
      src/gpu/shader.cpp
  3. +1
    -0
      src/gpu/shader.h
  4. +3
    -23
      test/tutorial/01_triangle.cpp
  5. +35
    -0
      test/tutorial/01_triangle.lolfx
  6. +9
    -0
      test/tutorial/Makefile.am
  7. +3
    -0
      win32/01_triangle.vcxproj

+ 1
- 1
src/Makefile.am Wyświetl plik

@@ -54,7 +54,7 @@ SUFFIXES = .lolfx
%.lolfx.cpp: %.lolfx
echo "/* This file was autogenerated. DO NOT MODIFY IT. */" > $@.tmp
echo "char const *lolfx_$(notdir $(^:%.lolfx=%)) =" >> $@.tmp
$(SED) 's/"/\\"/g' $^ | $(SED) 's/.*/"&"/' >> $@.tmp
$(SED) 's/"/\\"/g' $^ | $(SED) 's/.*/"&\\n"/' >> $@.tmp
echo ";" >> $@.tmp
mv $@.tmp $@



+ 59
- 0
src/gpu/shader.cpp Wyświetl plik

@@ -82,6 +82,65 @@ int ShaderData::nshaders = 0;
* Public Shader class
*/

Shader *Shader::Create(char const *lolfx)
{
char *src = new char[strlen(lolfx) + 2];
memcpy(src + 1, lolfx, strlen(lolfx) + 1);
src[0] = '\n';

/* Parse the crap */
Array<char const *, char const *> sections;
char *key = NULL;
for (char *parser = src; *parser; )
{
if (key == NULL && (parser[0] == '\n' || parser[0] == '\r')
&& parser[1] == '-' && parser[2] == '-' && parser[3] == ' ')
{
*parser = '\0';
parser += 4;
key = parser;
}
else if (key && parser[0] == ' ')
{
*parser++ = '\0';
}
else if (key && (parser[0] == '\n' || parser[0] == '\r'))
{
sections.Push(key, parser);
parser++;
key = NULL;
}
else
{
parser++;
}
}

char const *vert = NULL, *frag = NULL;
for (int i = 0; i < sections.Count(); i++)
{
#if !defined __CELLOS_LV2__ && !defined _XBOX && !defined USE_D3D9
if (!strcmp(sections[i].m1, "GLSL.Vert"))
vert = sections[i].m2;
if (!strcmp(sections[i].m1, "GLSL.Frag"))
frag = sections[i].m2;
#else
if (!strcmp(sections[i].m1, "HLSL.Vert"))
vert = sections[i].m2;
if (!strcmp(sections[i].m1, "HLSL.Frag"))
frag = sections[i].m2;
#endif
}

Shader *ret = NULL;
if (vert && frag)
ret = Create(vert, frag);

delete[] src;

return ret;
}

Shader *Shader::Create(char const *vert, char const *frag)
{
uint32_t new_vert_crc = Hash::Crc32(vert);


+ 1
- 0
src/gpu/shader.h Wyświetl plik

@@ -51,6 +51,7 @@ class ShaderData;
class Shader
{
public:
static Shader *Create(char const *lolfx);
static Shader *Create(char const *vert, char const *frag);
static void Destroy(Shader *shader);



+ 3
- 23
test/tutorial/01_triangle.cpp Wyświetl plik

@@ -31,6 +31,8 @@ using namespace lol;
# include <direct.h>
#endif

extern char const *lolfx_01_triangle;

class Triangle : public WorldEntity
{
public:
@@ -48,29 +50,7 @@ public:

if (!m_ready)
{
m_shader = Shader::Create(
#if !defined __CELLOS_LV2__ && !defined _XBOX && !defined USE_D3D9
"#version 120\n"
"attribute vec2 in_Position;"
"void main(void) {"
" gl_Position = vec4(in_Position, 0.0, 1.0);"
"}",

"#version 120\n"
"void main(void) {"
" gl_FragColor = vec4(0.7, 0.2, 0.5, 1.0);"
"}"
#else
"void main(float2 in_Position : POSITION,"
" out float4 out_Position : POSITION) {"
" out_Position = float4(in_Position, 0.0, 1.0);"
"}",

"void main(out float4 out_FragColor : COLOR) {"
" out_FragColor = float4(0.7, 0.2, 0.5, 1.0);"
"}"
#endif
);
m_shader = Shader::Create(lolfx_01_triangle);
m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);

m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));


+ 35
- 0
test/tutorial/01_triangle.lolfx Wyświetl plik

@@ -0,0 +1,35 @@
-- GLSL.Vert --

#version 120

attribute vec2 in_Position;

void main(void)
{
gl_Position = vec4(in_Position, 0.0, 1.0);
}

-- GLSL.Frag --

#version 120

void main(void)
{
gl_FragColor = vec4(0.7, 0.2, 0.5, 1.0);
}

-- HLSL.Vert --

void main(float2 in_Position : POSITION,
out float4 out_Position : POSITION)
{
out_Position = float4(in_Position, 0.0, 1.0);
}

-- HLSL.Frag --

void main(out float4 out_FragColor : COLOR)
{
out_FragColor = float4(0.7, 0.2, 0.5, 1.0);
}


+ 9
- 0
test/tutorial/Makefile.am Wyświetl plik

@@ -10,9 +10,18 @@ CLEANFILES = $(noinst_PROGRAMS:%$(EXEEXT)=%.self) \
$(noinst_PROGRAMS:%$(EXEEXT)=%.elf) \
$(noinst_PROGRAMS:%$(EXEEXT)=%.exe)

SUFFIXES = .lolfx
%.lolfx.cpp: %.lolfx
echo "/* This file was autogenerated. DO NOT MODIFY IT. */" > $@.tmp
echo "char const *lolfx_$(notdir $(^:%.lolfx=%)) =" >> $@.tmp
$(SED) 's/"/\\"/g' $^ | $(SED) 's/.*/"&\\n"/' >> $@.tmp
echo ";" >> $@.tmp
mv $@.tmp $@

noinst_PROGRAMS = 01_triangle 02_cube 03_fractal

01_triangle_SOURCES = 01_triangle.cpp
nodist_01_triangle_SOURCES = 01_triangle.lolfx.cpp
01_triangle_CPPFLAGS = @LOL_CFLAGS@ @PIPI_CFLAGS@
01_triangle_LDFLAGS = $(top_builddir)/src/liblol.a @LOL_LIBS@ @PIPI_LIBS@
01_triangle_DEPENDENCIES = $(top_builddir)/src/liblol.a


+ 3
- 0
win32/01_triangle.vcxproj Wyświetl plik

@@ -29,6 +29,9 @@
<ItemGroup>
<ClCompile Include="..\test\tutorial\01_triangle.cpp" />
</ItemGroup>
<ItemGroup>
<LolFxCompile Include="..\test\tutorial\01_triangle.lolfx" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="lolcore.vcxproj">
<Project>{9e62f2fe-3408-4eae-8238-fd84238ceeda}</Project>


Ładowanie…
Anuluj
Zapisz