From 0b9bbb6dc5d7dabecf23e8c6bb4a267ba8c34fe8 Mon Sep 17 00:00:00 2001 From: Jason Sams Date: Thu, 11 Feb 2010 18:16:21 -0800 Subject: DO NOT MERGE. Merge Froyo renderscript to Eclair to support live wallpapers on droid. This gives the necessary CPU reduction to allow the wallpapers to work on the slower CPU. Committer: Jason Sams On branch droid Changes to be committed: (use "git reset HEAD ..." to unstage) modified: libs/rs/rsAllocation.cpp modified: libs/rs/rsAllocation.h modified: libs/rs/rsContext.cpp modified: libs/rs/rsContext.h modified: libs/rs/rsProgram.cpp modified: libs/rs/rsProgram.h Delete the old rollo sample which is obsolete. Fix film init Begin gl2 support. Renderscript still uses GL1.1 by default. However, 2.0 can be enabled and will render most tests correctly. Fix film Beging GL2 user shaders. Switch master to using GL2 by default. Implement RS tracked defered texture and buffer object uploads. Committer: Jason Sams On branch droid Changes to be committed: (use "git reset HEAD ..." to unstage) modified: libs/rs/rsAllocation.cpp modified: libs/rs/rsAllocation.h modified: libs/rs/rsContext.h modified: libs/rs/rsProgramFragment.cpp modified: libs/rs/rsSimpleMesh.cpp Remove check for surface valid that is no longer valid. Continue development of es2.0 user shader support for renderscript. This change cleans up ProgramVertex creation and adds support for passing input, output, and constant type info. Continue es2 shader dev Conflicts: graphics/java/android/renderscript/Program.java graphics/java/android/renderscript/ProgramVertex.java Place shader logging behind prop to declutter logs. Fix emulated glColor in es2 mode. Fix live wallpaper many. Z coordinate was being ignored for draw quad call. Add argument checking to sampler builder to disallow illegal modes. Move texture bindings to base program object. Change ProgramFragment creation to require a texture format in 1.0 mode. Element restructuring. Add support for new basic Element types including the RS objects and vectors(2-4). In theory this paves the way for maintaining type info for RS objects, passing elements for GLSL uiforms/attribs/varyings, and supporting nested structures. This will break some apps, checkings for other projects will follow to unbreak them. Disable excessive RS logging. Add RS support for generic attribs as input to vertex programs. More complete support for named attribs. Adds user typed attribs as available to programVertex. Non user attribs are not treated like user for GL2 for simplicity. Support npot on es 2.0 HW. Change user attribs to look for empty slot rather than using them in order. Prevents conflict with numbered legacy slots. Fix npot but where mipmap level sizes were rounding in the wrong direction. Should always be floor. Implement type generation for user uniforms in vertex shader. Remove excessive logging, fix error in GLSL uniform generation. Fix RS mipmap generation for 8 bit alpha textures. Cleanup seperation of Legacy and user attribs. All user programs now use the new names. Legacy vertex attribs are given default names. Fix some minor bugs with GL state setup that were exposed by Droids driver. Implement drawSpriteCropped on es2.0 --- libs/rs/rsProgram.cpp | 227 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 219 insertions(+), 8 deletions(-) (limited to 'libs/rs/rsProgram.cpp') diff --git a/libs/rs/rsProgram.cpp b/libs/rs/rsProgram.cpp index 5f2a6097192e..656a3c339529 100644 --- a/libs/rs/rsProgram.cpp +++ b/libs/rs/rsProgram.cpp @@ -17,34 +17,245 @@ #include "rsContext.h" #include "rsProgram.h" +#include +#include + using namespace android; using namespace android::renderscript; -Program::Program(Context *rsc, Element *in, Element *out) : ObjectBase(rsc) +Program::Program(Context *rsc) : ObjectBase(rsc) { mAllocFile = __FILE__; mAllocLine = __LINE__; + mDirty = true; + mShaderID = 0; + mAttribCount = 0; + mUniformCount = 0; + + mInputElements = NULL; + mOutputElements = NULL; + mConstantTypes = NULL; + mInputCount = 0; + mOutputCount = 0; + mConstantCount = 0; +} + +Program::Program(Context *rsc, const char * shaderText, uint32_t shaderLength, + const uint32_t * params, uint32_t paramLength) : + ObjectBase(rsc) +{ + mAllocFile = __FILE__; + mAllocLine = __LINE__; + mDirty = true; + mShaderID = 0; + mAttribCount = 0; + mUniformCount = 0; + mTextureCount = 0; - mElementIn.set(in); - mElementOut.set(out); + mInputCount = 0; + mOutputCount = 0; + mConstantCount = 0; + + for (uint32_t ct=0; ct < paramLength; ct+=2) { + if (params[ct] == RS_PROGRAM_PARAM_INPUT) { + mInputCount++; + } + if (params[ct] == RS_PROGRAM_PARAM_OUTPUT) { + mOutputCount++; + } + if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) { + mConstantCount++; + } + if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_COUNT) { + mTextureCount = params[ct+1]; + } + } + + mInputElements = new ObjectBaseRef[mInputCount]; + mOutputElements = new ObjectBaseRef[mOutputCount]; + mConstantTypes = new ObjectBaseRef[mConstantCount]; + + uint32_t input = 0; + uint32_t output = 0; + uint32_t constant = 0; + for (uint32_t ct=0; ct < paramLength; ct+=2) { + if (params[ct] == RS_PROGRAM_PARAM_INPUT) { + mInputElements[input++].set(reinterpret_cast(params[ct+1])); + } + if (params[ct] == RS_PROGRAM_PARAM_OUTPUT) { + mOutputElements[output++].set(reinterpret_cast(params[ct+1])); + } + if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) { + mConstantTypes[constant++].set(reinterpret_cast(params[ct+1])); + } + } + mUserShader.setTo(shaderText, shaderLength); } Program::~Program() { + for (uint32_t ct=0; ct < MAX_UNIFORMS; ct++) { + bindAllocation(NULL, ct); + } + + delete[] mInputElements; + delete[] mOutputElements; + delete[] mConstantTypes; + mInputCount = 0; + mOutputCount = 0; + mConstantCount = 0; +} + + +void Program::bindAllocation(Allocation *alloc, uint32_t slot) +{ + if (mConstants[slot].get() == alloc) { + return; + } + if (mConstants[slot].get()) { + mConstants[slot].get()->removeProgramToDirty(this); + } + mConstants[slot].set(alloc); + if (alloc) { + alloc->addProgramToDirty(this); + } + mDirty = true; } +void Program::bindTexture(uint32_t slot, Allocation *a) +{ + if (slot >= MAX_TEXTURE) { + LOGE("Attempt to bind a texture to a slot > MAX_TEXTURE"); + return; + } + + //LOGE("bindtex %i %p", slot, a); + mTextures[slot].set(a); + mDirty = true; +} -void Program::bindAllocation(Allocation *alloc) +void Program::bindSampler(uint32_t slot, Sampler *s) { - mConstants.set(alloc); + if (slot >= MAX_TEXTURE) { + LOGE("Attempt to bind a Sampler to a slot > MAX_TEXTURE"); + return; + } + + mSamplers[slot].set(s); mDirty = true; } -void Program::checkUpdatedAllocation(const Allocation *alloc) +String8 Program::getGLSLInputString() const +{ + String8 s; + for (uint32_t ct=0; ct < mInputCount; ct++) { + const Element *e = mInputElements[ct].get(); + for (uint32_t field=0; field < e->getFieldCount(); field++) { + const Element *f = e->getField(field); + + // Cannot be complex + rsAssert(!f->getFieldCount()); + switch(f->getComponent().getVectorSize()) { + case 1: s.append("attribute float ATTRIB_"); break; + case 2: s.append("attribute vec2 ATTRIB_"); break; + case 3: s.append("attribute vec3 ATTRIB_"); break; + case 4: s.append("attribute vec4 ATTRIB_"); break; + default: + rsAssert(0); + } + + s.append(e->getFieldName(field)); + s.append(";\n"); + } + } + return s; +} + +String8 Program::getGLSLOutputString() const +{ + return String8(); +} + +String8 Program::getGLSLConstantString() const +{ + return String8(); +} + + +void Program::createShader() +{ +} + +bool Program::loadShader(Context *rsc, uint32_t type) { - if (mConstants.get() == alloc) { - mDirty = true; + mShaderID = glCreateShader(type); + rsAssert(mShaderID); + + if (rsc->props.mLogShaders) { + LOGV("Loading shader type %x, ID %i", type, mShaderID); + LOGV(mShader.string()); } + + if (mShaderID) { + const char * ss = mShader.string(); + glShaderSource(mShaderID, 1, &ss, NULL); + glCompileShader(mShaderID); + + GLint compiled = 0; + glGetShaderiv(mShaderID, GL_COMPILE_STATUS, &compiled); + if (!compiled) { + GLint infoLen = 0; + glGetShaderiv(mShaderID, GL_INFO_LOG_LENGTH, &infoLen); + if (infoLen) { + char* buf = (char*) malloc(infoLen); + if (buf) { + glGetShaderInfoLog(mShaderID, infoLen, NULL, buf); + LOGE("Could not compile shader \n%s\n", buf); + free(buf); + } + glDeleteShader(mShaderID); + mShaderID = 0; + return false; + } + } + } + + if (rsc->props.mLogShaders) { + LOGV("--Shader load result %x ", glGetError()); + } + return true; +} + +void Program::setShader(const char *txt, uint32_t len) +{ + mUserShader.setTo(txt, len); +} + + + +namespace android { +namespace renderscript { + + +void rsi_ProgramBindConstants(Context *rsc, RsProgram vp, uint32_t slot, RsAllocation constants) +{ + Program *p = static_cast(vp); + p->bindAllocation(static_cast(constants), slot); +} + +void rsi_ProgramBindTexture(Context *rsc, RsProgram vpf, uint32_t slot, RsAllocation a) +{ + Program *p = static_cast(vpf); + p->bindTexture(slot, static_cast(a)); +} + +void rsi_ProgramBindSampler(Context *rsc, RsProgram vpf, uint32_t slot, RsSampler s) +{ + Program *p = static_cast(vpf); + p->bindSampler(slot, static_cast(s)); +} + +} } -- cgit v1.2.3-59-g8ed1b