diff options
Diffstat (limited to 'libs')
52 files changed, 2024 insertions, 1140 deletions
diff --git a/libs/gui/Android.mk b/libs/gui/Android.mk index 58bb0d3..b5737ff 100644 --- a/libs/gui/Android.mk +++ b/libs/gui/Android.mk @@ -38,3 +38,7 @@ ifeq ($(TARGET_SIMULATOR),true) endif include $(BUILD_SHARED_LIBRARY) + +ifeq (,$(ONE_SHOT_MAKEFILE)) +include $(call first-makefiles-under,$(LOCAL_PATH)) +endif diff --git a/libs/gui/tests/Android.mk b/libs/gui/tests/Android.mk index ecd0995..8d3a9b5 100644 --- a/libs/gui/tests/Android.mk +++ b/libs/gui/tests/Android.mk @@ -36,9 +36,6 @@ LOCAL_C_INCLUDES := \ include $(BUILD_EXECUTABLE) -# Build the manual test programs. -include $(call all-subdir-makefiles) - endif # Include subdirectory makefiles diff --git a/libs/hwui/Caches.h b/libs/hwui/Caches.h index faecadd..596781e 100644 --- a/libs/hwui/Caches.h +++ b/libs/hwui/Caches.h @@ -62,8 +62,10 @@ static const TextureVertex gMeshVertices[] = { static const GLsizei gMeshStride = sizeof(TextureVertex); static const GLsizei gVertexStride = sizeof(Vertex); static const GLsizei gAlphaVertexStride = sizeof(AlphaVertex); +static const GLsizei gAAVertexStride = sizeof(AAVertex); static const GLsizei gMeshTextureOffset = 2 * sizeof(float); -static const GLsizei gVertexAlphaOffset = 2 * sizeof(float); +static const GLsizei gVertexAAWidthOffset = 2 * sizeof(float); +static const GLsizei gVertexAALengthOffset = 3 * sizeof(float); static const GLsizei gMeshCount = 4; /////////////////////////////////////////////////////////////////////////////// diff --git a/libs/hwui/Layer.h b/libs/hwui/Layer.h index 6c4a2a9..0310bc3 100644 --- a/libs/hwui/Layer.h +++ b/libs/hwui/Layer.h @@ -45,6 +45,9 @@ struct Layer { mesh = NULL; meshIndices = NULL; meshElementCount = 0; + isCacheable = true; + isTextureLayer = false; + renderTarget = GL_TEXTURE_2D; } ~Layer() { @@ -137,6 +140,27 @@ struct Layer { TextureVertex* mesh; uint16_t* meshIndices; GLsizei meshElementCount; + + /** + * If set to true (by default), the layer can be reused. + */ + bool isCacheable; + + /** + * When set to true, this layer must be treated as a texture + * layer. + */ + bool isTextureLayer; + + /** + * Optional texture coordinates transform. + */ + mat4 texTransform; + + /** + * Indicates the render target. + */ + GLenum renderTarget; }; // struct Layer }; // namespace uirenderer diff --git a/libs/hwui/LayerCache.cpp b/libs/hwui/LayerCache.cpp index a9710ad..b2d795f 100644 --- a/libs/hwui/LayerCache.cpp +++ b/libs/hwui/LayerCache.cpp @@ -154,6 +154,8 @@ bool LayerCache::resize(Layer* layer, const uint32_t width, const uint32_t heigh } bool LayerCache::put(Layer* layer) { + if (!layer->isCacheable) return false; + const uint32_t size = layer->width * layer->height * 4; // Don't even try to cache a layer that's bigger than the cache if (size < mMaxSize) { diff --git a/libs/hwui/LayerRenderer.cpp b/libs/hwui/LayerRenderer.cpp index ca1e7ae..f316ba7 100644 --- a/libs/hwui/LayerRenderer.cpp +++ b/libs/hwui/LayerRenderer.cpp @@ -20,6 +20,7 @@ #include "LayerCache.h" #include "LayerRenderer.h" +#include "Matrix.h" #include "Properties.h" #include "Rect.h" @@ -165,6 +166,30 @@ void LayerRenderer::generateMesh() { // Layers management /////////////////////////////////////////////////////////////////////////////// +Layer* LayerRenderer::createTextureLayer() { + LAYER_RENDERER_LOGD("Creating new texture layer"); + + Layer* layer = new Layer(0, 0); + layer->isCacheable = false; + layer->isTextureLayer = true; + layer->blend = true; + layer->empty = true; + layer->fbo = 0; + layer->colorFilter = NULL; + layer->fbo = 0; + layer->layer.set(0.0f, 0.0f, 0.0f, 0.0f); + layer->texCoords.set(0.0f, 1.0f, 0.0f, 1.0f); + layer->alpha = 255; + layer->mode = SkXfermode::kSrcOver_Mode; + layer->colorFilter = NULL; + layer->region.clear(); + + glActiveTexture(GL_TEXTURE0); + glGenTextures(1, &layer->texture); + + return layer; +} + Layer* LayerRenderer::createLayer(uint32_t width, uint32_t height, bool isOpaque) { LAYER_RENDERER_LOGD("Creating new layer %dx%d", width, height); @@ -244,6 +269,27 @@ bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) { return true; } +void LayerRenderer::updateTextureLayer(Layer* layer, uint32_t width, uint32_t height, + GLenum renderTarget, float* transform) { + if (layer) { + layer->width = width; + layer->height = height; + layer->layer.set(0.0f, 0.0f, width, height); + layer->region.set(width, height); + layer->regionRect.set(0.0f, 0.0f, width, height); + layer->texTransform.load(transform); + layer->renderTarget = renderTarget; + + glBindTexture(layer->renderTarget, layer->texture); + + glTexParameteri(layer->renderTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(layer->renderTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + glTexParameteri(layer->renderTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(layer->renderTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + } +} + void LayerRenderer::destroyLayer(Layer* layer) { if (layer) { LAYER_RENDERER_LOGD("Destroying layer, fbo = %d", layer->fbo); diff --git a/libs/hwui/LayerRenderer.h b/libs/hwui/LayerRenderer.h index d2f565e..59cab96 100644 --- a/libs/hwui/LayerRenderer.h +++ b/libs/hwui/LayerRenderer.h @@ -53,8 +53,11 @@ public: Region* getRegion(); GLint getTargetFbo(); + static Layer* createTextureLayer(); static Layer* createLayer(uint32_t width, uint32_t height, bool isOpaque = false); static bool resizeLayer(Layer* layer, uint32_t width, uint32_t height); + static void updateTextureLayer(Layer* layer, uint32_t width, uint32_t height, + GLenum renderTarget, float* transform); static void destroyLayer(Layer* layer); static void destroyLayerDeferred(Layer* layer); diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp index ea42838..6f751e8 100644 --- a/libs/hwui/OpenGLRenderer.cpp +++ b/libs/hwui/OpenGLRenderer.cpp @@ -633,15 +633,52 @@ void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { } } +void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) { + float alpha = layer->alpha / 255.0f; + + setupDraw(); + if (layer->renderTarget == GL_TEXTURE_2D) { + setupDrawWithTexture(); + } else { + setupDrawWithExternalTexture(); + } + setupDrawTextureTransform(); + setupDrawColor(alpha, alpha, alpha, alpha); + setupDrawColorFilter(); + setupDrawBlending(layer->blend, layer->mode); + setupDrawProgram(); + setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom); + setupDrawPureColorUniforms(); + setupDrawColorFilterUniforms(); + if (layer->renderTarget == GL_TEXTURE_2D) { + setupDrawTexture(layer->texture); + } else { + setupDrawExternalTexture(layer->texture); + } + setupDrawTextureTransformUniforms(layer->texTransform); + setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]); + + glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); + + finishDrawTexture(); +} + void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) { - const Rect& texCoords = layer->texCoords; - resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom); + if (!layer->isTextureLayer) { + const Rect& texCoords = layer->texCoords; + resetDrawTextureTexCoords(texCoords.left, texCoords.top, + texCoords.right, texCoords.bottom); - drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture, - layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0], - &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap); + drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture, + layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0], + &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap); - resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); + resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); + } else { + resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f); + drawTextureLayer(layer, rect); + resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); + } } void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) { @@ -882,8 +919,12 @@ void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) { mDescription.hasAlpha8Texture = isAlpha8; } +void OpenGLRenderer::setupDrawWithExternalTexture() { + mDescription.hasExternalTexture = true; +} + void OpenGLRenderer::setupDrawAALine() { - mDescription.hasWidth = true; + mDescription.isAA = true; } void OpenGLRenderer::setupDrawPoint(float pointSize) { @@ -1055,6 +1096,23 @@ void OpenGLRenderer::setupDrawTexture(GLuint texture) { glEnableVertexAttribArray(mTexCoordsSlot); } +void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) { + bindExternalTexture(texture); + glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++); + + mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); + glEnableVertexAttribArray(mTexCoordsSlot); +} + +void OpenGLRenderer::setupDrawTextureTransform() { + mDescription.hasTextureTransform = true; +} + +void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) { + glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1, + GL_FALSE, &transform.data[0]); +} + void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) { if (!vertices) { mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); @@ -1076,25 +1134,30 @@ void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) { /** * Sets up the shader to draw an AA line. We draw AA lines with quads, where there is an - * outer boundary that fades out to 0. The variables set in the shader define the width of the - * core line primitive ("width") and the width of the fading boundary ("boundaryWidth"). The - * "vtxDistance" attribute (one per vertex) is a value from zero to one that tells the fragment - * shader where the fragment is in relation to the line width overall; this value is then used - * to compute the proper color, based on whether the fragment lies in the fading AA region of - * the line. + * outer boundary that fades out to 0. The variables set in the shader define the proportion of + * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength + * attributes (one per vertex) are values from zero to one that tells the fragment + * shader where the fragment is in relation to the line width/length overall; these values are + * then used to compute the proper color, based on whether the fragment lies in the fading AA + * region of the line. + * Note that we only pass down the width values in this setup function. The length coordinates + * are set up for each individual segment. */ -void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* distanceCoords, float strokeWidth) { +void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords, + GLvoid* lengthCoords, float strokeWidth) { mCaches.unbindMeshBuffer(); glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, - gAlphaVertexStride, vertices); - int distanceSlot = mCaches.currentProgram->getAttrib("vtxDistance"); - glEnableVertexAttribArray(distanceSlot); - glVertexAttribPointer(distanceSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, distanceCoords); - int widthSlot = mCaches.currentProgram->getUniform("width"); + gAAVertexStride, vertices); + int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth"); + glEnableVertexAttribArray(widthSlot); + glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords); + int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength"); + glEnableVertexAttribArray(lengthSlot); + glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords); int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth"); + // Setting the inverse value saves computations per-fragment in the shader int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth"); float boundaryWidth = (1 - strokeWidth) / 2; - glUniform1f(widthSlot, strokeWidth); glUniform1f(boundaryWidthSlot, boundaryWidth); glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidth)); } @@ -1435,20 +1498,21 @@ void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) { } Vertex lines[verticesCount]; Vertex* vertices = &lines[0]; - AlphaVertex wLines[verticesCount]; - AlphaVertex* aaVertices = &wLines[0]; + AAVertex wLines[verticesCount]; + AAVertex* aaVertices = &wLines[0]; if (!isAA) { setupDrawVertices(vertices); } else { - void* alphaCoords = ((GLbyte*) aaVertices) + gVertexAlphaOffset; + void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset; + void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset; // innerProportion is the ratio of the inner (non-AA) port of the line to the total // AA stroke width (the base stroke width expanded by a half pixel on either side). // This value is used in the fragment shader to determine how to fill fragments. float innerProportion = fmax(strokeWidth - 1.0f, 0) / (strokeWidth + .5f); - setupDrawAALine((void*) aaVertices, alphaCoords, innerProportion); + setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, innerProportion); } - AlphaVertex *prevAAVertex = NULL; + AAVertex *prevAAVertex = NULL; Vertex *prevVertex = NULL; float inverseScaleX = 1.0f; float inverseScaleY = 1.0f; @@ -1471,15 +1535,17 @@ void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) { } } + int boundaryLengthSlot = -1; + int inverseBoundaryLengthSlot = -1; for (int i = 0; i < count; i += 4) { // a = start point, b = end point vec2 a(points[i], points[i + 1]); vec2 b(points[i + 2], points[i + 3]); + float length = 0; // Find the normal to the line vec2 n = (b - a).copyNormalized() * strokeWidth; if (isHairLine) { - n *= inverseScaleX; if (isAA) { float wideningFactor; if (fabs(n.x) >= fabs(n.y)) { @@ -1489,27 +1555,35 @@ void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) { } n *= wideningFactor; } + n.x *= inverseScaleX; + n.y *= inverseScaleY; } float x = n.x; n.x = -n.y; n.y = x; + // aa lines expand the endpoint vertices to encompass the AA boundary + if (isAA) { + vec2 abVector = (b - a); + length = abVector.length(); + abVector.normalize(); + a -= abVector; + b += abVector; + } + // Four corners of the rectangle defining a thick line vec2 p1 = a - n; vec2 p2 = a + n; vec2 p3 = b + n; vec2 p4 = b - n; + const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x))); const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x))); const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y))); const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y))); if (!quickReject(left, top, right, bottom)) { - // Draw the line as 2 triangles, could be optimized - // by using only 4 vertices and the correct indices - // Also we should probably used non textured vertices - // when line AA is disabled to save on bandwidth if (!isAA) { if (prevVertex != NULL) { // Issue two repeat vertices to create degenerate triangles to bridge @@ -1527,24 +1601,36 @@ void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) { prevVertex = vertices - 1; generatedVerticesCount += 4; } else { + if (boundaryLengthSlot < 0) { + boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength"); + inverseBoundaryLengthSlot = + mCaches.currentProgram->getUniform("inverseBoundaryLength"); + } + float innerProportion = (length) / (length + 2); + float boundaryLength = (1 - innerProportion) / 2; + glUniform1f(boundaryLengthSlot, boundaryLength); + glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLength)); + if (prevAAVertex != NULL) { // Issue two repeat vertices to create degenerate triangles to bridge // between the previous line and the new one. This is necessary because // we are creating a single triangle_strip which will contain // potentially discontinuous line segments. - AlphaVertex::set(aaVertices++,prevAAVertex->position[0], - prevAAVertex->position[1], prevAAVertex->alpha); - AlphaVertex::set(aaVertices++, p4.x, p4.y, 1); + AAVertex::set(aaVertices++,prevAAVertex->position[0], + prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length); + AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1); generatedVerticesCount += 2; } - AlphaVertex::set(aaVertices++, p4.x, p4.y, 1); - AlphaVertex::set(aaVertices++, p1.x, p1.y, 1); - AlphaVertex::set(aaVertices++, p3.x, p3.y, 0); - AlphaVertex::set(aaVertices++, p2.x, p2.y, 0); + AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1); + AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0); + AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1); + AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0); prevAAVertex = aaVertices - 1; generatedVerticesCount += 4; } - dirtyLayer(left, top, right, bottom, *mSnapshot->transform); + dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top, + a.x == b.x ? right: right, a.y == b.y ? bottom: bottom, + *mSnapshot->transform); } } if (generatedVerticesCount > 0) { diff --git a/libs/hwui/OpenGLRenderer.h b/libs/hwui/OpenGLRenderer.h index 918e1fb..b5c37c2 100644 --- a/libs/hwui/OpenGLRenderer.h +++ b/libs/hwui/OpenGLRenderer.h @@ -248,6 +248,8 @@ private: */ void composeLayerRect(Layer* layer, const Rect& rect, bool swap = false); + void drawTextureLayer(Layer* layer, const Rect& rect); + /** * Mark the layer as dirty at the specified coordinates. The coordinates * are transformed with the supplied matrix. @@ -387,6 +389,14 @@ private: } /** + * Binds the specified EGLImage texture. The texture unit must have been selected + * prior to calling this method. + */ + inline void bindExternalTexture(GLuint texture) { + glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture); + } + + /** * Sets the wrap modes for the specified texture. The wrap modes are modified * only when needed. */ @@ -425,6 +435,7 @@ private: * Various methods to setup OpenGL rendering. */ void setupDrawWithTexture(bool isAlpha8 = false); + void setupDrawWithExternalTexture(); void setupDrawAALine(); void setupDrawPoint(float pointSize); void setupDrawColor(int color); @@ -453,9 +464,13 @@ private: void setupDrawColorFilterUniforms(); void setupDrawSimpleMesh(); void setupDrawTexture(GLuint texture); + void setupDrawExternalTexture(GLuint texture); + void setupDrawTextureTransform(); + void setupDrawTextureTransformUniforms(mat4& transform); void setupDrawMesh(GLvoid* vertices, GLvoid* texCoords = NULL, GLuint vbo = 0); void setupDrawVertices(GLvoid* vertices); - void setupDrawAALine(GLvoid* vertices, GLvoid* distanceCoords, float strokeWidth); + void setupDrawAALine(GLvoid* vertices, GLvoid* distanceCoords, GLvoid* lengthCoords, + float strokeWidth); void finishDrawTexture(); void drawRegionRects(const Region& region); diff --git a/libs/hwui/ProgramCache.cpp b/libs/hwui/ProgramCache.cpp index 80b1917..d419e3e 100644 --- a/libs/hwui/ProgramCache.cpp +++ b/libs/hwui/ProgramCache.cpp @@ -39,8 +39,11 @@ const char* gVS_Header_Attributes = "attribute vec4 position;\n"; const char* gVS_Header_Attributes_TexCoords = "attribute vec2 texCoords;\n"; -const char* gVS_Header_Attributes_Distance = - "attribute float vtxDistance;\n"; +const char* gVS_Header_Attributes_AAParameters = + "attribute float vtxWidth;\n" + "attribute float vtxLength;\n"; +const char* gVS_Header_Uniforms_TextureTransform = + "uniform mat4 mainTextureTransform;\n"; const char* gVS_Header_Uniforms = "uniform mat4 transform;\n"; const char* gVS_Header_Uniforms_IsPoint = @@ -58,8 +61,9 @@ const char* gVS_Header_Uniforms_HasBitmap = "uniform mediump vec2 textureDimension;\n"; const char* gVS_Header_Varyings_HasTexture = "varying vec2 outTexCoords;\n"; -const char* gVS_Header_Varyings_HasWidth = - "varying float distance;\n"; +const char* gVS_Header_Varyings_IsAA = + "varying float widthProportion;\n" + "varying float lengthProportion;\n"; const char* gVS_Header_Varyings_HasBitmap = "varying vec2 outBitmapTexCoords;\n"; const char* gVS_Header_Varyings_PointHasBitmap = @@ -76,6 +80,8 @@ const char* gVS_Main = "\nvoid main(void) {\n"; const char* gVS_Main_OutTexCoords = " outTexCoords = texCoords;\n"; +const char* gVS_Main_OutTransformedTexCoords = + " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n"; const char* gVS_Main_OutGradient[3] = { // Linear " linear = vec2((screenSpace * position).x, 0.5);\n", @@ -92,8 +98,9 @@ const char* gVS_Main_Position = " gl_Position = transform * position;\n"; const char* gVS_Main_PointSize = " gl_PointSize = pointSize;\n"; -const char* gVS_Main_Width = - " distance = vtxDistance;\n"; +const char* gVS_Main_AA = + " widthProportion = vtxWidth;\n" + " lengthProportion = vtxLength;\n"; const char* gVS_Footer = "}\n\n"; @@ -103,19 +110,24 @@ const char* gVS_Footer = const char* gFS_Header_Extension_FramebufferFetch = "#extension GL_NV_shader_framebuffer_fetch : enable\n\n"; +const char* gFS_Header_Extension_ExternalTexture = + "#extension GL_OES_EGL_image_external : require\n\n"; const char* gFS_Header = "precision mediump float;\n\n"; const char* gFS_Uniforms_Color = "uniform vec4 color;\n"; -const char* gFS_Uniforms_Width = - "uniform float width;\n" +const char* gFS_Uniforms_AA = "uniform float boundaryWidth;\n" - "uniform float inverseBoundaryWidth;\n"; + "uniform float inverseBoundaryWidth;\n" + "uniform float boundaryLength;\n" + "uniform float inverseBoundaryLength;\n"; const char* gFS_Header_Uniforms_PointHasBitmap = "uniform vec2 textureDimension;\n" "uniform float pointSize;\n"; const char* gFS_Uniforms_TextureSampler = "uniform sampler2D sampler;\n"; +const char* gFS_Uniforms_ExternalTextureSampler = + "uniform samplerExternalOES sampler;\n"; const char* gFS_Uniforms_GradientSampler[3] = { // Linear "uniform sampler2D gradientSampler;\n", @@ -181,11 +193,16 @@ const char* gFS_Main_FetchColor = " fragColor = color;\n"; const char* gFS_Main_ModulateColor = " fragColor *= color.a;\n"; -const char* gFS_Main_AccountForWidth = - " if (distance < boundaryWidth) {\n" - " fragColor *= (distance * inverseBoundaryWidth);\n" - " } else if (distance > (1.0 - boundaryWidth)) {\n" - " fragColor *= ((1.0 - distance) * inverseBoundaryWidth);\n" +const char* gFS_Main_AccountForAA = + " if (widthProportion < boundaryWidth) {\n" + " fragColor *= (widthProportion * inverseBoundaryWidth);\n" + " } else if (widthProportion > (1.0 - boundaryWidth)) {\n" + " fragColor *= ((1.0 - widthProportion) * inverseBoundaryWidth);\n" + " }\n" + " if (lengthProportion < boundaryLength) {\n" + " fragColor *= (lengthProportion * inverseBoundaryLength);\n" + " } else if (lengthProportion > (1.0 - boundaryLength)) {\n" + " fragColor *= ((1.0 - lengthProportion) * inverseBoundaryLength);\n" " }\n"; const char* gFS_Main_FetchTexture[2] = { // Don't modulate @@ -369,14 +386,17 @@ Program* ProgramCache::generateProgram(const ProgramDescription& description, pr String8 ProgramCache::generateVertexShader(const ProgramDescription& description) { // Add attributes String8 shader(gVS_Header_Attributes); - if (description.hasTexture) { + if (description.hasTexture || description.hasExternalTexture) { shader.append(gVS_Header_Attributes_TexCoords); } - if (description.hasWidth) { - shader.append(gVS_Header_Attributes_Distance); + if (description.isAA) { + shader.append(gVS_Header_Attributes_AAParameters); } // Uniforms shader.append(gVS_Header_Uniforms); + if (description.hasTextureTransform) { + shader.append(gVS_Header_Uniforms_TextureTransform); + } if (description.hasGradient) { shader.append(gVS_Header_Uniforms_HasGradient[description.gradientType]); } @@ -387,11 +407,11 @@ String8 ProgramCache::generateVertexShader(const ProgramDescription& description shader.append(gVS_Header_Uniforms_IsPoint); } // Varyings - if (description.hasTexture) { + if (description.hasTexture || description.hasExternalTexture) { shader.append(gVS_Header_Varyings_HasTexture); } - if (description.hasWidth) { - shader.append(gVS_Header_Varyings_HasWidth); + if (description.isAA) { + shader.append(gVS_Header_Varyings_IsAA); } if (description.hasGradient) { shader.append(gVS_Header_Varyings_HasGradient[description.gradientType]); @@ -404,11 +424,13 @@ String8 ProgramCache::generateVertexShader(const ProgramDescription& description // Begin the shader shader.append(gVS_Main); { - if (description.hasTexture) { + if (description.hasTextureTransform) { + shader.append(gVS_Main_OutTransformedTexCoords); + } else if (description.hasTexture || description.hasExternalTexture) { shader.append(gVS_Main_OutTexCoords); } - if (description.hasWidth) { - shader.append(gVS_Main_Width); + if (description.isAA) { + shader.append(gVS_Main_AA); } if (description.hasGradient) { shader.append(gVS_Main_OutGradient[description.gradientType]); @@ -440,15 +462,18 @@ String8 ProgramCache::generateFragmentShader(const ProgramDescription& descripti if (blendFramebuffer) { shader.append(gFS_Header_Extension_FramebufferFetch); } + if (description.hasExternalTexture) { + shader.append(gFS_Header_Extension_ExternalTexture); + } shader.append(gFS_Header); // Varyings - if (description.hasTexture) { + if (description.hasTexture || description.hasExternalTexture) { shader.append(gVS_Header_Varyings_HasTexture); } - if (description.hasWidth) { - shader.append(gVS_Header_Varyings_HasWidth); + if (description.isAA) { + shader.append(gVS_Header_Varyings_IsAA); } if (description.hasGradient) { shader.append(gVS_Header_Varyings_HasGradient[description.gradientType]); @@ -461,7 +486,7 @@ String8 ProgramCache::generateFragmentShader(const ProgramDescription& descripti // Uniforms int modulateOp = MODULATE_OP_NO_MODULATE; - const bool singleColor = !description.hasTexture && + const bool singleColor = !description.hasTexture && !description.hasExternalTexture && !description.hasGradient && !description.hasBitmap; if (description.modulate || singleColor) { @@ -470,9 +495,11 @@ String8 ProgramCache::generateFragmentShader(const ProgramDescription& descripti } if (description.hasTexture) { shader.append(gFS_Uniforms_TextureSampler); + } else if (description.hasExternalTexture) { + shader.append(gFS_Uniforms_ExternalTextureSampler); } - if (description.hasWidth) { - shader.append(gFS_Uniforms_Width); + if (description.isAA) { + shader.append(gFS_Uniforms_AA); } if (description.hasGradient) { shader.append(gFS_Uniforms_GradientSampler[description.gradientType]); @@ -482,16 +509,16 @@ String8 ProgramCache::generateFragmentShader(const ProgramDescription& descripti } // Optimization for common cases - if (!description.hasWidth && !blendFramebuffer && + if (!description.isAA && !blendFramebuffer && description.colorOp == ProgramDescription::kColorNone && !description.isPoint) { bool fast = false; const bool noShader = !description.hasGradient && !description.hasBitmap; - const bool singleTexture = description.hasTexture && + const bool singleTexture = (description.hasTexture || description.hasExternalTexture) && !description.hasAlpha8Texture && noShader; const bool singleA8Texture = description.hasTexture && description.hasAlpha8Texture && noShader; - const bool singleGradient = !description.hasTexture && + const bool singleGradient = !description.hasTexture && !description.hasExternalTexture && description.hasGradient && !description.hasBitmap && description.gradientType == ProgramDescription::kGradientLinear; @@ -554,7 +581,7 @@ String8 ProgramCache::generateFragmentShader(const ProgramDescription& descripti // Begin the shader shader.append(gFS_Main); { // Stores the result in fragColor directly - if (description.hasTexture) { + if (description.hasTexture || description.hasExternalTexture) { if (description.hasAlpha8Texture) { if (!description.hasGradient && !description.hasBitmap) { shader.append(gFS_Main_FetchA8Texture[modulateOp]); @@ -567,8 +594,8 @@ String8 ProgramCache::generateFragmentShader(const ProgramDescription& descripti shader.append(gFS_Main_FetchColor); } } - if (description.hasWidth) { - shader.append(gFS_Main_AccountForWidth); + if (description.isAA) { + shader.append(gFS_Main_AccountForAA); } if (description.hasGradient) { shader.append(gFS_Main_FetchGradient[description.gradientType]); diff --git a/libs/hwui/ProgramCache.h b/libs/hwui/ProgramCache.h index 18d98cb..5c7197b 100644 --- a/libs/hwui/ProgramCache.h +++ b/libs/hwui/ProgramCache.h @@ -75,7 +75,10 @@ namespace uirenderer { #define PROGRAM_IS_POINT_SHIFT 36 -#define PROGRAM_HAS_WIDTH_SHIFT 37 +#define PROGRAM_HAS_AA_SHIFT 37 + +#define PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT 38 +#define PROGRAM_HAS_TEXTURE_TRANSFORM_SHIFT 39 /////////////////////////////////////////////////////////////////////////////// // Types @@ -113,6 +116,8 @@ struct ProgramDescription { // Texturing bool hasTexture; bool hasAlpha8Texture; + bool hasExternalTexture; + bool hasTextureTransform; // Modulate, this should only be set when setColor() return true bool modulate; @@ -121,7 +126,7 @@ struct ProgramDescription { bool hasBitmap; bool isBitmapNpot; - bool hasWidth; + bool isAA; bool hasGradient; Gradient gradientType; @@ -151,8 +156,10 @@ struct ProgramDescription { void reset() { hasTexture = false; hasAlpha8Texture = false; + hasExternalTexture = false; + hasTextureTransform = false; - hasWidth = false; + isAA = false; modulate = false; @@ -239,7 +246,9 @@ struct ProgramDescription { if (swapSrcDst) key |= PROGRAM_KEY_SWAP_SRC_DST; if (modulate) key |= programid(0x1) << PROGRAM_MODULATE_SHIFT; if (isPoint) key |= programid(0x1) << PROGRAM_IS_POINT_SHIFT; - if (hasWidth) key |= programid(0x1) << PROGRAM_HAS_WIDTH_SHIFT; + if (isAA) key |= programid(0x1) << PROGRAM_HAS_AA_SHIFT; + if (hasExternalTexture) key |= programid(0x1) << PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT; + if (hasTextureTransform) key |= programid(0x1) << PROGRAM_HAS_TEXTURE_TRANSFORM_SHIFT; return key; } diff --git a/libs/hwui/Vertex.h b/libs/hwui/Vertex.h index c120428..38455dc 100644 --- a/libs/hwui/Vertex.h +++ b/libs/hwui/Vertex.h @@ -68,6 +68,25 @@ struct AlphaVertex : Vertex { } }; // struct AlphaVertex +/** + * Simple structure to describe a vertex with a position and an alpha value. + */ +struct AAVertex : Vertex { + float width; + float length; + + static inline void set(AAVertex* vertex, float x, float y, float width, float length) { + Vertex::set(vertex, x, y); + vertex[0].width = width; + vertex[0].length = length; + } + + static inline void setColor(AAVertex* vertex, float width, float length) { + vertex[0].width = width; + vertex[0].length = length; + } +}; // struct AlphaVertex + }; // namespace uirenderer }; // namespace android diff --git a/libs/rs/Android.mk b/libs/rs/Android.mk index a99a599..232ab36 100644 --- a/libs/rs/Android.mk +++ b/libs/rs/Android.mk @@ -110,20 +110,23 @@ LOCAL_SRC_FILES:= \ rsScriptC.cpp \ rsScriptC_Lib.cpp \ rsScriptC_LibGL.cpp \ - rsShaderCache.cpp \ rsSignal.cpp \ rsStream.cpp \ rsThreadIO.cpp \ rsType.cpp \ - rsVertexArray.cpp \ driver/rsdBcc.cpp \ driver/rsdCore.cpp \ driver/rsdGL.cpp \ + driver/rsdMesh.cpp \ + driver/rsdMeshObj.cpp \ + driver/rsdProgram.cpp \ driver/rsdProgramRaster.cpp \ driver/rsdProgramStore.cpp \ driver/rsdRuntimeMath.cpp \ - driver/rsdRuntimeStubs.cpp - + driver/rsdRuntimeStubs.cpp \ + driver/rsdShader.cpp \ + driver/rsdShaderCache.cpp \ + driver/rsdVertexArray.cpp LOCAL_SHARED_LIBRARIES += libz libcutils libutils libEGL libGLESv1_CM libGLESv2 libui libbcc diff --git a/libs/rs/driver/rsdCore.cpp b/libs/rs/driver/rsdCore.cpp index 5b80439..d5d23c7 100644 --- a/libs/rs/driver/rsdCore.cpp +++ b/libs/rs/driver/rsdCore.cpp @@ -19,6 +19,9 @@ #include "rsdGL.h" #include "rsdProgramStore.h" #include "rsdProgramRaster.h" +#include "rsdProgramVertex.h" +#include "rsdProgramFragment.h" +#include "rsdMesh.h" #include <malloc.h> #include "rsContext.h" @@ -69,6 +72,24 @@ static RsdHalFunctions FunctionTable = { rsdProgramRasterInit, rsdProgramRasterSetActive, rsdProgramRasterDestroy + }, + + { + rsdProgramVertexInit, + rsdProgramVertexSetActive, + rsdProgramVertexDestroy + }, + + { + rsdProgramFragmentInit, + rsdProgramFragmentSetActive, + rsdProgramFragmentDestroy + }, + + { + rsdMeshInit, + rsdMeshDraw, + rsdMeshDestroy } }; diff --git a/libs/rs/driver/rsdGL.cpp b/libs/rs/driver/rsdGL.cpp index 26e1bdf..48690d5 100644 --- a/libs/rs/driver/rsdGL.cpp +++ b/libs/rs/driver/rsdGL.cpp @@ -40,6 +40,8 @@ #include <malloc.h> #include "rsContext.h" +#include "rsdShaderCache.h" +#include "rsdVertexArray.h" using namespace android; using namespace android::renderscript; @@ -128,6 +130,11 @@ static void DumpDebug(RsdHal *dc) { void rsdGLShutdown(const Context *rsc) { RsdHal *dc = (RsdHal *)rsc->mHal.drv; + dc->gl.shaderCache->cleanupAll(); + delete dc->gl.shaderCache; + + delete dc->gl.vertexArrayState; + LOGV("%p, deinitEGL", rsc); if (dc->gl.egl.context != EGL_NO_CONTEXT) { @@ -287,6 +294,10 @@ bool rsdGLInit(const Context *rsc) { DumpDebug(dc); } + dc->gl.shaderCache = new RsdShaderCache(); + dc->gl.vertexArrayState = new RsdVertexArrayState(); + dc->gl.vertexArrayState->init(dc->gl.gl.maxVertexAttribs); + LOGV("initGLThread end %p", rsc); return true; } diff --git a/libs/rs/driver/rsdGL.h b/libs/rs/driver/rsdGL.h index 246931f..351b2d5 100644 --- a/libs/rs/driver/rsdGL.h +++ b/libs/rs/driver/rsdGL.h @@ -19,7 +19,8 @@ #include <rs_hal.h> - +class RsdShaderCache; +class RsdVertexArrayState; typedef void (* InvokeFunc_t)(void); typedef void (*WorkerCallback_t)(void *usr, uint32_t idx); @@ -64,6 +65,8 @@ typedef struct RsdGLRec { ANativeWindow *wndSurface; uint32_t width; uint32_t height; + RsdShaderCache *shaderCache; + RsdVertexArrayState *vertexArrayState; } RsdGL; diff --git a/libs/rs/driver/rsdMesh.cpp b/libs/rs/driver/rsdMesh.cpp new file mode 100644 index 0000000..eb62ddb --- /dev/null +++ b/libs/rs/driver/rsdMesh.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include <rs_hal.h> +#include <rsContext.h> +#include <rsMesh.h> + +#include "rsdCore.h" +#include "rsdMesh.h" +#include "rsdMeshObj.h" +#include "rsdShaderCache.h" + +using namespace android; +using namespace android::renderscript; + +bool rsdMeshInit(const Context *rsc, const Mesh *m) { + RsdMeshObj *drv = NULL; + if(m->mHal.drv) { + drv = (RsdMeshObj*)m->mHal.drv; + delete drv; + } + drv = new RsdMeshObj(rsc, m); + m->mHal.drv = drv; + return drv->init(); +} + +void rsdMeshDraw(const Context *rsc, const Mesh *m, uint32_t primIndex, uint32_t start, uint32_t len) { + if(m->mHal.drv) { + RsdHal *dc = (RsdHal *)rsc->mHal.drv; + if (!dc->gl.shaderCache->setup(rsc)) { + return; + } + + RsdMeshObj *drv = (RsdMeshObj*)m->mHal.drv; + drv->renderPrimitiveRange(rsc, primIndex, start, len); + } +} + +void rsdMeshDestroy(const Context *rsc, const Mesh *m) { + if(m->mHal.drv) { + RsdMeshObj *drv = (RsdMeshObj*)m->mHal.drv; + delete drv; + } +} + + diff --git a/libs/rs/driver/rsdMesh.h b/libs/rs/driver/rsdMesh.h new file mode 100644 index 0000000..d2714fd --- /dev/null +++ b/libs/rs/driver/rsdMesh.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef RSD_MESH_H +#define RSD_MESH_H + +#include <rs_hal.h> + + +bool rsdMeshInit(const android::renderscript::Context *rsc, + const android::renderscript::Mesh *m); +void rsdMeshDraw(const android::renderscript::Context *rsc, + const android::renderscript::Mesh *m, + uint32_t primIndex, uint32_t start, uint32_t len); +void rsdMeshDestroy(const android::renderscript::Context *rsc, + const android::renderscript::Mesh *m); + + +#endif diff --git a/libs/rs/driver/rsdMeshObj.cpp b/libs/rs/driver/rsdMeshObj.cpp new file mode 100644 index 0000000..6bb33f7 --- /dev/null +++ b/libs/rs/driver/rsdMeshObj.cpp @@ -0,0 +1,178 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <GLES/gl.h> +#include <GLES2/gl2.h> +#include <GLES/glext.h> + +#include <rs_hal.h> +#include <rsContext.h> +#include <rsMesh.h> + +#include "rsdMeshObj.h" + +using namespace android; +using namespace android::renderscript; + +RsdMeshObj::RsdMeshObj(const Context *rsc, const Mesh *rsMesh) { + mRSMesh = rsMesh; + + mAttribs = NULL; + mAttribAllocationIndex = NULL; + mGLPrimitives = NULL; + + mAttribCount = 0; +} + +RsdMeshObj::~RsdMeshObj() { + if (mAttribs) { + delete[] mAttribs; + delete[] mAttribAllocationIndex; + } + if (mGLPrimitives) { + delete[] mGLPrimitives; + } +} + +bool RsdMeshObj::isValidGLComponent(const Element *elem, uint32_t fieldIdx) { + // Do not create attribs for padding + if (elem->getFieldName(fieldIdx)[0] == '#') { + return false; + } + + // Only GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_FIXED, GL_FLOAT are accepted. + // Filter rs types accordingly + RsDataType dt = elem->getField(fieldIdx)->getComponent().getType(); + if (dt != RS_TYPE_FLOAT_32 && dt != RS_TYPE_UNSIGNED_8 && + dt != RS_TYPE_UNSIGNED_16 && dt != RS_TYPE_SIGNED_8 && + dt != RS_TYPE_SIGNED_16) { + return false; + } + + // Now make sure they are not arrays + uint32_t arraySize = elem->getFieldArraySize(fieldIdx); + if (arraySize != 1) { + return false; + } + + return true; +} + +bool RsdMeshObj::init() { + + updateGLPrimitives(); + + // Count the number of gl attrs to initialize + mAttribCount = 0; + for (uint32_t ct=0; ct < mRSMesh->mHal.state.vertexBuffersCount; ct++) { + const Element *elem = mRSMesh->mHal.state.vertexBuffers[ct]->getType()->getElement(); + for (uint32_t ct=0; ct < elem->getFieldCount(); ct++) { + if (isValidGLComponent(elem, ct)) { + mAttribCount ++; + } + } + } + + if (mAttribs) { + delete [] mAttribs; + delete [] mAttribAllocationIndex; + mAttribs = NULL; + mAttribAllocationIndex = NULL; + } + if (!mAttribCount) { + return false; + } + + mAttribs = new RsdVertexArray::Attrib[mAttribCount]; + mAttribAllocationIndex = new uint32_t[mAttribCount]; + + uint32_t userNum = 0; + for (uint32_t ct=0; ct < mRSMesh->mHal.state.vertexBuffersCount; ct++) { + const Element *elem = mRSMesh->mHal.state.vertexBuffers[ct]->getType()->getElement(); + uint32_t stride = elem->getSizeBytes(); + for (uint32_t fieldI=0; fieldI < elem->getFieldCount(); fieldI++) { + const Component &c = elem->getField(fieldI)->getComponent(); + + if (!isValidGLComponent(elem, fieldI)) { + continue; + } + + mAttribs[userNum].size = c.getVectorSize(); + mAttribs[userNum].offset = elem->getFieldOffsetBytes(fieldI); + mAttribs[userNum].type = c.getGLType(); + mAttribs[userNum].normalized = c.getType() != RS_TYPE_FLOAT_32;//c.getIsNormalized(); + mAttribs[userNum].stride = stride; + String8 tmp(RS_SHADER_ATTR); + tmp.append(elem->getFieldName(fieldI)); + mAttribs[userNum].name.setTo(tmp.string()); + + // Remember which allocation this attribute came from + mAttribAllocationIndex[userNum] = ct; + userNum ++; + } + } + + return true; +} + +void RsdMeshObj::renderPrimitiveRange(const Context *rsc, uint32_t primIndex, uint32_t start, uint32_t len) const { + if (len < 1 || primIndex >= mRSMesh->mHal.state.primitivesCount || mAttribCount == 0) { + LOGE("Invalid mesh or parameters"); + return; + } + + rsc->checkError("Mesh::renderPrimitiveRange 1"); + // update attributes with either buffer information or data ptr based on their current state + for (uint32_t ct=0; ct < mAttribCount; ct++) { + uint32_t allocIndex = mAttribAllocationIndex[ct]; + Allocation *alloc = mRSMesh->mHal.state.vertexBuffers[allocIndex].get(); + if (alloc->getIsBufferObject() && alloc->getBufferObjectID()) { + mAttribs[ct].buffer = alloc->getBufferObjectID(); + mAttribs[ct].ptr = NULL; + } else { + mAttribs[ct].buffer = 0; + mAttribs[ct].ptr = (const uint8_t*)alloc->getPtr(); + } + } + + RsdVertexArray va(mAttribs, mAttribCount); + va.setupGL2(rsc); + + rsc->checkError("Mesh::renderPrimitiveRange 2"); + Mesh::Primitive_t *prim = mRSMesh->mHal.state.primitives[primIndex]; + if (prim->mIndexBuffer.get()) { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, prim->mIndexBuffer->getBufferObjectID()); + glDrawElements(mGLPrimitives[primIndex], len, GL_UNSIGNED_SHORT, (uint16_t *)(start * 2)); + } else { + glDrawArrays(mGLPrimitives[primIndex], start, len); + } + + rsc->checkError("Mesh::renderPrimitiveRange"); +} + +void RsdMeshObj::updateGLPrimitives() { + mGLPrimitives = new uint32_t[mRSMesh->mHal.state.primitivesCount]; + for (uint32_t i = 0; i < mRSMesh->mHal.state.primitivesCount; i ++) { + switch (mRSMesh->mHal.state.primitives[i]->mPrimitive) { + case RS_PRIMITIVE_POINT: mGLPrimitives[i] = GL_POINTS; break; + case RS_PRIMITIVE_LINE: mGLPrimitives[i] = GL_LINES; break; + case RS_PRIMITIVE_LINE_STRIP: mGLPrimitives[i] = GL_LINE_STRIP; break; + case RS_PRIMITIVE_TRIANGLE: mGLPrimitives[i] = GL_TRIANGLES; break; + case RS_PRIMITIVE_TRIANGLE_STRIP: mGLPrimitives[i] = GL_TRIANGLE_STRIP; break; + case RS_PRIMITIVE_TRIANGLE_FAN: mGLPrimitives[i] = GL_TRIANGLE_FAN; break; + } + } +} diff --git a/libs/rs/driver/rsdMeshObj.h b/libs/rs/driver/rsdMeshObj.h new file mode 100644 index 0000000..8b1271b --- /dev/null +++ b/libs/rs/driver/rsdMeshObj.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_RSD_MESH_OBJ_H +#define ANDROID_RSD_MESH_OBJ_H + +// --------------------------------------------------------------------------- +namespace android { +namespace renderscript { + + class Context; + class Mesh; + class Element; + +} +} + +#include "driver/rsdVertexArray.h" + +// An element is a group of Components that occupies one cell in a structure. +class RsdMeshObj { +public: + RsdMeshObj(const android::renderscript::Context *, + const android::renderscript::Mesh *); + ~RsdMeshObj(); + + void renderPrimitiveRange(const android::renderscript::Context *, uint32_t primIndex, uint32_t start, uint32_t len) const; + + bool init(); + +protected: + const android::renderscript::Mesh *mRSMesh; + + uint32_t *mGLPrimitives; + void updateGLPrimitives(); + + bool isValidGLComponent(const android::renderscript::Element *elem, uint32_t fieldIdx); + // Attribues that allow us to map to GL + RsdVertexArray::Attrib *mAttribs; + // This allows us to figure out which allocation the attribute + // belongs to. In the event the allocation is uploaded to GL + // buffer, it lets us properly map it + uint32_t *mAttribAllocationIndex; + uint32_t mAttribCount; +}; + +#endif //ANDROID_RSD_MESH_OBJ_H + + + diff --git a/libs/rs/driver/rsdProgram.cpp b/libs/rs/driver/rsdProgram.cpp new file mode 100644 index 0000000..502c5ee --- /dev/null +++ b/libs/rs/driver/rsdProgram.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include "rsdCore.h" +#include "rsdProgramVertex.h" +#include "rsdShader.h" +#include "rsdShaderCache.h" + +#include "rsContext.h" +#include "rsProgramVertex.h" +#include "rsProgramFragment.h" + +#include <GLES/gl.h> +#include <GLES/glext.h> +#include <GLES2/gl2.h> +#include <GLES2/gl2ext.h> + +using namespace android; +using namespace android::renderscript; + +bool rsdProgramVertexInit(const Context *rsc, const ProgramVertex *pv, + const char* shader, uint32_t shaderLen) { + RsdShader *drv = new RsdShader(pv, GL_VERTEX_SHADER, shader, shaderLen); + pv->mHal.drv = drv; + + return drv->createShader(); +} + +void rsdProgramVertexSetActive(const Context *rsc, const ProgramVertex *pv) { + RsdHal *dc = (RsdHal *)rsc->mHal.drv; + + dc->gl.shaderCache->setActiveVertex((RsdShader*)pv->mHal.drv); +} + +void rsdProgramVertexDestroy(const Context *rsc, const ProgramVertex *pv) { + RsdHal *dc = (RsdHal *)rsc->mHal.drv; + + RsdShader *drv = NULL; + if(pv->mHal.drv) { + drv = (RsdShader*)pv->mHal.drv; + if (rsc->props.mLogShaders) { + LOGV("Destroying vertex shader with ID %u", drv->getShaderID()); + } + if (drv->getShaderID()) { + dc->gl.shaderCache->cleanupVertex(drv->getShaderID()); + } + delete drv; + } +} + +bool rsdProgramFragmentInit(const Context *rsc, const ProgramFragment *pf, + const char* shader, uint32_t shaderLen) { + RsdShader *drv = new RsdShader(pf, GL_FRAGMENT_SHADER, shader, shaderLen); + pf->mHal.drv = drv; + + return drv->createShader(); +} + +void rsdProgramFragmentSetActive(const Context *rsc, const ProgramFragment *pf) { + RsdHal *dc = (RsdHal *)rsc->mHal.drv; + + dc->gl.shaderCache->setActiveFragment((RsdShader*)pf->mHal.drv); +} + +void rsdProgramFragmentDestroy(const Context *rsc, const ProgramFragment *pf) { + RsdHal *dc = (RsdHal *)rsc->mHal.drv; + + RsdShader *drv = NULL; + if(pf->mHal.drv) { + drv = (RsdShader*)pf->mHal.drv; + if (rsc->props.mLogShaders) { + LOGV("Destroying fragment shader with ID %u", drv->getShaderID()); + } + if (drv->getShaderID()) { + dc->gl.shaderCache->cleanupFragment(drv->getShaderID()); + } + delete drv; + } +} + + diff --git a/libs/rs/driver/rsdProgramFragment.h b/libs/rs/driver/rsdProgramFragment.h new file mode 100644 index 0000000..366cb40 --- /dev/null +++ b/libs/rs/driver/rsdProgramFragment.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef RSD_PROGRAM_FRAGMENT_H +#define RSD_PROGRAM_FRAGMENT_H + +#include <rs_hal.h> + + +bool rsdProgramFragmentInit(const android::renderscript::Context *rsc, + const android::renderscript::ProgramFragment *, + const char* shader, uint32_t shaderLen); +void rsdProgramFragmentSetActive(const android::renderscript::Context *rsc, + const android::renderscript::ProgramFragment *); +void rsdProgramFragmentDestroy(const android::renderscript::Context *rsc, + const android::renderscript::ProgramFragment *); + + +#endif //RSD_PROGRAM_Fragment_H diff --git a/libs/rs/driver/rsdProgramVertex.h b/libs/rs/driver/rsdProgramVertex.h new file mode 100644 index 0000000..e998572 --- /dev/null +++ b/libs/rs/driver/rsdProgramVertex.h @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef RSD_PROGRAM_VERTEX_H +#define RSD_PROGRAM_VERTEX_H + +#include <rs_hal.h> + +bool rsdProgramVertexInit(const android::renderscript::Context *rsc, + const android::renderscript::ProgramVertex *, + const char* shader, uint32_t shaderLen); +void rsdProgramVertexSetActive(const android::renderscript::Context *rsc, + const android::renderscript::ProgramVertex *); +void rsdProgramVertexDestroy(const android::renderscript::Context *rsc, + const android::renderscript::ProgramVertex *); + + +#endif //RSD_PROGRAM_VERTEX_H diff --git a/libs/rs/driver/rsdRuntimeMath.cpp b/libs/rs/driver/rsdRuntimeMath.cpp index 093e311..acb990d 100644 --- a/libs/rs/driver/rsdRuntimeMath.cpp +++ b/libs/rs/driver/rsdRuntimeMath.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/libs/rs/driver/rsdRuntimeStubs.cpp b/libs/rs/driver/rsdRuntimeStubs.cpp index b70a123..9cbff95 100644 --- a/libs/rs/driver/rsdRuntimeStubs.cpp +++ b/libs/rs/driver/rsdRuntimeStubs.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/libs/rs/driver/rsdShader.cpp b/libs/rs/driver/rsdShader.cpp new file mode 100644 index 0000000..fc623d6 --- /dev/null +++ b/libs/rs/driver/rsdShader.cpp @@ -0,0 +1,468 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <GLES2/gl2.h> +#include <GLES2/gl2ext.h> + +#include <rs_hal.h> +#include <rsContext.h> +#include <rsProgram.h> + +#include "rsdShader.h" +#include "rsdShaderCache.h" + +using namespace android; +using namespace android::renderscript; + +RsdShader::RsdShader(const Program *p, uint32_t type, + const char * shaderText, uint32_t shaderLength) { + + mUserShader.setTo(shaderText, shaderLength); + mRSProgram = p; + mType = type; + initMemberVars(); + initAttribAndUniformArray(); + init(); +} + +RsdShader::~RsdShader() { + if (mShaderID) { + glDeleteShader(mShaderID); + } + + delete[] mAttribNames; + delete[] mUniformNames; + delete[] mUniformArraySizes; +} + +void RsdShader::initMemberVars() { + mDirty = true; + mShaderID = 0; + mAttribCount = 0; + mUniformCount = 0; + + mAttribNames = NULL; + mUniformNames = NULL; + mUniformArraySizes = NULL; + + mIsValid = false; +} + +void RsdShader::init() { + uint32_t attribCount = 0; + uint32_t uniformCount = 0; + for (uint32_t ct=0; ct < mRSProgram->mHal.state.inputElementsCount; ct++) { + initAddUserElement(mRSProgram->mHal.state.inputElements[ct].get(), mAttribNames, NULL, &attribCount, RS_SHADER_ATTR); + } + for (uint32_t ct=0; ct < mRSProgram->mHal.state.constantsCount; ct++) { + initAddUserElement(mRSProgram->mHal.state.constantTypes[ct]->getElement(), mUniformNames, mUniformArraySizes, &uniformCount, RS_SHADER_UNI); + } + + mTextureUniformIndexStart = uniformCount; + char buf[256]; + for (uint32_t ct=0; ct < mRSProgram->mHal.state.texturesCount; ct++) { + snprintf(buf, sizeof(buf), "UNI_Tex%i", ct); + mUniformNames[uniformCount].setTo(buf); + mUniformArraySizes[uniformCount] = 1; + uniformCount++; + } +} + +String8 RsdShader::getGLSLInputString() const { + String8 s; + for (uint32_t ct=0; ct < mRSProgram->mHal.state.inputElementsCount; ct++) { + const Element *e = mRSProgram->mHal.state.inputElements[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; +} + +void RsdShader::appendAttributes() { + for (uint32_t ct=0; ct < mRSProgram->mHal.state.inputElementsCount; ct++) { + const Element *e = mRSProgram->mHal.state.inputElements[ct].get(); + for (uint32_t field=0; field < e->getFieldCount(); field++) { + const Element *f = e->getField(field); + const char *fn = e->getFieldName(field); + + if (fn[0] == '#') { + continue; + } + + // Cannot be complex + rsAssert(!f->getFieldCount()); + switch (f->getComponent().getVectorSize()) { + case 1: mShader.append("attribute float ATTRIB_"); break; + case 2: mShader.append("attribute vec2 ATTRIB_"); break; + case 3: mShader.append("attribute vec3 ATTRIB_"); break; + case 4: mShader.append("attribute vec4 ATTRIB_"); break; + default: + rsAssert(0); + } + + mShader.append(fn); + mShader.append(";\n"); + } + } +} + +void RsdShader::appendTextures() { + char buf[256]; + for (uint32_t ct=0; ct < mRSProgram->mHal.state.texturesCount; ct++) { + if (mRSProgram->mHal.state.textureTargets[ct] == RS_TEXTURE_2D) { + snprintf(buf, sizeof(buf), "uniform sampler2D UNI_Tex%i;\n", ct); + } else { + snprintf(buf, sizeof(buf), "uniform samplerCube UNI_Tex%i;\n", ct); + } + mShader.append(buf); + } +} + +bool RsdShader::createShader() { + + if (mType == GL_FRAGMENT_SHADER) { + mShader.append("precision mediump float;\n"); + } + appendUserConstants(); + appendAttributes(); + appendTextures(); + + mShader.append(mUserShader); + + return true; +} + +bool RsdShader::loadShader(const Context *rsc) { + mShaderID = glCreateShader(mType); + rsAssert(mShaderID); + + if (rsc->props.mLogShaders) { + LOGV("Loading shader type %x, ID %i", mType, mShaderID); + LOGV("%s", 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; + rsc->setError(RS_ERROR_BAD_SHADER, "Error returned from GL driver loading shader text,"); + return false; + } + } + } + + if (rsc->props.mLogShaders) { + LOGV("--Shader load result %x ", glGetError()); + } + mIsValid = true; + return true; +} + +void RsdShader::appendUserConstants() { + for (uint32_t ct=0; ct < mRSProgram->mHal.state.constantsCount; ct++) { + const Element *e = mRSProgram->mHal.state.constantTypes[ct]->getElement(); + for (uint32_t field=0; field < e->getFieldCount(); field++) { + const Element *f = e->getField(field); + const char *fn = e->getFieldName(field); + + if (fn[0] == '#') { + continue; + } + + // Cannot be complex + rsAssert(!f->getFieldCount()); + if (f->getType() == RS_TYPE_MATRIX_4X4) { + mShader.append("uniform mat4 UNI_"); + } else if (f->getType() == RS_TYPE_MATRIX_3X3) { + mShader.append("uniform mat3 UNI_"); + } else if (f->getType() == RS_TYPE_MATRIX_2X2) { + mShader.append("uniform mat2 UNI_"); + } else { + switch (f->getComponent().getVectorSize()) { + case 1: mShader.append("uniform float UNI_"); break; + case 2: mShader.append("uniform vec2 UNI_"); break; + case 3: mShader.append("uniform vec3 UNI_"); break; + case 4: mShader.append("uniform vec4 UNI_"); break; + default: + rsAssert(0); + } + } + + mShader.append(fn); + if (e->getFieldArraySize(field) > 1) { + mShader.appendFormat("[%d]", e->getFieldArraySize(field)); + } + mShader.append(";\n"); + } + } +} + +void RsdShader::logUniform(const Element *field, const float *fd, uint32_t arraySize ) { + RsDataType dataType = field->getType(); + uint32_t elementSize = field->getSizeBytes() / sizeof(float); + for (uint32_t i = 0; i < arraySize; i ++) { + if (arraySize > 1) { + LOGV("Array Element [%u]", i); + } + if (dataType == RS_TYPE_MATRIX_4X4) { + LOGV("Matrix4x4"); + LOGV("{%f, %f, %f, %f", fd[0], fd[4], fd[8], fd[12]); + LOGV(" %f, %f, %f, %f", fd[1], fd[5], fd[9], fd[13]); + LOGV(" %f, %f, %f, %f", fd[2], fd[6], fd[10], fd[14]); + LOGV(" %f, %f, %f, %f}", fd[3], fd[7], fd[11], fd[15]); + } else if (dataType == RS_TYPE_MATRIX_3X3) { + LOGV("Matrix3x3"); + LOGV("{%f, %f, %f", fd[0], fd[3], fd[6]); + LOGV(" %f, %f, %f", fd[1], fd[4], fd[7]); + LOGV(" %f, %f, %f}", fd[2], fd[5], fd[8]); + } else if (dataType == RS_TYPE_MATRIX_2X2) { + LOGV("Matrix2x2"); + LOGV("{%f, %f", fd[0], fd[2]); + LOGV(" %f, %f}", fd[1], fd[3]); + } else { + switch (field->getComponent().getVectorSize()) { + case 1: + LOGV("Uniform 1 = %f", fd[0]); + break; + case 2: + LOGV("Uniform 2 = %f %f", fd[0], fd[1]); + break; + case 3: + LOGV("Uniform 3 = %f %f %f", fd[0], fd[1], fd[2]); + break; + case 4: + LOGV("Uniform 4 = %f %f %f %f", fd[0], fd[1], fd[2], fd[3]); + break; + default: + rsAssert(0); + } + } + LOGE("Element size %u data=%p", elementSize, fd); + fd += elementSize; + LOGE("New data=%p", fd); + } +} + +void RsdShader::setUniform(const Context *rsc, const Element *field, const float *fd, + int32_t slot, uint32_t arraySize ) { + RsDataType dataType = field->getType(); + if (dataType == RS_TYPE_MATRIX_4X4) { + glUniformMatrix4fv(slot, arraySize, GL_FALSE, fd); + } else if (dataType == RS_TYPE_MATRIX_3X3) { + glUniformMatrix3fv(slot, arraySize, GL_FALSE, fd); + } else if (dataType == RS_TYPE_MATRIX_2X2) { + glUniformMatrix2fv(slot, arraySize, GL_FALSE, fd); + } else { + switch (field->getComponent().getVectorSize()) { + case 1: + glUniform1fv(slot, arraySize, fd); + break; + case 2: + glUniform2fv(slot, arraySize, fd); + break; + case 3: + glUniform3fv(slot, arraySize, fd); + break; + case 4: + glUniform4fv(slot, arraySize, fd); + break; + default: + rsAssert(0); + } + } +} + +void RsdShader::setupTextures(const Context *rsc, RsdShaderCache *sc) { + if (mRSProgram->mHal.state.texturesCount == 0) { + return; + } + + uint32_t numTexturesToBind = mRSProgram->mHal.state.texturesCount; + uint32_t numTexturesAvailable = rsc->getMaxFragmentTextures(); + if (numTexturesToBind >= numTexturesAvailable) { + LOGE("Attempting to bind %u textures on shader id %u, but only %u are available", + mRSProgram->mHal.state.texturesCount, (uint32_t)this, numTexturesAvailable); + rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind more textuers than available"); + numTexturesToBind = numTexturesAvailable; + } + + for (uint32_t ct=0; ct < numTexturesToBind; ct++) { + glActiveTexture(GL_TEXTURE0 + ct); + if (!mRSProgram->mHal.state.textures[ct].get()) { + LOGE("No texture bound for shader id %u, texture unit %u", (uint)this, ct); + rsc->setError(RS_ERROR_BAD_SHADER, "No texture bound"); + continue; + } + + GLenum target = (GLenum)mRSProgram->mHal.state.textures[ct]->getGLTarget(); + if (target != GL_TEXTURE_2D && target != GL_TEXTURE_CUBE_MAP) { + LOGE("Attempting to bind unknown texture to shader id %u, texture unit %u", (uint)this, ct); + rsc->setError(RS_ERROR_BAD_SHADER, "Non-texture allocation bound to a shader"); + } + glBindTexture(target, mRSProgram->mHal.state.textures[ct]->getTextureID()); + rsc->checkError("ProgramFragment::setupGL2 tex bind"); + if (mRSProgram->mHal.state.samplers[ct].get()) { + mRSProgram->mHal.state.samplers[ct]->setupGL(rsc, mRSProgram->mHal.state.textures[ct].get()); + } else { + glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + rsc->checkError("ProgramFragment::setupGL2 tex env"); + } + + glUniform1i(sc->fragUniformSlot(mTextureUniformIndexStart + ct), ct); + rsc->checkError("ProgramFragment::setupGL2 uniforms"); + } + + glActiveTexture(GL_TEXTURE0); + mDirty = false; + rsc->checkError("ProgramFragment::setupGL2"); +} + +void RsdShader::setupUserConstants(const Context *rsc, RsdShaderCache *sc, bool isFragment) { + uint32_t uidx = 0; + for (uint32_t ct=0; ct < mRSProgram->mHal.state.constantsCount; ct++) { + Allocation *alloc = mRSProgram->mHal.state.constants[ct].get(); + if (!alloc) { + LOGE("Attempting to set constants on shader id %u, but alloc at slot %u is not set", (uint32_t)this, ct); + rsc->setError(RS_ERROR_BAD_SHADER, "No constant allocation bound"); + continue; + } + + const uint8_t *data = static_cast<const uint8_t *>(alloc->getPtr()); + const Element *e = mRSProgram->mHal.state.constantTypes[ct]->getElement(); + for (uint32_t field=0; field < e->getFieldCount(); field++) { + const Element *f = e->getField(field); + const char *fieldName = e->getFieldName(field); + // If this field is padding, skip it + if (fieldName[0] == '#') { + continue; + } + + uint32_t offset = e->getFieldOffsetBytes(field); + const float *fd = reinterpret_cast<const float *>(&data[offset]); + + int32_t slot = -1; + uint32_t arraySize = 1; + if (!isFragment) { + slot = sc->vtxUniformSlot(uidx); + arraySize = sc->vtxUniformSize(uidx); + } else { + slot = sc->fragUniformSlot(uidx); + arraySize = sc->fragUniformSize(uidx); + } + if (rsc->props.mLogShadersUniforms) { + LOGV("Uniform slot=%i, offset=%i, constant=%i, field=%i, uidx=%i, name=%s", slot, offset, ct, field, uidx, fieldName); + } + uidx ++; + if (slot < 0) { + continue; + } + + if (rsc->props.mLogShadersUniforms) { + logUniform(f, fd, arraySize); + } + setUniform(rsc, f, fd, slot, arraySize); + } + } +} + +void RsdShader::setup(const android::renderscript::Context *rsc, RsdShaderCache *sc) { + + setupUserConstants(rsc, sc, mType == GL_FRAGMENT_SHADER); + setupTextures(rsc, sc); +} + +void RsdShader::initAttribAndUniformArray() { + mAttribCount = 0; + for (uint32_t ct=0; ct < mRSProgram->mHal.state.inputElementsCount; ct++) { + const Element *elem = mRSProgram->mHal.state.inputElements[ct].get(); + for (uint32_t field=0; field < elem->getFieldCount(); field++) { + if (elem->getFieldName(field)[0] != '#') { + mAttribCount ++; + } + } + } + + mUniformCount = 0; + for (uint32_t ct=0; ct < mRSProgram->mHal.state.constantsCount; ct++) { + const Element *elem = mRSProgram->mHal.state.constantTypes[ct]->getElement(); + + for (uint32_t field=0; field < elem->getFieldCount(); field++) { + if (elem->getFieldName(field)[0] != '#') { + mUniformCount ++; + } + } + } + mUniformCount += mRSProgram->mHal.state.texturesCount; + + if (mAttribCount) { + mAttribNames = new String8[mAttribCount]; + } + if (mUniformCount) { + mUniformNames = new String8[mUniformCount]; + mUniformArraySizes = new uint32_t[mUniformCount]; + } +} + +void RsdShader::initAddUserElement(const Element *e, String8 *names, uint32_t *arrayLengths, uint32_t *count, const char *prefix) { + rsAssert(e->getFieldCount()); + for (uint32_t ct=0; ct < e->getFieldCount(); ct++) { + const Element *ce = e->getField(ct); + if (ce->getFieldCount()) { + initAddUserElement(ce, names, arrayLengths, count, prefix); + } else if (e->getFieldName(ct)[0] != '#') { + String8 tmp(prefix); + tmp.append(e->getFieldName(ct)); + names[*count].setTo(tmp.string()); + if (arrayLengths) { + arrayLengths[*count] = e->getFieldArraySize(ct); + } + (*count)++; + } + } +} diff --git a/libs/rs/driver/rsdShader.h b/libs/rs/driver/rsdShader.h new file mode 100644 index 0000000..37b1c3d --- /dev/null +++ b/libs/rs/driver/rsdShader.h @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_RSD_SHADER_H +#define ANDROID_RSD_SHADER_H + +#include <utils/String8.h> + +// --------------------------------------------------------------------------- +namespace android { +namespace renderscript { + +class Element; +class Context; +class Program; + +} +} + +class RsdShaderCache; + +#define RS_SHADER_ATTR "ATTRIB_" +#define RS_SHADER_UNI "UNI_" + +class RsdShader { +public: + + RsdShader(const android::renderscript::Program *p, uint32_t type, + const char * shaderText, uint32_t shaderLength); + virtual ~RsdShader(); + + bool createShader(); + + uint32_t getShaderID() const {return mShaderID;} + + uint32_t getAttribCount() const {return mAttribCount;} + uint32_t getUniformCount() const {return mUniformCount;} + const android::String8 & getAttribName(uint32_t i) const {return mAttribNames[i];} + const android::String8 & getUniformName(uint32_t i) const {return mUniformNames[i];} + uint32_t getUniformArraySize(uint32_t i) const {return mUniformArraySizes[i];} + + android::String8 getGLSLInputString() const; + + bool isValid() const {return mIsValid;} + void forceDirty() const {mDirty = true;} + + bool loadShader(const android::renderscript::Context *); + void setup(const android::renderscript::Context *, RsdShaderCache *sc); + +protected: + + const android::renderscript::Program *mRSProgram; + bool mIsValid; + + // Applies to vertex and fragment shaders only + void appendUserConstants(); + void setupUserConstants(const android::renderscript::Context *rsc, RsdShaderCache *sc, bool isFragment); + void initAddUserElement(const android::renderscript::Element *e, android::String8 *names, uint32_t *arrayLengths, uint32_t *count, const char *prefix); + void setupTextures(const android::renderscript::Context *rsc, RsdShaderCache *sc); + + void appendAttributes(); + void appendTextures(); + + void initAttribAndUniformArray(); + + mutable bool mDirty; + android::String8 mShader; + android::String8 mUserShader; + uint32_t mShaderID; + uint32_t mType; + + uint32_t mTextureCount; + uint32_t mAttribCount; + uint32_t mUniformCount; + android::String8 *mAttribNames; + android::String8 *mUniformNames; + uint32_t *mUniformArraySizes; + + int32_t mTextureUniformIndexStart; + + void logUniform(const android::renderscript::Element *field, const float *fd, uint32_t arraySize ); + void setUniform(const android::renderscript::Context *rsc, const android::renderscript::Element *field, const float *fd, int32_t slot, uint32_t arraySize ); + void initMemberVars(); + void init(); +}; + +#endif //ANDROID_RSD_SHADER_H + + + + diff --git a/libs/rs/rsShaderCache.cpp b/libs/rs/driver/rsdShaderCache.cpp index e8d89c2..18a8225 100644 --- a/libs/rs/rsShaderCache.cpp +++ b/libs/rs/driver/rsdShaderCache.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,25 +14,30 @@ * limitations under the License. */ -#include "rsContext.h" -#ifndef ANDROID_RS_SERIALIZE +#include <rs_hal.h> +#include <rsContext.h> + +#include "rsdShader.h" +#include "rsdShaderCache.h" + #include <GLES/gl.h> #include <GLES2/gl2.h> -#endif //ANDROID_RS_SERIALIZE using namespace android; using namespace android::renderscript; -ShaderCache::ShaderCache() { +RsdShaderCache::RsdShaderCache() { mEntries.setCapacity(16); + mVertexDirty = true; + mFragmentDirty = true; } -ShaderCache::~ShaderCache() { +RsdShaderCache::~RsdShaderCache() { cleanupAll(); } -void ShaderCache::updateUniformArrayData(Context *rsc, Program *prog, uint32_t linkedID, +void RsdShaderCache::updateUniformArrayData(const Context *rsc, RsdShader *prog, uint32_t linkedID, UniformData *data, const char* logTag, UniformQueryData **uniformList, uint32_t uniListSize) { @@ -54,14 +59,14 @@ void ShaderCache::updateUniformArrayData(Context *rsc, Program *prog, uint32_t l } } -void ShaderCache::populateUniformData(Program *prog, uint32_t linkedID, UniformData *data) { +void RsdShaderCache::populateUniformData(RsdShader *prog, uint32_t linkedID, UniformData *data) { for (uint32_t ct=0; ct < prog->getUniformCount(); ct++) { data[ct].slot = glGetUniformLocation(linkedID, prog->getUniformName(ct)); data[ct].arraySize = prog->getUniformArraySize(ct); } } -bool ShaderCache::hasArrayUniforms(ProgramVertex *vtx, ProgramFragment *frag) { +bool RsdShaderCache::hasArrayUniforms(RsdShader *vtx, RsdShader *frag) { UniformData *data = mCurrent->vtxUniforms; for (uint32_t ct=0; ct < vtx->getUniformCount(); ct++) { if (data[ct].slot >= 0 && data[ct].arraySize > 1) { @@ -77,7 +82,31 @@ bool ShaderCache::hasArrayUniforms(ProgramVertex *vtx, ProgramFragment *frag) { return false; } -bool ShaderCache::lookup(Context *rsc, ProgramVertex *vtx, ProgramFragment *frag) { +bool RsdShaderCache::setup(const Context *rsc) { + if (!mVertexDirty && !mFragmentDirty) { + return true; + } + + if (!link(rsc)) { + return false; + } + + if (mFragmentDirty) { + mFragment->setup(rsc, this); + mFragmentDirty = false; + } + if (mVertexDirty) { + mVertex->setup(rsc, this); + mVertexDirty = false; + } + + return true; +} + +bool RsdShaderCache::link(const Context *rsc) { + + RsdShader *vtx = mVertex; + RsdShader *frag = mFragment; if (!vtx->getShaderID()) { vtx->loadShader(rsc); } @@ -89,7 +118,7 @@ bool ShaderCache::lookup(Context *rsc, ProgramVertex *vtx, ProgramFragment *frag if (!vtx->getShaderID() || !frag->getShaderID()) { return false; } - //LOGV("ShaderCache lookup vtx %i, frag %i", vtx->getShaderID(), frag->getShaderID()); + //LOGV("rsdShaderCache lookup vtx %i, frag %i", vtx->getShaderID(), frag->getShaderID()); uint32_t entryCount = mEntries.size(); for (uint32_t ct = 0; ct < entryCount; ct ++) { if ((mEntries[ct]->vtx == vtx->getShaderID()) && @@ -98,13 +127,13 @@ bool ShaderCache::lookup(Context *rsc, ProgramVertex *vtx, ProgramFragment *frag //LOGV("SC using program %i", mEntries[ct]->program); glUseProgram(mEntries[ct]->program); mCurrent = mEntries[ct]; - //LOGV("ShaderCache hit, using %i", ct); - rsc->checkError("ShaderCache::lookup (hit)"); + //LOGV("RsdShaderCache hit, using %i", ct); + rsc->checkError("RsdShaderCache::link (hit)"); return true; } } - //LOGV("ShaderCache miss"); + //LOGV("RsdShaderCache miss"); //LOGE("e0 %x", glGetError()); ProgramEntry *e = new ProgramEntry(vtx->getAttribCount(), vtx->getUniformCount(), @@ -120,12 +149,10 @@ bool ShaderCache::lookup(Context *rsc, ProgramVertex *vtx, ProgramFragment *frag //LOGE("e1 %x", glGetError()); glAttachShader(pgm, frag->getShaderID()); - if (!vtx->isUserProgram()) { - glBindAttribLocation(pgm, 0, "ATTRIB_position"); - glBindAttribLocation(pgm, 1, "ATTRIB_color"); - glBindAttribLocation(pgm, 2, "ATTRIB_normal"); - glBindAttribLocation(pgm, 3, "ATTRIB_texture0"); - } + glBindAttribLocation(pgm, 0, "ATTRIB_position"); + glBindAttribLocation(pgm, 1, "ATTRIB_color"); + glBindAttribLocation(pgm, 2, "ATTRIB_normal"); + glBindAttribLocation(pgm, 3, "ATTRIB_texture0"); //LOGE("e2 %x", glGetError()); glLinkProgram(pgm); @@ -203,11 +230,12 @@ bool ShaderCache::lookup(Context *rsc, ProgramVertex *vtx, ProgramFragment *frag //LOGV("SC made program %i", e->program); glUseProgram(e->program); - rsc->checkError("ShaderCache::lookup (miss)"); + rsc->checkError("RsdShaderCache::link (miss)"); + return true; } -int32_t ShaderCache::vtxAttribSlot(const String8 &attrName) const { +int32_t RsdShaderCache::vtxAttribSlot(const String8 &attrName) const { for (uint32_t ct=0; ct < mCurrent->vtxAttrCount; ct++) { if (attrName == mCurrent->vtxAttrs[ct].name) { return mCurrent->vtxAttrs[ct].slot; @@ -216,7 +244,7 @@ int32_t ShaderCache::vtxAttribSlot(const String8 &attrName) const { return -1; } -void ShaderCache::cleanupVertex(uint32_t id) { +void RsdShaderCache::cleanupVertex(uint32_t id) { int32_t numEntries = (int32_t)mEntries.size(); for (int32_t ct = 0; ct < numEntries; ct ++) { if (mEntries[ct]->vtx == id) { @@ -230,7 +258,7 @@ void ShaderCache::cleanupVertex(uint32_t id) { } } -void ShaderCache::cleanupFragment(uint32_t id) { +void RsdShaderCache::cleanupFragment(uint32_t id) { int32_t numEntries = (int32_t)mEntries.size(); for (int32_t ct = 0; ct < numEntries; ct ++) { if (mEntries[ct]->frag == id) { @@ -244,7 +272,7 @@ void ShaderCache::cleanupFragment(uint32_t id) { } } -void ShaderCache::cleanupAll() { +void RsdShaderCache::cleanupAll() { for (uint32_t ct=0; ct < mEntries.size(); ct++) { glDeleteProgram(mEntries[ct]->program); free(mEntries[ct]); diff --git a/libs/rs/rsShaderCache.h b/libs/rs/driver/rsdShaderCache.h index 3540366..17ee3e8 100644 --- a/libs/rs/rsShaderCache.h +++ b/libs/rs/driver/rsdShaderCache.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,38 +14,59 @@ * limitations under the License. */ -#ifndef ANDROID_SHADER_CACHE_H -#define ANDROID_SHADER_CACHE_H +#ifndef ANDROID_RSD_SHADER_CACHE_H +#define ANDROID_RSD_SHADER_CACHE_H - -#include "rsObjectBase.h" -#include "rsVertexArray.h" - -// --------------------------------------------------------------------------- namespace android { namespace renderscript { +class Context; + +} +} + +#include <utils/String8.h> +#include <utils/Vector.h> +class RsdShader; + +// --------------------------------------------------------------------------- // An element is a group of Components that occupies one cell in a structure. -class ShaderCache { +class RsdShaderCache { public: - ShaderCache(); - virtual ~ShaderCache(); + RsdShaderCache(); + virtual ~RsdShaderCache(); + + void setActiveVertex(RsdShader *pv) { + mVertexDirty = true; + mVertex = pv; + } - bool lookup(Context *rsc, ProgramVertex *, ProgramFragment *); + void setActiveFragment(RsdShader *pf) { + mFragmentDirty = true; + mFragment = pf; + } + + bool setup(const android::renderscript::Context *rsc); void cleanupVertex(uint32_t id); void cleanupFragment(uint32_t id); void cleanupAll(); - int32_t vtxAttribSlot(const String8 &attrName) const; + int32_t vtxAttribSlot(const android::String8 &attrName) const; int32_t vtxUniformSlot(uint32_t a) const {return mCurrent->vtxUniforms[a].slot;} uint32_t vtxUniformSize(uint32_t a) const {return mCurrent->vtxUniforms[a].arraySize;} int32_t fragUniformSlot(uint32_t a) const {return mCurrent->fragUniforms[a].slot;} uint32_t fragUniformSize(uint32_t a) const {return mCurrent->fragUniforms[a].arraySize;} protected: + bool link(const android::renderscript::Context *rsc); + bool mFragmentDirty; + bool mVertexDirty; + RsdShader *mVertex; + RsdShader *mFragment; + struct UniformQueryData { char *name; uint32_t nameLength; @@ -111,21 +132,19 @@ protected: UniformData *vtxUniforms; UniformData *fragUniforms; }; - Vector<ProgramEntry*> mEntries; + android::Vector<ProgramEntry*> mEntries; ProgramEntry *mCurrent; - bool hasArrayUniforms(ProgramVertex *vtx, ProgramFragment *frag); - void populateUniformData(Program *prog, uint32_t linkedID, UniformData *data); - void updateUniformArrayData(Context *rsc, Program *prog, uint32_t linkedID, + bool hasArrayUniforms(RsdShader *vtx, RsdShader *frag); + void populateUniformData(RsdShader *prog, uint32_t linkedID, UniformData *data); + void updateUniformArrayData(const android::renderscript::Context *rsc, + RsdShader *prog, uint32_t linkedID, UniformData *data, const char* logTag, UniformQueryData **uniformList, uint32_t uniListSize); }; - -} -} -#endif //ANDROID_SHADER_CACHE_H +#endif //ANDROID_RSD_SHADER_CACHE_H diff --git a/libs/rs/rsVertexArray.cpp b/libs/rs/driver/rsdVertexArray.cpp index 354ee89..d0a5a54 100644 --- a/libs/rs/rsVertexArray.cpp +++ b/libs/rs/driver/rsdVertexArray.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,28 +14,32 @@ * limitations under the License. */ -#include "rsContext.h" -#ifndef ANDROID_RS_SERIALIZE +#include <rs_hal.h> +#include <rsContext.h> + #include <GLES/gl.h> #include <GLES2/gl2.h> -#endif + +#include "rsdCore.h" +#include "rsdVertexArray.h" +#include "rsdShaderCache.h" using namespace android; using namespace android::renderscript; -VertexArray::VertexArray(const Attrib *attribs, uint32_t numAttribs) { +RsdVertexArray::RsdVertexArray(const Attrib *attribs, uint32_t numAttribs) { mAttribs = attribs; mCount = numAttribs; } -VertexArray::~VertexArray() { +RsdVertexArray::~RsdVertexArray() { } -VertexArray::Attrib::Attrib() { +RsdVertexArray::Attrib::Attrib() { clear(); } -void VertexArray::Attrib::clear() { +void RsdVertexArray::Attrib::clear() { buffer = 0; offset = 0; type = 0; @@ -46,7 +50,7 @@ void VertexArray::Attrib::clear() { name.setTo(""); } -void VertexArray::Attrib::set(uint32_t type, uint32_t size, uint32_t stride, +void RsdVertexArray::Attrib::set(uint32_t type, uint32_t size, uint32_t stride, bool normalized, uint32_t offset, const char *name) { clear(); @@ -58,7 +62,7 @@ void VertexArray::Attrib::set(uint32_t type, uint32_t size, uint32_t stride, this->name.setTo(name); } -void VertexArray::logAttrib(uint32_t idx, uint32_t slot) const { +void RsdVertexArray::logAttrib(uint32_t idx, uint32_t slot) const { if (idx == 0) { LOGV("Starting vertex attribute binding"); } @@ -74,11 +78,15 @@ void VertexArray::logAttrib(uint32_t idx, uint32_t slot) const { mAttribs[idx].offset); } -void VertexArray::setupGL2(const Context *rsc, - class VertexArrayState *state, - ShaderCache *sc) const { - rsc->checkError("VertexArray::setupGL2 start"); +void RsdVertexArray::setupGL2(const Context *rsc) const { + + RsdHal *dc = (RsdHal *)rsc->mHal.drv; + RsdVertexArrayState *state = dc->gl.vertexArrayState; + RsdShaderCache *sc = dc->gl.shaderCache; + + rsc->checkError("RsdVertexArray::setupGL2 start"); uint32_t maxAttrs = state->mAttrsEnabledSize; + for (uint32_t ct=1; ct < maxAttrs; ct++) { if(state->mAttrsEnabled[ct]) { glDisableVertexAttribArray(ct); @@ -86,7 +94,7 @@ void VertexArray::setupGL2(const Context *rsc, } } - rsc->checkError("VertexArray::setupGL2 disabled"); + rsc->checkError("RsdVertexArray::setupGL2 disabled"); for (uint32_t ct=0; ct < mCount; ct++) { int32_t slot = sc->vtxAttribSlot(mAttribs[ct].name); if (rsc->props.mLogShadersAttr) { @@ -105,22 +113,22 @@ void VertexArray::setupGL2(const Context *rsc, mAttribs[ct].stride, mAttribs[ct].ptr + mAttribs[ct].offset); } - rsc->checkError("VertexArray::setupGL2 done"); + rsc->checkError("RsdVertexArray::setupGL2 done"); } //////////////////////////////////////////// -VertexArrayState::VertexArrayState() { +RsdVertexArrayState::RsdVertexArrayState() { mAttrsEnabled = NULL; mAttrsEnabledSize = 0; } -VertexArrayState::~VertexArrayState() { +RsdVertexArrayState::~RsdVertexArrayState() { if (mAttrsEnabled) { delete[] mAttrsEnabled; mAttrsEnabled = NULL; } } -void VertexArrayState::init(Context *rsc) { - mAttrsEnabledSize = rsc->getMaxVertexAttributes(); +void RsdVertexArrayState::init(uint32_t maxAttrs) { + mAttrsEnabledSize = maxAttrs; mAttrsEnabled = new bool[mAttrsEnabledSize]; for (uint32_t ct = 0; ct < mAttrsEnabledSize; ct++) { mAttrsEnabled[ct] = false; diff --git a/libs/rs/rsVertexArray.h b/libs/rs/driver/rsdVertexArray.h index 45d9e82..925a6ae 100644 --- a/libs/rs/rsVertexArray.h +++ b/libs/rs/driver/rsdVertexArray.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,20 +14,21 @@ * limitations under the License. */ -#ifndef ANDROID_VERTEX_ARRAY_H -#define ANDROID_VERTEX_ARRAY_H +#ifndef ANDROID_RSD_VERTEX_ARRAY_H +#define ANDROID_RSD_VERTEX_ARRAY_H - -#include "rsObjectBase.h" - -// --------------------------------------------------------------------------- namespace android { namespace renderscript { -class ShaderCache; +class Context; + +} +} + +#include <utils/String8.h> // An element is a group of Components that occupies one cell in a structure. -class VertexArray { +class RsdVertexArray { public: class Attrib { public: @@ -38,17 +39,17 @@ public: uint32_t size; uint32_t stride; bool normalized; - String8 name; + android::String8 name; Attrib(); void clear(); void set(uint32_t type, uint32_t size, uint32_t stride, bool normalized, uint32_t offset, const char *name); }; - VertexArray(const Attrib *attribs, uint32_t numAttribs); - virtual ~VertexArray(); + RsdVertexArray(const Attrib *attribs, uint32_t numAttribs); + virtual ~RsdVertexArray(); - void setupGL2(const Context *rsc, class VertexArrayState *, ShaderCache *) const; + void setupGL2(const android::renderscript::Context *rsc) const; void logAttrib(uint32_t idx, uint32_t slot) const; protected: @@ -61,20 +62,18 @@ protected: }; -class VertexArrayState { +class RsdVertexArrayState { public: - VertexArrayState(); - ~VertexArrayState(); - void init(Context *); + RsdVertexArrayState(); + ~RsdVertexArrayState(); + void init(uint32_t maxAttrs); bool *mAttrsEnabled; uint32_t mAttrsEnabledSize; }; -} -} -#endif //ANDROID_VERTEX_ARRAY_H +#endif //ANDROID_RSD_VERTEX_ARRAY_H diff --git a/libs/rs/rs.spec b/libs/rs/rs.spec index 6310cf6..0c4e1ed 100644 --- a/libs/rs/rs.spec +++ b/libs/rs/rs.spec @@ -66,7 +66,7 @@ ContextDeinitToClient { direct } -aTypeCreate { +TypeCreate { direct param RsElement e param uint32_t dimX @@ -77,7 +77,7 @@ aTypeCreate { ret RsType } -aAllocationCreateTyped { +AllocationCreateTyped { direct param RsType vtype param RsAllocationMipmapControl mips @@ -85,7 +85,7 @@ aAllocationCreateTyped { ret RsAllocation } -aAllocationCreateFromBitmap { +AllocationCreateFromBitmap { direct param RsType vtype param RsAllocationMipmapControl mips @@ -94,7 +94,7 @@ aAllocationCreateFromBitmap { ret RsAllocation } -aAllocationCubeCreateFromBitmap { +AllocationCubeCreateFromBitmap { direct param RsType vtype param RsAllocationMipmapControl mips diff --git a/libs/rs/rsAllocation.cpp b/libs/rs/rsAllocation.cpp index 743b2c4..b5f6f56 100644 --- a/libs/rs/rsAllocation.cpp +++ b/libs/rs/rsAllocation.cpp @@ -649,11 +649,12 @@ void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) { // #ifndef ANDROID_RS_SERIALIZE -static void rsaAllocationGenerateScriptMips(RsContext con, RsAllocation va); namespace android { namespace renderscript { +static void AllocationGenerateScriptMips(RsContext con, RsAllocation va); + void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel) { Allocation *alloc = static_cast<Allocation *>(va); alloc->deferredUploadToTexture(rsc); @@ -740,7 +741,7 @@ void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) { Allocation *texAlloc = static_cast<Allocation *>(va); - rsaAllocationGenerateScriptMips(rsc, texAlloc); + AllocationGenerateScriptMips(rsc, texAlloc); } void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) { @@ -795,10 +796,7 @@ void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32 a->resize2D(rsc, dimX, dimY); } -} -} - -static void rsaAllocationGenerateScriptMips(RsContext con, RsAllocation va) { +static void AllocationGenerateScriptMips(RsContext con, RsAllocation va) { Context *rsc = static_cast<Context *>(con); Allocation *texAlloc = static_cast<Allocation *>(va); uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1; @@ -815,29 +813,20 @@ static void rsaAllocationGenerateScriptMips(RsContext con, RsAllocation va) { } } -const void * rsaAllocationGetType(RsContext con, RsAllocation va) { - Allocation *a = static_cast<Allocation *>(va); - a->getType()->incUserRef(); - - return a->getType(); -} - -RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype, - RsAllocationMipmapControl mips, - uint32_t usages) { - Context *rsc = static_cast<Context *>(con); +RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype, + RsAllocationMipmapControl mips, + uint32_t usages) { Allocation * alloc = new Allocation(rsc, static_cast<Type *>(vtype), usages, mips); alloc->incUserRef(); return alloc; } -RsAllocation rsaAllocationCreateFromBitmap(RsContext con, RsType vtype, - RsAllocationMipmapControl mips, - const void *data, size_t data_length, uint32_t usages) { - Context *rsc = static_cast<Context *>(con); +RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype, + RsAllocationMipmapControl mips, + const void *data, size_t data_length, uint32_t usages) { Type *t = static_cast<Type *>(vtype); - RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, vtype, mips, usages); + RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages); Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); if (texAlloc == NULL) { LOGE("Memory allocation failure"); @@ -846,23 +835,22 @@ RsAllocation rsaAllocationCreateFromBitmap(RsContext con, RsType vtype, memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes()); if (mips == RS_ALLOCATION_MIPMAP_FULL) { - rsaAllocationGenerateScriptMips(rsc, texAlloc); + AllocationGenerateScriptMips(rsc, texAlloc); } texAlloc->deferredUploadToTexture(rsc); return texAlloc; } -RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, RsType vtype, - RsAllocationMipmapControl mips, - const void *data, size_t data_length, uint32_t usages) { - Context *rsc = static_cast<Context *>(con); +RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype, + RsAllocationMipmapControl mips, + const void *data, size_t data_length, uint32_t usages) { Type *t = static_cast<Type *>(vtype); // Cubemap allocation's faces should be Width by Width each. // Source data should have 6 * Width by Width pixels // Error checking is done in the java layer - RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, t, mips, usages); + RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages); Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); if (texAlloc == NULL) { LOGE("Memory allocation failure"); @@ -887,11 +875,21 @@ RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, RsType vtype, } if (mips == RS_ALLOCATION_MIPMAP_FULL) { - rsaAllocationGenerateScriptMips(rsc, texAlloc); + AllocationGenerateScriptMips(rsc, texAlloc); } texAlloc->deferredUploadToTexture(rsc); return texAlloc; } +} +} + +const void * rsaAllocationGetType(RsContext con, RsAllocation va) { + Allocation *a = static_cast<Allocation *>(va); + a->getType()->incUserRef(); + + return a->getType(); +} + #endif //ANDROID_RS_SERIALIZE diff --git a/libs/rs/rsContext.cpp b/libs/rs/rsContext.cpp index 50f5f55..6d63f67 100644 --- a/libs/rs/rsContext.cpp +++ b/libs/rs/rsContext.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -215,15 +215,11 @@ void Context::timerPrint() { } bool Context::setupCheck() { - if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) { - LOGE("Context::setupCheck() 1 fail"); - return false; - } mFragmentStore->setup(this, &mStateFragmentStore); - mFragment->setupGL2(this, &mStateFragment, &mShaderCache); + mFragment->setupGL2(this, &mStateFragment); mRaster->setup(this, &mStateRaster); - mVertex->setupGL2(this, &mStateVertex, &mShaderCache); + mVertex->setupGL2(this, &mStateVertex); mFBOCache.setupGL2(this); return true; } @@ -295,7 +291,6 @@ void * Context::threadProc(void *vrsc) { rsc->setProgramStore(NULL); rsc->mStateFont.init(rsc); rsc->setFont(NULL); - rsc->mStateVertexArray.init(rsc); } rsc->mRunning = true; @@ -356,7 +351,6 @@ void Context::destroyWorkerThreadResources() { mStateFragment.deinit(this); mStateFragmentStore.deinit(this); mStateFont.deinit(this); - mShaderCache.cleanupAll(); } //LOGV("destroyWorkerThreadResources 2"); mExit = true; @@ -598,7 +592,7 @@ RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen *subID = d[0]; //LOGE("getMessageToClient %i %i", commandID, *subID); - if (bufferLen >= bytesData) { + if (bufferLen >= (*receiveLen)) { memcpy(data, d+1, *receiveLen); mIO.mToClient.next(); return (RsMessageToClientType)commandID; @@ -740,25 +734,21 @@ void rsi_ContextDestroyWorker(Context *rsc) { rsc->destroyWorkerThreadResources();; } -} -} - -void rsContextDestroy(RsContext vcon) { - LOGV("rsContextDestroy %p", vcon); - Context *rsc = static_cast<Context *>(vcon); +void rsi_ContextDestroy(Context *rsc) { + LOGV("rsContextDestroy %p", rsc); rsContextDestroyWorker(rsc); delete rsc; - LOGV("rsContextDestroy 2 %p", vcon); + LOGV("rsContextDestroy 2 %p", rsc); } -RsContext rsContextCreate(RsDevice vdev, uint32_t version) { +RsContext rsi_ContextCreate(RsDevice vdev, uint32_t version) { LOGV("rsContextCreate %p", vdev); Device * dev = static_cast<Device *>(vdev); Context *rsc = Context::createContext(dev, NULL); return rsc; } -RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, +RsContext rsi_ContextCreateGL(RsDevice vdev, uint32_t version, RsSurfaceConfig sc, uint32_t dpi) { LOGV("rsContextCreateGL %p", vdev); Device * dev = static_cast<Device *>(vdev); @@ -768,32 +758,31 @@ RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, return rsc; } -RsMessageToClientType rsContextPeekMessage(RsContext vrsc, +RsMessageToClientType rsi_ContextPeekMessage(Context *rsc, size_t * receiveLen, size_t receiveLen_length, uint32_t * subID, size_t subID_length, bool wait) { - Context * rsc = static_cast<Context *>(vrsc); return rsc->peekMessageToClient(receiveLen, subID, wait); } -RsMessageToClientType rsContextGetMessage(RsContext vrsc, void * data, size_t data_length, +RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length, size_t * receiveLen, size_t receiveLen_length, uint32_t * subID, size_t subID_length, bool wait) { - Context * rsc = static_cast<Context *>(vrsc); rsAssert(subID_length == sizeof(uint32_t)); rsAssert(receiveLen_length == sizeof(size_t)); return rsc->getMessageToClient(data, receiveLen, subID, data_length, wait); } -void rsContextInitToClient(RsContext vrsc) { - Context * rsc = static_cast<Context *>(vrsc); +void rsi_ContextInitToClient(Context *rsc) { rsc->initToClient(); } -void rsContextDeinitToClient(RsContext vrsc) { - Context * rsc = static_cast<Context *>(vrsc); +void rsi_ContextDeinitToClient(Context *rsc) { rsc->deinitToClient(); } +} +} + // Only to be called at a3d load time, before object is visible to user // not thread safe void rsaGetName(RsContext con, void * obj, const char **name) { diff --git a/libs/rs/rsContext.h b/libs/rs/rsContext.h index df85a6b..107f639 100644 --- a/libs/rs/rsContext.h +++ b/libs/rs/rsContext.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,9 +37,7 @@ #include "rsProgramStore.h" #include "rsProgramRaster.h" #include "rsProgramVertex.h" -#include "rsShaderCache.h" #include "rsFBOCache.h" -#include "rsVertexArray.h" #include "rsgApiStructs.h" #include "rsLocklessFifo.h" @@ -111,11 +109,9 @@ public: ProgramStoreState mStateFragmentStore; ProgramRasterState mStateRaster; ProgramVertexState mStateVertex; - VertexArrayState mStateVertexArray; FontState mStateFont; ScriptCState mScriptC; - ShaderCache mShaderCache; FBOCache mFBOCache; void swapBuffers(); diff --git a/libs/rs/rsDevice.cpp b/libs/rs/rsDevice.cpp index d7d03f6..849fd98 100644 --- a/libs/rs/rsDevice.cpp +++ b/libs/rs/rsDevice.cpp @@ -40,17 +40,20 @@ void Device::removeContext(Context *rsc) { } } -RsDevice rsDeviceCreate() { +namespace android { +namespace renderscript { + +RsDevice rsi_DeviceCreate() { Device * d = new Device(); return d; } -void rsDeviceDestroy(RsDevice dev) { +void rsi_DeviceDestroy(RsDevice dev) { Device * d = static_cast<Device *>(dev); delete d; } -void rsDeviceSetConfig(RsDevice dev, RsDeviceParam p, int32_t value) { +void rsi_DeviceSetConfig(RsDevice dev, RsDeviceParam p, int32_t value) { Device * d = static_cast<Device *>(dev); if (p == RS_DEVICE_PARAM_FORCE_SOFTWARE_GL) { d->mForceSW = value != 0; @@ -59,3 +62,5 @@ void rsDeviceSetConfig(RsDevice dev, RsDeviceParam p, int32_t value) { rsAssert(0); } +} +} diff --git a/libs/rs/rsFont.cpp b/libs/rs/rsFont.cpp index b7b85b6..5e47ddb 100644 --- a/libs/rs/rsFont.cpp +++ b/libs/rs/rsFont.cpp @@ -25,11 +25,6 @@ #include FT_FREETYPE_H #include FT_BITMAP_H -#include <GLES/gl.h> -#include <GLES/glext.h> -#include <GLES2/gl2.h> -#include <GLES2/gl2ext.h> - using namespace android; using namespace android::renderscript; @@ -457,7 +452,7 @@ bool FontState::cacheBitmap(FT_Bitmap *bitmap, uint32_t *retOriginX, uint32_t *r // This will dirty the texture and the shader so next time // we draw it will upload the data - mTextTexture->syncAll(mRSC, RS_ALLOCATION_USAGE_SCRIPT); + mTextTexture->deferredUploadToTexture(mRSC); mFontShaderF->bindTexture(mRSC, 0, mTextTexture.get()); // Some debug code @@ -568,7 +563,6 @@ void FontState::initVertexArrayBuffers() { } indexAlloc->deferredUploadToBufferObject(mRSC); - mIndexBuffer.set(indexAlloc); const Element *posElem = Element::create(mRSC, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 3); const Element *texElem = Element::create(mRSC, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 2); @@ -585,7 +579,10 @@ void FontState::initVertexArrayBuffers() { Allocation *vertexAlloc = new Allocation(mRSC, vertexDataType, RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_VERTEX); mTextMeshPtr = (float*)vertexAlloc->getPtr(); - mVertexArray.set(vertexAlloc); + mMesh.set(new Mesh(mRSC, 1, 1)); + mMesh->setVertexBuffer(vertexAlloc, 0); + mMesh->setPrimitive(indexAlloc, RS_PRIMITIVE_TRIANGLE, 0); + mMesh->init(); } // We don't want to allocate anything unless we actually draw text @@ -625,18 +622,7 @@ void FontState::issueDrawCommand() { return; } - float *vtx = (float*)mVertexArray->getPtr(); - float *tex = vtx + 3; - - VertexArray::Attrib attribs[2]; - attribs[0].set(GL_FLOAT, 3, 20, false, (uint32_t)vtx, "ATTRIB_position"); - attribs[1].set(GL_FLOAT, 2, 20, false, (uint32_t)tex, "ATTRIB_texture0"); - VertexArray va(attribs, 2); - va.setupGL2(mRSC, &mRSC->mStateVertexArray, &mRSC->mShaderCache); - - mIndexBuffer->uploadCheck(mRSC); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer->getBufferObjectID()); - glDrawElements(GL_TRIANGLES, mCurrentQuadIndex * 6, GL_UNSIGNED_SHORT, (uint16_t *)(0)); + mMesh->renderPrimitiveRange(mRSC, 0, 0, mCurrentQuadIndex * 6); } void FontState::appendMeshQuad(float x1, float y1, float z1, @@ -787,8 +773,7 @@ void FontState::deinit(Context *rsc) { mFontShaderFConstant.clear(); - mIndexBuffer.clear(); - mVertexArray.clear(); + mMesh.clear(); mFontShaderF.clear(); mFontSampler.clear(); diff --git a/libs/rs/rsFont.h b/libs/rs/rsFont.h index 91a5da9..d18c0d9 100644 --- a/libs/rs/rsFont.h +++ b/libs/rs/rsFont.h @@ -230,9 +230,7 @@ protected: uint32_t mMaxNumberOfQuads; void initVertexArrayBuffers(); - ObjectBaseRef<Allocation> mIndexBuffer; - ObjectBaseRef<Allocation> mVertexArray; - + ObjectBaseRef<Mesh> mMesh; bool mInitialized; diff --git a/libs/rs/rsMesh.cpp b/libs/rs/rsMesh.cpp index e29c800..ed29063 100644 --- a/libs/rs/rsMesh.cpp +++ b/libs/rs/rsMesh.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,46 +15,53 @@ */ #include "rsContext.h" -#ifndef ANDROID_RS_SERIALIZE -#include <GLES/gl.h> -#include <GLES2/gl2.h> -#include <GLES/glext.h> -#endif using namespace android; using namespace android::renderscript; Mesh::Mesh(Context *rsc) : ObjectBase(rsc) { - mPrimitives = NULL; - mPrimitivesCount = 0; - mVertexBuffers = NULL; - mVertexBufferCount = 0; - -#ifndef ANDROID_RS_SERIALIZE - mAttribs = NULL; - mAttribAllocationIndex = NULL; + mHal.drv = NULL; + mHal.state.primitives = NULL; + mHal.state.primitivesCount = 0; + mHal.state.vertexBuffers = NULL; + mHal.state.vertexBuffersCount = 0; + mInitialized = false; +} - mAttribCount = 0; -#endif +Mesh::Mesh(Context *rsc, + uint32_t vertexBuffersCount, + uint32_t primitivesCount) : ObjectBase(rsc) { + mHal.drv = NULL; + mHal.state.primitivesCount = primitivesCount; + mHal.state.primitives = new Primitive_t *[mHal.state.primitivesCount]; + for (uint32_t i = 0; i < mHal.state.primitivesCount; i ++) { + mHal.state.primitives[i] = new Primitive_t; + } + mHal.state.vertexBuffersCount = vertexBuffersCount; + mHal.state.vertexBuffers = new ObjectBaseRef<Allocation>[mHal.state.vertexBuffersCount]; } Mesh::~Mesh() { - if (mVertexBuffers) { - delete[] mVertexBuffers; +#ifndef ANDROID_RS_SERIALIZE + mRSC->mHal.funcs.mesh.destroy(mRSC, this); +#endif + + if (mHal.state.vertexBuffers) { + delete[] mHal.state.vertexBuffers; } - if (mPrimitives) { - for (uint32_t i = 0; i < mPrimitivesCount; i ++) { - delete mPrimitives[i]; + if (mHal.state.primitives) { + for (uint32_t i = 0; i < mHal.state.primitivesCount; i ++) { + mHal.state.primitives[i]->mIndexBuffer.clear(); + delete mHal.state.primitives[i]; } - delete[] mPrimitives; + delete[] mHal.state.primitives; } +} +void Mesh::init() { #ifndef ANDROID_RS_SERIALIZE - if (mAttribs) { - delete[] mAttribs; - delete[] mAttribAllocationIndex; - } + mRSC->mHal.funcs.mesh.init(mRSC, this); #endif } @@ -66,15 +73,15 @@ void Mesh::serialize(OStream *stream) const { stream->addString(&name); // Store number of vertex streams - stream->addU32(mVertexBufferCount); - for (uint32_t vCount = 0; vCount < mVertexBufferCount; vCount ++) { - mVertexBuffers[vCount]->serialize(stream); + stream->addU32(mHal.state.vertexBuffersCount); + for (uint32_t vCount = 0; vCount < mHal.state.vertexBuffersCount; vCount ++) { + mHal.state.vertexBuffers[vCount]->serialize(stream); } - stream->addU32(mPrimitivesCount); + stream->addU32(mHal.state.primitivesCount); // Store the primitives - for (uint32_t pCount = 0; pCount < mPrimitivesCount; pCount ++) { - Primitive_t * prim = mPrimitives[pCount]; + for (uint32_t pCount = 0; pCount < mHal.state.primitivesCount; pCount ++) { + Primitive_t * prim = mHal.state.primitives[pCount]; stream->addU8((uint8_t)prim->mPrimitive); @@ -95,213 +102,119 @@ Mesh *Mesh::createFromStream(Context *rsc, IStream *stream) { return NULL; } - Mesh * mesh = new Mesh(rsc); - String8 name; stream->loadString(&name); - mesh->setName(name.string(), name.size()); - mesh->mVertexBufferCount = stream->loadU32(); - if (mesh->mVertexBufferCount) { - mesh->mVertexBuffers = new ObjectBaseRef<Allocation>[mesh->mVertexBufferCount]; + uint32_t vertexBuffersCount = stream->loadU32(); + ObjectBaseRef<Allocation> *vertexBuffers = NULL; + if (vertexBuffersCount) { + vertexBuffers = new ObjectBaseRef<Allocation>[vertexBuffersCount]; - for (uint32_t vCount = 0; vCount < mesh->mVertexBufferCount; vCount ++) { + for (uint32_t vCount = 0; vCount < vertexBuffersCount; vCount ++) { Allocation *vertexAlloc = Allocation::createFromStream(rsc, stream); - mesh->mVertexBuffers[vCount].set(vertexAlloc); + vertexBuffers[vCount].set(vertexAlloc); } } - mesh->mPrimitivesCount = stream->loadU32(); - if (mesh->mPrimitivesCount) { - mesh->mPrimitives = new Primitive_t *[mesh->mPrimitivesCount]; + uint32_t primitivesCount = stream->loadU32(); + ObjectBaseRef<Allocation> *indexBuffers = NULL; + RsPrimitive *primitives = NULL; + if (primitivesCount) { + indexBuffers = new ObjectBaseRef<Allocation>[primitivesCount]; + primitives = new RsPrimitive[primitivesCount]; // load all primitives - for (uint32_t pCount = 0; pCount < mesh->mPrimitivesCount; pCount ++) { - Primitive_t * prim = new Primitive_t; - mesh->mPrimitives[pCount] = prim; - - prim->mPrimitive = (RsPrimitive)stream->loadU8(); + for (uint32_t pCount = 0; pCount < primitivesCount; pCount ++) { + primitives[pCount] = (RsPrimitive)stream->loadU8(); // Check to see if the index buffer was stored uint32_t isIndexPresent = stream->loadU32(); if (isIndexPresent) { Allocation *indexAlloc = Allocation::createFromStream(rsc, stream); - prim->mIndexBuffer.set(indexAlloc); + indexBuffers[pCount].set(indexAlloc); } } } -#ifndef ANDROID_RS_SERIALIZE - mesh->updateGLPrimitives(); - mesh->initVertexAttribs(); - mesh->uploadAll(rsc); -#endif - return mesh; -} - -#ifndef ANDROID_RS_SERIALIZE - -bool Mesh::isValidGLComponent(const Element *elem, uint32_t fieldIdx) { - // Do not create attribs for padding - if (elem->getFieldName(fieldIdx)[0] == '#') { - return false; - } - - // Only GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_FIXED, GL_FLOAT are accepted. - // Filter rs types accordingly - RsDataType dt = elem->getField(fieldIdx)->getComponent().getType(); - if (dt != RS_TYPE_FLOAT_32 && dt != RS_TYPE_UNSIGNED_8 && - dt != RS_TYPE_UNSIGNED_16 && dt != RS_TYPE_SIGNED_8 && - dt != RS_TYPE_SIGNED_16) { - return false; - } - - // Now make sure they are not arrays - uint32_t arraySize = elem->getFieldArraySize(fieldIdx); - if (arraySize != 1) { - return false; + Mesh *mesh = new Mesh(rsc, vertexBuffersCount, primitivesCount); + mesh->setName(name.string(), name.size()); + for (uint32_t vCount = 0; vCount < vertexBuffersCount; vCount ++) { + mesh->setVertexBuffer(vertexBuffers[vCount].get(), vCount); } - - return true; -} - -void Mesh::initVertexAttribs() { - // Count the number of gl attrs to initialize - mAttribCount = 0; - for (uint32_t ct=0; ct < mVertexBufferCount; ct++) { - const Element *elem = mVertexBuffers[ct]->getType()->getElement(); - for (uint32_t ct=0; ct < elem->getFieldCount(); ct++) { - if (isValidGLComponent(elem, ct)) { - mAttribCount ++; - } - } + for (uint32_t pCount = 0; pCount < primitivesCount; pCount ++) { + mesh->setPrimitive(indexBuffers[pCount].get(), primitives[pCount], pCount); } - if (mAttribs) { - delete [] mAttribs; - delete [] mAttribAllocationIndex; - mAttribs = NULL; - mAttribAllocationIndex = NULL; + // Cleanup + if (vertexBuffersCount) { + delete[] vertexBuffers; } - if (!mAttribCount) { - return; + if (primitivesCount) { + delete[] indexBuffers; + delete[] primitives; } - mAttribs = new VertexArray::Attrib[mAttribCount]; - mAttribAllocationIndex = new uint32_t[mAttribCount]; - - uint32_t userNum = 0; - for (uint32_t ct=0; ct < mVertexBufferCount; ct++) { - const Element *elem = mVertexBuffers[ct]->getType()->getElement(); - uint32_t stride = elem->getSizeBytes(); - for (uint32_t fieldI=0; fieldI < elem->getFieldCount(); fieldI++) { - const Component &c = elem->getField(fieldI)->getComponent(); - - if (!isValidGLComponent(elem, fieldI)) { - continue; - } - - mAttribs[userNum].size = c.getVectorSize(); - mAttribs[userNum].offset = elem->getFieldOffsetBytes(fieldI); - mAttribs[userNum].type = c.getGLType(); - mAttribs[userNum].normalized = c.getType() != RS_TYPE_FLOAT_32;//c.getIsNormalized(); - mAttribs[userNum].stride = stride; - String8 tmp(RS_SHADER_ATTR); - tmp.append(elem->getFieldName(fieldI)); - mAttribs[userNum].name.setTo(tmp.string()); - - // Remember which allocation this attribute came from - mAttribAllocationIndex[userNum] = ct; - userNum ++; - } - } +#ifndef ANDROID_RS_SERIALIZE + mesh->init(); + mesh->uploadAll(rsc); +#endif + return mesh; } +#ifndef ANDROID_RS_SERIALIZE + void Mesh::render(Context *rsc) const { - for (uint32_t ct = 0; ct < mPrimitivesCount; ct ++) { + for (uint32_t ct = 0; ct < mHal.state.primitivesCount; ct ++) { renderPrimitive(rsc, ct); } } void Mesh::renderPrimitive(Context *rsc, uint32_t primIndex) const { - if (primIndex >= mPrimitivesCount) { + if (primIndex >= mHal.state.primitivesCount) { LOGE("Invalid primitive index"); return; } - Primitive_t *prim = mPrimitives[primIndex]; + Primitive_t *prim = mHal.state.primitives[primIndex]; if (prim->mIndexBuffer.get()) { renderPrimitiveRange(rsc, primIndex, 0, prim->mIndexBuffer->getType()->getDimX()); return; } - renderPrimitiveRange(rsc, primIndex, 0, mVertexBuffers[0]->getType()->getDimX()); + renderPrimitiveRange(rsc, primIndex, 0, mHal.state.vertexBuffers[0]->getType()->getDimX()); } void Mesh::renderPrimitiveRange(Context *rsc, uint32_t primIndex, uint32_t start, uint32_t len) const { - if (len < 1 || primIndex >= mPrimitivesCount || mAttribCount == 0) { + if (len < 1 || primIndex >= mHal.state.primitivesCount) { LOGE("Invalid mesh or parameters"); return; } - rsc->checkError("Mesh::renderPrimitiveRange 1"); - for (uint32_t ct=0; ct < mVertexBufferCount; ct++) { - mVertexBuffers[ct]->uploadCheck(rsc); + for (uint32_t ct=0; ct < mHal.state.vertexBuffersCount; ct++) { + mHal.state.vertexBuffers[ct]->uploadCheck(rsc); } - // update attributes with either buffer information or data ptr based on their current state - for (uint32_t ct=0; ct < mAttribCount; ct++) { - uint32_t allocIndex = mAttribAllocationIndex[ct]; - Allocation *alloc = mVertexBuffers[allocIndex].get(); - if (alloc->getIsBufferObject()) { - mAttribs[ct].buffer = alloc->getBufferObjectID(); - mAttribs[ct].ptr = NULL; - } else { - mAttribs[ct].buffer = 0; - mAttribs[ct].ptr = (const uint8_t*)alloc->getPtr(); - } - } - - VertexArray va(mAttribs, mAttribCount); - va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache); - rsc->checkError("Mesh::renderPrimitiveRange 2"); - Primitive_t *prim = mPrimitives[primIndex]; + Primitive_t *prim = mHal.state.primitives[primIndex]; if (prim->mIndexBuffer.get()) { prim->mIndexBuffer->uploadCheck(rsc); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, prim->mIndexBuffer->getBufferObjectID()); - glDrawElements(prim->mGLPrimitive, len, GL_UNSIGNED_SHORT, (uint16_t *)(start * 2)); - } else { - glDrawArrays(prim->mGLPrimitive, start, len); } + rsc->checkError("Mesh::renderPrimitiveRange upload check"); - rsc->checkError("Mesh::renderPrimitiveRange"); + mRSC->mHal.funcs.mesh.draw(mRSC, this, primIndex, start, len); + rsc->checkError("Mesh::renderPrimitiveRange draw"); } - void Mesh::uploadAll(Context *rsc) { - for (uint32_t ct = 0; ct < mVertexBufferCount; ct ++) { - if (mVertexBuffers[ct].get()) { - mVertexBuffers[ct]->deferredUploadToBufferObject(rsc); + for (uint32_t ct = 0; ct < mHal.state.vertexBuffersCount; ct ++) { + if (mHal.state.vertexBuffers[ct].get()) { + mHal.state.vertexBuffers[ct]->deferredUploadToBufferObject(rsc); } } - for (uint32_t ct = 0; ct < mPrimitivesCount; ct ++) { - if (mPrimitives[ct]->mIndexBuffer.get()) { - mPrimitives[ct]->mIndexBuffer->deferredUploadToBufferObject(rsc); - } - } -} - -void Mesh::updateGLPrimitives() { - for (uint32_t i = 0; i < mPrimitivesCount; i ++) { - switch (mPrimitives[i]->mPrimitive) { - case RS_PRIMITIVE_POINT: mPrimitives[i]->mGLPrimitive = GL_POINTS; break; - case RS_PRIMITIVE_LINE: mPrimitives[i]->mGLPrimitive = GL_LINES; break; - case RS_PRIMITIVE_LINE_STRIP: mPrimitives[i]->mGLPrimitive = GL_LINE_STRIP; break; - case RS_PRIMITIVE_TRIANGLE: mPrimitives[i]->mGLPrimitive = GL_TRIANGLES; break; - case RS_PRIMITIVE_TRIANGLE_STRIP: mPrimitives[i]->mGLPrimitive = GL_TRIANGLE_STRIP; break; - case RS_PRIMITIVE_TRIANGLE_FAN: mPrimitives[i]->mGLPrimitive = GL_TRIANGLE_FAN; break; + for (uint32_t ct = 0; ct < mHal.state.primitivesCount; ct ++) { + if (mHal.state.primitives[ct]->mIndexBuffer.get()) { + mHal.state.primitives[ct]->mIndexBuffer->deferredUploadToBufferObject(rsc); } } } @@ -312,8 +225,8 @@ void Mesh::computeBBox() { uint32_t stride = 0; uint32_t numVerts = 0; // First we need to find the position ptr and stride - for (uint32_t ct=0; ct < mVertexBufferCount; ct++) { - const Type *bufferType = mVertexBuffers[ct]->getType(); + for (uint32_t ct=0; ct < mHal.state.vertexBuffersCount; ct++) { + const Type *bufferType = mHal.state.vertexBuffers[ct]->getType(); const Element *bufferElem = bufferType->getElement(); for (uint32_t ct=0; ct < bufferElem->getFieldCount(); ct++) { @@ -321,7 +234,7 @@ void Mesh::computeBBox() { vectorSize = bufferElem->getField(ct)->getComponent().getVectorSize(); stride = bufferElem->getSizeBytes() / sizeof(float); uint32_t offset = bufferElem->getFieldOffsetBytes(ct); - posPtr = (float*)((uint8_t*)mVertexBuffers[ct]->getPtr() + offset); + posPtr = (float*)((uint8_t*)mHal.state.vertexBuffers[ct]->getPtr() + offset); numVerts = bufferType->getDimX(); break; } @@ -353,73 +266,62 @@ namespace android { namespace renderscript { RsMesh rsi_MeshCreate(Context *rsc, uint32_t vtxCount, uint32_t idxCount) { - Mesh *sm = new Mesh(rsc); + Mesh *sm = new Mesh(rsc, vtxCount, idxCount); sm->incUserRef(); - sm->mPrimitivesCount = idxCount; - sm->mPrimitives = new Mesh::Primitive_t *[sm->mPrimitivesCount]; - for (uint32_t ct = 0; ct < idxCount; ct ++) { - sm->mPrimitives[ct] = new Mesh::Primitive_t; - } - - sm->mVertexBufferCount = vtxCount; - sm->mVertexBuffers = new ObjectBaseRef<Allocation>[vtxCount]; - return sm; } void rsi_MeshBindVertex(Context *rsc, RsMesh mv, RsAllocation va, uint32_t slot) { Mesh *sm = static_cast<Mesh *>(mv); - rsAssert(slot < sm->mVertexBufferCount); + rsAssert(slot < sm->mHal.state.vertexBuffersCount); - sm->mVertexBuffers[slot].set((Allocation *)va); + sm->setVertexBuffer((Allocation *)va, slot); } void rsi_MeshBindIndex(Context *rsc, RsMesh mv, RsAllocation va, uint32_t primType, uint32_t slot) { Mesh *sm = static_cast<Mesh *>(mv); - rsAssert(slot < sm->mPrimitivesCount); + rsAssert(slot < sm->mHal.state.primitivesCount); - sm->mPrimitives[slot]->mIndexBuffer.set((Allocation *)va); - sm->mPrimitives[slot]->mPrimitive = (RsPrimitive)primType; - sm->updateGLPrimitives(); + sm->setPrimitive((Allocation *)va, (RsPrimitive)primType, slot); } void rsi_MeshInitVertexAttribs(Context *rsc, RsMesh mv) { Mesh *sm = static_cast<Mesh *>(mv); - sm->initVertexAttribs(); + sm->init(); } }} void rsaMeshGetVertexBufferCount(RsContext con, RsMesh mv, int32_t *numVtx) { Mesh *sm = static_cast<Mesh *>(mv); - *numVtx = sm->mVertexBufferCount; + *numVtx = sm->mHal.state.vertexBuffersCount; } void rsaMeshGetIndexCount(RsContext con, RsMesh mv, int32_t *numIdx) { Mesh *sm = static_cast<Mesh *>(mv); - *numIdx = sm->mPrimitivesCount; + *numIdx = sm->mHal.state.primitivesCount; } void rsaMeshGetVertices(RsContext con, RsMesh mv, RsAllocation *vtxData, uint32_t vtxDataCount) { Mesh *sm = static_cast<Mesh *>(mv); - rsAssert(vtxDataCount == sm->mVertexBufferCount); + rsAssert(vtxDataCount == sm->mHal.state.vertexBuffersCount); for (uint32_t ct = 0; ct < vtxDataCount; ct ++) { - vtxData[ct] = sm->mVertexBuffers[ct].get(); - sm->mVertexBuffers[ct]->incUserRef(); + vtxData[ct] = sm->mHal.state.vertexBuffers[ct].get(); + sm->mHal.state.vertexBuffers[ct]->incUserRef(); } } void rsaMeshGetIndices(RsContext con, RsMesh mv, RsAllocation *va, uint32_t *primType, uint32_t idxDataCount) { Mesh *sm = static_cast<Mesh *>(mv); - rsAssert(idxDataCount == sm->mPrimitivesCount); + rsAssert(idxDataCount == sm->mHal.state.primitivesCount); for (uint32_t ct = 0; ct < idxDataCount; ct ++) { - va[ct] = sm->mPrimitives[ct]->mIndexBuffer.get(); - primType[ct] = sm->mPrimitives[ct]->mPrimitive; - if (sm->mPrimitives[ct]->mIndexBuffer.get()) { - sm->mPrimitives[ct]->mIndexBuffer->incUserRef(); + va[ct] = sm->mHal.state.primitives[ct]->mIndexBuffer.get(); + primType[ct] = sm->mHal.state.primitives[ct]->mPrimitive; + if (sm->mHal.state.primitives[ct]->mIndexBuffer.get()) { + sm->mHal.state.primitives[ct]->mIndexBuffer->incUserRef(); } } } diff --git a/libs/rs/rsMesh.h b/libs/rs/rsMesh.h index 3e080e2..1e279f4 100644 --- a/libs/rs/rsMesh.h +++ b/libs/rs/rsMesh.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,57 +29,65 @@ namespace renderscript { class Mesh : public ObjectBase { public: Mesh(Context *); + Mesh(Context *, uint32_t vertexBuffersCount, uint32_t primitivesCount); ~Mesh(); - // Contains vertex data - // Position, normal, texcoord, etc could either be strided in one allocation - // of provided separetely in multiple ones - ObjectBaseRef<Allocation> *mVertexBuffers; - uint32_t mVertexBufferCount; - // Either mIndexBuffer, mPrimitiveBuffer or both could have a NULL reference // If both are null, mPrimitive only would be used to render the mesh - struct Primitive_t - { + struct Primitive_t { ObjectBaseRef<Allocation> mIndexBuffer; - RsPrimitive mPrimitive; - uint32_t mGLPrimitive; }; + // compatibility to not break the build + ObjectBaseRef<Allocation> *mVertexBuffers; + uint32_t mVertexBufferCount; Primitive_t ** mPrimitives; uint32_t mPrimitivesCount; + // end compatibility virtual void serialize(OStream *stream) const; virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_MESH; } static Mesh *createFromStream(Context *rsc, IStream *stream); + void init(); + + struct Hal { + mutable void *drv; + + struct State { + // Contains vertex data + // Position, normal, texcoord, etc could either be strided in one allocation + // of provided separetely in multiple ones + ObjectBaseRef<Allocation> *vertexBuffers; + uint32_t vertexBuffersCount; + + Primitive_t ** primitives; + uint32_t primitivesCount; + }; + State state; + }; + Hal mHal; + + void setVertexBuffer(Allocation *vb, uint32_t index) { + mHal.state.vertexBuffers[index].set(vb); + } + + void setPrimitive(Allocation *idx, RsPrimitive prim, uint32_t index) { + mHal.state.primitives[index]->mIndexBuffer.set(idx); + mHal.state.primitives[index]->mPrimitive = prim; + } -#ifndef ANDROID_RS_SERIALIZE void render(Context *) const; void renderPrimitive(Context *, uint32_t primIndex) const; void renderPrimitiveRange(Context *, uint32_t primIndex, uint32_t start, uint32_t len) const; void uploadAll(Context *); - void updateGLPrimitives(); - - // Bounding volumes float mBBoxMin[3]; float mBBoxMax[3]; void computeBBox(); - - void initVertexAttribs(); - protected: - bool isValidGLComponent(const Element *elem, uint32_t fieldIdx); - // Attribues that allow us to map to GL - VertexArray::Attrib *mAttribs; - // This allows us to figure out which allocation the attribute - // belongs to. In the event the allocation is uploaded to GL - // buffer, it lets us properly map it - uint32_t *mAttribAllocationIndex; - uint32_t mAttribCount; -#endif + bool mInitialized; }; class MeshContext { @@ -92,7 +100,7 @@ public: } } -#endif //ANDROID_RS_TRIANGLE_MESH_H +#endif //ANDROID_RS_MESH_H diff --git a/libs/rs/rsProgram.cpp b/libs/rs/rsProgram.cpp index 4ef05bf..28fa061 100644 --- a/libs/rs/rsProgram.cpp +++ b/libs/rs/rsProgram.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,11 +15,6 @@ */ #include "rsContext.h" -#ifndef ANDROID_RS_SERIALIZE -#include <GLES2/gl2.h> -#include <GLES2/gl2ext.h> -#endif //ANDROID_RS_SERIALIZE - #include "rsProgram.h" using namespace android; @@ -36,26 +31,22 @@ Program::Program(Context *rsc, const char * shaderText, uint32_t shaderLength, initMemberVars(); 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++; + mHal.state.inputElementsCount++; } if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) { - mConstantCount++; + mHal.state.constantsCount++; } if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_TYPE) { - mTextureCount++; + mHal.state.texturesCount++; } } - mTextures = new ObjectBaseRef<Allocation>[mTextureCount]; - mSamplers = new ObjectBaseRef<Sampler>[mTextureCount]; - mTextureTargets = new RsTextureTarget[mTextureCount]; - mInputElements = new ObjectBaseRef<Element>[mInputCount]; - mOutputElements = new ObjectBaseRef<Element>[mOutputCount]; - mConstantTypes = new ObjectBaseRef<Type>[mConstantCount]; - mConstants = new ObjectBaseRef<Allocation>[mConstantCount]; + mHal.state.textures = new ObjectBaseRef<Allocation>[mHal.state.texturesCount]; + mHal.state.samplers = new ObjectBaseRef<Sampler>[mHal.state.texturesCount]; + mHal.state.textureTargets = new RsTextureTarget[mHal.state.texturesCount]; + mHal.state.inputElements = new ObjectBaseRef<Element>[mHal.state.inputElementsCount]; + mHal.state.constantTypes = new ObjectBaseRef<Type>[mHal.state.constantsCount]; + mHal.state.constants = new ObjectBaseRef<Allocation>[mHal.state.constantsCount]; uint32_t input = 0; uint32_t output = 0; @@ -63,16 +54,13 @@ Program::Program(Context *rsc, const char * shaderText, uint32_t shaderLength, uint32_t texture = 0; for (uint32_t ct=0; ct < paramLength; ct+=2) { if (params[ct] == RS_PROGRAM_PARAM_INPUT) { - mInputElements[input++].set(reinterpret_cast<Element *>(params[ct+1])); - } - if (params[ct] == RS_PROGRAM_PARAM_OUTPUT) { - mOutputElements[output++].set(reinterpret_cast<Element *>(params[ct+1])); + mHal.state.inputElements[input++].set(reinterpret_cast<Element *>(params[ct+1])); } if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) { - mConstantTypes[constant++].set(reinterpret_cast<Type *>(params[ct+1])); + mHal.state.constantTypes[constant++].set(reinterpret_cast<Type *>(params[ct+1])); } if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_TYPE) { - mTextureTargets[texture++] = (RsTextureTarget)params[ct+1]; + mHal.state.textureTargets[texture++] = (RsTextureTarget)params[ct+1]; } } mIsInternal = false; @@ -84,88 +72,69 @@ Program::Program(Context *rsc, const char * shaderText, uint32_t shaderLength, shaderLength -= internalTokenLen; } mUserShader.setTo(shaderText, shaderLength); - - initAttribAndUniformArray(); } Program::~Program() { - if (mRSC->props.mLogShaders) { - LOGV("Program::~Program with shader id %u", mShaderID); - } - if (mShaderID) { - glDeleteShader(mShaderID); - } - - for (uint32_t ct=0; ct < mConstantCount; ct++) { + for (uint32_t ct=0; ct < mHal.state.constantsCount; ct++) { bindAllocation(NULL, NULL, ct); } - for (uint32_t ct=0; ct < mTextureCount; ct++) { + for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) { bindTexture(NULL, ct, NULL); bindSampler(NULL, ct, NULL); } - delete[] mTextures; - delete[] mSamplers; - delete[] mTextureTargets; - delete[] mInputElements; - delete[] mOutputElements; - delete[] mConstantTypes; - delete[] mConstants; - delete[] mAttribNames; - delete[] mUniformNames; - delete[] mUniformArraySizes; - mInputCount = 0; - mOutputCount = 0; - mConstantCount = 0; + delete[] mHal.state.textures; + delete[] mHal.state.samplers; + delete[] mHal.state.textureTargets; + delete[] mHal.state.inputElements; + delete[] mHal.state.constantTypes; + delete[] mHal.state.constants; + mHal.state.inputElementsCount = 0; + mHal.state.constantsCount = 0; + mHal.state.texturesCount = 0; } void Program::initMemberVars() { mDirty = true; - mShaderID = 0; - mAttribCount = 0; - mUniformCount = 0; - mTextureCount = 0; - mTextures = NULL; - mSamplers = NULL; - mTextureTargets = NULL; - mInputElements = NULL; - mOutputElements = NULL; - mConstantTypes = NULL; - mConstants = NULL; - mAttribNames = NULL; - mUniformNames = NULL; - mUniformArraySizes = NULL; - mInputCount = 0; - mOutputCount = 0; - mConstantCount = 0; - mIsValid = false; + mHal.drv = NULL; + mHal.state.textures = NULL; + mHal.state.samplers = NULL; + mHal.state.textureTargets = NULL; + mHal.state.inputElements = NULL; + mHal.state.constantTypes = NULL; + mHal.state.constants = NULL; + + mHal.state.inputElementsCount = 0; + mHal.state.constantsCount = 0; + mHal.state.texturesCount = 0; + mIsInternal = false; } void Program::bindAllocation(Context *rsc, Allocation *alloc, uint32_t slot) { if (alloc != NULL) { - if (slot >= mConstantCount) { + if (slot >= mHal.state.constantsCount) { LOGE("Attempt to bind alloc at slot %u, on shader id %u, but const count is %u", - slot, (uint32_t)this, mConstantCount); + slot, (uint32_t)this, mHal.state.constantsCount); rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind allocation"); return; } - if (!alloc->getType()->isEqual(mConstantTypes[slot].get())) { + if (!alloc->getType()->isEqual(mHal.state.constantTypes[slot].get())) { LOGE("Attempt to bind alloc at slot %u, on shader id %u, but types mismatch", slot, (uint32_t)this); rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind allocation"); return; } } - if (mConstants[slot].get() == alloc) { + if (mHal.state.constants[slot].get() == alloc) { return; } - if (mConstants[slot].get()) { - mConstants[slot].get()->removeProgramToDirty(this); + if (mHal.state.constants[slot].get()) { + mHal.state.constants[slot].get()->removeProgramToDirty(this); } - mConstants[slot].set(alloc); + mHal.state.constants[slot].set(alloc); if (alloc) { alloc->addProgramToDirty(this); } @@ -173,327 +142,38 @@ void Program::bindAllocation(Context *rsc, Allocation *alloc, uint32_t slot) { } void Program::bindTexture(Context *rsc, uint32_t slot, Allocation *a) { - if (slot >= mTextureCount) { - LOGE("Attempt to bind texture to slot %u but tex count is %u", slot, mTextureCount); + if (slot >= mHal.state.texturesCount) { + LOGE("Attempt to bind texture to slot %u but tex count is %u", slot, mHal.state.texturesCount); rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind texture"); return; } - if (a && a->getType()->getDimFaces() && mTextureTargets[slot] != RS_TEXTURE_CUBE) { + if (a && a->getType()->getDimFaces() && mHal.state.textureTargets[slot] != RS_TEXTURE_CUBE) { LOGE("Attempt to bind cubemap to slot %u but 2d texture needed", slot); rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind cubemap to 2d texture slot"); return; } //LOGE("bindtex %i %p", slot, a); - mTextures[slot].set(a); + mHal.state.textures[slot].set(a); mDirty = true; } void Program::bindSampler(Context *rsc, uint32_t slot, Sampler *s) { - if (slot >= mTextureCount) { - LOGE("Attempt to bind sampler to slot %u but tex count is %u", slot, mTextureCount); + if (slot >= mHal.state.texturesCount) { + LOGE("Attempt to bind sampler to slot %u but tex count is %u", slot, mHal.state.texturesCount); rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind sampler"); return; } - mSamplers[slot].set(s); + mHal.state.samplers[slot].set(s); mDirty = true; } -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) { - mShaderID = glCreateShader(type); - rsAssert(mShaderID); - - if (rsc->props.mLogShaders) { - LOGV("Loading shader type %x, ID %i", type, mShaderID); - LOGV("%s", 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; - rsc->setError(RS_ERROR_BAD_SHADER, "Error returned from GL driver loading shader text,"); - return false; - } - } - } - - if (rsc->props.mLogShaders) { - LOGV("--Shader load result %x ", glGetError()); - } - mIsValid = true; - return true; -} - void Program::setShader(const char *txt, uint32_t len) { mUserShader.setTo(txt, len); } -void Program::appendUserConstants() { - for (uint32_t ct=0; ct < mConstantCount; ct++) { - const Element *e = mConstantTypes[ct]->getElement(); - for (uint32_t field=0; field < e->getFieldCount(); field++) { - const Element *f = e->getField(field); - const char *fn = e->getFieldName(field); - - if (fn[0] == '#') { - continue; - } - - // Cannot be complex - rsAssert(!f->getFieldCount()); - if (f->getType() == RS_TYPE_MATRIX_4X4) { - mShader.append("uniform mat4 UNI_"); - } else if (f->getType() == RS_TYPE_MATRIX_3X3) { - mShader.append("uniform mat3 UNI_"); - } else if (f->getType() == RS_TYPE_MATRIX_2X2) { - mShader.append("uniform mat2 UNI_"); - } else { - switch (f->getComponent().getVectorSize()) { - case 1: mShader.append("uniform float UNI_"); break; - case 2: mShader.append("uniform vec2 UNI_"); break; - case 3: mShader.append("uniform vec3 UNI_"); break; - case 4: mShader.append("uniform vec4 UNI_"); break; - default: - rsAssert(0); - } - } - - mShader.append(fn); - if (e->getFieldArraySize(field) > 1) { - mShader.appendFormat("[%d]", e->getFieldArraySize(field)); - } - mShader.append(";\n"); - } - } -} - -void Program::logUniform(const Element *field, const float *fd, uint32_t arraySize ) { - RsDataType dataType = field->getType(); - uint32_t elementSize = field->getSizeBytes() / sizeof(float); - for (uint32_t i = 0; i < arraySize; i ++) { - if (arraySize > 1) { - LOGV("Array Element [%u]", i); - } - if (dataType == RS_TYPE_MATRIX_4X4) { - LOGV("Matrix4x4"); - LOGV("{%f, %f, %f, %f", fd[0], fd[4], fd[8], fd[12]); - LOGV(" %f, %f, %f, %f", fd[1], fd[5], fd[9], fd[13]); - LOGV(" %f, %f, %f, %f", fd[2], fd[6], fd[10], fd[14]); - LOGV(" %f, %f, %f, %f}", fd[3], fd[7], fd[11], fd[15]); - } else if (dataType == RS_TYPE_MATRIX_3X3) { - LOGV("Matrix3x3"); - LOGV("{%f, %f, %f", fd[0], fd[3], fd[6]); - LOGV(" %f, %f, %f", fd[1], fd[4], fd[7]); - LOGV(" %f, %f, %f}", fd[2], fd[5], fd[8]); - } else if (dataType == RS_TYPE_MATRIX_2X2) { - LOGV("Matrix2x2"); - LOGV("{%f, %f", fd[0], fd[2]); - LOGV(" %f, %f}", fd[1], fd[3]); - } else { - switch (field->getComponent().getVectorSize()) { - case 1: - LOGV("Uniform 1 = %f", fd[0]); - break; - case 2: - LOGV("Uniform 2 = %f %f", fd[0], fd[1]); - break; - case 3: - LOGV("Uniform 3 = %f %f %f", fd[0], fd[1], fd[2]); - break; - case 4: - LOGV("Uniform 4 = %f %f %f %f", fd[0], fd[1], fd[2], fd[3]); - break; - default: - rsAssert(0); - } - } - LOGE("Element size %u data=%p", elementSize, fd); - fd += elementSize; - LOGE("New data=%p", fd); - } -} - -void Program::setUniform(Context *rsc, const Element *field, const float *fd, - int32_t slot, uint32_t arraySize ) { - RsDataType dataType = field->getType(); - if (dataType == RS_TYPE_MATRIX_4X4) { - glUniformMatrix4fv(slot, arraySize, GL_FALSE, fd); - } else if (dataType == RS_TYPE_MATRIX_3X3) { - glUniformMatrix3fv(slot, arraySize, GL_FALSE, fd); - } else if (dataType == RS_TYPE_MATRIX_2X2) { - glUniformMatrix2fv(slot, arraySize, GL_FALSE, fd); - } else { - switch (field->getComponent().getVectorSize()) { - case 1: - glUniform1fv(slot, arraySize, fd); - break; - case 2: - glUniform2fv(slot, arraySize, fd); - break; - case 3: - glUniform3fv(slot, arraySize, fd); - break; - case 4: - glUniform4fv(slot, arraySize, fd); - break; - default: - rsAssert(0); - } - } -} - -void Program::setupUserConstants(Context *rsc, ShaderCache *sc, bool isFragment) { - uint32_t uidx = 0; - for (uint32_t ct=0; ct < mConstantCount; ct++) { - Allocation *alloc = mConstants[ct].get(); - if (!alloc) { - LOGE("Attempting to set constants on shader id %u, but alloc at slot %u is not set", (uint32_t)this, ct); - rsc->setError(RS_ERROR_BAD_SHADER, "No constant allocation bound"); - continue; - } - - const uint8_t *data = static_cast<const uint8_t *>(alloc->getPtr()); - const Element *e = mConstantTypes[ct]->getElement(); - for (uint32_t field=0; field < e->getFieldCount(); field++) { - const Element *f = e->getField(field); - const char *fieldName = e->getFieldName(field); - // If this field is padding, skip it - if (fieldName[0] == '#') { - continue; - } - - uint32_t offset = e->getFieldOffsetBytes(field); - const float *fd = reinterpret_cast<const float *>(&data[offset]); - - int32_t slot = -1; - uint32_t arraySize = 1; - if (!isFragment) { - slot = sc->vtxUniformSlot(uidx); - arraySize = sc->vtxUniformSize(uidx); - } else { - slot = sc->fragUniformSlot(uidx); - arraySize = sc->fragUniformSize(uidx); - } - if (rsc->props.mLogShadersUniforms) { - LOGV("Uniform slot=%i, offset=%i, constant=%i, field=%i, uidx=%i, name=%s", slot, offset, ct, field, uidx, fieldName); - } - uidx ++; - if (slot < 0) { - continue; - } - - if (rsc->props.mLogShadersUniforms) { - logUniform(f, fd, arraySize); - } - setUniform(rsc, f, fd, slot, arraySize); - } - } -} - -void Program::initAttribAndUniformArray() { - mAttribCount = 0; - for (uint32_t ct=0; ct < mInputCount; ct++) { - const Element *elem = mInputElements[ct].get(); - for (uint32_t field=0; field < elem->getFieldCount(); field++) { - if (elem->getFieldName(field)[0] != '#') { - mAttribCount ++; - } - } - } - - mUniformCount = 0; - for (uint32_t ct=0; ct < mConstantCount; ct++) { - const Element *elem = mConstantTypes[ct]->getElement(); - - for (uint32_t field=0; field < elem->getFieldCount(); field++) { - if (elem->getFieldName(field)[0] != '#') { - mUniformCount ++; - } - } - } - mUniformCount += mTextureCount; - - if (mAttribCount) { - mAttribNames = new String8[mAttribCount]; - } - if (mUniformCount) { - mUniformNames = new String8[mUniformCount]; - mUniformArraySizes = new uint32_t[mUniformCount]; - } -} - -void Program::initAddUserElement(const Element *e, String8 *names, uint32_t *arrayLengths, uint32_t *count, const char *prefix) { - rsAssert(e->getFieldCount()); - for (uint32_t ct=0; ct < e->getFieldCount(); ct++) { - const Element *ce = e->getField(ct); - if (ce->getFieldCount()) { - initAddUserElement(ce, names, arrayLengths, count, prefix); - } else if (e->getFieldName(ct)[0] != '#') { - String8 tmp(prefix); - tmp.append(e->getFieldName(ct)); - names[*count].setTo(tmp.string()); - if (arrayLengths) { - arrayLengths[*count] = e->getFieldArraySize(ct); - } - (*count)++; - } - } -} - namespace android { namespace renderscript { diff --git a/libs/rs/rsProgram.h b/libs/rs/rsProgram.h index c48464d..bcf5519 100644 --- a/libs/rs/rsProgram.h +++ b/libs/rs/rsProgram.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,6 @@ // --------------------------------------------------------------------------- namespace android { namespace renderscript { -class ShaderCache; #define RS_SHADER_INTERNAL "//rs_shader_internal\n" #define RS_SHADER_ATTR "ATTRIB_" @@ -38,75 +37,53 @@ public: virtual ~Program(); void bindAllocation(Context *, Allocation *, uint32_t slot); - virtual void createShader(); bool isUserProgram() const {return !mIsInternal;} void bindTexture(Context *, uint32_t slot, Allocation *); void bindSampler(Context *, uint32_t slot, Sampler *); - uint32_t getShaderID() const {return mShaderID;} void setShader(const char *, uint32_t len); - uint32_t getAttribCount() const {return mAttribCount;} - uint32_t getUniformCount() const {return mUniformCount;} - const String8 & getAttribName(uint32_t i) const {return mAttribNames[i];} - const String8 & getUniformName(uint32_t i) const {return mUniformNames[i];} - uint32_t getUniformArraySize(uint32_t i) const {return mUniformArraySizes[i];} - - String8 getGLSLInputString() const; - String8 getGLSLOutputString() const; - String8 getGLSLConstantString() const; - - bool isValid() const {return mIsValid;} void forceDirty() const {mDirty = true;} + struct Hal { + mutable void *drv; + + struct State { + // The difference between Textures and Constants is how they are accessed + // Texture lookups go though a sampler which in effect converts normalized + // coordinates into type specific. Multiple samples may also be taken + // and filtered. + // + // Constants are strictly accessed by the shader code + ObjectBaseRef<Allocation> *textures; + RsTextureTarget *textureTargets; + uint32_t texturesCount; + + ObjectBaseRef<Sampler> *samplers; + uint32_t samplersCount; + + ObjectBaseRef<Allocation> *constants; + ObjectBaseRef<Type> *constantTypes; + uint32_t constantsCount; + + ObjectBaseRef<Element> *inputElements; + uint32_t inputElementsCount; + }; + State state; + }; + Hal mHal; + protected: - // Components not listed in "in" will be passed though - // unless overwritten by components in out. - ObjectBaseRef<Element> *mInputElements; - ObjectBaseRef<Element> *mOutputElements; - ObjectBaseRef<Type> *mConstantTypes; - ObjectBaseRef<Allocation> *mConstants; - uint32_t mInputCount; - uint32_t mOutputCount; - uint32_t mConstantCount; - bool mIsValid; bool mIsInternal; - // Applies to vertex and fragment shaders only - void appendUserConstants(); - void setupUserConstants(Context *rsc, ShaderCache *sc, bool isFragment); - void initAddUserElement(const Element *e, String8 *names, uint32_t *arrayLengths, uint32_t *count, const char *prefix); - - void initAttribAndUniformArray(); - mutable bool mDirty; - String8 mShader; String8 mUserShader; - uint32_t mShaderID; - - uint32_t mTextureCount; - uint32_t mAttribCount; - uint32_t mUniformCount; - String8 *mAttribNames; - String8 *mUniformNames; - uint32_t *mUniformArraySizes; void logUniform(const Element *field, const float *fd, uint32_t arraySize ); void setUniform(Context *rsc, const Element *field, const float *fd, int32_t slot, uint32_t arraySize ); void initMemberVars(); - - // The difference between Textures and Constants is how they are accessed - // Texture lookups go though a sampler which in effect converts normalized - // coordinates into type specific. Multiple samples may also be taken - // and filtered. - // - // Constants are strictly accessed by programetic loads. - ObjectBaseRef<Allocation> *mTextures; - ObjectBaseRef<Sampler> *mSamplers; - RsTextureTarget *mTextureTargets; - bool loadShader(Context *, uint32_t type); }; } diff --git a/libs/rs/rsProgramFragment.cpp b/libs/rs/rsProgramFragment.cpp index ff314b7..39887ca 100644 --- a/libs/rs/rsProgramFragment.cpp +++ b/libs/rs/rsProgramFragment.cpp @@ -15,13 +15,6 @@ */ #include "rsContext.h" -#ifndef ANDROID_RS_SERIALIZE -#include <GLES/gl.h> -#include <GLES/glext.h> -#include <GLES2/gl2.h> -#include <GLES2/gl2ext.h> -#endif //ANDROID_RS_SERIALIZE - #include "rsProgramFragment.h" using namespace android; @@ -31,19 +24,16 @@ ProgramFragment::ProgramFragment(Context *rsc, const char * shaderText, uint32_t shaderLength, const uint32_t * params, uint32_t paramLength) : Program(rsc, shaderText, shaderLength, params, paramLength) { - mConstantColor[0] = 1.f; mConstantColor[1] = 1.f; mConstantColor[2] = 1.f; mConstantColor[3] = 1.f; - init(rsc); + mRSC->mHal.funcs.fragment.init(mRSC, this, mUserShader.string(), mUserShader.length()); } ProgramFragment::~ProgramFragment() { - if (mShaderID) { - mRSC->mShaderCache.cleanupFragment(mShaderID); - } + mRSC->mHal.funcs.fragment.destroy(mRSC, this); } void ProgramFragment::setConstantColor(Context *rsc, float r, float g, float b, float a) { @@ -52,7 +42,7 @@ void ProgramFragment::setConstantColor(Context *rsc, float r, float g, float b, rsc->setError(RS_ERROR_BAD_SHADER, "Cannot set fixed function emulation color on user program"); return; } - if (mConstants[0].get() == NULL) { + if (mHal.state.constants[0].get() == NULL) { LOGE("Unable to set fixed function emulation color because allocation is missing"); rsc->setError(RS_ERROR_BAD_SHADER, "Unable to set fixed function emulation color because allocation is missing"); return; @@ -61,11 +51,11 @@ void ProgramFragment::setConstantColor(Context *rsc, float r, float g, float b, mConstantColor[1] = g; mConstantColor[2] = b; mConstantColor[3] = a; - memcpy(mConstants[0]->getPtr(), mConstantColor, 4*sizeof(float)); + memcpy(mHal.state.constants[0]->getPtr(), mConstantColor, 4*sizeof(float)); mDirty = true; } -void ProgramFragment::setupGL2(Context *rsc, ProgramFragmentState *state, ShaderCache *sc) { +void ProgramFragment::setupGL2(Context *rsc, ProgramFragmentState *state) { //LOGE("sgl2 frag1 %x", glGetError()); if ((state->mLast.get() == this) && !mDirty) { return; @@ -74,94 +64,16 @@ void ProgramFragment::setupGL2(Context *rsc, ProgramFragmentState *state, Shader rsc->checkError("ProgramFragment::setupGL2 start"); - rsc->checkError("ProgramFragment::setupGL2 begin uniforms"); - setupUserConstants(rsc, sc, true); - - uint32_t numTexturesToBind = mTextureCount; - uint32_t numTexturesAvailable = rsc->getMaxFragmentTextures(); - if (numTexturesToBind >= numTexturesAvailable) { - LOGE("Attempting to bind %u textures on shader id %u, but only %u are available", - mTextureCount, (uint32_t)this, numTexturesAvailable); - rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind more textuers than available"); - numTexturesToBind = numTexturesAvailable; - } - - for (uint32_t ct=0; ct < numTexturesToBind; ct++) { - glActiveTexture(GL_TEXTURE0 + ct); - if (!mTextures[ct].get()) { + for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) { + if (!mHal.state.textures[ct].get()) { LOGE("No texture bound for shader id %u, texture unit %u", (uint)this, ct); rsc->setError(RS_ERROR_BAD_SHADER, "No texture bound"); continue; } - - mTextures[ct]->uploadCheck(rsc); - GLenum target = (GLenum)mTextures[ct]->getGLTarget(); - if (target != GL_TEXTURE_2D && target != GL_TEXTURE_CUBE_MAP) { - LOGE("Attempting to bind unknown texture to shader id %u, texture unit %u", (uint)this, ct); - rsc->setError(RS_ERROR_BAD_SHADER, "Non-texture allocation bound to a shader"); - } - glBindTexture(target, mTextures[ct]->getTextureID()); - rsc->checkError("ProgramFragment::setupGL2 tex bind"); - if (mSamplers[ct].get()) { - mSamplers[ct]->setupGL(rsc, mTextures[ct].get()); - } else { - glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - rsc->checkError("ProgramFragment::setupGL2 tex env"); - } - - glUniform1i(sc->fragUniformSlot(mTextureUniformIndexStart + ct), ct); - rsc->checkError("ProgramFragment::setupGL2 uniforms"); - } - - glActiveTexture(GL_TEXTURE0); - mDirty = false; - rsc->checkError("ProgramFragment::setupGL2"); -} - -void ProgramFragment::loadShader(Context *rsc) { - Program::loadShader(rsc, GL_FRAGMENT_SHADER); -} - -void ProgramFragment::createShader() { - if (mUserShader.length() > 1) { - mShader.append("precision mediump float;\n"); - appendUserConstants(); - char buf[256]; - for (uint32_t ct=0; ct < mTextureCount; ct++) { - if (mTextureTargets[ct] == RS_TEXTURE_2D) { - snprintf(buf, sizeof(buf), "uniform sampler2D UNI_Tex%i;\n", ct); - } else { - snprintf(buf, sizeof(buf), "uniform samplerCube UNI_Tex%i;\n", ct); - } - mShader.append(buf); - } - mShader.append(mUserShader); - } else { - LOGE("ProgramFragment::createShader cannot create program, shader code not defined"); - rsAssert(0); - } -} - -void ProgramFragment::init(Context *rsc) { - uint32_t uniformIndex = 0; - if (mUserShader.size() > 0) { - for (uint32_t ct=0; ct < mConstantCount; ct++) { - initAddUserElement(mConstantTypes[ct]->getElement(), mUniformNames, mUniformArraySizes, &uniformIndex, RS_SHADER_UNI); - } - } - mTextureUniformIndexStart = uniformIndex; - char buf[256]; - for (uint32_t ct=0; ct < mTextureCount; ct++) { - snprintf(buf, sizeof(buf), "UNI_Tex%i", ct); - mUniformNames[uniformIndex].setTo(buf); - mUniformArraySizes[uniformIndex] = 1; - uniformIndex++; + mHal.state.textures[ct]->uploadCheck(rsc); } - createShader(); + rsc->mHal.funcs.fragment.setActive(rsc, this); } void ProgramFragment::serialize(OStream *stream) const { diff --git a/libs/rs/rsProgramFragment.h b/libs/rs/rsProgramFragment.h index 3d28946..7520af0 100644 --- a/libs/rs/rsProgramFragment.h +++ b/libs/rs/rsProgramFragment.h @@ -32,11 +32,8 @@ public: uint32_t paramLength); virtual ~ProgramFragment(); - virtual void setupGL2(Context *, ProgramFragmentState *, ShaderCache *sc); + virtual void setupGL2(Context *, ProgramFragmentState *); - virtual void createShader(); - virtual void loadShader(Context *rsc); - virtual void init(Context *rsc); virtual void serialize(OStream *stream) const; virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_PROGRAM_FRAGMENT; } static ProgramFragment *createFromStream(Context *rsc, IStream *stream); diff --git a/libs/rs/rsProgramStore.h b/libs/rs/rsProgramStore.h index bfe276d..88a06a8 100644 --- a/libs/rs/rsProgramStore.h +++ b/libs/rs/rsProgramStore.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -62,8 +62,6 @@ public: RsDepthFunc depthFunc; }; State state; - - }; Hal mHal; diff --git a/libs/rs/rsProgramVertex.cpp b/libs/rs/rsProgramVertex.cpp index e407d3a..dfd732f 100644 --- a/libs/rs/rsProgramVertex.cpp +++ b/libs/rs/rsProgramVertex.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,13 +15,6 @@ */ #include "rsContext.h" -#ifndef ANDROID_RS_SERIALIZE -#include <GLES/gl.h> -#include <GLES/glext.h> -#include <GLES2/gl2.h> -#include <GLES2/gl2ext.h> -#endif //ANDROID_RS_SERIALIZE - #include "rsProgramVertex.h" using namespace android; @@ -32,57 +25,14 @@ ProgramVertex::ProgramVertex(Context *rsc, const char * shaderText, uint32_t shaderLength, const uint32_t * params, uint32_t paramLength) : Program(rsc, shaderText, shaderLength, params, paramLength) { - init(rsc); + mRSC->mHal.funcs.vertex.init(mRSC, this, mUserShader.string(), mUserShader.length()); } ProgramVertex::~ProgramVertex() { - if (mShaderID) { - mRSC->mShaderCache.cleanupVertex(mShaderID); - } -} - -void ProgramVertex::loadShader(Context *rsc) { - Program::loadShader(rsc, GL_VERTEX_SHADER); -} - -void ProgramVertex::createShader(Context *rsc) { - if (mUserShader.length() > 1) { - - appendUserConstants(); - - 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); - const char *fn = e->getFieldName(field); - - if (fn[0] == '#') { - continue; - } - - // Cannot be complex - rsAssert(!f->getFieldCount()); - switch (f->getComponent().getVectorSize()) { - case 1: mShader.append("attribute float ATTRIB_"); break; - case 2: mShader.append("attribute vec2 ATTRIB_"); break; - case 3: mShader.append("attribute vec3 ATTRIB_"); break; - case 4: mShader.append("attribute vec4 ATTRIB_"); break; - default: - rsAssert(0); - } - - mShader.append(fn); - mShader.append(";\n"); - } - } - mShader.append(mUserShader); - } else { - rsc->setError(RS_ERROR_FATAL_UNKNOWN, - "ProgramFragment::createShader cannot create program, shader code not defined"); - } + mRSC->mHal.funcs.vertex.destroy(mRSC, this); } -void ProgramVertex::setupGL2(Context *rsc, ProgramVertexState *state, ShaderCache *sc) { +void ProgramVertex::setupGL2(Context *rsc, ProgramVertexState *state) { if ((state->mLast.get() == this) && !mDirty) { return; } @@ -90,12 +40,12 @@ void ProgramVertex::setupGL2(Context *rsc, ProgramVertexState *state, ShaderCach rsc->checkError("ProgramVertex::setupGL2 start"); if (!isUserProgram()) { - if (mConstants[0].get() == NULL) { + if (mHal.state.constants[0].get() == NULL) { rsc->setError(RS_ERROR_FATAL_UNKNOWN, "Unable to set fixed function emulation matrices because allocation is missing"); return; } - float *f = static_cast<float *>(mConstants[0]->getPtr()); + float *f = static_cast<float *>(mHal.state.constants[0]->getPtr()); Matrix4x4 mvp; mvp.load(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]); Matrix4x4 t; @@ -106,10 +56,10 @@ void ProgramVertex::setupGL2(Context *rsc, ProgramVertexState *state, ShaderCach } } - rsc->checkError("ProgramVertex::setupGL2 begin uniforms"); - setupUserConstants(rsc, sc, false); - state->mLast.set(this); + + rsc->mHal.funcs.vertex.setActive(rsc, this); + rsc->checkError("ProgramVertex::setupGL2"); } @@ -119,12 +69,12 @@ void ProgramVertex::setProjectionMatrix(Context *rsc, const rsc_Matrix *m) const "Attempting to set fixed function emulation matrix projection on user program"); return; } - if (mConstants[0].get() == NULL) { + if (mHal.state.constants[0].get() == NULL) { rsc->setError(RS_ERROR_FATAL_UNKNOWN, "Unable to set fixed function emulation matrix projection because allocation is missing"); return; } - float *f = static_cast<float *>(mConstants[0]->getPtr()); + float *f = static_cast<float *>(mHal.state.constants[0]->getPtr()); memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m, sizeof(rsc_Matrix)); mDirty = true; } @@ -135,12 +85,12 @@ void ProgramVertex::setModelviewMatrix(Context *rsc, const rsc_Matrix *m) const "Attempting to set fixed function emulation matrix modelview on user program"); return; } - if (mConstants[0].get() == NULL) { + if (mHal.state.constants[0].get() == NULL) { rsc->setError(RS_ERROR_FATAL_UNKNOWN, "Unable to set fixed function emulation matrix modelview because allocation is missing"); return; } - float *f = static_cast<float *>(mConstants[0]->getPtr()); + float *f = static_cast<float *>(mHal.state.constants[0]->getPtr()); memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m, sizeof(rsc_Matrix)); mDirty = true; } @@ -151,12 +101,12 @@ void ProgramVertex::setTextureMatrix(Context *rsc, const rsc_Matrix *m) const { "Attempting to set fixed function emulation matrix texture on user program"); return; } - if (mConstants[0].get() == NULL) { + if (mHal.state.constants[0].get() == NULL) { rsc->setError(RS_ERROR_FATAL_UNKNOWN, "Unable to set fixed function emulation matrix texture because allocation is missing"); return; } - float *f = static_cast<float *>(mConstants[0]->getPtr()); + float *f = static_cast<float *>(mHal.state.constants[0]->getPtr()); memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m, sizeof(rsc_Matrix)); mDirty = true; } @@ -167,12 +117,12 @@ void ProgramVertex::getProjectionMatrix(Context *rsc, rsc_Matrix *m) const { "Attempting to get fixed function emulation matrix projection on user program"); return; } - if (mConstants[0].get() == NULL) { + if (mHal.state.constants[0].get() == NULL) { rsc->setError(RS_ERROR_FATAL_UNKNOWN, "Unable to get fixed function emulation matrix projection because allocation is missing"); return; } - float *f = static_cast<float *>(mConstants[0]->getPtr()); + float *f = static_cast<float *>(mHal.state.constants[0]->getPtr()); memcpy(m, &f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], sizeof(rsc_Matrix)); } @@ -180,27 +130,13 @@ void ProgramVertex::transformToScreen(Context *rsc, float *v4out, const float *v if (isUserProgram()) { return; } - float *f = static_cast<float *>(mConstants[0]->getPtr()); + float *f = static_cast<float *>(mHal.state.constants[0]->getPtr()); Matrix4x4 mvp; mvp.loadMultiply((Matrix4x4 *)&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], (Matrix4x4 *)&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]); mvp.vectorMultiply(v4out, v3in); } -void ProgramVertex::init(Context *rsc) { - uint32_t attribCount = 0; - uint32_t uniformCount = 0; - if (mUserShader.size() > 0) { - for (uint32_t ct=0; ct < mInputCount; ct++) { - initAddUserElement(mInputElements[ct].get(), mAttribNames, NULL, &attribCount, RS_SHADER_ATTR); - } - for (uint32_t ct=0; ct < mConstantCount; ct++) { - initAddUserElement(mConstantTypes[ct]->getElement(), mUniformNames, mUniformArraySizes, &uniformCount, RS_SHADER_UNI); - } - } - createShader(rsc); -} - void ProgramVertex::serialize(OStream *stream) const { } diff --git a/libs/rs/rsProgramVertex.h b/libs/rs/rsProgramVertex.h index 2a5c863..04224a7 100644 --- a/libs/rs/rsProgramVertex.h +++ b/libs/rs/rsProgramVertex.h @@ -31,7 +31,7 @@ public: const uint32_t * params, uint32_t paramLength); virtual ~ProgramVertex(); - virtual void setupGL2(Context *rsc, ProgramVertexState *state, ShaderCache *sc); + virtual void setupGL2(Context *rsc, ProgramVertexState *state); void setProjectionMatrix(Context *, const rsc_Matrix *) const; void getProjectionMatrix(Context *, rsc_Matrix *) const; @@ -40,10 +40,6 @@ public: void transformToScreen(Context *, float *v4out, const float *v3in) const; - virtual void createShader(Context *); - virtual void loadShader(Context *); - virtual void init(Context *); - virtual void serialize(OStream *stream) const; virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_PROGRAM_VERTEX; } static ProgramVertex *createFromStream(Context *rsc, IStream *stream); diff --git a/libs/rs/rsScriptC_LibGL.cpp b/libs/rs/rsScriptC_LibGL.cpp index 71f1312..ecda485 100644 --- a/libs/rs/rsScriptC_LibGL.cpp +++ b/libs/rs/rsScriptC_LibGL.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,9 @@ #include "rsMatrix2x2.h" #include "utils/Timers.h" +#include "driver/rsdVertexArray.h" +#include "driver/rsdShaderCache.h" +#include "driver/rsdCore.h" #define GL_GLEXT_PROTOTYPES @@ -134,6 +137,11 @@ void rsrDrawQuadTexCoords(Context *rsc, Script *sc, return; } + RsdHal *dc = (RsdHal *)rsc->mHal.drv; + if (!dc->gl.shaderCache->setup(rsc)) { + return; + } + //LOGE("Quad"); //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1); //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2); @@ -143,12 +151,12 @@ void rsrDrawQuadTexCoords(Context *rsc, Script *sc, float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4}; const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4}; - VertexArray::Attrib attribs[2]; + RsdVertexArray::Attrib attribs[2]; attribs[0].set(GL_FLOAT, 3, 12, false, (uint32_t)vtx, "ATTRIB_position"); attribs[1].set(GL_FLOAT, 2, 8, false, (uint32_t)tex, "ATTRIB_texture0"); - VertexArray va(attribs, 2); - va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache); + RsdVertexArray va(attribs, 2); + va.setupGL2(rsc); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); } diff --git a/libs/rs/rsType.cpp b/libs/rs/rsType.cpp index cd2be94..10e3182 100644 --- a/libs/rs/rsType.cpp +++ b/libs/rs/rsType.cpp @@ -274,17 +274,16 @@ Type * Type::cloneAndResize2D(Context *rsc, namespace android { namespace renderscript { -} -} - -RsType rsaTypeCreate(RsContext con, RsElement _e, uint32_t dimX, +RsType rsi_TypeCreate(Context *rsc, RsElement _e, uint32_t dimX, uint32_t dimY, uint32_t dimZ, bool mips, bool faces) { - Context *rsc = static_cast<Context *>(con); Element *e = static_cast<Element *>(_e); return Type::getType(rsc, e, dimX, dimY, dimZ, mips, faces); } +} +} + void rsaTypeGetNativeData(RsContext con, RsType type, uint32_t *typeData, uint32_t typeDataSize) { rsAssert(typeDataSize == 6); // Pack the data in the follofing way mDimX; mDimY; mDimZ; diff --git a/libs/rs/rsType.h b/libs/rs/rsType.h index 90ae039..086db33 100644 --- a/libs/rs/rsType.h +++ b/libs/rs/rsType.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,6 @@ #define ANDROID_STRUCTURED_TYPE_H #include "rsElement.h" -#include "rsVertexArray.h" // --------------------------------------------------------------------------- namespace android { diff --git a/libs/rs/rs_hal.h b/libs/rs/rs_hal.h index 4283d4a..d2f273b 100644 --- a/libs/rs/rs_hal.h +++ b/libs/rs/rs_hal.h @@ -32,6 +32,9 @@ class Script; class ScriptC; class ProgramStore; class ProgramRaster; +class ProgramVertex; +class ProgramFragment; +class Mesh; typedef void *(*RsHalSymbolLookupFunc)(void *usrptr, char const *symbolName); @@ -98,6 +101,25 @@ typedef struct { void (*destroy)(const Context *rsc, const ProgramRaster *ps); } raster; + struct { + bool (*init)(const Context *rsc, const ProgramVertex *pv, + const char* shader, uint32_t shaderLen); + void (*setActive)(const Context *rsc, const ProgramVertex *pv); + void (*destroy)(const Context *rsc, const ProgramVertex *pv); + } vertex; + + struct { + bool (*init)(const Context *rsc, const ProgramFragment *pf, + const char* shader, uint32_t shaderLen); + void (*setActive)(const Context *rsc, const ProgramFragment *pf); + void (*destroy)(const Context *rsc, const ProgramFragment *pf); + } fragment; + + struct { + bool (*init)(const Context *rsc, const Mesh *m); + void (*draw)(const Context *rsc, const Mesh *m, uint32_t primIndex, uint32_t start, uint32_t len); + void (*destroy)(const Context *rsc, const Mesh *m); + } mesh; } RsdHalFunctions; diff --git a/libs/rs/rsg_generator.c b/libs/rs/rsg_generator.c index 0059f19..0e914fc 100644 --- a/libs/rs/rsg_generator.c +++ b/libs/rs/rsg_generator.c @@ -97,9 +97,20 @@ void printStructures(FILE *f) { } } -void printFuncDecl(FILE *f, const ApiEntry *api, const char *prefix, int addContext) { +void printFuncDecl(FILE *f, const ApiEntry *api, const char *prefix, int addContext, int isFnPtr) { printVarTypeAndName(f, &api->ret); - fprintf(f, " %s%s (", prefix, api->name); + if (isFnPtr) { + char t[1024]; + strcpy(t, api->name); + if (strlen(prefix) == 0) { + if (t[0] > 'A' && t[0] < 'Z') { + t[0] -= 'A' - 'a'; + } + } + fprintf(f, " (* %s%s) (", prefix, api->name); + } else { + fprintf(f, " %s%s (", prefix, api->name); + } if (!api->nocontext) { if (addContext) { fprintf(f, "Context *"); @@ -114,12 +125,24 @@ void printFuncDecl(FILE *f, const ApiEntry *api, const char *prefix, int addCont void printFuncDecls(FILE *f, const char *prefix, int addContext) { int ct; for (ct=0; ct < apiCount; ct++) { - printFuncDecl(f, &apis[ct], prefix, addContext); + printFuncDecl(f, &apis[ct], prefix, addContext, 0); fprintf(f, ";\n"); } fprintf(f, "\n\n"); } +void printFuncPointers(FILE *f, int addContext) { + fprintf(f, "\n"); + fprintf(f, "typedef struct RsApiEntrypoints {\n"); + int ct; + for (ct=0; ct < apiCount; ct++) { + fprintf(f, " "); + printFuncDecl(f, &apis[ct], "", addContext, 1); + fprintf(f, ";\n"); + } + fprintf(f, "} RsApiEntrypoints_t;\n\n"); +} + void printPlaybackFuncs(FILE *f, const char *prefix) { int ct; for (ct=0; ct < apiCount; ct++) { @@ -134,7 +157,8 @@ void printPlaybackFuncs(FILE *f, const char *prefix) { static int hasInlineDataPointers(const ApiEntry * api) { int ret = 0; int ct; - if (api->sync || api->ret.typeName[0]) { + // Temporarly disable inbanding while we sort though the bugs. + if (1|| api->sync || api->ret.typeName[0]) { return 0; } for (ct=0; ct < api->paramCount; ct++) { @@ -172,21 +196,35 @@ void printApiCpp(FILE *f) { fprintf(f, "#include \"rsHandcode.h\"\n"); fprintf(f, "\n"); + printFuncPointers(f, 0); + + // Generate RS funcs for local fifo for (ct=0; ct < apiCount; ct++) { int needFlush = 0; const ApiEntry * api = &apis[ct]; - if (api->direct) { - continue; - } - - printFuncDecl(f, api, "rs", 0); + fprintf(f, "static "); + printFuncDecl(f, api, "LF_", 0, 0); fprintf(f, "\n{\n"); - if (api->handcodeApi) { - fprintf(f, " rsHCAPI_%s(rsc", api->name); + if (api->handcodeApi || api->direct) { + if (api->handcodeApi) { + fprintf(f, " rsHCAPI_%s(rsc", api->name); + } else { + fprintf(f, " "); + if (api->ret.typeName[0]) { + fprintf(f, "return "); + } + fprintf(f, "rsi_%s(", api->name); + if (!api->nocontext) { + fprintf(f, "(Context *)rsc"); + } + } for (ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; - fprintf(f, ", %s", vt->name); + if (ct2 > 0 || !api->nocontext) { + fprintf(f, ", "); + } + fprintf(f, "%s", vt->name); } fprintf(f, ");\n"); } else { @@ -203,9 +241,11 @@ void printApiCpp(FILE *f) { } //fprintf(f, " LOGE(\"add command %s\\n\");\n", api->name); - fprintf(f, " RS_CMD_%s *cmd = static_cast<RS_CMD_%s *>(io->mToCore.reserve(sizeof(RS_CMD_%s)));\n", api->name, api->name, api->name); if (hasInlineDataPointers(api)) { + fprintf(f, " RS_CMD_%s *cmd = static_cast<RS_CMD_%s *>(io->mToCore.reserve(dataSize + sizeof(RS_CMD_%s)));\n", api->name, api->name, api->name); fprintf(f, " uint8_t *payload = (uint8_t *)&cmd[1];\n"); + } else { + fprintf(f, " RS_CMD_%s *cmd = static_cast<RS_CMD_%s *>(io->mToCore.reserve(sizeof(RS_CMD_%s)));\n", api->name, api->name, api->name); } for (ct2=0; ct2 < api->paramCount; ct2++) { @@ -252,6 +292,43 @@ void printApiCpp(FILE *f) { } fprintf(f, "};\n\n"); } + + fprintf(f, "\n"); + fprintf(f, "static RsApiEntrypoints_t s_LocalTable = {\n"); + for (ct=0; ct < apiCount; ct++) { + fprintf(f, " LF_%s,\n", apis[ct].name); + } + fprintf(f, "};\n"); + + fprintf(f, "static RsApiEntrypoints_t *s_CurrentTable = &s_LocalTable;\n\n"); + + for (ct=0; ct < apiCount; ct++) { + int needFlush = 0; + const ApiEntry * api = &apis[ct]; + + printFuncDecl(f, api, "rs", 0, 0); + fprintf(f, "\n{\n"); + fprintf(f, " "); + if (api->ret.typeName[0]) { + fprintf(f, "return "); + } + fprintf(f, "s_CurrentTable->%s(", api->name); + + if (!api->nocontext) { + fprintf(f, "(Context *)rsc"); + } + + for (ct2=0; ct2 < api->paramCount; ct2++) { + const VarType *vt = &api->params[ct2]; + if (ct2 > 0 || !api->nocontext) { + fprintf(f, ", "); + } + fprintf(f, "%s", vt->name); + } + fprintf(f, ");\n"); + fprintf(f, "}\n\n"); + } + } void printPlaybackCpp(FILE *f) { @@ -373,6 +450,19 @@ int main(int argc, char **argv) { printPlaybackCpp(f); } break; + + case '4': // rsgApiStream.cpp + { + printFileHeader(f); + printPlaybackCpp(f); + } + + case '5': // rsgApiStreamReplay.cpp + { + printFileHeader(f); + printPlaybackCpp(f); + } + break; } fclose(f); return 0; |