diff options
author | David Li <davidxli@google.com> | 2011-02-03 17:00:20 -0800 |
---|---|---|
committer | David Li <davidxli@google.com> | 2011-02-03 17:00:20 -0800 |
commit | fee3eee644164b2dc85a5dcf654ce75367f32a4e (patch) | |
tree | f20db837aac113e06835751df4457793fd3ced23 | |
parent | ed77b4a192d693bc2697c5d99e0f4c8db81ccbf5 (diff) |
Checkpoint: work on test and debugging
-rw-r--r-- | include/pixelflinger2/pixelflinger2_interface.h | 2 | ||||
-rw-r--r-- | include/pixelflinger2/pixelflinger2_vector4.h | 18 | ||||
-rw-r--r-- | src/glsl/ir_to_llvm.cpp | 5 | ||||
-rw-r--r-- | src/glsl/linker.cpp | 4 | ||||
-rw-r--r-- | src/pixelflinger2/llvm_helper.h | 8 | ||||
-rw-r--r-- | src/pixelflinger2/shader.cpp | 69 | ||||
-rw-r--r-- | test/cmain.c | 164 | ||||
-rw-r--r-- | test/main.cpp | 7 |
8 files changed, 253 insertions, 24 deletions
diff --git a/include/pixelflinger2/pixelflinger2_interface.h b/include/pixelflinger2/pixelflinger2_interface.h index d40e271..4a23ce2 100644 --- a/include/pixelflinger2/pixelflinger2_interface.h +++ b/include/pixelflinger2/pixelflinger2_interface.h @@ -146,7 +146,7 @@ struct GGLInterface { gl_shader_t * (* ShaderCreate)(const GGLInterface_t * iface, GLenum type); // compiles a shader given glsl; returns GL_TRUE on success; glsl only used during call; use infoLog to retrieve status GLboolean (* ShaderCompile)(const GGLInterface_t * iface, gl_shader_t * shader, - const char * glsl, char ** infoLog); + const char * glsl, const char ** infoLog); // could be used after link if original shaders will not be linked in another program void (* ShaderDelete)(const GGLInterface_t * iface, gl_shader_t * shader); diff --git a/include/pixelflinger2/pixelflinger2_vector4.h b/include/pixelflinger2/pixelflinger2_vector4.h index a77bda8..4f10663 100644 --- a/include/pixelflinger2/pixelflinger2_vector4.h +++ b/include/pixelflinger2/pixelflinger2_vector4.h @@ -192,8 +192,24 @@ typedef struct Vec4<VectorComp_t> Vector4; #else // #ifdef __cplusplus -typedef float Vector4 [4]; +//typedef float Vector4 [4]; +typedef struct { float x, y, z, w; } Vector4; +#define VECTOR4_OP_UNARY(v,op,s) \ + v.x op s.x; \ + v.y op s.y; \ + v.z op s.z; \ + v.w op s.w; + +#define VECTOR4_OP_UNARY_SCALAR(v,op,s) \ + v.x op s; \ + v.y op s; \ + v.z op s; \ + v.w op s; + +#define VECTOR4_CTR(x,y,z,w) \ + ((Vector4){x,y,z,w}) + #endif // #ifdef __cplusplus #endif // #ifndef _PIXELFLINGER2_VECTOR4_H_
\ No newline at end of file diff --git a/src/glsl/ir_to_llvm.cpp b/src/glsl/ir_to_llvm.cpp index c069712..bb0c515 100644 --- a/src/glsl/ir_to_llvm.cpp +++ b/src/glsl/ir_to_llvm.cpp @@ -173,7 +173,12 @@ public: linkage = llvm::GlobalValue::ExternalLinkage; llvm::Constant* init = 0; if(var->constant_value) + { init = llvm_constant(var->constant_value); + // this constants need to be external (ie. written to output) + if (llvm::GlobalValue::ExternalLinkage == linkage) + linkage = llvm::GlobalValue::AvailableExternallyLinkage; + } else if(linkage == llvm::GlobalValue::InternalLinkage) init = llvm::UndefValue::get(llvm_type(var->type)); v = new llvm::GlobalVariable(*mod, type, var->read_only, linkage, init, var->name); diff --git a/src/glsl/linker.cpp b/src/glsl/linker.cpp index 5aecb86..1f0359c 100644 --- a/src/glsl/linker.cpp +++ b/src/glsl/linker.cpp @@ -1707,8 +1707,8 @@ link_shaders(struct gl_context *ctx, struct gl_shader_program *prog) } if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) { - demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX], - ir_var_out); + //demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX], + // ir_var_out); for testing, don't demote } if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) { diff --git a/src/pixelflinger2/llvm_helper.h b/src/pixelflinger2/llvm_helper.h index 3ad083b..5a7ca1d 100644 --- a/src/pixelflinger2/llvm_helper.h +++ b/src/pixelflinger2/llvm_helper.h @@ -89,11 +89,11 @@ static Value * constFloatVec(IRBuilder<> & builder, float x, float y, float z, f static std::vector<Value *> extractVector(IRBuilder<> & builder, Value *vec) { + const VectorType * type = (const VectorType *)vec->getType(); std::vector<Value*> elems(4); - elems[0] = builder.CreateExtractElement(vec, builder.getInt32(0), name("x")); - elems[1] = builder.CreateExtractElement(vec, builder.getInt32(1), name("y")); - elems[2] = builder.CreateExtractElement(vec, builder.getInt32(2), name("z")); - elems[3] = builder.CreateExtractElement(vec, builder.getInt32(3), name("w")); + assert(type->getNumElements() <= 4); + for (unsigned i = 0; i < type->getNumElements(); i++) + elems[i] = builder.CreateExtractElement(vec, builder.getInt32(i), name("xtract")); return elems; } diff --git a/src/pixelflinger2/shader.cpp b/src/pixelflinger2/shader.cpp index c36c796..fc75a1c 100644 --- a/src/pixelflinger2/shader.cpp +++ b/src/pixelflinger2/shader.cpp @@ -23,6 +23,8 @@ #include <llvm/LLVMContext.h> #include <llvm/Module.h> #include <bcc/bcc.h> +#include <dlfcn.h> + #include "src/talloc/hieralloc.h" #include "src/mesa/main/mtypes.h" @@ -118,7 +120,7 @@ static gl_shader * ShaderCreate(const GGLInterface * iface, GLenum type) } static GLboolean ShaderCompile(const GGLInterface * iface, gl_shader * shader, - const char * glsl, char ** infoLog) + const char * glsl, const char ** infoLog) { GGL_GET_CONST_CONTEXT(ctx, iface); if (!glsl) { @@ -201,6 +203,18 @@ static GLboolean ShaderProgramLink(const GGLInterface * iface, gl_shader_program { GGL_GET_CONST_CONTEXT(ctx, iface); link_shaders(ctx->glCtx, program); + for (unsigned i = 0; i < program->Attributes->NumParameters; i++) { + const gl_program_parameter & attribute = program->Attributes->Parameters[i]; + printf("attribute '%s': location=%d slots=%d \n", attribute.Name, attribute.Location, attribute.Slots); + } + for (unsigned i = 0; i < program->Varying->NumParameters; i++) { + const gl_program_parameter & varying = program->Varying->Parameters[i]; + printf("varying '%s': vs_location=%d fs_location=%d \n", varying.Name, varying.BindLocation, varying.Location); + } + for (unsigned i = 0; i < program->Uniforms->NumUniforms; i++) { + const gl_uniform & uniform = program->Uniforms->Uniforms[i]; + printf("uniform '%s': location=%d type=%s \n", uniform.Name, uniform.Pos, uniform.Type->name); + } if (infoLog) *infoLog = program->InfoLog; return program->LinkStatus; @@ -280,9 +294,6 @@ static char * GetScanlineKeyString(const ShaderKey * key, char * buffer, return buffer; } -#include <bcc/bcc.h> -#include <dlfcn.h> - struct SymbolLookupContext { const GGLContext * gglCtx; const gl_shader_program * program; @@ -329,7 +340,6 @@ static void* SymbolLookup(void* pContext, const char* name) }; } printf("symbolLookup '%s'=%p \n", name, symbol); - //getchar(); assert(symbol); return (void *)symbol; } @@ -510,13 +520,48 @@ static GLint ShaderUniform(const GGLInterface * iface, gl_shader_program * progr { // TODO: sampler uniform GGL_GET_CONST_CONTEXT(ctx, iface); -// if (!program) -// { -// gglError(GL_INVALID_OPERATION); -// return -2; -// } -// return _mesa_uniform(ctx->glCtx, program, location, count, values, type); - return -2; + if (!program) + { + gglError(GL_INVALID_OPERATION); + return -2; + } + int start = location; + int slots = 0, elems = 0; + switch (type) { + case GL_INT: + case GL_FLOAT: + case GL_BOOL: + slots = count; + elems = 1; + break; + case GL_FLOAT_VEC2: // fall through + case GL_INT_VEC2: // fall through + case GL_BOOL_VEC2: + slots = count; + elems = 2; + break; + case GL_INT_VEC3: // fall through + case GL_BOOL_VEC3: // fall through + case GL_FLOAT_VEC3: // fall through + slots = count; + elems = 3; + break; + case GL_INT_VEC4: // fall through + case GL_FLOAT_VEC4: // fall through + case GL_BOOL_VEC4: // fall through + slots = count; + elems = 4; + break; + default: + assert(0); + } + if (0 < start) + return -1; + if (start + slots > program->Uniforms->Slots) + return -1; + for (int i = 0; i < slots; i++) + memcpy(program->ValuesUniform + start + i, values, elems * sizeof(float)); + return start; } static void ShaderUniformMatrix(const GGLInterface * iface, gl_shader_program * program, diff --git a/test/cmain.c b/test/cmain.c index 2b1b55c..0ed9264 100644 --- a/test/cmain.c +++ b/test/cmain.c @@ -1,9 +1,169 @@ #include <stdio.h> +#include <math.h> +#include <assert.h> +#include <stdlib.h> #include <pixelflinger2/pixelflinger2_interface.h> +int ApproximatelyEqual(const Vector4 lhs, const Vector4 rhs, const float t) +{ + if (fabs(lhs.x - rhs.x) > t) + return 0; + if (fabs(lhs.y - rhs.y) > t) + return 0; + if (fabs(lhs.z - rhs.z) > t) + return 0; + if (fabs(lhs.w - rhs.w) > t) + return 0; + return 1; +} + int cmain(int argc, char **argv) { - printf("hello world\n"); - return 0; + const char * infoLog = NULL; + + GGLInterface_t * ggl = CreateGGLInterface(); + + gl_shader_t * shader0 = ggl->ShaderCreate(ggl, GL_VERTEX_SHADER); + assert(shader0); + const char * glsl0 = + "uniform vec4 uVec4; \n" + "uniform sampler2D sampler2d; \n" + "attribute vec4 aPosition; \n" + "attribute vec4 aTexCoord; \n" + "varying vec4 vTexCoord; \n" + "varying vec4 vTexColor; \n" + "void main() { \n" + " gl_Position = aPosition; \n" + " vTexCoord = aTexCoord + uVec4; \n" + " vTexColor = texture2D(sampler2d, aTexCoord.zw); \n" + " gl_PointSize = 432.0; \n" + "}"; + puts(glsl0); + GLboolean compileStatus = ggl->ShaderCompile(ggl, shader0, glsl0, &infoLog); + if (!compileStatus) + fprintf(stderr, "failed to compile vertex shader 0, infoLog: \n %s \n", infoLog); + assert(compileStatus); + + gl_shader_t * shader1 = ggl->ShaderCreate(ggl, GL_FRAGMENT_SHADER); + assert(shader1); + const char * glsl1 = + "uniform vec4 uVec4; \n" + "uniform sampler2D sampler2d; \n" + "varying vec4 vTexCoord; \n" + "varying vec4 vTexColor; \n" + "void main() { \n" + " gl_FragColor = vTexCoord + vTexColor; \n" + "}"; + puts(glsl1); + compileStatus = ggl->ShaderCompile(ggl, shader1, glsl1, &infoLog); + if (!compileStatus) + fprintf(stderr, "failed to compile fragment shader 0, infoLog: \n %s \n", infoLog); + assert(compileStatus); + + gl_shader_program_t * program0 = ggl->ShaderProgramCreate(ggl); + assert(program0); + + ggl->ShaderAttach(ggl, program0, shader0); + ggl->ShaderAttach(ggl, program0, shader1); + ggl->ShaderAttributeBind(ggl, program0, 2, "aTexCoord"); + ggl->ShaderAttributeBind(ggl, program0, 3, "aPosition"); + + GLboolean linkStatus = ggl->ShaderProgramLink(ggl, program0, &infoLog); + if (!linkStatus) + fprintf(stderr, "failed to link program 0, infoLog: \n %s \n", infoLog); + assert(linkStatus); + + ggl->ShaderUse(ggl, program0); + + unsigned texels0 [] = {0xffffffff, 0x22222222, 0x66666666, 0xffffffff}; + GGLTexture_t texture0 = {GL_TEXTURE_2D, GGL_PIXEL_FORMAT_RGBA_8888, + 2, 2, 1, // width, height, levelCount + texels0, 1, 2, 1, 1 + }; // levels, wrapS, wrapT, minFilter, magFilter + + int sampler2dLoc = ggl->ShaderUniformLocation(ggl, program0, "sampler2d"); + if (0 <= sampler2dLoc) { + int samplerUnit = -1; + //ggl->ShaderUniformGetiv(ggl, program0, sampler2dLoc, &samplerUnit); + samplerUnit = sampler2dLoc; + ggl->SetSampler(ggl, samplerUnit, &texture0); + } + + Vector4 uVec4 = {1.125f, 1.5f, 1.75f, 1.75f}; + int uVec4Loc = ggl->ShaderUniformLocation(ggl, program0, "uVec4"); + ggl->ShaderUniform(ggl, program0, uVec4Loc, 1, &uVec4, GL_FLOAT_VEC4); + + VertexInput_t v0 = {0}; + v0.attributes[2] = VECTOR4_CTR(0,0,1,1); // aTexCoord + v0.attributes[3] = VECTOR4_CTR(0.25f, 0.25f, 0.5f,1); // aPosition + + VertexOutput_t vout0 = {0}; + ggl->ProcessVertex(ggl, &v0, &vout0); + if (memcmp(&vout0.position,&v0.attributes[3],sizeof(vout0.position))) { + fprintf(stderr, "gl_Position != aPosition \n"); + assert(0); + } + + int vTexCoordIndex = ggl->ShaderVaryingLocation(ggl, program0, "vTexCoord", NULL) - 2; + VECTOR4_OP_UNARY(vout0.varyings[vTexCoordIndex],-=,uVec4); + if (memcmp(&vout0.varyings[vTexCoordIndex],&v0.attributes[2],sizeof uVec4)) { + fprintf(stderr, "vTexCoord != aTexCoord + uVec4 \n"); + assert(0); + } + Vector4 ones = {1,1,1,1}; + int vTexColorIndex = ggl->ShaderVaryingLocation(ggl, program0, "vTexColor", NULL) - 2; + if (memcmp(&vout0.varyings[vTexColorIndex],&ones,sizeof ones)) { // should be the last texel color + fprintf(stderr, "vTexColor != Vector4(1,1,1,1) \n"); + assert(0); + } + if (vout0.pointSize.x != 432) { + fprintf(stderr, "gl_PointSize != 432 \n"); + assert(0); + } + +// TODO DXL linear filtering needs to be fixed for texcoord outside of [0,1] + v0.attributes[2] = VECTOR4_CTR(0,0, 0.5f, 0.5f); + ggl->ProcessVertex(ggl, &v0, &vout0); + const float filtered = (float)(0xff + 0x22 + 0x66 + 0xff) / (4 * 0xff); + if (!ApproximatelyEqual(vout0.varyings[vTexColorIndex], + VECTOR4_CTR(filtered, filtered, filtered, filtered), 1.0f / 255)) { + fprintf(stderr, "failed linear filter and/or wrapS and wrapT test"); + assert(0); + } + + const unsigned width = 60, height = 100; + + GGLSurface_t colorSurface = {width, height, GGL_PIXEL_FORMAT_RGBA_8888, malloc(width * height * 4)}; + assert(colorSurface.data); + ggl->SetBuffer(ggl, GL_COLOR_BUFFER_BIT, &colorSurface); + + GGLSurface_t depthSurface = {width, height, GGL_PIXEL_FORMAT_Z_32, malloc(width * height * 4)}; + assert(depthSurface.data); + ggl->SetBuffer(ggl, GL_DEPTH_BUFFER_BIT, &depthSurface); + + GGLSurface_t stencilSurface = {width, height, GGL_PIXEL_FORMAT_S_8, malloc(width * height * 1)}; + assert(stencilSurface.data); + ggl->SetBuffer(ggl, GL_STENCIL_BUFFER_BIT, &stencilSurface); + + ggl->ClearColor(ggl, 0.1f, 0.1f, 0.1f, 1.0f); + ggl->ClearDepthf(ggl, 0.5f); + +// TODO DXL scanline and fs test + + free(colorSurface.data); + colorSurface.data = NULL; + free(depthSurface.data); + depthSurface.data = NULL; + free(stencilSurface.data); + stencilSurface.data = NULL; + + ggl->ShaderProgramDelete(ggl, program0); + + puts("*******************"); + puts("*** end of test ***"); + puts("*******************"); + + DestroyGGLInterface(ggl); + return 0; } diff --git a/test/main.cpp b/test/main.cpp index fdb7dea..fd1a561 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -181,7 +181,7 @@ void execute(const gl_shader_program * program) clock_t c0 = clock(); //while(true) - for (frames = 1; frames <= 20; frames++) { + for (frames = 1; frames <= 10; frames++) { for (unsigned y = 0; y < portHeight; y++) { VertexOutput v0, v1; v0.position = Vector4(0, y, 0, 0); @@ -250,9 +250,12 @@ void execute(const gl_shader_program * program) } +extern "C" int cmain(int,char**); int main(int argc, char **argv) { + cmain(0,NULL); + static char basePath [256] = {0}; static char texturePath [256] = {0}; static char fragPath [256] = {0}; @@ -319,7 +322,7 @@ main(int argc, char **argv) exit(EXIT_FAILURE); } - char * infoLog = NULL; + const char * infoLog = NULL; if (!ggl->ShaderCompile(ggl, shader, source, &infoLog)) { printf("Info log for %s:\n%s\n", argv[optind], infoLog); status = EXIT_FAILURE; |