diff options
author | Jason Sams <rjsams@android.com> | 2010-02-11 18:16:21 -0800 |
---|---|---|
committer | Jason Sams <rjsams@android.com> | 2010-02-11 18:16:21 -0800 |
commit | 0b9bbb6dc5d7dabecf23e8c6bb4a267ba8c34fe8 (patch) | |
tree | 12057a18bb056620ed374ad79ea08b8b16e31ef9 /libs | |
parent | c1eba82ba479006045c1d7a4c8ceba116a9602d1 (diff) | |
download | frameworks_base-0b9bbb6dc5d7dabecf23e8c6bb4a267ba8c34fe8.zip frameworks_base-0b9bbb6dc5d7dabecf23e8c6bb4a267ba8c34fe8.tar.gz frameworks_base-0b9bbb6dc5d7dabecf23e8c6bb4a267ba8c34fe8.tar.bz2 |
DO NOT MERGE. Merge Froyo renderscript to Eclair to support live wallpapers on droid. This gives the necessary CPU reduction to allow the wallpapers to work on the slower CPU.
Committer: Jason Sams <rjsams@android.com>
On branch droid
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: libs/rs/rsAllocation.cpp
modified: libs/rs/rsAllocation.h
modified: libs/rs/rsContext.cpp
modified: libs/rs/rsContext.h
modified: libs/rs/rsProgram.cpp
modified: libs/rs/rsProgram.h
Delete the old rollo sample which is obsolete.
Fix film init
Begin gl2 support. Renderscript still uses GL1.1 by default. However, 2.0 can be enabled and will render most tests correctly.
Fix film
Beging GL2 user shaders. Switch master to using GL2 by default.
Implement RS tracked defered texture and buffer object uploads.
Committer: Jason Sams <rjsams@android.com>
On branch droid
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: libs/rs/rsAllocation.cpp
modified: libs/rs/rsAllocation.h
modified: libs/rs/rsContext.h
modified: libs/rs/rsProgramFragment.cpp
modified: libs/rs/rsSimpleMesh.cpp
Remove check for surface valid that is no longer valid.
Continue development of es2.0 user shader support for renderscript. This change cleans up ProgramVertex creation and adds support for passing input, output, and constant type info.
Continue es2 shader dev
Conflicts:
graphics/java/android/renderscript/Program.java
graphics/java/android/renderscript/ProgramVertex.java
Place shader logging behind prop to declutter logs.
Fix emulated glColor in es2 mode.
Fix live wallpaper many. Z coordinate was being ignored for draw quad call.
Add argument checking to sampler builder to disallow illegal modes.
Move texture bindings to base program object. Change ProgramFragment creation to require a texture format in 1.0 mode.
Element restructuring. Add support for new basic Element types including the RS objects and vectors(2-4). In theory this paves the way for maintaining type info for RS objects, passing elements for GLSL uiforms/attribs/varyings, and supporting nested structures.
This will break some apps, checkings for other projects will follow to unbreak them.
Disable excessive RS logging.
Add RS support for generic attribs as input to vertex programs.
More complete support for named attribs. Adds user typed attribs as available to programVertex. Non user attribs are not treated like user for GL2 for simplicity.
Support npot on es 2.0 HW.
Change user attribs to look for empty slot rather than using them in order. Prevents conflict with numbered legacy slots.
Fix npot but where mipmap level sizes were rounding in the wrong direction. Should always be floor.
Implement type generation for user uniforms in vertex shader.
Remove excessive logging, fix error in GLSL uniform generation.
Fix RS mipmap generation for 8 bit alpha textures.
Cleanup seperation of Legacy and user attribs. All user programs now use the new names. Legacy vertex attribs are given default names.
Fix some minor bugs with GL state setup that were exposed by Droids driver.
Implement drawSpriteCropped on es2.0
Diffstat (limited to 'libs')
68 files changed, 2424 insertions, 1979 deletions
diff --git a/libs/rs/Android.mk b/libs/rs/Android.mk index d364277..88521f6 100644 --- a/libs/rs/Android.mk +++ b/libs/rs/Android.mk @@ -96,11 +96,14 @@ LOCAL_SRC_FILES:= \ rsScript.cpp \ rsScriptC.cpp \ rsScriptC_Lib.cpp \ + rsShaderCache.cpp \ rsSimpleMesh.cpp \ rsThreadIO.cpp \ - rsType.cpp + rsType.cpp \ + rsVertexArray.cpp -LOCAL_SHARED_LIBRARIES += libcutils libutils libEGL libGLESv1_CM libui libacc + +LOCAL_SHARED_LIBRARIES += libcutils libutils libEGL libGLESv1_CM libGLESv2 libui libacc LOCAL_LDLIBS := -lpthread -ldl LOCAL_MODULE:= libRS LOCAL_MODULE_TAGS := optional diff --git a/libs/rs/RenderScript.h b/libs/rs/RenderScript.h index 3ca8b15..6662333 100644 --- a/libs/rs/RenderScript.h +++ b/libs/rs/RenderScript.h @@ -36,11 +36,11 @@ typedef void * RsElement; typedef void * RsFile; typedef void * RsSampler; typedef void * RsScript; -typedef void * RsScriptBasicTemp; typedef void * RsSimpleMesh; typedef void * RsType; typedef void * RsLight; +typedef void * RsProgram; typedef void * RsProgramVertex; typedef void * RsProgramFragment; typedef void * RsProgramFragmentStore; @@ -64,34 +64,53 @@ void rsContextInitToClient(RsContext); void rsContextDeinitToClient(RsContext); #define RS_MAX_TEXTURE 2 +#define RS_MAX_ATTRIBS 16 enum RsDataType { - RS_TYPE_FLOAT, - RS_TYPE_UNSIGNED, - RS_TYPE_SIGNED + RS_TYPE_NONE, + RS_TYPE_FLOAT_16, + RS_TYPE_FLOAT_32, + RS_TYPE_FLOAT_64, + RS_TYPE_SIGNED_8, + RS_TYPE_SIGNED_16, + RS_TYPE_SIGNED_32, + RS_TYPE_SIGNED_64, + RS_TYPE_UNSIGNED_8, + RS_TYPE_UNSIGNED_16, + RS_TYPE_UNSIGNED_32, + RS_TYPE_UNSIGNED_64, + + RS_TYPE_UNSIGNED_5_6_5, + RS_TYPE_UNSIGNED_5_5_5_1, + RS_TYPE_UNSIGNED_4_4_4_4, + + RS_TYPE_ELEMENT, + RS_TYPE_TYPE, + RS_TYPE_ALLOCATION, + RS_TYPE_SAMPLER, + RS_TYPE_SCRIPT, + RS_TYPE_MESH, + RS_TYPE_PROGRAM_FRAGMENT, + RS_TYPE_PROGRAM_VERTEX, + RS_TYPE_PROGRAM_RASTER, + RS_TYPE_PROGRAM_STORE }; enum RsDataKind { RS_KIND_USER, - RS_KIND_RED, - RS_KIND_GREEN, - RS_KIND_BLUE, - RS_KIND_ALPHA, - RS_KIND_LUMINANCE, - RS_KIND_INTENSITY, - RS_KIND_X, - RS_KIND_Y, - RS_KIND_Z, - RS_KIND_W, - RS_KIND_S, - RS_KIND_T, - RS_KIND_Q, - RS_KIND_R, - RS_KIND_NX, - RS_KIND_NY, - RS_KIND_NZ, + RS_KIND_COLOR, + RS_KIND_POSITION, + RS_KIND_TEXTURE, + RS_KIND_NORMAL, RS_KIND_INDEX, - RS_KIND_POINT_SIZE + RS_KIND_POINT_SIZE, + + RS_KIND_PIXEL_L, + RS_KIND_PIXEL_A, + RS_KIND_PIXEL_LA, + RS_KIND_PIXEL_RGB, + RS_KIND_PIXEL_RGBA, + }; enum RsSamplerParam { @@ -158,11 +177,19 @@ enum RsBlendDstFunc { }; enum RsTexEnvMode { + RS_TEX_ENV_MODE_NONE, RS_TEX_ENV_MODE_REPLACE, RS_TEX_ENV_MODE_MODULATE, RS_TEX_ENV_MODE_DECAL }; +enum RsProgramParam { + RS_PROGRAM_PARAM_INPUT, + RS_PROGRAM_PARAM_OUTPUT, + RS_PROGRAM_PARAM_CONSTANT, + RS_PROGRAM_PARAM_TEXTURE_COUNT, +}; + enum RsPrimitive { RS_PRIMITIVE_POINT, RS_PRIMITIVE_LINE, diff --git a/libs/rs/RenderScriptEnv.h b/libs/rs/RenderScriptEnv.h index ff0a7b1..99b8c04 100644 --- a/libs/rs/RenderScriptEnv.h +++ b/libs/rs/RenderScriptEnv.h @@ -9,7 +9,6 @@ typedef void * RsDevice; typedef void * RsElement; typedef void * RsSampler; typedef void * RsScript; -typedef void * RsScriptBasicTemp; typedef void * RsSimpleMesh; typedef void * RsType; typedef void * RsProgramFragment; diff --git a/libs/rs/java/Film/src/com/android/film/FilmRS.java b/libs/rs/java/Film/src/com/android/film/FilmRS.java index b5b6723..b80e619 100644 --- a/libs/rs/java/Film/src/com/android/film/FilmRS.java +++ b/libs/rs/java/Film/src/com/android/film/FilmRS.java @@ -121,13 +121,13 @@ public class FilmRS { bs.setWrapT(Sampler.Value.WRAP); mSampler = bs.create(); - ProgramFragment.Builder b = new ProgramFragment.Builder(mRS, null, null); - + ProgramFragment.Builder b = new ProgramFragment.Builder(mRS); mPFBackground = b.create(); mPFBackground.setName("PFBackground"); - b.setTexEnable(true, 0); - b.setTexEnvMode(ProgramFragment.EnvMode.REPLACE, 0); + b = new ProgramFragment.Builder(mRS); + b.setTexture(ProgramFragment.Builder.EnvMode.REPLACE, + ProgramFragment.Builder.Format.RGBA, 0); mPFImages = b.create(); mPFImages.bindSampler(mSampler, 0); mPFImages.setName("PFImages"); @@ -138,7 +138,7 @@ public class FilmRS { mLight.setPosition(0, -0.5f, -1.0f); ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null); - pvb.addLight(mLight); + //pvb.addLight(mLight); mPVBackground = pvb.create(); mPVBackground.setName("PVBackground"); @@ -152,9 +152,10 @@ public class FilmRS { mBufferIDs = new int[13]; mImages = new Allocation[13]; mAllocIDs = Allocation.createSized(mRS, - Element.USER_F32(mRS), mBufferIDs.length); + Element.createUser(mRS, Element.DataType.FLOAT_32), + mBufferIDs.length); - Element ie = Element.RGB_565(mRS); + Element ie = Element.createPixel(mRS, Element.DataType.UNSIGNED_5_6_5, Element.DataKind.PIXEL_RGB); mImages[0] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p01, ie, true); mImages[1] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p02, ie, true); mImages[2] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p03, ie, true); @@ -195,7 +196,8 @@ public class FilmRS { { mBufferState = new int[10]; mAllocState = Allocation.createSized(mRS, - Element.USER_F32(mRS), mBufferState.length); + Element.createUser(mRS, Element.DataType.FLOAT_32), + mBufferState.length); mBufferState[STATE_LAST_FOCUS] = -1; mAllocState.data(mBufferState); } @@ -238,12 +240,12 @@ public class FilmRS { mAllocOffsets = Allocation.createSized(mRS, - Element.USER_I32(mRS), mFSM.mTriangleOffsets.length); + Element.createUser(mRS, Element.DataType.SIGNED_32), mFSM.mTriangleOffsets.length); mAllocOffsets.data(mFSM.mTriangleOffsets); mScriptStrip.bindAllocation(mAllocOffsets, 4); mAllocOffsetsTex = Allocation.createSized(mRS, - Element.USER_F32(mRS), mFSM.mTriangleOffsetsTex.length); + Element.createUser(mRS, Element.DataType.FLOAT_32), mFSM.mTriangleOffsetsTex.length); mAllocOffsetsTex.data(mFSM.mTriangleOffsetsTex); mScriptStrip.bindAllocation(mAllocOffsetsTex, 5); diff --git a/libs/rs/java/Film/src/com/android/film/FilmView.java b/libs/rs/java/Film/src/com/android/film/FilmView.java index 1c5b2bc..4a201fd 100644 --- a/libs/rs/java/Film/src/com/android/film/FilmView.java +++ b/libs/rs/java/Film/src/com/android/film/FilmView.java @@ -42,19 +42,29 @@ public class FilmView extends RSSurfaceView { public FilmView(Context context) { super(context); - //setFocusable(true); } private RenderScript mRS; private FilmRS mRender; + public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { super.surfaceChanged(holder, format, w, h); + if (mRS == null) { + mRS = createRenderScript(true); + mRS.contextSetSurface(w, h, holder.getSurface()); + mRender = new FilmRS(); + mRender.init(mRS, getResources(), w, h); + } + } - mRS = createRenderScript(true); - mRender = new FilmRS(); - mRender.init(mRS, getResources(), w, h); + @Override + protected void onDetachedFromWindow() { + if(mRS != null) { + mRS = null; + destroyRenderScript(); + } } @Override diff --git a/libs/rs/java/Fountain/res/raw/fountain.c b/libs/rs/java/Fountain/res/raw/fountain.c index f218f9b..73b819b 100644 --- a/libs/rs/java/Fountain/res/raw/fountain.c +++ b/libs/rs/java/Fountain/res/raw/fountain.c @@ -14,19 +14,17 @@ int main(int launchID) { float rMax = ((float)rate) * 0.005f; int x = Control->x; int y = Control->y; - char r = Control->r * 255.f; - char g = Control->g * 255.f; - char b = Control->b * 255.f; + int color = ((int)(Control->r * 255.f)) | + ((int)(Control->g * 255.f)) << 8 | + ((int)(Control->b * 255.f)) << 16 | + (0xf0 << 24); struct point_s * np = &p[newPart]; while (rate--) { - vec2Rand((float *)np, rMax); - np->x = x; - np->y = y; - np->r = r; - np->g = g; - np->b = b; - np->a = 0xf0; + vec2Rand((float *)&np->delta.x, rMax); + np->position.x = x; + np->position.y = y; + np->color = color; newPart++; np++; if (newPart >= count) { @@ -37,14 +35,14 @@ int main(int launchID) { } for (ct=0; ct < count; ct++) { - float dy = p->dy + 0.15f; - float posy = p->y + dy; + float dy = p->delta.y + 0.15f; + float posy = p->position.y + dy; if ((posy > height) && (dy > 0)) { dy *= -0.3f; } - p->dy = dy; - p->x += p->dx; - p->y = posy; + p->delta.y = dy; + p->position.x += p->delta.x; + p->position.y = posy; p++; } diff --git a/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java b/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java index f4f9b0c..71f95a7 100644 --- a/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java +++ b/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java @@ -79,10 +79,9 @@ public class FountainRS { mIntAlloc.data(mSD); Element.Builder eb = new Element.Builder(mRS); - eb.addFloat(Element.DataKind.USER, "dx"); - eb.addFloat(Element.DataKind.USER, "dy"); - eb.addFloatXY(""); - eb.addUNorm8RGBA(""); + eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "delta"); + eb.add(Element.createAttrib(mRS, Element.DataType.FLOAT_32, Element.DataKind.POSITION, 2), "position"); + eb.add(Element.createAttrib(mRS, Element.DataType.UNSIGNED_8, Element.DataKind.COLOR, 4), "color"); Element primElement = eb.create(); diff --git a/libs/rs/java/Fountain/src/com/android/fountain/FountainView.java b/libs/rs/java/Fountain/src/com/android/fountain/FountainView.java index 1e7c5a2..fcb93f4 100644 --- a/libs/rs/java/Fountain/src/com/android/fountain/FountainView.java +++ b/libs/rs/java/Fountain/src/com/android/fountain/FountainView.java @@ -51,7 +51,7 @@ public class FountainView extends RSSurfaceView { public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { super.surfaceChanged(holder, format, w, h); if (mRS == null) { - mRS = createRenderScript(false, true); + mRS = createRenderScript(false); mRS.contextSetSurface(w, h, holder.getSurface()); mRender = new FountainRS(); mRender.init(mRS, getResources(), w, h); diff --git a/libs/rs/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java b/libs/rs/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java index 334fd9c..0ca00b3 100644 --- a/libs/rs/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java +++ b/libs/rs/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java @@ -62,7 +62,7 @@ public class ImageProcessingActivity extends Activity implements SurfaceHolder.C public float threshold; } - + static class Pixel { public byte a; public byte r; @@ -78,7 +78,7 @@ public class ImageProcessingActivity extends Activity implements SurfaceHolder.C mParams.outWidth, mParams.outHeight); mDisplayView.invalidate(); } - }; + }; @Override public void run() { @@ -131,7 +131,7 @@ public class ImageProcessingActivity extends Activity implements SurfaceHolder.C public void surfaceDestroyed(SurfaceHolder holder) { } - + private Script.Invokable createScript() { mRS = new RenderScript(false, false); mRS.mMessageCallback = new FilterCallback(); @@ -143,9 +143,11 @@ public class ImageProcessingActivity extends Activity implements SurfaceHolder.C final int pixelCount = mParams.inWidth * mParams.inHeight; mPixelType = Type.createFromClass(mRS, Pixel.class, 1, "Pixel"); - mInPixelsAllocation = Allocation.createSized(mRS, Element.USER_I32(mRS), + mInPixelsAllocation = Allocation.createSized(mRS, + Element.createUser(mRS, Element.DataType.SIGNED_32), pixelCount); - mOutPixelsAllocation = Allocation.createSized(mRS, Element.USER_I32(mRS), + mOutPixelsAllocation = Allocation.createSized(mRS, + Element.createUser(mRS, Element.DataType.SIGNED_32), pixelCount); final int[] data = new int[pixelCount]; @@ -154,7 +156,7 @@ public class ImageProcessingActivity extends Activity implements SurfaceHolder.C mOutData = new int[pixelCount]; mOutPixelsAllocation.data(mOutData); - + ScriptC.Builder sb = new ScriptC.Builder(mRS); sb.setType(mParamsType, "Params", 0); sb.setType(mPixelType, "InPixel", 1); diff --git a/libs/rs/java/Rollo/Android.mk b/libs/rs/java/Rollo/Android.mk deleted file mode 100644 index 5a4957c..0000000 --- a/libs/rs/java/Rollo/Android.mk +++ /dev/null @@ -1,25 +0,0 @@ -# -# Copyright (C) 2008 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. -# - -LOCAL_PATH := $(call my-dir) -include $(CLEAR_VARS) - -LOCAL_SRC_FILES := $(call all-java-files-under, src) -#LOCAL_STATIC_JAVA_LIBRARIES := android.renderscript - -LOCAL_PACKAGE_NAME := Rollo - -include $(BUILD_PACKAGE) diff --git a/libs/rs/java/Rollo/AndroidManifest.xml b/libs/rs/java/Rollo/AndroidManifest.xml deleted file mode 100644 index 12cb28f..0000000 --- a/libs/rs/java/Rollo/AndroidManifest.xml +++ /dev/null @@ -1,16 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<manifest xmlns:android="http://schemas.android.com/apk/res/android" - package="com.android.rollo"> - <application - android:label="Rollo" - android:icon="@drawable/test_pattern"> - <activity android:name="Rollo" - android:theme="@android:style/Theme.Translucent" - android:icon="@drawable/test_pattern"> - <intent-filter> - <action android:name="android.intent.action.MAIN" /> - <category android:name="android.intent.category.LAUNCHER" /> - </intent-filter> - </activity> - </application> -</manifest> diff --git a/libs/rs/java/Rollo/res/drawable/test_pattern.png b/libs/rs/java/Rollo/res/drawable/test_pattern.png Binary files differdeleted file mode 100644 index e7d1455..0000000 --- a/libs/rs/java/Rollo/res/drawable/test_pattern.png +++ /dev/null diff --git a/libs/rs/java/Rollo/res/raw/browser.png b/libs/rs/java/Rollo/res/raw/browser.png Binary files differdeleted file mode 100644 index 513f0be..0000000 --- a/libs/rs/java/Rollo/res/raw/browser.png +++ /dev/null diff --git a/libs/rs/java/Rollo/res/raw/calendar.png b/libs/rs/java/Rollo/res/raw/calendar.png Binary files differdeleted file mode 100644 index 030ae73..0000000 --- a/libs/rs/java/Rollo/res/raw/calendar.png +++ /dev/null diff --git a/libs/rs/java/Rollo/res/raw/g1155.png b/libs/rs/java/Rollo/res/raw/g1155.png Binary files differdeleted file mode 100644 index 68e1843..0000000 --- a/libs/rs/java/Rollo/res/raw/g1155.png +++ /dev/null diff --git a/libs/rs/java/Rollo/res/raw/g2140.png b/libs/rs/java/Rollo/res/raw/g2140.png Binary files differdeleted file mode 100644 index 8c4e853..0000000 --- a/libs/rs/java/Rollo/res/raw/g2140.png +++ /dev/null diff --git a/libs/rs/java/Rollo/res/raw/maps.png b/libs/rs/java/Rollo/res/raw/maps.png Binary files differdeleted file mode 100644 index fd5fc39..0000000 --- a/libs/rs/java/Rollo/res/raw/maps.png +++ /dev/null diff --git a/libs/rs/java/Rollo/res/raw/market.png b/libs/rs/java/Rollo/res/raw/market.png Binary files differdeleted file mode 100644 index 83b6910..0000000 --- a/libs/rs/java/Rollo/res/raw/market.png +++ /dev/null diff --git a/libs/rs/java/Rollo/res/raw/path1920.png b/libs/rs/java/Rollo/res/raw/path1920.png Binary files differdeleted file mode 100644 index 3510665..0000000 --- a/libs/rs/java/Rollo/res/raw/path1920.png +++ /dev/null diff --git a/libs/rs/java/Rollo/res/raw/path1927.png b/libs/rs/java/Rollo/res/raw/path1927.png Binary files differdeleted file mode 100644 index fccc846..0000000 --- a/libs/rs/java/Rollo/res/raw/path1927.png +++ /dev/null diff --git a/libs/rs/java/Rollo/res/raw/path3099.png b/libs/rs/java/Rollo/res/raw/path3099.png Binary files differdeleted file mode 100644 index 527ebf6..0000000 --- a/libs/rs/java/Rollo/res/raw/path3099.png +++ /dev/null diff --git a/libs/rs/java/Rollo/res/raw/path3950.png b/libs/rs/java/Rollo/res/raw/path3950.png Binary files differdeleted file mode 100644 index 59a646a..0000000 --- a/libs/rs/java/Rollo/res/raw/path3950.png +++ /dev/null diff --git a/libs/rs/java/Rollo/res/raw/path431.png b/libs/rs/java/Rollo/res/raw/path431.png Binary files differdeleted file mode 100644 index 5d2ed75..0000000 --- a/libs/rs/java/Rollo/res/raw/path431.png +++ /dev/null diff --git a/libs/rs/java/Rollo/res/raw/path4481.png b/libs/rs/java/Rollo/res/raw/path4481.png Binary files differdeleted file mode 100644 index 78be0fc..0000000 --- a/libs/rs/java/Rollo/res/raw/path4481.png +++ /dev/null diff --git a/libs/rs/java/Rollo/res/raw/path5168.png b/libs/rs/java/Rollo/res/raw/path5168.png Binary files differdeleted file mode 100644 index a7c3a19..0000000 --- a/libs/rs/java/Rollo/res/raw/path5168.png +++ /dev/null diff --git a/libs/rs/java/Rollo/res/raw/path676.png b/libs/rs/java/Rollo/res/raw/path676.png Binary files differdeleted file mode 100644 index 2099690..0000000 --- a/libs/rs/java/Rollo/res/raw/path676.png +++ /dev/null diff --git a/libs/rs/java/Rollo/res/raw/path754.png b/libs/rs/java/Rollo/res/raw/path754.png Binary files differdeleted file mode 100644 index 88aed5b..0000000 --- a/libs/rs/java/Rollo/res/raw/path754.png +++ /dev/null diff --git a/libs/rs/java/Rollo/res/raw/path815.png b/libs/rs/java/Rollo/res/raw/path815.png Binary files differdeleted file mode 100644 index 407570f..0000000 --- a/libs/rs/java/Rollo/res/raw/path815.png +++ /dev/null diff --git a/libs/rs/java/Rollo/res/raw/photos.png b/libs/rs/java/Rollo/res/raw/photos.png Binary files differdeleted file mode 100644 index 1ed8f1e..0000000 --- a/libs/rs/java/Rollo/res/raw/photos.png +++ /dev/null diff --git a/libs/rs/java/Rollo/res/raw/polygon2408.png b/libs/rs/java/Rollo/res/raw/polygon2408.png Binary files differdeleted file mode 100644 index 4413954..0000000 --- a/libs/rs/java/Rollo/res/raw/polygon2408.png +++ /dev/null diff --git a/libs/rs/java/Rollo/res/raw/rollo.c b/libs/rs/java/Rollo/res/raw/rollo.c deleted file mode 100644 index b31be81..0000000 --- a/libs/rs/java/Rollo/res/raw/rollo.c +++ /dev/null @@ -1,184 +0,0 @@ -#pragma version(1) -#pragma stateVertex(PV) -#pragma stateFragment(PF) -#pragma stateStore(PFS) - -// Scratch buffer layout -#define SCRATCH_FADE 0 -#define SCRATCH_ZOOM 1 -#define SCRATCH_ROT 2 - -//#define STATE_POS_X 0 -#define STATE_DONE 1 -//#define STATE_PRESSURE 2 -#define STATE_ZOOM 3 -//#define STATE_WARP 4 -#define STATE_ORIENTATION 5 -#define STATE_SELECTION 6 -#define STATE_FIRST_VISIBLE 7 -#define STATE_COUNT 8 -#define STATE_TOUCH 9 - - -float filter(float val, float target, float str) -{ - float delta = (target - val); - return val + delta * str; -} - -int main(void* con, int ft, int launchID) -{ - int rowCount; - int row; - int col; - int imageID; - int done = loadI32(0, STATE_DONE); - int selectedID = loadI32(0, STATE_SELECTION); - - float f = loadF(2, 0); - - pfClearColor(0.0f, 0.0f, 0.0f, f); - if (done) { - if (f > 0.02f) { - //f = f - 0.02f; - //storeF(2, 0, f); - } - } else { - if (f < 0.8f) { - f = f + 0.02f; - storeF(2, 0, f); - } - } - - float touchCut = 1.f; - if (loadI32(0, STATE_TOUCH)) { - touchCut = 4.f; - } - - - float targetZoom = ((float)loadI32(0, STATE_ZOOM)) / 1000.f; - float zoom = filter(loadF(2, SCRATCH_ZOOM), targetZoom, 0.15 * touchCut); - storeF(2, SCRATCH_ZOOM, zoom); - - float targetRot = loadI32(0, STATE_FIRST_VISIBLE) / 180.0f * 3.14f; - targetRot = targetRot * 0.80f - .12f; - float drawRot = filter(loadF(2, SCRATCH_ROT), targetRot, 0.1f * touchCut); - storeF(2, SCRATCH_ROT, drawRot); - - float diam = 8.f; - float scale = 1.0f / zoom; - - // Bug makes 1.0f alpha fail. - color(1.0f, 1.0f, 1.0f, 0.99f); - - float rot = drawRot * scale; - float rotStep = 16.0f / 180.0f * 3.14f * scale; - rowCount = 4; - int index = 0; - int iconCount = loadI32(0, STATE_COUNT); - while (iconCount) { - float tmpSin = sinf(rot); - float tmpCos = cosf(rot); - //debugF("rot", rot); - - float tx1 = tmpSin * diam - (tmpCos * scale * 0.9f); - float tx2 = tx1 + (tmpCos * scale * 1.8f); - float tz1 = tmpCos * diam + (tmpSin * scale * 0.9f); - float tz2 = tz1 - (tmpSin * scale * 1.8f); - - int y; - for (y = rowCount -1; (y >= 0) && iconCount; y--) { - float ty1 = ((y * 3.1f) - 5.f) * scale; - float ty2 = ty1 + scale * 1.8f; - bindTexture(NAMED_PF, 0, loadI32(1, index)); - drawQuad(tx1, ty1, tz1, - tx2, ty1, tz2, - tx2, ty2, tz2, - tx1, ty2, tz1); - - iconCount--; - index++; - } - rot = rot + rotStep; - } - - if ((zoom < 1.1f) && (zoom > 0.9f)) { - bindProgramVertex(NAMED_PVOrtho); - bindProgramFragment(NAMED_PFText); - bindProgramStore(NAMED_PFSText); - - rot = drawRot * scale; - index = 0; - iconCount = loadI32(0, STATE_COUNT); - while (iconCount) { - int y; - - float tx = 240.f + floorf(sinf(rot) * 430.f) - 64.f + 16.f; - - float alpha = 2.4f - (fabsf(tx - 240.f + 48.f) / 76.f); - if (alpha > 0.99f) { - alpha = 0.99f; - } - alpha = alpha * (1.f - (fabsf(zoom - 1.f) * 10.f)); - - tx = tx + 0.25f; - - for (y = rowCount -1; (y >= 0) && iconCount; y--) { - - if (alpha > 0) { - color(1.0f, 1.0f, 1.0f, alpha); - - float ty = 605.f - y * 150.f; - - ty = ty + 0.25f; - - bindTexture(NAMED_PFText, 0, loadI32(3, index)); - drawRect(tx, ty, tx + 128.f, ty + 32.f, 0.5f); - } - iconCount--; - index++; - } - rot = rot + rotStep; - } - - - bindProgramVertex(NAMED_PV); - bindProgramFragment(NAMED_PF); - bindProgramStore(NAMED_PFS); - } - - // Draw the selected icon - color(1.0f, 1.0f, 1.0f, 0.9f); - rot = drawRot * scale; - index = 0; - iconCount = loadI32(0, STATE_COUNT); - while (iconCount) { - int y; - for (y = rowCount -1; (y >= 0) && iconCount; y--) { - if (index == selectedID) { - - float tmpSin = sinf(rot) * scale; - float tmpCos = cosf(rot) * scale; - float tx1 = tmpSin * diam * 0.9f - tmpCos * 2.f; - float tx2 = tx1 + (tmpCos * 4.f); - float tz1 = tmpCos * diam * 0.9f + tmpSin * 2.f; - float tz2 = tz1 - (tmpSin * 4.f); - - float ty1 = ((y * 3.1f) - 4.5f) * scale; - float ty2 = ty1 + scale * 4.f; - bindTexture(NAMED_PF, 0, loadI32(1, index)); - drawQuad(tx1, ty1, tz1, - tx2, ty1, tz2, - tx2, ty2, tz2, - tx1, ty2, tz1); - } - iconCount--; - index++; - } - rot = rot + rotStep; - } - - return 1; -} - - diff --git a/libs/rs/java/Rollo/res/raw/rollo2.c b/libs/rs/java/Rollo/res/raw/rollo2.c deleted file mode 100644 index 5b5cb2d..0000000 --- a/libs/rs/java/Rollo/res/raw/rollo2.c +++ /dev/null @@ -1,155 +0,0 @@ -#pragma version(1) -#pragma stateVertex(PV) -#pragma stateFragment(PF) -#pragma stateStore(PFS) - -// Scratch buffer layout -#define SCRATCH_FADE 0 -#define SCRATCH_ZOOM 1 -#define SCRATCH_ROT 2 - -//#define STATE_POS_X 0 -#define STATE_DONE 1 -//#define STATE_PRESSURE 2 -#define STATE_ZOOM 3 -//#define STATE_WARP 4 -#define STATE_ORIENTATION 5 -#define STATE_SELECTION 6 -#define STATE_FIRST_VISIBLE 7 -#define STATE_COUNT 8 -#define STATE_TOUCH 9 - -float filter(float val, float target, float str) -{ - float delta = (target - val); - return val + delta * str; -} - - -int main(void* con, int ft, int launchID) -{ - int rowCount; - int imageID; - int done = loadI32(0, STATE_DONE); - int selectedID = loadI32(0, STATE_SELECTION); - int iconCount = loadI32(0, STATE_COUNT); - - float f = loadF(2, 0); - - float iconSize = 1.f; - float iconSpacing = 0.2f; - float z = 4.f; - - pfClearColor(0.0f, 0.0f, 0.0f, f); - if (done) { - } else { - if (f < 0.8f) { - f = f + 0.02f; - storeF(2, 0, f); - } - } - - float touchCut = 1.f; - if (loadI32(0, STATE_TOUCH)) { - touchCut = 5.f; - } - - - float targetZoom = ((float)loadI32(0, STATE_ZOOM)) / 1000.f; - float zoom = filter(loadF(2, SCRATCH_ZOOM), targetZoom, 0.15 * touchCut); - storeF(2, SCRATCH_ZOOM, zoom); - - float targetPos = loadI32(0, STATE_FIRST_VISIBLE) / (-20.0f); - float pos = filter(loadF(2, SCRATCH_ROT), targetPos, 0.1f * touchCut); - storeF(2, SCRATCH_ROT, pos); - pos = pos - 1.f; - - color(1.0f, 1.0f, 1.0f, 1.0f); - - - // Draw flat icons first - int index = ((int)pos) * 4; - int row; - int col; - float xoffset = -0.3f; - float gridSize = iconSize * 4.f + iconSpacing * 3.f; - float yoffset = (pos - ((int)pos)); - for (row = 0; row < 4; row ++) { - float ty1 = (gridSize / 2.f) - ((float)row - yoffset) * (iconSize + iconSpacing) - iconSize; - float ty2 = ty1 + iconSize; - - for (col = 0; (col < 4) && (index < iconCount); col ++) { - if (index >= 0) { - bindTexture(NAMED_PF, 0, loadI32(1, index)); - float fcol = col; - float tx1 = xoffset + (-gridSize / 2.f) + (fcol * (iconSize + iconSpacing)); - float tx2 = tx1 + iconSize; - - drawQuad(tx1, ty1, z, - tx2, ty1, z, - tx2, ty2, z, - tx1, ty2, z); - } - index++; - } - } - - // bottom roller - { - float roll = (1.f - yoffset) * 0.5f * 3.14f; - float tmpSin = sinf(roll); - float tmpCos = cosf(roll); - - for (col = 0; (col < 4) && (index < iconCount) && (index >= 0); col ++) { - float ty2 = (gridSize / 2.f) - ((float)row - yoffset) * (iconSize + iconSpacing); - float ty1 = ty2 - tmpCos * iconSize; - - float tz1 = z + tmpSin * iconSize; - float tz2 = z; - - float tx1 = xoffset + (-gridSize / 2.f) + ((float)col * (iconSize + iconSpacing)); - float tx2 = tx1 + iconSize; - - bindTexture(NAMED_PF, 0, loadI32(1, index)); - drawQuad(tx1, ty1, tz1, - tx2, ty1, tz1, - tx2, ty2, tz2, - tx1, ty2, tz2); - index++; - } - } - - // Top roller - { - index = (((int)pos) * 4) - 4; - float roll = yoffset * 0.5f * 3.14f; - float tmpSin = sinf(roll); - float tmpCos = cosf(roll); - - for (col = 0; (col < 4) && (index < iconCount) && (index >= 0); col ++) { - float ty1 = (gridSize / 2.f) - ((float)-1.f - yoffset) * (iconSize + iconSpacing) - iconSize; - float ty2 = ty1 + tmpCos * iconSize; - - float tz1 = z; - float tz2 = z + tmpSin * iconSize; - - float tx1 = xoffset + (-gridSize / 2.f) + ((float)col * (iconSize + iconSpacing)); - float tx2 = tx1 + iconSize; - - bindTexture(NAMED_PF, 0, loadI32(1, index)); - drawQuad(tx1, ty1, tz1, - tx2, ty1, tz1, - tx2, ty2, tz2, - tx1, ty2, tz2); - index++; - } - } - - - - - return 1; -} - - - diff --git a/libs/rs/java/Rollo/res/raw/settings.png b/libs/rs/java/Rollo/res/raw/settings.png Binary files differdeleted file mode 100644 index dd2cd95..0000000 --- a/libs/rs/java/Rollo/res/raw/settings.png +++ /dev/null diff --git a/libs/rs/java/Rollo/src/com/android/rollo/Rollo.java b/libs/rs/java/Rollo/src/com/android/rollo/Rollo.java deleted file mode 100644 index 400d801..0000000 --- a/libs/rs/java/Rollo/src/com/android/rollo/Rollo.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (C) 2008 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. - */ - -package com.android.rollo; - -import android.renderscript.RSSurfaceView; -import android.renderscript.RenderScript; - -import android.app.Activity; -import android.content.res.Configuration; -import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; -import android.os.Message; -import android.provider.Settings.System; -import android.util.Config; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.view.View; -import android.view.Window; -import android.widget.Button; -import android.widget.ListView; - -import java.lang.Runtime; - -public class Rollo extends Activity { - //EventListener mListener = new EventListener(); - - private static final String LOG_TAG = "libRS_jni"; - private static final boolean DEBUG = false; - private static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV; - - private RolloView mView; - - // get the current looper (from your Activity UI thread for instance - - - - @Override - public void onCreate(Bundle icicle) { - super.onCreate(icicle); - - // Create our Preview view and set it as the content of our - // Activity - mView = new RolloView(this); - setContentView(mView); - } - - @Override - protected void onResume() { - // Ideally a game should implement onResume() and onPause() - // to take appropriate action when the activity looses focus - super.onResume(); - mView.onResume(); - } - - @Override - protected void onPause() { - // Ideally a game should implement onResume() and onPause() - // to take appropriate action when the activity looses focus - super.onPause(); - mView.onPause(); - - Runtime.getRuntime().exit(0); - } - - - static void log(String message) { - if (LOG_ENABLED) { - Log.v(LOG_TAG, message); - } - } - - -} - diff --git a/libs/rs/java/Rollo/src/com/android/rollo/RolloRS.java b/libs/rs/java/Rollo/src/com/android/rollo/RolloRS.java deleted file mode 100644 index ff89bc3..0000000 --- a/libs/rs/java/Rollo/src/com/android/rollo/RolloRS.java +++ /dev/null @@ -1,315 +0,0 @@ -/* - * Copyright (C) 2008 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. - */ - -package com.android.rollo; - -import java.io.Writer; - -import android.renderscript.RenderScript; -import android.renderscript.ProgramVertex; -import android.renderscript.Element; -import android.renderscript.Allocation; -import android.renderscript.Script; -import android.renderscript.ScriptC; -import android.renderscript.ProgramFragment; -import android.renderscript.ProgramStore; -import android.renderscript.Sampler; - -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.Canvas; -import android.graphics.Paint; -import android.graphics.drawable.BitmapDrawable; -import android.graphics.drawable.Drawable; -import android.graphics.Typeface; - -import android.content.Context; -import android.content.res.Resources; -import android.util.Log; - -public class RolloRS { - //public static final int STATE_SELECTED_ID = 0; - public static final int STATE_DONE = 1; - //public static final int STATE_PRESSURE = 2; - public static final int STATE_ZOOM = 3; - //public static final int STATE_WARP = 4; - public static final int STATE_ORIENTATION = 5; - public static final int STATE_SELECTION = 6; - public static final int STATE_FIRST_VISIBLE = 7; - public static final int STATE_COUNT = 8; - public static final int STATE_TOUCH = 9; - - - public RolloRS() { - } - - public void init(RenderScript rs, Resources res, int width, int height) { - mRS = rs; - mRes = res; - mWidth = width; - mHeight = height; - initNamed(); - initRS(); - } - - public void setPosition(float column) { - mAllocStateBuf[STATE_FIRST_VISIBLE] = (int)(column * (-20)); - mAllocState.data(mAllocStateBuf); - } - - public void setTouch(boolean touch) { - mAllocStateBuf[STATE_TOUCH] = touch ? 1 : 0; - mAllocState.data(mAllocStateBuf); - } - - public void setZoom(float z) { - //Log.e("rs", "zoom " + Float.toString(z)); - - mAllocStateBuf[STATE_ZOOM] = (int)(z * 1000.f); - mAllocState.data(mAllocStateBuf); - } - - public void setSelected(int index) { - //Log.e("rs", "setSelected " + Integer.toString(index)); - - mAllocStateBuf[STATE_SELECTION] = index; - mAllocStateBuf[STATE_DONE] = 1; - mAllocState.data(mAllocStateBuf); - } - - private int mWidth; - private int mHeight; - - private Resources mRes; - private RenderScript mRS; - private Script mScript; - private Sampler mSampler; - private Sampler mSamplerText; - private ProgramStore mPSBackground; - private ProgramStore mPSText; - private ProgramFragment mPFImages; - private ProgramFragment mPFText; - private ProgramVertex mPV; - private ProgramVertex.MatrixAllocation mPVAlloc; - private ProgramVertex mPVOrtho; - private ProgramVertex.MatrixAllocation mPVOrthoAlloc; - private Allocation[] mIcons; - private Allocation[] mLabels; - - private int[] mAllocStateBuf; - private Allocation mAllocState; - - private int[] mAllocIconIDBuf; - private Allocation mAllocIconID; - - private int[] mAllocLabelIDBuf; - private Allocation mAllocLabelID; - - private int[] mAllocScratchBuf; - private Allocation mAllocScratch; - - private void initNamed() { - Sampler.Builder sb = new Sampler.Builder(mRS); - sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR); - sb.setMag(Sampler.Value.LINEAR); - sb.setWrapS(Sampler.Value.CLAMP); - sb.setWrapT(Sampler.Value.CLAMP); - mSampler = sb.create(); - - sb.setMin(Sampler.Value.NEAREST); - sb.setMag(Sampler.Value.NEAREST); - mSamplerText = sb.create(); - - - ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null); - bf.setTexEnable(true, 0); - bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0); - mPFImages = bf.create(); - mPFImages.setName("PF"); - mPFImages.bindSampler(mSampler, 0); - - bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0); - mPFText = bf.create(); - mPFText.setName("PFText"); - mPFText.bindSampler(mSamplerText, 0); - - ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null); - bs.setDepthFunc(ProgramStore.DepthFunc.LESS); - bs.setDitherEnable(false); - bs.setDepthMask(true); - bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA, - ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA); - mPSBackground = bs.create(); - mPSBackground.setName("PFS"); - - bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS); - bs.setDepthMask(false); - bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA, - ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA); - mPSText = bs.create(); - mPSText.setName("PFSText"); - - mPVAlloc = new ProgramVertex.MatrixAllocation(mRS); - mPVAlloc.setupProjectionNormalized(mWidth, mHeight); - - ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null); - mPV = pvb.create(); - mPV.setName("PV"); - mPV.bindAllocation(mPVAlloc); - - mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS); - mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight); - - pvb.setTextureMatrixEnable(true); - mPVOrtho = pvb.create(); - mPVOrtho.setName("PVOrtho"); - mPVOrtho.bindAllocation(mPVOrthoAlloc); - - mRS.contextBindProgramVertex(mPV); - - mAllocScratchBuf = new int[32]; - mAllocScratch = Allocation.createSized(mRS, - Element.USER_I32(mRS), mAllocScratchBuf.length); - mAllocScratch.data(mAllocScratchBuf); - - Log.e("rs", "Done loading named"); - - - - { - mIcons = new Allocation[29]; - mAllocIconIDBuf = new int[mIcons.length]; - mAllocIconID = Allocation.createSized(mRS, - Element.USER_I32(mRS), mAllocIconIDBuf.length); - - mLabels = new Allocation[29]; - mAllocLabelIDBuf = new int[mLabels.length]; - mAllocLabelID = Allocation.createSized(mRS, - Element.USER_I32(mRS), mLabels.length); - - Element ie8888 = Element.RGBA_8888(mRS); - - mIcons[0] = Allocation.createFromBitmapResource(mRS, mRes, R.raw.browser, ie8888, true); - mIcons[1] = Allocation.createFromBitmapResource(mRS, mRes, R.raw.market, ie8888, true); - mIcons[2] = Allocation.createFromBitmapResource(mRS, mRes, R.raw.photos, ie8888, true); - mIcons[3] = Allocation.createFromBitmapResource(mRS, mRes, R.raw.settings, ie8888, true); - mIcons[4] = Allocation.createFromBitmapResource(mRS, mRes, R.raw.calendar, ie8888, true); - mIcons[5] = Allocation.createFromBitmapResource(mRS, mRes, R.raw.g1155, ie8888, true); - mIcons[6] = Allocation.createFromBitmapResource(mRS, mRes, R.raw.g2140, ie8888, true); - mIcons[7] = Allocation.createFromBitmapResource(mRS, mRes, R.raw.maps, ie8888, true); - mIcons[8] = Allocation.createFromBitmapResource(mRS, mRes, R.raw.path431, ie8888, true); - mIcons[9] = Allocation.createFromBitmapResource(mRS, mRes, R.raw.path676, ie8888, true); - mIcons[10] = Allocation.createFromBitmapResource(mRS, mRes, R.raw.path754, ie8888, true); - mIcons[11] = Allocation.createFromBitmapResource(mRS, mRes, R.raw.path815, ie8888, true); - mIcons[12] = Allocation.createFromBitmapResource(mRS, mRes, R.raw.path1920, ie8888, true); - mIcons[13] = Allocation.createFromBitmapResource(mRS, mRes, R.raw.path1927, ie8888, true); - mIcons[14] = Allocation.createFromBitmapResource(mRS, mRes, R.raw.path3099, ie8888, true); - mIcons[15] = Allocation.createFromBitmapResource(mRS, mRes, R.raw.path3950, ie8888, true); - mIcons[16] = Allocation.createFromBitmapResource(mRS, mRes, R.raw.path4481, ie8888, true); - mIcons[17] = Allocation.createFromBitmapResource(mRS, mRes, R.raw.path5168, ie8888, true); - mIcons[18] = Allocation.createFromBitmapResource(mRS, mRes, R.raw.polygon2408, ie8888, true); - - mLabels[0] = makeTextBitmap("browser"); - mLabels[1] = makeTextBitmap("market"); - mLabels[2] = makeTextBitmap("photos"); - mLabels[3] = makeTextBitmap("settings"); - mLabels[4] = makeTextBitmap("calendar"); - mLabels[5] = makeTextBitmap("g1155"); - mLabels[6] = makeTextBitmap("g2140"); - mLabels[7] = makeTextBitmap("maps"); - mLabels[8] = makeTextBitmap("path431"); - mLabels[9] = makeTextBitmap("path676"); - mLabels[10] = makeTextBitmap("path754"); - mLabels[11] = makeTextBitmap("path815"); - mLabels[12] = makeTextBitmap("path1920"); - mLabels[13] = makeTextBitmap("path1927"); - mLabels[14] = makeTextBitmap("path3099"); - mLabels[15] = makeTextBitmap("path3950"); - mLabels[16] = makeTextBitmap("path4481"); - mLabels[17] = makeTextBitmap("path5168"); - mLabels[18] = makeTextBitmap("polygon2408"); - - mIcons[19] = mIcons[0]; - mIcons[20] = mIcons[1]; - mIcons[21] = mIcons[2]; - mIcons[22] = mIcons[3]; - mIcons[23] = mIcons[4]; - mIcons[24] = mIcons[5]; - mIcons[25] = mIcons[6]; - mIcons[26] = mIcons[7]; - mIcons[27] = mIcons[8]; - mIcons[28] = mIcons[9]; - - mLabels[19] = mLabels[0]; - mLabels[20] = mLabels[1]; - mLabels[21] = mLabels[2]; - mLabels[22] = mLabels[3]; - mLabels[23] = mLabels[4]; - mLabels[24] = mLabels[5]; - mLabels[25] = mLabels[6]; - mLabels[26] = mLabels[7]; - mLabels[27] = mLabels[8]; - mLabels[28] = mLabels[9]; - - for(int ct=0; ct < mIcons.length; ct++) { - mIcons[ct].uploadToTexture(0); - mLabels[ct].uploadToTexture(0); - mAllocIconIDBuf[ct] = mIcons[ct].getID(); - mAllocLabelIDBuf[ct] = mLabels[ct].getID(); - } - mAllocIconID.data(mAllocIconIDBuf); - mAllocLabelID.data(mAllocLabelIDBuf); - } - - } - - Allocation makeTextBitmap(String t) { - Bitmap b = Bitmap.createBitmap(128, 32, Bitmap.Config.ARGB_8888); - Canvas c = new Canvas(b); - Paint p = new Paint(); - p.setTypeface(Typeface.DEFAULT_BOLD); - p.setTextSize(20); - p.setColor(0xffffffff); - c.drawText(t, 2, 26, p); - return Allocation.createFromBitmap(mRS, b, Element.RGBA_8888(mRS), true); - } - - - private void initRS() { - ScriptC.Builder sb = new ScriptC.Builder(mRS); - sb.setScript(mRes, R.raw.rollo); - //sb.setScript(mRes, R.raw.rollo2); - sb.setRoot(true); - mScript = sb.create(); - mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f); - - mAllocStateBuf = new int[] {0, 0, 0, 8, 0, 0, -1, 0, mAllocIconIDBuf.length, 0, 0}; - mAllocState = Allocation.createSized(mRS, - Element.USER_I32(mRS), mAllocStateBuf.length); - mScript.bindAllocation(mAllocState, 0); - mScript.bindAllocation(mAllocIconID, 1); - mScript.bindAllocation(mAllocScratch, 2); - mScript.bindAllocation(mAllocLabelID, 3); - setPosition(0); - setZoom(1); - - //RenderScript.File f = mRS.fileOpen("/sdcard/test.a3d"); - - mRS.contextBindRootScript(mScript); - } -} - - diff --git a/libs/rs/java/Rollo/src/com/android/rollo/RolloView.java b/libs/rs/java/Rollo/src/com/android/rollo/RolloView.java deleted file mode 100644 index 7524a0e..0000000 --- a/libs/rs/java/Rollo/src/com/android/rollo/RolloView.java +++ /dev/null @@ -1,214 +0,0 @@ -/* - * Copyright (C) 2008 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. - */ - -package com.android.rollo; - -import java.io.Writer; -import java.util.ArrayList; -import java.util.concurrent.Semaphore; -import java.lang.Float; - -import android.renderscript.RSSurfaceView; -import android.renderscript.RenderScript; - -import android.content.Context; -import android.content.res.Resources; -import android.graphics.Bitmap; -import android.graphics.drawable.BitmapDrawable; -import android.graphics.drawable.Drawable; -import android.os.Handler; -import android.os.Message; -import android.util.AttributeSet; -import android.util.Log; -import android.view.Surface; -import android.view.SurfaceHolder; -import android.view.SurfaceView; -import android.view.KeyEvent; -import android.view.MotionEvent; -import android.graphics.PixelFormat; - - -public class RolloView extends RSSurfaceView { - public RolloView(Context context) { - super(context); - setFocusable(true); - getHolder().setFormat(PixelFormat.TRANSLUCENT); - } - - private RenderScript mRS; - private RolloRS mRender; - - public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { - super.surfaceChanged(holder, format, w, h); - - mRS = createRenderScript(false); - mRender = new RolloRS(); - mRender.init(mRS, getResources(), w, h); - } - - @Override - public boolean onKeyDown(int keyCode, KeyEvent event) - { - // break point at here - // this method doesn't work when 'extends View' include 'extends ScrollView'. - return super.onKeyDown(keyCode, event); - } - - boolean mControlMode = false; - boolean mZoomMode = false; - boolean mFlingMode = false; - float mFlingX = 0; - float mFlingY = 0; - float mColumn = -1; - float mOldColumn; - float mZoom = 1; - - int mIconCount = 29; - int mRows = 4; - int mColumns = (mIconCount + mRows - 1) / mRows; - - float mMaxZoom = ((float)mColumns) / 3.f; - - - void setColumn(boolean clamp) - { - //Log.e("rs", " col = " + Float.toString(mColumn)); - float c = mColumn; - if(c > (mColumns -2)) { - c = (mColumns -2); - } - if(c < 0) { - c = 0; - } - mRender.setPosition(c); - if(clamp) { - mColumn = c; - } - } - - void computeSelection(float x, float y) - { - float col = mColumn + (x - 0.5f) * 4 + 1.25f; - int iCol = (int)(col + 0.25f); - - float row = (y / 0.8f) * mRows; - int iRow = (int)(row - 0.5f); - - mRender.setSelected(iCol * mRows + iRow); - } - - @Override - public boolean onTouchEvent(MotionEvent ev) - { - boolean ret = true; - int act = ev.getAction(); - if (act == ev.ACTION_UP) { - ret = false; - } - - float nx = ev.getX() / getWidth(); - float ny = ev.getY() / getHeight(); - - //Log.e("rs", "width=" + Float.toString(getWidth())); - //Log.e("rs", "height=" + Float.toString(getHeight())); - - mRender.setTouch(ret); - - if((ny > 0.85f) || mControlMode) { - mFlingMode = false; - - // Projector control - if((nx > 0.2f) && (nx < 0.8f) || mControlMode) { - if(act != ev.ACTION_UP) { - float zoom = mMaxZoom; - if(mControlMode) { - if(!mZoomMode) { - zoom = 1.f; - } - float dx = nx - mFlingX; - - if((ny < 0.9) && mZoomMode) { - zoom = mMaxZoom - ((0.9f - ny) * 10.f); - if(zoom < 1) { - zoom = 1; - mZoomMode = false; - } - mOldColumn = mColumn; - } - mColumn += dx * 4;// * zoom; - if(zoom > 1.01f) { - mColumn += (mZoom - zoom) * (nx - 0.5f) * 4 * zoom; - } - } else { - mOldColumn = mColumn; - mColumn = ((float)mColumns) / 2; - mControlMode = true; - mZoomMode = true; - } - mZoom = zoom; - mFlingX = nx; - mRender.setZoom(zoom); - if(mZoom < 1.01f) { - computeSelection(nx, ny); - } - } else { - mControlMode = false; - mColumn = mOldColumn; - mRender.setZoom(1.f); - mRender.setSelected(-1); - } - } else { - // Do something with corners here.... - } - setColumn(true); - - } else { - // icon control - if(act != ev.ACTION_UP) { - if(mFlingMode) { - mColumn += (mFlingX - nx) * 4; - setColumn(true); - } - mFlingMode = true; - mFlingX = nx; - mFlingY = ny; - } else { - mFlingMode = false; - mColumn = (float)(java.lang.Math.floor(mColumn * 0.25f + 0.3f) * 4.f) + 1.f; - setColumn(true); - } - } - - - return ret; - } - - @Override - public boolean onTrackballEvent(MotionEvent ev) - { - float x = ev.getX(); - float y = ev.getY(); - //Float tx = new Float(x); - //Float ty = new Float(y); - //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString()); - - - return true; - } - -} - - diff --git a/libs/rs/rs.spec b/libs/rs/rs.spec index a4e72d9..ac115d3 100644 --- a/libs/rs/rs.spec +++ b/libs/rs/rs.spec @@ -20,16 +20,6 @@ ContextBindProgramRaster { param RsProgramRaster pgm } -ContextSetDefineF { - param const char* name - param float value - } - -ContextSetDefineI32 { - param const char* name - param int32_t value - } - ContextPause { } @@ -60,18 +50,19 @@ ObjDestroy { param void *obj } -ElementBegin { -} - -ElementAdd { - param RsDataKind dataKind - param RsDataType dataType - param bool isNormalized - param size_t bits - param const char * name +ElementCreate { + param RsDataType mType + param RsDataKind mKind + param bool mNormalized + param uint32_t mVectorSize + ret RsElement } -ElementCreate { +ElementCreate2 { + param size_t count + param const RsElement * elements + param const char ** names + param const size_t * nameLengths ret RsElement } @@ -366,56 +357,50 @@ ProgramRasterSetPointSize{ } -ProgramFragmentBegin { - param RsElement in - param RsElement out - param bool pointSpriteEnable +ProgramBindConstants { + param RsProgram vp + param uint32_t slot + param RsAllocation constants } -ProgramFragmentBindTexture { + +ProgramBindTexture { param RsProgramFragment pf param uint32_t slot param RsAllocation a } -ProgramFragmentBindSampler { +ProgramBindSampler { param RsProgramFragment pf param uint32_t slot param RsSampler s } -ProgramFragmentSetSlot { - param uint32_t slot - param bool enable - param RsTexEnvMode env - param RsType t - } - ProgramFragmentCreate { + param const uint32_t * params + param uint32_t paramLength ret RsProgramFragment } - -ProgramVertexBegin { - param RsElement in - param RsElement out +ProgramFragmentCreate2 { + param const char * shaderText + param uint32_t shaderLength + param const uint32_t * params + param uint32_t paramLength + ret RsProgramFragment } ProgramVertexCreate { + param bool texMat ret RsProgramVertex } -ProgramVertexBindAllocation { - param RsProgramVertex vpgm - param RsAllocation constants - } - -ProgramVertexSetTextureMatrixEnable { - param bool enable - } - -ProgramVertexAddLight { - param RsLight light +ProgramVertexCreate2 { + param const char * shaderText + param uint32_t shaderLength + param const uint32_t * params + param uint32_t paramLength + ret RsProgramVertex } LightBegin { diff --git a/libs/rs/rsAllocation.cpp b/libs/rs/rsAllocation.cpp index ff8d29f..eb6c503 100644 --- a/libs/rs/rsAllocation.cpp +++ b/libs/rs/rsAllocation.cpp @@ -38,9 +38,9 @@ Allocation::Allocation(Context *rsc, const Type *type) : ObjectBase(rsc) mIsTexture = false; mTextureID = 0; - mIsVertexBuffer = false; mBufferID = 0; + mUploadDefered = false; mType.set(type); rsAssert(type); @@ -88,13 +88,26 @@ bool Allocation::fixAllocation() return false; } -void Allocation::uploadToTexture(Context *rsc, uint32_t lodOffset) +void Allocation::deferedUploadToTexture(const Context *rsc, uint32_t lodOffset) { - //rsAssert(!mTextureId); rsAssert(lodOffset < mType->getLODCount()); + mIsTexture = true; + mTextureLOD = lodOffset; + mUploadDefered = true; +} + +void Allocation::uploadToTexture(const Context *rsc) +{ + //rsAssert(!mTextureId); + + mIsTexture = true; + if (!rsc->checkDriver()) { + mUploadDefered = true; + return; + } - GLenum type = mType->getElement()->getGLType(); - GLenum format = mType->getElement()->getGLFormat(); + GLenum type = mType->getElement()->getComponent().getGLType(); + GLenum format = mType->getElement()->getComponent().getGLFormat(); if (!type || !format) { return; @@ -109,15 +122,16 @@ void Allocation::uploadToTexture(Context *rsc, uint32_t lodOffset) // Force a crash to 1: restart the app, 2: make sure we get a bugreport. LOGE("Upload to texture failed to gen mTextureID"); rsc->dumpDebug(); - ((char *)0)[0] = 0; + mUploadDefered = true; + return; } } glBindTexture(GL_TEXTURE_2D, mTextureID); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); Adapter2D adapt(getContext(), this); - for(uint32_t lod = 0; (lod + lodOffset) < mType->getLODCount(); lod++) { - adapt.setLOD(lod+lodOffset); + for(uint32_t lod = 0; (lod + mTextureLOD) < mType->getLODCount(); lod++) { + adapt.setLOD(lod+mTextureLOD); uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0)); glTexImage2D(GL_TEXTURE_2D, lod, format, @@ -126,19 +140,50 @@ void Allocation::uploadToTexture(Context *rsc, uint32_t lodOffset) } } -void Allocation::uploadToBufferObject() +void Allocation::deferedUploadToBufferObject(const Context *rsc) +{ + mIsVertexBuffer = true; + mUploadDefered = true; +} + +void Allocation::uploadToBufferObject(const Context *rsc) { rsAssert(!mType->getDimY()); rsAssert(!mType->getDimZ()); + mIsVertexBuffer = true; + if (!rsc->checkDriver()) { + mUploadDefered = true; + return; + } + if (!mBufferID) { glGenBuffers(1, &mBufferID); } + if (!mBufferID) { + LOGE("Upload to buffer object failed"); + mUploadDefered = true; + return; + } + glBindBuffer(GL_ARRAY_BUFFER, mBufferID); glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); } +void Allocation::uploadCheck(const Context *rsc) +{ + if (mUploadDefered) { + mUploadDefered = false; + if (mIsVertexBuffer) { + uploadToBufferObject(rsc); + } + if (mIsTexture) { + uploadToTexture(rsc); + } + } +} + void Allocation::data(const void *data, uint32_t sizeBytes) { @@ -148,6 +193,8 @@ void Allocation::data(const void *data, uint32_t sizeBytes) return; } memcpy(mPtr, data, size); + sendDirty(); + mUploadDefered = true; } void Allocation::read(void *data) @@ -168,6 +215,8 @@ void Allocation::subData(uint32_t xoff, uint32_t count, const void *data, uint32 return; } memcpy(ptr, data, size); + sendDirty(); + mUploadDefered = true; } void Allocation::subData(uint32_t xoff, uint32_t yoff, @@ -192,6 +241,8 @@ void Allocation::subData(uint32_t xoff, uint32_t yoff, src += lineSize; dst += destW * eSize; } + sendDirty(); + mUploadDefered = true; } void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff, @@ -214,10 +265,30 @@ void Allocation::dumpLOGV(const char *prefix) const LOGV("%s allocation mIsTexture=%i mTextureID=%i, mIsVertexBuffer=%i, mBufferID=%i", prefix, mIsTexture, mTextureID, mIsVertexBuffer, mBufferID); +} +void Allocation::addProgramToDirty(const Program *p) +{ + mToDirtyList.add(p); +} +void Allocation::removeProgramToDirty(const Program *p) +{ + for (size_t ct=0; ct < mToDirtyList.size(); ct++) { + if (mToDirtyList[ct] == p) { + mToDirtyList.removeAt(ct); + return; + } + } + rsAssert(0); } +void Allocation::sendDirty() const +{ + for (size_t ct=0; ct < mToDirtyList.size(); ct++) { + mToDirtyList[ct]->forceDirty(); + } +} ///////////////// // @@ -247,13 +318,13 @@ RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count) void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, uint32_t baseMipLevel) { Allocation *alloc = static_cast<Allocation *>(va); - alloc->uploadToTexture(rsc, baseMipLevel); + alloc->deferedUploadToTexture(rsc, baseMipLevel); } void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) { Allocation *alloc = static_cast<Allocation *>(va); - alloc->uploadToBufferObject(); + alloc->deferedUploadToBufferObject(rsc); } static void mip565(const Adapter2D &out, const Adapter2D &in) @@ -294,6 +365,25 @@ static void mip8888(const Adapter2D &out, const Adapter2D &in) } } +static void mip8(const Adapter2D &out, const Adapter2D &in) +{ + uint32_t w = out.getDimX(); + uint32_t h = out.getDimY(); + + for (uint32_t y=0; y < h; y++) { + uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y)); + const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2)); + const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1)); + + for (uint32_t x=0; x < w; x++) { + *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f); + oPtr ++; + i1 += 2; + i2 += 2; + } + } +} + static void mip(const Adapter2D &out, const Adapter2D &in) { switch(out.getBaseType()->getElement()->getSizeBits()) { @@ -303,6 +393,9 @@ static void mip(const Adapter2D &out, const Adapter2D &in) case 16: mip565(out, in); break; + case 8: + mip8(out, in); + break; } @@ -350,10 +443,10 @@ static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t co static ElementConverter_t pickConverter(const Element *dst, const Element *src) { - GLenum srcGLType = src->getGLType(); - GLenum srcGLFmt = src->getGLFormat(); - GLenum dstGLType = dst->getGLType(); - GLenum dstGLFmt = dst->getGLFormat(); + GLenum srcGLType = src->getComponent().getGLType(); + GLenum srcGLFmt = src->getComponent().getGLFormat(); + GLenum dstGLType = dst->getComponent().getGLType(); + GLenum dstGLFmt = dst->getComponent().getGLFormat(); if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) { switch(dst->getSizeBytes()) { @@ -391,8 +484,9 @@ RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, uint32_t w, uint32_t h { const Element *src = static_cast<const Element *>(_src); const Element *dst = static_cast<const Element *>(_dst); - rsAssert(!(w & (w-1))); - rsAssert(!(h & (h-1))); + + // Check for pow2 on pre es 2.0 versions. + rsAssert(rsc->checkVersion2_0() || (!(w & (w-1)) && !(h & (h-1)))); //LOGE("rsi_AllocationCreateFromBitmap %i %i %i %i %i", w, h, dstFmt, srcFmt, genMips); rsi_TypeBegin(rsc, _dst); @@ -452,31 +546,24 @@ RsAllocation rsi_AllocationCreateFromBitmapBoxed(Context *rsc, uint32_t w, uint3 RsAllocation ret = rsi_AllocationCreateFromBitmap(rsc, w2, h2, _dst, _src, genMips, tmp); free(tmp); return ret; - - - - } void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes) { Allocation *a = static_cast<Allocation *>(va); a->data(data, sizeBytes); - rsc->allocationCheck(a); } void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes) { Allocation *a = static_cast<Allocation *>(va); a->subData(xoff, count, data, sizeBytes); - rsc->allocationCheck(a); } void rsi_Allocation2DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) { Allocation *a = static_cast<Allocation *>(va); a->subData(xoff, yoff, w, h, data, sizeBytes); - rsc->allocationCheck(a); } void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) diff --git a/libs/rs/rsAllocation.h b/libs/rs/rsAllocation.h index f54284a..f0b2122 100644 --- a/libs/rs/rsAllocation.h +++ b/libs/rs/rsAllocation.h @@ -23,7 +23,7 @@ namespace android { namespace renderscript { - +class Program; class Allocation : public ObjectBase { @@ -46,10 +46,12 @@ public: void * getPtr() const {return mPtr;} const Type * getType() const {return mType.get();} - void uploadToTexture(Context *rsc, uint32_t lodOffset = 0); + void deferedUploadToTexture(const Context *rsc, uint32_t lodOffset); + void uploadToTexture(const Context *rsc); uint32_t getTextureID() const {return mTextureID;} - void uploadToBufferObject(); + void deferedUploadToBufferObject(const Context *rsc); + void uploadToBufferObject(const Context *rsc); uint32_t getBufferObjectID() const {return mBufferID;} @@ -65,13 +67,21 @@ public: void enableGLVertexBuffers() const; void setupGLIndexBuffers() const; + void addProgramToDirty(const Program *); + void removeProgramToDirty(const Program *); + virtual void dumpLOGV(const char *prefix) const; + virtual void uploadCheck(const Context *rsc); protected: + void sendDirty() const; + ObjectBaseRef<const Type> mType; void * mPtr; + Vector<const Program *> mToDirtyList; + // Usage restrictions bool mCpuWrite; bool mCpuRead; @@ -88,6 +98,7 @@ protected: // Is this a legal structure to be used as a texture source. // Initially this will require 1D or 2D and color data bool mIsTexture; + uint32_t mTextureLOD; uint32_t mTextureID; // Is this a legal structure to be used as a vertex source. @@ -95,6 +106,8 @@ protected: // is allowed. bool mIsVertexBuffer; uint32_t mBufferID; + + bool mUploadDefered; }; } diff --git a/libs/rs/rsComponent.cpp b/libs/rs/rsComponent.cpp index de074c8..15a56f7 100644 --- a/libs/rs/rsComponent.cpp +++ b/libs/rs/rsComponent.cpp @@ -15,96 +15,323 @@ */ #include "rsComponent.h" + #include <GLES/gl.h> using namespace android; using namespace android::renderscript; +Component::Component() +{ + set(RS_TYPE_NONE, RS_KIND_USER, false, 1); +} -Component::Component(Context *rsc) : ObjectBase(rsc) +Component::~Component() { - mAllocFile = __FILE__; - mAllocLine = __LINE__; - mType = FLOAT; - mKind = USER; - mIsNormalized = false; - mBits = 0; } -Component::Component(Context *rsc, - DataKind dk, DataType dt, - bool isNormalized, uint32_t bits, const char * name) : ObjectBase(rsc) +void Component::set(RsDataType dt, RsDataKind dk, bool norm, uint32_t vecSize) { - mAllocFile = __FILE__; - mAllocLine = __LINE__; mType = dt; mKind = dk; - mIsNormalized = isNormalized; - mBits = bits; - if (name) { - mName = name; + mNormalized = norm; + mVectorSize = vecSize; + rsAssert(vecSize <= 4); + + mBits = 0; + mTypeBits = 0; + mIsFloat = false; + mIsSigned = false; + mIsPixel = false; + + switch(mKind) { + case RS_KIND_PIXEL_L: + case RS_KIND_PIXEL_A: + mIsPixel = true; + rsAssert(mVectorSize == 1); + rsAssert(mNormalized == true); + break; + case RS_KIND_PIXEL_LA: + mIsPixel = true; + rsAssert(mVectorSize == 2); + rsAssert(mNormalized == true); + break; + case RS_KIND_PIXEL_RGB: + mIsPixel = true; + rsAssert(mVectorSize == 3); + rsAssert(mNormalized == true); + break; + case RS_KIND_PIXEL_RGBA: + mIsPixel = true; + rsAssert(mVectorSize == 4); + rsAssert(mNormalized == true); + break; + default: + break; } -} -const char * Component::getCType() const -{ switch(mType) { - case FLOAT: - return "float"; - case SIGNED: - case UNSIGNED: - switch(mBits) { - case 32: - return "int"; - case 16: - return "short"; - case 8: - return "char"; - } + case RS_TYPE_NONE: + return; + case RS_TYPE_UNSIGNED_5_6_5: + mVectorSize = 3; + mBits = 16; + mNormalized = true; + rsAssert(mKind == RS_KIND_PIXEL_RGB); + return; + case RS_TYPE_UNSIGNED_5_5_5_1: + mVectorSize = 4; + mBits = 16; + mNormalized = true; + rsAssert(mKind == RS_KIND_PIXEL_RGBA); + return; + case RS_TYPE_UNSIGNED_4_4_4_4: + mVectorSize = 4; + mBits = 16; + mNormalized = true; + rsAssert(mKind == RS_KIND_PIXEL_RGBA); + return; + case RS_TYPE_ELEMENT: + case RS_TYPE_TYPE: + case RS_TYPE_ALLOCATION: + case RS_TYPE_SAMPLER: + case RS_TYPE_SCRIPT: + case RS_TYPE_MESH: + case RS_TYPE_PROGRAM_FRAGMENT: + case RS_TYPE_PROGRAM_VERTEX: + case RS_TYPE_PROGRAM_RASTER: + case RS_TYPE_PROGRAM_STORE: + rsAssert(mVectorSize == 1); + rsAssert(mNormalized == false); + rsAssert(mKind == RS_KIND_USER); + mBits = 32; + mTypeBits = 32; + return; + + case RS_TYPE_FLOAT_16: + mTypeBits = 16; + mIsFloat = true; + break; + case RS_TYPE_FLOAT_32: + mTypeBits = 32; + mIsFloat = true; + break; + case RS_TYPE_FLOAT_64: + mTypeBits = 64; + mIsFloat = true; + break; + case RS_TYPE_SIGNED_8: + mTypeBits = 8; + mIsSigned = true; + break; + case RS_TYPE_SIGNED_16: + mTypeBits = 16; + mIsSigned = true; + break; + case RS_TYPE_SIGNED_32: + mTypeBits = 32; + mIsSigned = true; + break; + case RS_TYPE_SIGNED_64: + mTypeBits = 64; + mIsSigned = true; + break; + case RS_TYPE_UNSIGNED_8: + mTypeBits = 8; + break; + case RS_TYPE_UNSIGNED_16: + mTypeBits = 16; + break; + case RS_TYPE_UNSIGNED_32: + mTypeBits = 32; + break; + case RS_TYPE_UNSIGNED_64: + mTypeBits = 64; break; } - return NULL; + + mBits = mTypeBits * mVectorSize; } -Component::~Component() + + + +uint32_t Component::getGLType() const { + switch (mType) { + case RS_TYPE_UNSIGNED_5_6_5: return GL_UNSIGNED_SHORT_5_6_5; + case RS_TYPE_UNSIGNED_5_5_5_1: return GL_UNSIGNED_SHORT_5_5_5_1; + case RS_TYPE_UNSIGNED_4_4_4_4: return GL_UNSIGNED_SHORT_4_4_4_4; + + //case RS_TYPE_FLOAT_16: return GL_HALF_FLOAT; + case RS_TYPE_FLOAT_32: return GL_FLOAT; + case RS_TYPE_UNSIGNED_8: return GL_UNSIGNED_BYTE; + case RS_TYPE_UNSIGNED_16: return GL_UNSIGNED_SHORT; + case RS_TYPE_SIGNED_8: return GL_BYTE; + case RS_TYPE_SIGNED_16: return GL_SHORT; + default: break; + } + + return 0; } -uint32_t Component::getGLType() const +uint32_t Component::getGLFormat() const { - switch(mType) { - case RS_TYPE_FLOAT: - rsAssert(mBits == 32); - return GL_FLOAT; - case RS_TYPE_SIGNED: - switch(mBits) { - case 32: - return 0;//GL_INT; - case 16: - return GL_SHORT; - case 8: - return GL_BYTE; + switch (mKind) { + case RS_KIND_PIXEL_L: return GL_LUMINANCE; + case RS_KIND_PIXEL_A: return GL_ALPHA; + case RS_KIND_PIXEL_LA: return GL_LUMINANCE_ALPHA; + case RS_KIND_PIXEL_RGB: return GL_RGB; + case RS_KIND_PIXEL_RGBA: return GL_RGBA; + default: break; + } + return 0; +} + +static const char * gCTypeStrings[] = { + 0, + 0,//"F16", + "float", + "double", + "char", + "short", + "int", + 0,//"S64", + "char",//U8", + "short",//U16", + "int",//U32", + 0,//"U64", + 0,//"UP_565", + 0,//"UP_5551", + 0,//"UP_4444", + 0,//"ELEMENT", + 0,//"TYPE", + 0,//"ALLOCATION", + 0,//"SAMPLER", + 0,//"SCRIPT", + 0,//"MESH", + 0,//"PROGRAM_FRAGMENT", + 0,//"PROGRAM_VERTEX", + 0,//"PROGRAM_RASTER", + 0,//"PROGRAM_STORE", +}; + +static const char * gCVecTypeStrings[] = { + 0, + 0,//"F16", + "vecF32", + "vecF64", + "vecI8", + "vecI16", + "vecI32", + 0,//"S64", + "vecU8",//U8", + "vecU16",//U16", + "vecU32",//U32", + 0,//"U64", + 0,//"UP_565", + 0,//"UP_5551", + 0,//"UP_4444", + 0,//"ELEMENT", + 0,//"TYPE", + 0,//"ALLOCATION", + 0,//"SAMPLER", + 0,//"SCRIPT", + 0,//"MESH", + 0,//"PROGRAM_FRAGMENT", + 0,//"PROGRAM_VERTEX", + 0,//"PROGRAM_RASTER", + 0,//"PROGRAM_STORE", +}; + +String8 Component::getCType() const +{ + char buf[64]; + if (mVectorSize == 1) { + return String8(gCTypeStrings[mType]); + } + + // Yuck, acc WAR + // Appears to have problems packing chars + if (mVectorSize == 4 && mType == RS_TYPE_UNSIGNED_8) { + return String8("int"); + } + + + String8 s(gCVecTypeStrings[mType]); + sprintf(buf, "_%i_t", mVectorSize); + s.append(buf); + return s; +} + +String8 Component::getGLSLType() const +{ + if (mType == RS_TYPE_SIGNED_32) { + switch(mVectorSize) { + case 1: return String8("int"); + case 2: return String8("ivec2"); + case 3: return String8("ivec3"); + case 4: return String8("ivec4"); } - break; - case RS_TYPE_UNSIGNED: - switch(mBits) { - case 32: - return 0;//GL_UNSIGNED_INT; - case 16: - return GL_UNSIGNED_SHORT; - case 8: - return GL_UNSIGNED_BYTE; + } + if (mType == RS_TYPE_FLOAT_32) { + switch(mVectorSize) { + case 1: return String8("float"); + case 2: return String8("vec2"); + case 3: return String8("vec3"); + case 4: return String8("vec4"); } - break; } - //rsAssert(!"Bad type"); - //LOGE("mType %i, mKind %i, mBits %i, mIsNormalized %i", mType, mKind, mBits, mIsNormalized); - return 0; + return String8(); } +static const char * gTypeStrings[] = { + "NONE", + "F16", + "F32", + "F64", + "S8", + "S16", + "S32", + "S64", + "U8", + "U16", + "U32", + "U64", + "UP_565", + "UP_5551", + "UP_4444", + "ELEMENT", + "TYPE", + "ALLOCATION", + "SAMPLER", + "SCRIPT", + "MESH", + "PROGRAM_FRAGMENT", + "PROGRAM_VERTEX", + "PROGRAM_RASTER", + "PROGRAM_STORE", +}; + +static const char * gKindStrings[] = { + "USER", + "COLOR", + "POSITION", + "TEXTURE", + "NORMAL", + "INDEX", + "POINT_SIZE", + "PIXEL_L", + "PIXEL_A", + "PIXEL_LA", + "PIXEL_RGB", + "PIXEL_RGBA", +}; + void Component::dumpLOGV(const char *prefix) const { - ObjectBase::dumpLOGV(prefix); - LOGV("%s component: %i %i %i %i", prefix, mType, mKind, mIsNormalized, mBits); + LOGV("%s Component: %s, %s, vectorSize=%i, bits=%i", + prefix, gTypeStrings[mType], gKindStrings[mKind], mVectorSize, mBits); } + diff --git a/libs/rs/rsComponent.h b/libs/rs/rsComponent.h index 5366cc4..71de324 100644 --- a/libs/rs/rsComponent.h +++ b/libs/rs/rsComponent.h @@ -14,65 +14,56 @@ * limitations under the License. */ -#ifndef ANDROID_RS_STRUCTURED_COMPONENT_H -#define ANDROID_RS_STRUCTURED_COMPONENT_H +#ifndef ANDROID_COMPONENT_H +#define ANDROID_COMPONENT_H #include "rsUtils.h" -#include "rsObjectBase.h" // --------------------------------------------------------------------------- namespace android { namespace renderscript { -class Component : public ObjectBase + +// An element is a group of Components that occupies one cell in a structure. +class Component { public: - enum DataType { - FLOAT, - UNSIGNED, - SIGNED - }; - - enum DataKind { - USER, - RED, GREEN, BLUE, ALPHA, LUMINANCE, INTENSITY, - X, Y, Z, W, - S, T, Q, R, - NX, NY, NZ, - INDEX, - POINT_SIZE - }; - + Component(); + ~Component(); - Component(Context *rsc, DataKind dk, DataType dt, bool isNorm, uint32_t bits, const char *); - virtual ~Component(); - - DataType getType() const {return mType;} - bool getIsNormalized() const {return mIsNormalized;} - DataKind getKind() const {return mKind;} - uint32_t getBits() const {return mBits;} + void set(RsDataType dt, RsDataKind dk, bool norm, uint32_t vecSize=1); uint32_t getGLType() const; - const char * getCType() const; - - const char * getComponentName() const {return mName.string();} - virtual void dumpLOGV(const char *prefix) const; + uint32_t getGLFormat() const; + String8 getCType() const; + String8 getGLSLType() const; + void dumpLOGV(const char *prefix) const; + + + RsDataType getType() const {return mType;} + RsDataKind getKind() const {return mKind;} + bool getIsNormalized() const {return mNormalized;} + uint32_t getVectorSize() const {return mVectorSize;} + bool getIsFloat() const {return mIsFloat;} + bool getIsSigned() const {return mIsSigned;} + uint32_t getBits() const {return mBits;} protected: + RsDataType mType; + RsDataKind mKind; + bool mNormalized; + uint32_t mVectorSize; - DataType mType; - bool mIsNormalized; - DataKind mKind; + // derived uint32_t mBits; - String8 mName; - -private: - Component(Context *rsc); + uint32_t mTypeBits; + bool mIsFloat; + bool mIsSigned; + bool mIsPixel; }; - } } -#endif //ANDROID_RS_STRUCTURED_COMPONENT_H +#endif diff --git a/libs/rs/rsContext.cpp b/libs/rs/rsContext.cpp index 3652f0a..261b827 100644 --- a/libs/rs/rsContext.cpp +++ b/libs/rs/rsContext.cpp @@ -27,6 +27,8 @@ #include <GLES/gl.h> #include <GLES/glext.h> +#include <GLES2/gl2.h> +#include <GLES2/gl2ext.h> #include <cutils/sched_policy.h> @@ -50,11 +52,12 @@ static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) { } } -void Context::initEGL() +void Context::initEGL(bool useGL2) { mEGL.mNumConfigs = -1; EGLint configAttribs[128]; EGLint *configAttribsPtr = configAttribs; + EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; memset(configAttribs, 0, sizeof(configAttribs)); @@ -62,6 +65,12 @@ void Context::initEGL() configAttribsPtr[1] = EGL_WINDOW_BIT; configAttribsPtr += 2; + if (useGL2) { + configAttribsPtr[0] = EGL_RENDERABLE_TYPE; + configAttribsPtr[1] = EGL_OPENGL_ES2_BIT; + configAttribsPtr += 2; + } + if (mUseDepth) { configAttribsPtr[0] = EGL_DEPTH_SIZE; configAttribsPtr[1] = 16; @@ -91,7 +100,11 @@ void Context::initEGL() //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs); - mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, NULL); + if (useGL2) { + mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, context_attribs2); + } else { + mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, NULL); + } checkEglError("eglCreateContext"); if (mEGL.mContext == EGL_NO_CONTEXT) { LOGE("eglCreateContext returned EGL_NO_CONTEXT"); @@ -129,6 +142,13 @@ uint32_t Context::runScript(Script *s, uint32_t launchID) return ret; } +void Context::checkError(const char *msg) const +{ + GLenum err = glGetError(); + if (err != GL_NO_ERROR) { + LOGE("GL Error, 0x%x, from %s", err, msg); + } +} uint32_t Context::runRootScript() { @@ -156,11 +176,7 @@ uint32_t Context::runRootScript() mStateFragmentStore.mLast.clear(); uint32_t ret = runScript(mRootScript.get(), 0); - GLenum err = glGetError(); - if (err != GL_NO_ERROR) { - LOGE("Pending GL Error, 0x%x", err); - } - + checkError("runRootScript"); return ret; } @@ -225,10 +241,20 @@ void Context::timerPrint() void Context::setupCheck() { - mFragmentStore->setupGL(this, &mStateFragmentStore); - mFragment->setupGL(this, &mStateFragment); - mRaster->setupGL(this, &mStateRaster); - mVertex->setupGL(this, &mStateVertex); + if (checkVersion2_0()) { + mShaderCache.lookup(this, mVertex.get(), mFragment.get()); + + mFragmentStore->setupGL2(this, &mStateFragmentStore); + mFragment->setupGL2(this, &mStateFragment, &mShaderCache); + mRaster->setupGL2(this, &mStateRaster); + mVertex->setupGL2(this, &mStateVertex, &mShaderCache); + + } else { + mFragmentStore->setupGL(this, &mStateFragmentStore); + mFragment->setupGL(this, &mStateFragment); + mRaster->setupGL(this, &mStateRaster); + mVertex->setupGL(this, &mStateVertex); + } } static bool getProp(const char *str) @@ -248,11 +274,8 @@ void * Context::threadProc(void *vrsc) rsc->props.mLogTimes = getProp("debug.rs.profile"); rsc->props.mLogScripts = getProp("debug.rs.script"); - rsc->props.mLogObjects = getProp("debug.rs.objects"); - - //pthread_mutex_lock(&gInitMutex); - //rsc->initEGL(); - //pthread_mutex_unlock(&gInitMutex); + rsc->props.mLogObjects = getProp("debug.rs.object"); + rsc->props.mLogShaders = getProp("debug.rs.shader"); ScriptTLSStruct *tlsStruct = new ScriptTLSStruct; if (!tlsStruct) { @@ -274,6 +297,7 @@ void * Context::threadProc(void *vrsc) rsc->setFragment(NULL); rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight); rsc->setFragmentStore(NULL); + rsc->mStateVertexArray.init(rsc); rsc->mRunning = true; bool mDraw = true; @@ -319,10 +343,6 @@ void * Context::threadProc(void *vrsc) rsc->mObjDestroy.mNeedToEmpty = true; rsc->objDestroyOOBRun(); - glClearColor(0,0,0,0); - glClear(GL_COLOR_BUFFER_BIT); - eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface); - pthread_mutex_lock(&gInitMutex); rsc->deinitEGL(); pthread_mutex_unlock(&gInitMutex); @@ -457,7 +477,7 @@ void Context::setSurface(uint32_t w, uint32_t h, Surface *sur) if (!mEGL.mContext) { first = true; pthread_mutex_lock(&gInitMutex); - initEGL(); + initEGL(true); pthread_mutex_unlock(&gInitMutex); } @@ -488,15 +508,35 @@ void Context::setSurface(uint32_t w, uint32_t h, Surface *sur) //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion); LOGV("GL Version %s", mGL.mVersion); - LOGV("GL Vendor %s", mGL.mVendor); + //LOGV("GL Vendor %s", mGL.mVendor); LOGV("GL Renderer %s", mGL.mRenderer); //LOGV("GL Extensions %s", mGL.mExtensions); - if ((strlen((const char *)mGL.mVersion) < 12) || memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) { + const char *verptr = NULL; + if (strlen((const char *)mGL.mVersion) > 9) { + if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) { + verptr = (const char *)mGL.mVersion + 12; + } + if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) { + verptr = (const char *)mGL.mVersion + 9; + } + } + + if (!verptr) { LOGE("Error, OpenGL ES Lite not supported"); } else { - sscanf((const char *)mGL.mVersion + 13, "%i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion); + sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion); } + + glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs); + glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors); + glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits); + + glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors); + glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits); + + glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits); + glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors); } } @@ -544,13 +584,6 @@ void Context::setRaster(ProgramRaster *pr) } } -void Context::allocationCheck(const Allocation *a) -{ - mVertex->checkUpdatedAllocation(a); - mFragment->checkUpdatedAllocation(a); - mFragmentStore->checkUpdatedAllocation(a); -} - void Context::setVertex(ProgramVertex *pv) { if (pv == NULL) { @@ -558,7 +591,6 @@ void Context::setVertex(ProgramVertex *pv) } else { mVertex.set(pv); } - mVertex->forceDirty(); } void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) @@ -600,26 +632,6 @@ void Context::appendNameDefines(String8 *str) const } } -void Context::appendVarDefines(String8 *str) const -{ - char buf[256]; - for (size_t ct=0; ct < mInt32Defines.size(); ct++) { - str->append("#define "); - str->append(mInt32Defines.keyAt(ct)); - str->append(" "); - sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct)); - str->append(buf); - - } - for (size_t ct=0; ct < mFloatDefines.size(); ct++) { - str->append("#define "); - str->append(mFloatDefines.keyAt(ct)); - str->append(" "); - sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct)); - str->append(buf); - } -} - bool Context::objDestroyOOBInit() { int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL); @@ -751,6 +763,10 @@ void Context::dumpDebug() const LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused); LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId); + LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits); + LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs); + LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors); + LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors); } /////////////////////////////////////////////////////////////////////////////////////////// @@ -815,16 +831,6 @@ void rsi_ObjDestroy(Context *rsc, void *obj) ob->decUserRef(); } -void rsi_ContextSetDefineF(Context *rsc, const char* name, float value) -{ - rsc->addInt32Define(name, value); -} - -void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value) -{ - rsc->addFloatDefine(name, value); -} - void rsi_ContextPause(Context *rsc) { rsc->pause(); diff --git a/libs/rs/rsContext.h b/libs/rs/rsContext.h index 1770ee6..2edd16d 100644 --- a/libs/rs/rsContext.h +++ b/libs/rs/rsContext.h @@ -37,6 +37,8 @@ #include "rsProgramFragmentStore.h" #include "rsProgramRaster.h" #include "rsProgramVertex.h" +#include "rsShaderCache.h" +#include "rsVertexArray.h" #include "rsgApiStructs.h" #include "rsLocklessFifo.h" @@ -72,8 +74,10 @@ public: ProgramRasterState mStateRaster; ProgramVertexState mStateVertex; LightState mStateLight; + VertexArrayState mStateVertexArray; ScriptCState mScriptC; + ShaderCache mShaderCache; void swapBuffers(); void setRootScript(Script *); @@ -90,7 +94,7 @@ public: const ProgramVertex * getVertex() {return mVertex.get();} void setupCheck(); - void allocationCheck(const Allocation *); + bool checkDriver() const {return mEGL.mSurface != 0;} void pause(); void resume(); @@ -101,7 +105,6 @@ public: void removeName(ObjectBase *obj); ObjectBase * lookupName(const char *name) const; void appendNameDefines(String8 *str) const; - void appendVarDefines(String8 *str) const; uint32_t getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait); bool sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace); @@ -123,14 +126,6 @@ public: return mStateRaster.mDefault.get(); } - void addInt32Define(const char* name, int32_t value) { - mInt32Defines.add(String8(name), value); - } - - void addFloatDefine(const char* name, float value) { - mFloatDefines.add(String8(name), value); - } - uint32_t getWidth() const {return mEGL.mWidth;} uint32_t getHeight() const {return mEGL.mHeight;} @@ -160,9 +155,11 @@ public: bool mLogTimes; bool mLogScripts; bool mLogObjects; + bool mLogShaders; } props; void dumpDebug() const; + void checkError(const char *) const; mutable const ObjectBase * mObjHead; @@ -190,6 +187,15 @@ protected: uint32_t mMajorVersion; uint32_t mMinorVersion; + int32_t mMaxVaryingVectors; + int32_t mMaxTextureImageUnits; + + int32_t mMaxFragmentTextureImageUnits; + int32_t mMaxFragmentUniformVectors; + + int32_t mMaxVertexAttribs; + int32_t mMaxVertexUniformVectors; + int32_t mMaxVertexTextureUnits; } mGL; uint32_t mWidth; @@ -224,7 +230,7 @@ protected: private: Context(); - void initEGL(); + void initEGL(bool useGL2); void deinitEGL(); uint32_t runRootScript(); @@ -234,8 +240,6 @@ private: Surface *mWndSurface; Vector<ObjectBase *> mNames; - KeyedVector<String8,int> mInt32Defines; - KeyedVector<String8,float> mFloatDefines; uint64_t mTimers[_RS_TIMER_TOTAL]; Timers mTimerActive; diff --git a/libs/rs/rsElement.cpp b/libs/rs/rsElement.cpp index 67e4f14..207ad15 100644 --- a/libs/rs/rsElement.cpp +++ b/libs/rs/rsElement.cpp @@ -24,19 +24,13 @@ using namespace android::renderscript; Element::Element(Context *rsc) : ObjectBase(rsc) { + mBits = 0; mAllocFile = __FILE__; mAllocLine = __LINE__; - mComponents = NULL; - mComponentCount = 0; + mFields = NULL; + mFieldCount = 0; } -Element::Element(Context *rsc, uint32_t count) : ObjectBase(rsc) -{ - mAllocFile = __FILE__; - mAllocLine = __LINE__; - mComponents = new ObjectBaseRef<Component> [count]; - mComponentCount = count; -} Element::~Element() { @@ -45,153 +39,129 @@ Element::~Element() void Element::clear() { - delete [] mComponents; - mComponents = NULL; - mComponentCount = 0; + delete [] mFields; + mFields = NULL; + mFieldCount = 0; } -void Element::setComponent(uint32_t idx, Component *c) -{ - rsAssert(!mComponents[idx].get()); - rsAssert(idx < mComponentCount); - mComponents[idx].set(c); - -// Fixme: This should probably not be here - c->incUserRef(); -} - - size_t Element::getSizeBits() const { + if (!mFieldCount) { + return mBits; + } + size_t total = 0; - for (size_t ct=0; ct < mComponentCount; ct++) { - total += mComponents[ct]->getBits(); + for (size_t ct=0; ct < mFieldCount; ct++) { + total += mFields[ct].e->mBits; } return total; } -size_t Element::getComponentOffsetBits(uint32_t componentNumber) const +size_t Element::getFieldOffsetBits(uint32_t componentNumber) const { size_t offset = 0; for (uint32_t ct = 0; ct < componentNumber; ct++) { - offset += mComponents[ct]->getBits(); + offset += mFields[ct].e->mBits; } return offset; } -uint32_t Element::getGLType() const +void Element::dumpLOGV(const char *prefix) const { - int bits[4]; + ObjectBase::dumpLOGV(prefix); + LOGV("%s Element: components %i, size %i", prefix, mFieldCount, mBits); + for (uint32_t ct = 0; ct < mFieldCount; ct++) { + char buf[1024]; + sprintf(buf, "%s component %i: ", prefix, ct); + //mComponents[ct]->dumpLOGV(buf); + } +} + + +Element * Element::create(Context *rsc, RsDataType dt, RsDataKind dk, + bool isNorm, uint32_t vecSize) +{ + Element *e = new Element(rsc); + e->mComponent.set(dt, dk, isNorm, vecSize); + e->mBits = e->mComponent.getBits(); + return e; +} + +Element * Element::create(Context *rsc, size_t count, const Element **ein, + const char **nin, const size_t * lengths) +{ + Element *e = new Element(rsc); + e->mFields = new ElementField_t [count]; + e->mFieldCount = count; - if (mComponentCount > 4) { - return 0; + for (size_t ct=0; ct < count; ct++) { + e->mFields[ct].e.set(ein[ct]); + e->mFields[ct].name.setTo(nin[ct], lengths[ct]); } - for (uint32_t ct=0; ct < mComponentCount; ct++) { - bits[ct] = mComponents[ct]->getBits(); - if (mComponents[ct]->getType() != Component::UNSIGNED) { - return 0; - } - if (!mComponents[ct]->getIsNormalized()) { - return 0; - } + return e; +} + +String8 Element::getCStructBody(uint32_t indent) const +{ + String8 si; + for (uint32_t ct=0; ct < indent; ct++) { + si.append(" "); } - switch(mComponentCount) { - case 1: - if (bits[0] == 8) { - return GL_UNSIGNED_BYTE; - } - return 0; - case 2: - if ((bits[0] == 8) && - (bits[1] == 8)) { - return GL_UNSIGNED_BYTE; - } - return 0; - case 3: - if ((bits[0] == 8) && - (bits[1] == 8) && - (bits[2] == 8)) { - return GL_UNSIGNED_BYTE; - } - if ((bits[0] == 5) && - (bits[1] == 6) && - (bits[2] == 5)) { - return GL_UNSIGNED_SHORT_5_6_5; - } - return 0; - case 4: - if ((bits[0] == 8) && - (bits[1] == 8) && - (bits[2] == 8) && - (bits[3] == 8)) { - return GL_UNSIGNED_BYTE; - } - if ((bits[0] == 4) && - (bits[1] == 4) && - (bits[2] == 4) && - (bits[3] == 4)) { - return GL_UNSIGNED_SHORT_4_4_4_4; - } - if ((bits[0] == 5) && - (bits[1] == 5) && - (bits[2] == 5) && - (bits[3] == 1)) { - return GL_UNSIGNED_SHORT_5_5_5_1; - } + String8 s(si); + s.append("{\n"); + for (uint32_t ct = 0; ct < mFieldCount; ct++) { + s.append(si); + s.append(mFields[ct].e->getCType(indent+4)); + s.append(" "); + s.append(mFields[ct].name); + s.append(";\n"); } - return 0; + s.append(si); + s.append("}"); + return s; } -uint32_t Element::getGLFormat() const +String8 Element::getCType(uint32_t indent) const { - switch(mComponentCount) { - case 1: - if (mComponents[0]->getKind() == Component::ALPHA) { - return GL_ALPHA; - } - if (mComponents[0]->getKind() == Component::LUMINANCE) { - return GL_LUMINANCE; - } - break; - case 2: - if ((mComponents[0]->getKind() == Component::LUMINANCE) && - (mComponents[1]->getKind() == Component::ALPHA)) { - return GL_LUMINANCE_ALPHA; - } - break; - case 3: - if ((mComponents[0]->getKind() == Component::RED) && - (mComponents[1]->getKind() == Component::GREEN) && - (mComponents[2]->getKind() == Component::BLUE)) { - return GL_RGB; - } - break; - case 4: - if ((mComponents[0]->getKind() == Component::RED) && - (mComponents[1]->getKind() == Component::GREEN) && - (mComponents[2]->getKind() == Component::BLUE) && - (mComponents[3]->getKind() == Component::ALPHA)) { - return GL_RGBA; - } - break; + String8 s; + for (uint32_t ct=0; ct < indent; ct++) { + s.append(" "); + } + + if (!mFieldCount) { + // Basic component. + s.append(mComponent.getCType()); + } else { + s.append("struct "); + s.append(getCStructBody(indent)); } - return 0; -} + return s; +} -void Element::dumpLOGV(const char *prefix) const +String8 Element::getGLSLType(uint32_t indent) const { - ObjectBase::dumpLOGV(prefix); - LOGV("%s Element: components %i, size %i", prefix, mComponentCount, getSizeBytes()); - for (uint32_t ct = 0; ct < mComponentCount; ct++) { - char buf[1024]; - sprintf(buf, "%s component %i: ", prefix, ct); - mComponents[ct]->dumpLOGV(buf); + String8 s; + for (uint32_t ct=0; ct < indent; ct++) { + s.append(" "); } + + if (!mFieldCount) { + // Basic component. + s.append(mComponent.getGLSLType()); + } else { + rsAssert(0); + //s.append("struct "); + //s.append(getCStructBody(indent)); + } + + return s; } + + ElementState::ElementState() { } @@ -200,46 +170,35 @@ ElementState::~ElementState() { } + ///////////////////////////////////////// // namespace android { namespace renderscript { -void rsi_ElementBegin(Context *rsc) -{ - rsc->mStateElement.mComponentBuildList.clear(); -} - -void rsi_ElementAdd(Context *rsc, RsDataKind dk, RsDataType dt, bool isNormalized, size_t bits, const char *name) +RsElement rsi_ElementCreate(Context *rsc, + RsDataType dt, + RsDataKind dk, + bool norm, + uint32_t vecSize) { - ElementState * sec = &rsc->mStateElement; - - rsAssert(bits > 0); - - Component *c = new Component(rsc, - static_cast<Component::DataKind>(dk), - static_cast<Component::DataType>(dt), - isNormalized, - bits, - name); - sec->mComponentBuildList.add(c); + //LOGE("rsi_ElementCreate %i %i %i %i", dt, dk, norm, vecSize); + Element *e = Element::create(rsc, dt, dk, norm, vecSize); + e->incUserRef(); + return e; } -RsElement rsi_ElementCreate(Context *rsc) +RsElement rsi_ElementCreate2(Context *rsc, + size_t count, + const RsElement * ein, + const char ** names, + const size_t * nameLengths) { - ElementState * sec = &rsc->mStateElement; - Element *se = new Element(rsc, sec->mComponentBuildList.size()); - - rsAssert(se->getComponentCount() > 0); - - for (size_t ct = 0; ct < se->getComponentCount(); ct++) { - se->setComponent(ct, sec->mComponentBuildList[ct]); - } - - rsc->mStateElement.mComponentBuildList.clear(); - se->incUserRef(); - return se; + //LOGE("rsi_ElementCreate2 %i", count); + Element *e = Element::create(rsc, count, (const Element **)ein, names, nameLengths); + e->incUserRef(); + return e; } diff --git a/libs/rs/rsElement.h b/libs/rs/rsElement.h index b41c552..777e8ee 100644 --- a/libs/rs/rsElement.h +++ b/libs/rs/rsElement.h @@ -18,6 +18,8 @@ #define ANDROID_STRUCTURED_ELEMENT_H #include "rsComponent.h" +#include "rsUtils.h" +#include "rsObjectBase.h" // --------------------------------------------------------------------------- namespace android { @@ -28,41 +30,57 @@ namespace renderscript { class Element : public ObjectBase { public: - Element(Context *, uint32_t count); ~Element(); - - void setComponent(uint32_t idx, Component *c); - uint32_t getGLType() const; uint32_t getGLFormat() const; - size_t getSizeBits() const; size_t getSizeBytes() const { return (getSizeBits() + 7) >> 3; } - size_t getComponentOffsetBits(uint32_t componentNumber) const; - size_t getComponentOffsetBytes(uint32_t componentNumber) const { - return (getComponentOffsetBits(componentNumber) + 7) >> 3; + size_t getFieldOffsetBits(uint32_t componentNumber) const; + size_t getFieldOffsetBytes(uint32_t componentNumber) const { + return (getFieldOffsetBits(componentNumber) + 7) >> 3; } - uint32_t getComponentCount() const {return mComponentCount;} - Component * getComponent(uint32_t idx) const {return mComponents[idx].get();} + uint32_t getFieldCount() const {return mFieldCount;} + const Element * getField(uint32_t idx) const {return mFields[idx].e.get();} + const char * getFieldName(uint32_t idx) const {return mFields[idx].name.string();} + + const Component & getComponent() const {return mComponent;} + RsDataType getType() const {return mComponent.getType();} + RsDataKind getKind() const {return mComponent.getKind();} + uint32_t getBits() const {return mBits;} + String8 getCType(uint32_t indent=0) const; + String8 getCStructBody(uint32_t indent=0) const; + String8 getGLSLType(uint32_t indent=0) const; void dumpLOGV(const char *prefix) const; + static Element * create(Context *rsc, RsDataType dt, RsDataKind dk, + bool isNorm, uint32_t vecSize); + static Element * create(Context *rsc, size_t count, const Element **, + const char **, const size_t * lengths); + protected: // deallocate any components that are part of this element. void clear(); - size_t mComponentCount; - ObjectBaseRef<Component> * mComponents; - //uint32_t *mOffsetTable; + typedef struct { + String8 name; + ObjectBaseRef<const Element> e; + } ElementField_t; + ElementField_t *mFields; + size_t mFieldCount; + Element(Context *); + + Component mComponent; + uint32_t mBits; }; @@ -71,7 +89,6 @@ public: ElementState(); ~ElementState(); - Vector<Component *> mComponentBuildList; }; diff --git a/libs/rs/rsFileA3D.cpp b/libs/rs/rsFileA3D.cpp index c566665..e3272c5 100644 --- a/libs/rs/rsFileA3D.cpp +++ b/libs/rs/rsFileA3D.cpp @@ -324,6 +324,7 @@ void FileA3D::processChunk_Verticies(Context *rsc, IO *io, A3DIndexEntry *ie) void FileA3D::processChunk_Element(Context *rsc, IO *io, A3DIndexEntry *ie) { + /* rsi_ElementBegin(rsc); uint32_t count = io->loadU32(); @@ -338,6 +339,7 @@ void FileA3D::processChunk_Element(Context *rsc, IO *io, A3DIndexEntry *ie) } LOGE("processChunk_Element create"); ie->mRsObj = rsi_ElementCreate(rsc); + */ } void FileA3D::processChunk_ElementSource(Context *rsc, IO *io, A3DIndexEntry *ie) diff --git a/libs/rs/rsProgram.cpp b/libs/rs/rsProgram.cpp index 5f2a609..656a3c3 100644 --- a/libs/rs/rsProgram.cpp +++ b/libs/rs/rsProgram.cpp @@ -17,34 +17,245 @@ #include "rsContext.h" #include "rsProgram.h" +#include <GLES2/gl2.h> +#include <GLES2/gl2ext.h> + using namespace android; using namespace android::renderscript; -Program::Program(Context *rsc, Element *in, Element *out) : ObjectBase(rsc) +Program::Program(Context *rsc) : ObjectBase(rsc) { mAllocFile = __FILE__; mAllocLine = __LINE__; + mDirty = true; + mShaderID = 0; + mAttribCount = 0; + mUniformCount = 0; + + mInputElements = NULL; + mOutputElements = NULL; + mConstantTypes = NULL; + mInputCount = 0; + mOutputCount = 0; + mConstantCount = 0; +} + +Program::Program(Context *rsc, const char * shaderText, uint32_t shaderLength, + const uint32_t * params, uint32_t paramLength) : + ObjectBase(rsc) +{ + mAllocFile = __FILE__; + mAllocLine = __LINE__; + mDirty = true; + mShaderID = 0; + mAttribCount = 0; + mUniformCount = 0; + mTextureCount = 0; - mElementIn.set(in); - mElementOut.set(out); + mInputCount = 0; + mOutputCount = 0; + mConstantCount = 0; + + for (uint32_t ct=0; ct < paramLength; ct+=2) { + if (params[ct] == RS_PROGRAM_PARAM_INPUT) { + mInputCount++; + } + if (params[ct] == RS_PROGRAM_PARAM_OUTPUT) { + mOutputCount++; + } + if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) { + mConstantCount++; + } + if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_COUNT) { + mTextureCount = params[ct+1]; + } + } + + mInputElements = new ObjectBaseRef<Element>[mInputCount]; + mOutputElements = new ObjectBaseRef<Element>[mOutputCount]; + mConstantTypes = new ObjectBaseRef<Type>[mConstantCount]; + + uint32_t input = 0; + uint32_t output = 0; + uint32_t constant = 0; + for (uint32_t ct=0; ct < paramLength; ct+=2) { + if (params[ct] == RS_PROGRAM_PARAM_INPUT) { + mInputElements[input++].set(reinterpret_cast<Element *>(params[ct+1])); + } + if (params[ct] == RS_PROGRAM_PARAM_OUTPUT) { + mOutputElements[output++].set(reinterpret_cast<Element *>(params[ct+1])); + } + if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) { + mConstantTypes[constant++].set(reinterpret_cast<Type *>(params[ct+1])); + } + } + mUserShader.setTo(shaderText, shaderLength); } Program::~Program() { + for (uint32_t ct=0; ct < MAX_UNIFORMS; ct++) { + bindAllocation(NULL, ct); + } + + delete[] mInputElements; + delete[] mOutputElements; + delete[] mConstantTypes; + mInputCount = 0; + mOutputCount = 0; + mConstantCount = 0; +} + + +void Program::bindAllocation(Allocation *alloc, uint32_t slot) +{ + if (mConstants[slot].get() == alloc) { + return; + } + if (mConstants[slot].get()) { + mConstants[slot].get()->removeProgramToDirty(this); + } + mConstants[slot].set(alloc); + if (alloc) { + alloc->addProgramToDirty(this); + } + mDirty = true; } +void Program::bindTexture(uint32_t slot, Allocation *a) +{ + if (slot >= MAX_TEXTURE) { + LOGE("Attempt to bind a texture to a slot > MAX_TEXTURE"); + return; + } + + //LOGE("bindtex %i %p", slot, a); + mTextures[slot].set(a); + mDirty = true; +} -void Program::bindAllocation(Allocation *alloc) +void Program::bindSampler(uint32_t slot, Sampler *s) { - mConstants.set(alloc); + if (slot >= MAX_TEXTURE) { + LOGE("Attempt to bind a Sampler to a slot > MAX_TEXTURE"); + return; + } + + mSamplers[slot].set(s); mDirty = true; } -void Program::checkUpdatedAllocation(const Allocation *alloc) +String8 Program::getGLSLInputString() const +{ + String8 s; + for (uint32_t ct=0; ct < mInputCount; ct++) { + const Element *e = mInputElements[ct].get(); + for (uint32_t field=0; field < e->getFieldCount(); field++) { + const Element *f = e->getField(field); + + // Cannot be complex + rsAssert(!f->getFieldCount()); + switch(f->getComponent().getVectorSize()) { + case 1: s.append("attribute float ATTRIB_"); break; + case 2: s.append("attribute vec2 ATTRIB_"); break; + case 3: s.append("attribute vec3 ATTRIB_"); break; + case 4: s.append("attribute vec4 ATTRIB_"); break; + default: + rsAssert(0); + } + + s.append(e->getFieldName(field)); + s.append(";\n"); + } + } + return s; +} + +String8 Program::getGLSLOutputString() const +{ + return String8(); +} + +String8 Program::getGLSLConstantString() const +{ + return String8(); +} + + +void Program::createShader() +{ +} + +bool Program::loadShader(Context *rsc, uint32_t type) { - if (mConstants.get() == alloc) { - mDirty = true; + mShaderID = glCreateShader(type); + rsAssert(mShaderID); + + if (rsc->props.mLogShaders) { + LOGV("Loading shader type %x, ID %i", type, mShaderID); + LOGV(mShader.string()); } + + if (mShaderID) { + const char * ss = mShader.string(); + glShaderSource(mShaderID, 1, &ss, NULL); + glCompileShader(mShaderID); + + GLint compiled = 0; + glGetShaderiv(mShaderID, GL_COMPILE_STATUS, &compiled); + if (!compiled) { + GLint infoLen = 0; + glGetShaderiv(mShaderID, GL_INFO_LOG_LENGTH, &infoLen); + if (infoLen) { + char* buf = (char*) malloc(infoLen); + if (buf) { + glGetShaderInfoLog(mShaderID, infoLen, NULL, buf); + LOGE("Could not compile shader \n%s\n", buf); + free(buf); + } + glDeleteShader(mShaderID); + mShaderID = 0; + return false; + } + } + } + + if (rsc->props.mLogShaders) { + LOGV("--Shader load result %x ", glGetError()); + } + return true; +} + +void Program::setShader(const char *txt, uint32_t len) +{ + mUserShader.setTo(txt, len); +} + + + +namespace android { +namespace renderscript { + + +void rsi_ProgramBindConstants(Context *rsc, RsProgram vp, uint32_t slot, RsAllocation constants) +{ + Program *p = static_cast<Program *>(vp); + p->bindAllocation(static_cast<Allocation *>(constants), slot); +} + +void rsi_ProgramBindTexture(Context *rsc, RsProgram vpf, uint32_t slot, RsAllocation a) +{ + Program *p = static_cast<Program *>(vpf); + p->bindTexture(slot, static_cast<Allocation *>(a)); +} + +void rsi_ProgramBindSampler(Context *rsc, RsProgram vpf, uint32_t slot, RsSampler s) +{ + Program *p = static_cast<Program *>(vpf); + p->bindSampler(slot, static_cast<Sampler *>(s)); +} + +} } diff --git a/libs/rs/rsProgram.h b/libs/rs/rsProgram.h index 57c654f..238ee7f 100644 --- a/libs/rs/rsProgram.h +++ b/libs/rs/rsProgram.h @@ -25,29 +25,78 @@ namespace android { namespace renderscript { +class ShaderCache; class Program : public ObjectBase { public: - Program(Context *, Element *in, Element *out); + const static uint32_t MAX_ATTRIBS = 8; + const static uint32_t MAX_UNIFORMS = 16; + const static uint32_t MAX_TEXTURE = 2; + + Program(Context *); + Program(Context *, const char * shaderText, uint32_t shaderLength, + const uint32_t * params, uint32_t paramLength); virtual ~Program(); - void bindAllocation(Allocation *); - void checkUpdatedAllocation(const Allocation *); + void bindAllocation(Allocation *, uint32_t slot); + virtual void createShader(); + + bool isUserProgram() const {return mUserShader.size() > 0;} + + void bindTexture(uint32_t slot, Allocation *); + void bindSampler(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];} + + String8 getGLSLInputString() const; + String8 getGLSLOutputString() const; + String8 getGLSLConstantString() const; + + const Allocation * getTexture(uint32_t i) const {return mTextures[i].get();} protected: // Components not listed in "in" will be passed though // unless overwritten by components in out. - ObjectBaseRef<Element> mElementIn; - ObjectBaseRef<Element> mElementOut; + ObjectBaseRef<Element> *mInputElements; + ObjectBaseRef<Element> *mOutputElements; + ObjectBaseRef<Type> *mConstantTypes; + uint32_t mInputCount; + uint32_t mOutputCount; + uint32_t mConstantCount; - ObjectBaseRef<Allocation> mConstants; + ObjectBaseRef<Allocation> mConstants[MAX_UNIFORMS]; mutable bool mDirty; - + String8 mShader; + String8 mUserShader; + uint32_t mShaderID; + + uint32_t mTextureCount; + uint32_t mAttribCount; + uint32_t mUniformCount; + String8 mAttribNames[MAX_ATTRIBS]; + String8 mUniformNames[MAX_UNIFORMS]; + + // 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[MAX_TEXTURE]; + ObjectBaseRef<Sampler> mSamplers[MAX_TEXTURE]; + + bool loadShader(Context *, uint32_t type); public: - void forceDirty() {mDirty = true;} + void forceDirty() const {mDirty = true;} }; diff --git a/libs/rs/rsProgramFragment.cpp b/libs/rs/rsProgramFragment.cpp index 5f2dfae..15f3269 100644 --- a/libs/rs/rsProgramFragment.cpp +++ b/libs/rs/rsProgramFragment.cpp @@ -19,25 +19,50 @@ #include <GLES/gl.h> #include <GLES/glext.h> +#include <GLES2/gl2.h> +#include <GLES2/gl2ext.h> using namespace android; using namespace android::renderscript; -ProgramFragment::ProgramFragment(Context *rsc, Element *in, Element *out, bool pointSpriteEnable) : - Program(rsc, in, out) +ProgramFragment::ProgramFragment(Context *rsc, const uint32_t * params, + uint32_t paramLength) : + Program(rsc) { mAllocFile = __FILE__; mAllocLine = __LINE__; - for (uint32_t ct=0; ct < MAX_TEXTURE; ct++) { - mEnvModes[ct] = RS_TEX_ENV_MODE_REPLACE; - mTextureDimensions[ct] = 2; - } + rsAssert(paramLength = 5); + + mEnvModes[0] = (RsTexEnvMode)params[0]; + mTextureFormats[0] = params[1]; + mEnvModes[1] = (RsTexEnvMode)params[2]; + mTextureFormats[1] = params[3]; + mPointSpriteEnable = params[4] != 0; + mTextureEnableMask = 0; - mPointSpriteEnable = pointSpriteEnable; - mEnvModes[1] = RS_TEX_ENV_MODE_DECAL; + if (mEnvModes[0]) { + mTextureEnableMask |= 1; + } + if (mEnvModes[1]) { + mTextureEnableMask |= 2; + } + init(rsc); } +ProgramFragment::ProgramFragment(Context *rsc, const char * shaderText, + uint32_t shaderLength, const uint32_t * params, + uint32_t paramLength) : + Program(rsc, shaderText, shaderLength, params, paramLength) +{ + mAllocFile = __FILE__; + mAllocLine = __LINE__; + + init(rsc); + mTextureEnableMask = (1 << mTextureCount) -1; +} + + ProgramFragment::~ProgramFragment() { } @@ -65,23 +90,13 @@ void ProgramFragment::setupGL(const Context *rsc, ProgramFragmentState *state) } glTexEnvi(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, mPointSpriteEnable); } - - rsAssert(mTextures[ct]->getTextureID() != 0); - if (mTextures[ct]->getTextureID() == 0) { - // This is a hack for eclair to try to fix the white squares bug. - Allocation *a = (Allocation *)mTextures[ct].get(); - a->uploadToTexture((Context *)rsc, 0); - if (mTextures[ct]->getTextureID() == 0) { - // At this point we are screwed. Crash to restart the app. - rsc->dumpDebug(); - LOGE("Multiple failures during texture upload. Driver appears wedged."); - ((char *)0)[0] = 0; - } - - } + mTextures[ct]->uploadCheck(rsc); glBindTexture(GL_TEXTURE_2D, mTextures[ct]->getTextureID()); switch(mEnvModes[ct]) { + case RS_TEX_ENV_MODE_NONE: + rsAssert(0); + break; case RS_TEX_ENV_MODE_REPLACE: glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); break; @@ -94,7 +109,7 @@ void ProgramFragment::setupGL(const Context *rsc, ProgramFragmentState *state) } if (mSamplers[ct].get()) { - mSamplers[ct]->setupGL(); + mSamplers[ct]->setupGL(rsc); } else { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); @@ -121,73 +136,158 @@ void ProgramFragment::setupGL(const Context *rsc, ProgramFragmentState *state) } glActiveTexture(GL_TEXTURE0); mDirty = false; + rsc->checkError("ProgramFragment::setupGL"); } - -void ProgramFragment::bindTexture(uint32_t slot, Allocation *a) +void ProgramFragment::setupGL2(const Context *rsc, ProgramFragmentState *state, ShaderCache *sc) { - if (slot >= MAX_TEXTURE) { - LOGE("Attempt to bind a texture to a slot > MAX_TEXTURE"); - return; + + //LOGE("sgl2 frag1 %x", glGetError()); + if ((state->mLast.get() == this) && !mDirty) { + //return; } + state->mLast.set(this); - //LOGE("bindtex %i %p", slot, a); - mTextures[slot].set(a); - mDirty = true; -} + rsc->checkError("ProgramFragment::setupGL2 start"); + for (uint32_t ct=0; ct < MAX_TEXTURE; ct++) { + glActiveTexture(GL_TEXTURE0 + ct); + if (!(mTextureEnableMask & (1 << ct)) || !mTextures[ct].get()) { + continue; + } -void ProgramFragment::bindSampler(uint32_t slot, Sampler *s) -{ - if (slot >= MAX_TEXTURE) { - LOGE("Attempt to bind a Sampler to a slot > MAX_TEXTURE"); - return; + mTextures[ct]->uploadCheck(rsc); + glBindTexture(GL_TEXTURE_2D, mTextures[ct]->getTextureID()); + rsc->checkError("ProgramFragment::setupGL2 tex bind"); + if (mSamplers[ct].get()) { + mSamplers[ct]->setupGL(rsc); + } else { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + rsc->checkError("ProgramFragment::setupGL2 tex env"); + } + + glUniform1i(sc->fragUniformSlot(ct), ct); + rsc->checkError("ProgramFragment::setupGL2 uniforms"); } - mSamplers[slot].set(s); - mDirty = true; + glActiveTexture(GL_TEXTURE0); + mDirty = false; + rsc->checkError("ProgramFragment::setupGL2"); +} + +void ProgramFragment::loadShader(Context *rsc) { + Program::loadShader(rsc, GL_FRAGMENT_SHADER); } -void ProgramFragment::setType(uint32_t slot, const Element *e, uint32_t dim) +void ProgramFragment::createShader() { - if (slot >= MAX_TEXTURE) { - LOGE("Attempt to setType to a slot > MAX_TEXTURE"); - return; - } + mShader.setTo("precision mediump float;\n"); + mShader.append("varying vec4 varColor;\n"); + mShader.append("varying vec4 varTex0;\n"); + + if (mUserShader.length() > 1) { + for (uint32_t ct=0; ct < mTextureCount; ct++) { + char buf[256]; + sprintf(buf, "uniform sampler2D uni_Tex%i;\n", ct); + mShader.append(buf); + } - if (dim >= 4) { - LOGE("Attempt to setType to a dimension > 3"); - return; - } + mShader.append(mUserShader); + } else { + uint32_t mask = mTextureEnableMask; + uint32_t texNum = 0; + while (mask) { + if (mask & 1) { + char buf[64]; + mShader.append("uniform sampler2D uni_Tex"); + sprintf(buf, "%i", texNum); + mShader.append(buf); + mShader.append(";\n"); + } + mask >>= 1; + texNum++; + } - mTextureFormats[slot].set(e); - mTextureDimensions[slot] = dim; -} -void ProgramFragment::setEnvMode(uint32_t slot, RsTexEnvMode env) -{ - if (slot >= MAX_TEXTURE) { - LOGE("Attempt to setEnvMode to a slot > MAX_TEXTURE"); - return; - } + mShader.append("void main() {\n"); + mShader.append(" vec4 col = varColor;\n"); - mEnvModes[slot] = env; -} + if (mTextureEnableMask) { + if (mPointSpriteEnable) { + mShader.append(" vec2 t0 = gl_PointCoord;\n"); + } else { + mShader.append(" vec2 t0 = varTex0.xy;\n"); + } + } -void ProgramFragment::setTexEnable(uint32_t slot, bool enable) -{ - if (slot >= MAX_TEXTURE) { - LOGE("Attempt to setEnvMode to a slot > MAX_TEXTURE"); - return; - } + mask = mTextureEnableMask; + texNum = 0; + while (mask) { + if (mask & 1) { + switch(mEnvModes[texNum]) { + case RS_TEX_ENV_MODE_NONE: + rsAssert(0); + break; + case RS_TEX_ENV_MODE_REPLACE: + switch(mTextureFormats[texNum]) { + case 1: + mShader.append(" col.a = texture2D(uni_Tex0, t0).a;\n"); + break; + case 2: + mShader.append(" col.rgba = texture2D(uni_Tex0, t0).rgba;\n"); + break; + case 3: + mShader.append(" col.rgb = texture2D(uni_Tex0, t0).rgb;\n"); + break; + case 4: + mShader.append(" col.rgba = texture2D(uni_Tex0, t0).rgba;\n"); + break; + } + break; + case RS_TEX_ENV_MODE_MODULATE: + switch(mTextureFormats[texNum]) { + case 1: + mShader.append(" col.a *= texture2D(uni_Tex0, t0).a;\n"); + break; + case 2: + mShader.append(" col.rgba *= texture2D(uni_Tex0, t0).rgba;\n"); + break; + case 3: + mShader.append(" col.rgb *= texture2D(uni_Tex0, t0).rgb;\n"); + break; + case 4: + mShader.append(" col.rgba *= texture2D(uni_Tex0, t0).rgba;\n"); + break; + } + break; + case RS_TEX_ENV_MODE_DECAL: + mShader.append(" col = texture2D(uni_Tex0, t0);\n"); + break; + } - uint32_t bit = 1 << slot; - mTextureEnableMask &= ~bit; - if (enable) { - mTextureEnableMask |= bit; + } + mask >>= 1; + texNum++; + } + + //mShader.append(" col.a = 1.0;\n"); + //mShader.append(" col.r = 0.5;\n"); + + mShader.append(" gl_FragColor = col;\n"); + mShader.append("}\n"); } } +void ProgramFragment::init(Context *rsc) +{ + mUniformCount = 2; + mUniformNames[0].setTo("uni_Tex0"); + mUniformNames[1].setTo("uni_Tex1"); + createShader(); +} ProgramFragmentState::ProgramFragmentState() { @@ -202,8 +302,14 @@ ProgramFragmentState::~ProgramFragmentState() void ProgramFragmentState::init(Context *rsc, int32_t w, int32_t h) { - ProgramFragment *pf = new ProgramFragment(rsc, NULL, NULL, false); + uint32_t tmp[5] = { + RS_TEX_ENV_MODE_NONE, 0, + RS_TEX_ENV_MODE_NONE, 0, + 0 + }; + ProgramFragment *pf = new ProgramFragment(rsc, tmp, 5); mDefault.set(pf); + pf->init(rsc); } void ProgramFragmentState::deinit(Context *rsc) @@ -216,50 +322,24 @@ void ProgramFragmentState::deinit(Context *rsc) namespace android { namespace renderscript { -void rsi_ProgramFragmentBegin(Context * rsc, RsElement in, RsElement out, bool pointSpriteEnable) +RsProgramFragment rsi_ProgramFragmentCreate(Context *rsc, + const uint32_t * params, + uint32_t paramLength) { - delete rsc->mStateFragment.mPF; - rsc->mStateFragment.mPF = new ProgramFragment(rsc, (Element *)in, (Element *)out, pointSpriteEnable); -} - -void rsi_ProgramFragmentBindTexture(Context *rsc, RsProgramFragment vpf, uint32_t slot, RsAllocation a) -{ - ProgramFragment *pf = static_cast<ProgramFragment *>(vpf); - pf->bindTexture(slot, static_cast<Allocation *>(a)); -} - -void rsi_ProgramFragmentBindSampler(Context *rsc, RsProgramFragment vpf, uint32_t slot, RsSampler s) -{ - ProgramFragment *pf = static_cast<ProgramFragment *>(vpf); - pf->bindSampler(slot, static_cast<Sampler *>(s)); -} - -void rsi_ProgramFragmentSetSlot(Context *rsc, uint32_t slot, bool enable, RsTexEnvMode env, RsType vt) -{ - const Type *t = static_cast<const Type *>(vt); - if (t) { - uint32_t dim = 1; - if (t->getDimY()) { - dim ++; - if (t->getDimZ()) { - dim ++; - } - } - rsc->mStateFragment.mPF->setType(slot, t->getElement(), dim); - } - rsc->mStateFragment.mPF->setEnvMode(slot, env); - rsc->mStateFragment.mPF->setTexEnable(slot, enable); + ProgramFragment *pf = new ProgramFragment(rsc, params, paramLength); + pf->incUserRef(); + return pf; } -RsProgramFragment rsi_ProgramFragmentCreate(Context *rsc) +RsProgramFragment rsi_ProgramFragmentCreate2(Context *rsc, const char * shaderText, + uint32_t shaderLength, const uint32_t * params, + uint32_t paramLength) { - ProgramFragment *pf = rsc->mStateFragment.mPF; + ProgramFragment *pf = new ProgramFragment(rsc, shaderText, shaderLength, params, paramLength); pf->incUserRef(); - rsc->mStateFragment.mPF = 0; return pf; } - } } diff --git a/libs/rs/rsProgramFragment.h b/libs/rs/rsProgramFragment.h index e26c6e8..9fa565d 100644 --- a/libs/rs/rsProgramFragment.h +++ b/libs/rs/rsProgramFragment.h @@ -28,40 +28,23 @@ class ProgramFragmentState; class ProgramFragment : public Program { public: - const static uint32_t MAX_TEXTURE = 2; - - - - ProgramFragment(Context *, Element *in, Element *out, bool pointSpriteEnable); + ProgramFragment(Context *, const uint32_t * params, uint32_t paramLength); + ProgramFragment(Context *rsc, const char * shaderText, + uint32_t shaderLength, const uint32_t * params, + uint32_t paramLength); virtual ~ProgramFragment(); virtual void setupGL(const Context *, ProgramFragmentState *); + virtual void setupGL2(const Context *, ProgramFragmentState *, ShaderCache *sc); - - - void bindTexture(uint32_t slot, Allocation *); - void bindSampler(uint32_t slot, Sampler *); - void setType(uint32_t slot, const Element *, uint32_t dim); - - void setEnvMode(uint32_t slot, RsTexEnvMode); - void setTexEnable(uint32_t slot, bool); - - + virtual void createShader(); + virtual void loadShader(Context *rsc); + virtual void init(Context *rsc); protected: - // 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[MAX_TEXTURE]; - ObjectBaseRef<Sampler> mSamplers[MAX_TEXTURE]; - ObjectBaseRef<const Element> mTextureFormats[MAX_TEXTURE]; - uint32_t mTextureDimensions[MAX_TEXTURE]; - - // Hacks to create a program for now + uint32_t mTextureFormats[MAX_TEXTURE]; + uint32_t mTextureDimensions[MAX_TEXTURE]; RsTexEnvMode mEnvModes[MAX_TEXTURE]; uint32_t mTextureEnableMask; bool mPointSpriteEnable; diff --git a/libs/rs/rsProgramFragmentStore.cpp b/libs/rs/rsProgramFragmentStore.cpp index de33d9c..8a2157f 100644 --- a/libs/rs/rsProgramFragmentStore.cpp +++ b/libs/rs/rsProgramFragmentStore.cpp @@ -24,8 +24,8 @@ using namespace android; using namespace android::renderscript; -ProgramFragmentStore::ProgramFragmentStore(Context *rsc, Element *in, Element *out) : - Program(rsc, in, out) +ProgramFragmentStore::ProgramFragmentStore(Context *rsc) : + Program(rsc) { mAllocFile = __FILE__; mAllocLine = __LINE__; @@ -83,10 +83,44 @@ void ProgramFragmentStore::setupGL(const Context *rsc, ProgramFragmentStoreState } else { glDisable(GL_DITHER); } +} +void ProgramFragmentStore::setupGL2(const Context *rsc, ProgramFragmentStoreState *state) +{ + if (state->mLast.get() == this) { + return; + } + state->mLast.set(this); + + glColorMask(mColorRWriteEnable, + mColorGWriteEnable, + mColorBWriteEnable, + mColorAWriteEnable); + if (mBlendEnable) { + glEnable(GL_BLEND); + glBlendFunc(mBlendSrc, mBlendDst); + } else { + glDisable(GL_BLEND); + } + //LOGE("pfs %i, %i, %x", mDepthWriteEnable, mDepthTestEnable, mDepthFunc); + + glDepthMask(mDepthWriteEnable); + if(mDepthTestEnable || mDepthWriteEnable) { + glEnable(GL_DEPTH_TEST); + glDepthFunc(mDepthFunc); + } else { + glDisable(GL_DEPTH_TEST); + } + + if (mDitherEnable) { + glEnable(GL_DITHER); + } else { + glDisable(GL_DITHER); + } } + void ProgramFragmentStore::setDitherEnable(bool enable) { mDitherEnable = enable; @@ -215,7 +249,7 @@ ProgramFragmentStoreState::~ProgramFragmentStoreState() void ProgramFragmentStoreState::init(Context *rsc, int32_t w, int32_t h) { - ProgramFragmentStore *pfs = new ProgramFragmentStore(rsc, NULL, NULL); + ProgramFragmentStore *pfs = new ProgramFragmentStore(rsc); mDefault.set(pfs); } @@ -232,7 +266,7 @@ namespace renderscript { void rsi_ProgramFragmentStoreBegin(Context * rsc, RsElement in, RsElement out) { delete rsc->mStateFragmentStore.mPFS; - rsc->mStateFragmentStore.mPFS = new ProgramFragmentStore(rsc, (Element *)in, (Element *)out); + rsc->mStateFragmentStore.mPFS = new ProgramFragmentStore(rsc); } diff --git a/libs/rs/rsProgramFragmentStore.h b/libs/rs/rsProgramFragmentStore.h index a344387..3412c99 100644 --- a/libs/rs/rsProgramFragmentStore.h +++ b/libs/rs/rsProgramFragmentStore.h @@ -28,10 +28,11 @@ class ProgramFragmentStoreState; class ProgramFragmentStore : public Program { public: - ProgramFragmentStore(Context *, Element *in, Element *out); + ProgramFragmentStore(Context *); virtual ~ProgramFragmentStore(); virtual void setupGL(const Context *, ProgramFragmentStoreState *); + virtual void setupGL2(const Context *, ProgramFragmentStoreState *); void setDepthFunc(RsDepthFunc); void setDepthMask(bool); diff --git a/libs/rs/rsProgramRaster.cpp b/libs/rs/rsProgramRaster.cpp index 51ae7cf..13887d1 100644 --- a/libs/rs/rsProgramRaster.cpp +++ b/libs/rs/rsProgramRaster.cpp @@ -25,12 +25,10 @@ using namespace android::renderscript; ProgramRaster::ProgramRaster(Context *rsc, - Element *in, - Element *out, bool pointSmooth, bool lineSmooth, bool pointSprite) : - Program(rsc, in, out) + Program(rsc) { mAllocFile = __FILE__; mAllocLine = __LINE__; @@ -86,6 +84,14 @@ void ProgramRaster::setupGL(const Context *rsc, ProgramRasterState *state) } } +void ProgramRaster::setupGL2(const Context *rsc, ProgramRasterState *state) +{ + if (state->mLast.get() == this) { + return; + } + state->mLast.set(this); +} + ProgramRasterState::ProgramRasterState() @@ -98,7 +104,7 @@ ProgramRasterState::~ProgramRasterState() void ProgramRasterState::init(Context *rsc, int32_t w, int32_t h) { - ProgramRaster *pr = new ProgramRaster(rsc, NULL, NULL, false, false, false); + ProgramRaster *pr = new ProgramRaster(rsc, false, false, false); mDefault.set(pr); } @@ -118,8 +124,6 @@ RsProgramRaster rsi_ProgramRasterCreate(Context * rsc, RsElement in, RsElement o bool pointSprite) { ProgramRaster *pr = new ProgramRaster(rsc, - static_cast<Element *>(in), - static_cast<Element *>(out), pointSmooth, lineSmooth, pointSprite); diff --git a/libs/rs/rsProgramRaster.h b/libs/rs/rsProgramRaster.h index a6d5ba8..c3a9c90 100644 --- a/libs/rs/rsProgramRaster.h +++ b/libs/rs/rsProgramRaster.h @@ -29,14 +29,13 @@ class ProgramRaster : public Program { public: ProgramRaster(Context *rsc, - Element *in, - Element *out, bool pointSmooth, bool lineSmooth, bool pointSprite); virtual ~ProgramRaster(); virtual void setupGL(const Context *, ProgramRasterState *); + virtual void setupGL2(const Context *, ProgramRasterState *); void setLineWidth(float w); void setPointSize(float s); diff --git a/libs/rs/rsProgramVertex.cpp b/libs/rs/rsProgramVertex.cpp index 68f589f..28f13d4 100644 --- a/libs/rs/rsProgramVertex.cpp +++ b/libs/rs/rsProgramVertex.cpp @@ -19,18 +19,34 @@ #include <GLES/gl.h> #include <GLES/glext.h> +#include <GLES2/gl2.h> +#include <GLES2/gl2ext.h> using namespace android; using namespace android::renderscript; -ProgramVertex::ProgramVertex(Context *rsc, Element *in, Element *out) : - Program(rsc, in, out) +ProgramVertex::ProgramVertex(Context *rsc, bool texMat) : + Program(rsc) +{ + mAllocFile = __FILE__; + mAllocLine = __LINE__; + mTextureMatrixEnable = texMat; + mLightCount = 0; + init(rsc); +} + +ProgramVertex::ProgramVertex(Context *rsc, const char * shaderText, + uint32_t shaderLength, const uint32_t * params, + uint32_t paramLength) : + Program(rsc, shaderText, shaderLength, params, paramLength) { mAllocFile = __FILE__; mAllocLine = __LINE__; mTextureMatrixEnable = false; mLightCount = 0; + + init(rsc); } ProgramVertex::~ProgramVertex() @@ -40,10 +56,10 @@ ProgramVertex::~ProgramVertex() static void logMatrix(const char *txt, const float *f) { LOGV("Matrix %s, %p", txt, f); - LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[0], f[4], f[8], f[12]); - LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[1], f[5], f[9], f[13]); - LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[2], f[6], f[10], f[14]); - LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[3], f[7], f[11], f[15]); + LOGV("%6.4f, %6.4f, %6.4f, %6.4f", f[0], f[4], f[8], f[12]); + LOGV("%6.4f, %6.4f, %6.4f, %6.4f", f[1], f[5], f[9], f[13]); + LOGV("%6.4f, %6.4f, %6.4f, %6.4f", f[2], f[6], f[10], f[14]); + LOGV("%6.4f, %6.4f, %6.4f, %6.4f", f[3], f[7], f[11], f[15]); } void ProgramVertex::setupGL(const Context *rsc, ProgramVertexState *state) @@ -53,7 +69,7 @@ void ProgramVertex::setupGL(const Context *rsc, ProgramVertexState *state) } state->mLast.set(this); - const float *f = static_cast<const float *>(mConstants->getPtr()); + const float *f = static_cast<const float *>(mConstants[0]->getPtr()); glMatrixMode(GL_TEXTURE); if (mTextureMatrixEnable) { @@ -92,6 +108,172 @@ void ProgramVertex::setupGL(const Context *rsc, ProgramVertexState *state) mDirty = false; } +void ProgramVertex::loadShader(Context *rsc) { + Program::loadShader(rsc, GL_VERTEX_SHADER); +} + +void ProgramVertex::createShader() +{ + mShader.setTo(""); + + mShader.append("varying vec4 varColor;\n"); + mShader.append("varying vec4 varTex0;\n"); + + if (mUserShader.length() > 1) { + mShader.append("uniform mat4 "); + mShader.append(mUniformNames[0]); + mShader.append(";\n"); + + 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); + + // Cannot be complex + rsAssert(!f->getFieldCount()); + 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(e->getFieldName(field)); + mShader.append(";\n"); + } + } + + + 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: 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(e->getFieldName(field)); + mShader.append(";\n"); + } + } + mShader.append(mUserShader); + } else { + mShader.append("attribute vec4 ATTRIB_LegacyPosition;\n"); + mShader.append("attribute vec4 ATTRIB_LegacyColor;\n"); + mShader.append("attribute vec3 ATTRIB_LegacyNormal;\n"); + mShader.append("attribute float ATTRIB_LegacyPointSize;\n"); + mShader.append("attribute vec4 ATTRIB_LegacyTexture;\n"); + + for (uint32_t ct=0; ct < mUniformCount; ct++) { + mShader.append("uniform mat4 "); + mShader.append(mUniformNames[ct]); + mShader.append(";\n"); + } + + mShader.append("void main() {\n"); + mShader.append(" gl_Position = UNI_MVP * ATTRIB_LegacyPosition;\n"); + mShader.append(" gl_PointSize = ATTRIB_LegacyPointSize;\n"); + + mShader.append(" varColor = ATTRIB_LegacyColor;\n"); + if (mTextureMatrixEnable) { + mShader.append(" varTex0 = UNI_TexMatrix * ATTRIB_LegacyTexture;\n"); + } else { + mShader.append(" varTex0 = ATTRIB_LegacyTexture;\n"); + } + //mShader.append(" pos.x = pos.x / 480.0;\n"); + //mShader.append(" pos.y = pos.y / 800.0;\n"); + //mShader.append(" gl_Position = pos;\n"); + mShader.append("}\n"); + } +} + +void ProgramVertex::setupGL2(const Context *rsc, ProgramVertexState *state, ShaderCache *sc) +{ + //LOGE("sgl2 vtx1 %x", glGetError()); + if ((state->mLast.get() == this) && !mDirty) { + //return; + } + + rsc->checkError("ProgramVertex::setupGL2 start"); + glVertexAttrib4f(1, state->color[0], state->color[1], state->color[2], state->color[3]); + + const float *f = static_cast<const float *>(mConstants[0]->getPtr()); + + Matrix mvp; + mvp.load(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]); + Matrix t; + t.load(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]); + mvp.multiply(&t); + + glUniformMatrix4fv(sc->vtxUniformSlot(0), 1, GL_FALSE, mvp.m); + if (mTextureMatrixEnable) { + glUniformMatrix4fv(sc->vtxUniformSlot(1), 1, GL_FALSE, + &f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET]); + } + + rsc->checkError("ProgramVertex::setupGL2 begin uniforms"); + uint32_t uidx = 1; + for (uint32_t ct=0; ct < mConstantCount; ct++) { + Allocation *alloc = mConstants[ct+1].get(); + if (!alloc) { + 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); + uint32_t offset = e->getFieldOffsetBytes(field); + int32_t slot = sc->vtxUniformSlot(uidx); + + const float *fd = reinterpret_cast<const float *>(&data[offset]); + + //LOGE("Uniform slot=%i, offset=%i, constant=%i, field=%i, uidx=%i", slot, offset, ct, field, uidx); + if (slot >= 0) { + switch(f->getComponent().getVectorSize()) { + case 1: + //LOGE("Uniform 1 = %f", fd[0]); + glUniform1fv(slot, 1, fd); + break; + case 2: + //LOGE("Uniform 2 = %f %f", fd[0], fd[1]); + glUniform2fv(slot, 1, fd); + break; + case 3: + //LOGE("Uniform 3 = %f %f %f", fd[0], fd[1], fd[2]); + glUniform3fv(slot, 1, fd); + break; + case 4: + //LOGE("Uniform 4 = %f %f %f %f", fd[0], fd[1], fd[2], fd[3]); + glUniform4fv(slot, 1, fd); + break; + default: + rsAssert(0); + } + } + uidx ++; + } + } + + for (uint32_t ct=0; ct < mConstantCount; ct++) { + uint32_t glSlot = sc->vtxUniformSlot(ct + 1); + + } + + state->mLast.set(this); + rsc->checkError("ProgramVertex::setupGL2"); +} + void ProgramVertex::addLight(const Light *l) { if (mLightCount < MAX_LIGHTS) { @@ -102,60 +284,103 @@ void ProgramVertex::addLight(const Light *l) void ProgramVertex::setProjectionMatrix(const rsc_Matrix *m) const { - float *f = static_cast<float *>(mConstants->getPtr()); + float *f = static_cast<float *>(mConstants[0]->getPtr()); memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m, sizeof(rsc_Matrix)); mDirty = true; } void ProgramVertex::setModelviewMatrix(const rsc_Matrix *m) const { - float *f = static_cast<float *>(mConstants->getPtr()); + float *f = static_cast<float *>(mConstants[0]->getPtr()); memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m, sizeof(rsc_Matrix)); mDirty = true; } void ProgramVertex::setTextureMatrix(const rsc_Matrix *m) const { - float *f = static_cast<float *>(mConstants->getPtr()); + float *f = static_cast<float *>(mConstants[0]->getPtr()); memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m, sizeof(rsc_Matrix)); mDirty = true; } void ProgramVertex::transformToScreen(const Context *rsc, float *v4out, const float *v3in) const { - float *f = static_cast<float *>(mConstants->getPtr()); + float *f = static_cast<float *>(mConstants[0]->getPtr()); Matrix mvp; mvp.loadMultiply((Matrix *)&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], (Matrix *)&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]); mvp.vectorMultiply(v4out, v3in); } +void ProgramVertex::initAddUserElement(const Element *e, String8 *names, 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, count, prefix); + } else { + String8 tmp(prefix); + tmp.append(e->getFieldName(ct)); + names[*count].setTo(tmp.string()); + (*count)++; + } + } +} + + +void ProgramVertex::init(Context *rsc) +{ + mAttribCount = 0; + if (mUserShader.size() > 0) { + for (uint32_t ct=0; ct < mInputCount; ct++) { + initAddUserElement(mInputElements[ct].get(), mAttribNames, &mAttribCount, "ATTRIB_"); + } + + mUniformCount = 1; + mUniformNames[0].setTo("UNI_MVP"); + for (uint32_t ct=0; ct < mConstantCount; ct++) { + initAddUserElement(mConstantTypes[ct]->getElement(), mUniformNames, &mUniformCount, "UNI_"); + } + } else { + mUniformCount = 2; + mUniformNames[0].setTo("UNI_MVP"); + mUniformNames[1].setTo("UNI_TexMatrix"); + } + + createShader(); +} + + +/////////////////////////////////////////////////////////////////////// + ProgramVertexState::ProgramVertexState() { - mPV = NULL; } ProgramVertexState::~ProgramVertexState() { - delete mPV; } void ProgramVertexState::init(Context *rsc, int32_t w, int32_t h) { - rsi_ElementBegin(rsc); - rsi_ElementAdd(rsc, RS_KIND_USER, RS_TYPE_FLOAT, false, 32, NULL); - RsElement e = rsi_ElementCreate(rsc); + RsElement e = Element::create(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 1); rsi_TypeBegin(rsc, e); rsi_TypeAdd(rsc, RS_DIMENSION_X, 48); mAllocType.set((Type *)rsi_TypeCreate(rsc)); - ProgramVertex *pv = new ProgramVertex(rsc, NULL, NULL); + ProgramVertex *pv = new ProgramVertex(rsc, false); Allocation *alloc = (Allocation *)rsi_AllocationCreateTyped(rsc, mAllocType.get()); mDefaultAlloc.set(alloc); mDefault.set(pv); + pv->init(rsc); + pv->bindAllocation(alloc, 0); - pv->bindAllocation(alloc); + color[0] = 1.f; + color[1] = 1.f; + color[2] = 1.f; + color[3] = 1.f; updateSize(rsc, w, h); } @@ -176,42 +401,27 @@ void ProgramVertexState::deinit(Context *rsc) mDefault.clear(); mAllocType.clear(); mLast.clear(); - delete mPV; - mPV = NULL; } namespace android { namespace renderscript { -void rsi_ProgramVertexBegin(Context *rsc, RsElement in, RsElement out) -{ - delete rsc->mStateVertex.mPV; - rsc->mStateVertex.mPV = new ProgramVertex(rsc, (Element *)in, (Element *)out); -} -RsProgramVertex rsi_ProgramVertexCreate(Context *rsc) +RsProgramVertex rsi_ProgramVertexCreate(Context *rsc, bool texMat) { - ProgramVertex *pv = rsc->mStateVertex.mPV; + ProgramVertex *pv = new ProgramVertex(rsc, texMat); pv->incUserRef(); - rsc->mStateVertex.mPV = 0; return pv; } -void rsi_ProgramVertexBindAllocation(Context *rsc, RsProgramVertex vpgm, RsAllocation constants) +RsProgramVertex rsi_ProgramVertexCreate2(Context *rsc, const char * shaderText, + uint32_t shaderLength, const uint32_t * params, + uint32_t paramLength) { - ProgramVertex *pv = static_cast<ProgramVertex *>(vpgm); - pv->bindAllocation(static_cast<Allocation *>(constants)); -} - -void rsi_ProgramVertexSetTextureMatrixEnable(Context *rsc, bool enable) -{ - rsc->mStateVertex.mPV->setTextureMatrixEnable(enable); -} - -void rsi_ProgramVertexAddLight(Context *rsc, RsLight light) -{ - rsc->mStateVertex.mPV->addLight(static_cast<const Light *>(light)); + ProgramVertex *pv = new ProgramVertex(rsc, shaderText, shaderLength, params, paramLength); + pv->incUserRef(); + return pv; } diff --git a/libs/rs/rsProgramVertex.h b/libs/rs/rsProgramVertex.h index a97ba38..28554cc 100644 --- a/libs/rs/rsProgramVertex.h +++ b/libs/rs/rsProgramVertex.h @@ -30,10 +30,13 @@ class ProgramVertex : public Program public: const static uint32_t MAX_LIGHTS = 8; - ProgramVertex(Context *, Element *in, Element *out); + ProgramVertex(Context *,const char * shaderText, uint32_t shaderLength, + const uint32_t * params, uint32_t paramLength); + ProgramVertex(Context *, bool texMat); virtual ~ProgramVertex(); virtual void setupGL(const Context *rsc, ProgramVertexState *state); + virtual void setupGL2(const Context *rsc, ProgramVertexState *state, ShaderCache *sc); void setTextureMatrixEnable(bool e) {mTextureMatrixEnable = e;} @@ -45,6 +48,10 @@ public: void transformToScreen(const Context *, float *v4out, const float *v3in) const; + virtual void createShader(); + virtual void loadShader(Context *); + virtual void init(Context *); + protected: uint32_t mLightCount; @@ -52,6 +59,9 @@ protected: // Hacks to create a program for now bool mTextureMatrixEnable; + +private: + void initAddUserElement(const Element *e, String8 *names, uint32_t *count, const char *prefix); }; @@ -71,11 +81,8 @@ public: ObjectBaseRef<Type> mAllocType; - ProgramVertex *mPV; - - //ObjectBaseRef<Type> mTextureTypes[ProgramFragment::MAX_TEXTURE]; - + float color[4]; }; diff --git a/libs/rs/rsSampler.cpp b/libs/rs/rsSampler.cpp index b793750..7552d54 100644 --- a/libs/rs/rsSampler.cpp +++ b/libs/rs/rsSampler.cpp @@ -53,7 +53,7 @@ Sampler::~Sampler() { } -void Sampler::setupGL() +void Sampler::setupGL(const Context *rsc) { GLenum trans[] = { GL_NEAREST, //RS_SAMPLER_NEAREST, @@ -64,12 +64,12 @@ void Sampler::setupGL() }; - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, trans[mWrapS]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, trans[mWrapT]); + rsc->checkError("ProgramFragment::setupGL2 tex env"); } void Sampler::bindToContext(SamplerState *ss, uint32_t slot) @@ -84,18 +84,18 @@ void Sampler::unbindFromContext(SamplerState *ss) mBoundSlot = -1; ss->mSamplers[slot].clear(); } - +/* void SamplerState::setupGL() { for (uint32_t ct=0; ct < RS_MAX_SAMPLER_SLOT; ct++) { Sampler *s = mSamplers[ct].get(); if (s) { - s->setupGL(); + s->setupGL(rsc); } else { glBindTexture(GL_TEXTURE_2D, 0); } } -} +}*/ //////////////////////////////// diff --git a/libs/rs/rsSampler.h b/libs/rs/rsSampler.h index ccf9b4d..9e20a2f 100644 --- a/libs/rs/rsSampler.h +++ b/libs/rs/rsSampler.h @@ -41,7 +41,7 @@ public: virtual ~Sampler(); void bind(Allocation *); - void setupGL(); + void setupGL(const Context *); void bindToContext(SamplerState *, uint32_t slot); void unbindFromContext(SamplerState *); @@ -74,7 +74,7 @@ public: ObjectBaseRef<Sampler> mSamplers[RS_MAX_SAMPLER_SLOT]; - void setupGL(); + //void setupGL(); }; diff --git a/libs/rs/rsScriptC.cpp b/libs/rs/rsScriptC.cpp index f11b862..b7e0b86 100644 --- a/libs/rs/rsScriptC.cpp +++ b/libs/rs/rsScriptC.cpp @@ -137,7 +137,6 @@ void ScriptCState::runCompiler(Context *rsc, ScriptC *s) rsc->appendNameDefines(&tmp); appendDecls(&tmp); - rsc->appendVarDefines(&tmp); appendVarDefines(rsc, &tmp); appendTypes(rsc, &tmp); tmp.append("#line 1\n"); @@ -270,12 +269,12 @@ void ScriptCState::runCompiler(Context *rsc, ScriptC *s) static void appendElementBody(String8 *s, const Element *e) { s->append(" {\n"); - for (size_t ct2=0; ct2 < e->getComponentCount(); ct2++) { - const Component *c = e->getComponent(ct2); + for (size_t ct2=0; ct2 < e->getFieldCount(); ct2++) { + const Element *c = e->getField(ct2); s->append(" "); s->append(c->getCType()); s->append(" "); - s->append(c->getComponentName()); + s->append(e->getFieldName(ct2)); s->append(";\n"); } s->append("}"); @@ -311,9 +310,15 @@ void ScriptCState::appendTypes(const Context *rsc, String8 *str) char buf[256]; String8 tmp; - str->append("struct vec2_s {float x; float y;};"); - str->append("struct vec3_s {float x; float y; float z;};"); - str->append("struct vec4_s {float x; float y; float z; float w;};"); + str->append("struct vecF32_2_s {float x; float y;};\n"); + str->append("struct vecF32_3_s {float x; float y; float z;};\n"); + str->append("struct vecF32_4_s {float x; float y; float z; float w;};\n"); + str->append("struct vecU8_4_s {char r; char g; char b; char a;};\n"); + str->append("#define vecF32_2_t struct vecF32_2_s\n"); + str->append("#define vecF32_3_t struct vecF32_3_s\n"); + str->append("#define vecF32_4_t struct vecF32_4_s\n"); + str->append("#define vecU8_4_t struct vecU8_4_s\n"); + str->append("#define vecI8_4_t struct vecU8_4_s\n"); for (size_t ct=0; ct < MAX_SCRIPT_BANKS; ct++) { const Type *t = mConstantBufferTypes[ct].get(); @@ -321,11 +326,12 @@ void ScriptCState::appendTypes(const Context *rsc, String8 *str) continue; } const Element *e = t->getElement(); - if (e->getName() && (e->getComponentCount() > 1)) { + if (e->getName() && (e->getFieldCount() > 1)) { String8 s("struct struct_"); s.append(e->getName()); - appendElementBody(&s, e); + s.append(e->getCStructBody()); s.append(";\n"); + s.append("#define "); s.append(e->getName()); s.append("_t struct struct_"); @@ -337,45 +343,25 @@ void ScriptCState::appendTypes(const Context *rsc, String8 *str) str->append(s); } - if (t->getName()) { - for (size_t ct2=0; ct2 < e->getComponentCount(); ct2++) { - const Component *c = e->getComponent(ct2); - tmp.setTo("#define OFFSETOF_"); - tmp.append(t->getName()); - tmp.append("_"); - tmp.append(c->getComponentName()); - sprintf(buf, " %i\n", ct2); - tmp.append(buf); - if (rsc->props.mLogScripts) { - LOGV(tmp); - } - str->append(tmp); - } - } - if (mSlotNames[ct].length() > 0) { String8 s; - if (e->getComponentCount() > 1) { - if (e->getName()) { - // Use the named struct - s.setTo(e->getName()); - s.append("_t *"); - } else { - // create an struct named from the slot. - s.setTo("struct "); - s.append(mSlotNames[ct]); - s.append("_s"); - appendElementBody(&s, e); - s.append(";\n"); - s.append("struct "); - s.append(mSlotNames[ct]); - s.append("_s * "); - } + if (e->getName()) { + // Use the named struct + s.setTo(e->getName()); } else { - // Just make an array - s.setTo(e->getComponent(0)->getCType()); - s.append("_t *"); + // create an struct named from the slot. + s.setTo("struct "); + s.append(mSlotNames[ct]); + s.append("_s"); + s.append(e->getCStructBody()); + //appendElementBody(&s, e); + s.append(";\n"); + s.append("struct "); + s.append(mSlotNames[ct]); + s.append("_s"); } + + s.append(" * "); s.append(mSlotNames[ct]); s.append(";\n"); if (rsc->props.mLogScripts) { diff --git a/libs/rs/rsScriptC_Lib.cpp b/libs/rs/rsScriptC_Lib.cpp index de8df39..61bb16f 100644 --- a/libs/rs/rsScriptC_Lib.cpp +++ b/libs/rs/rsScriptC_Lib.cpp @@ -26,6 +26,8 @@ #include <GLES/gl.h> #include <GLES/glext.h> +#include <GLES2/gl2.h> +#include <GLES2/gl2ext.h> #include <time.h> @@ -102,8 +104,9 @@ static float* SC_loadSimpleMeshVerticesF(RsSimpleMesh mesh, uint32_t idx) static void SC_updateSimpleMesh(RsSimpleMesh mesh) { + GET_TLS(); SimpleMesh *sm = static_cast<SimpleMesh *>(mesh); - sm->uploadAll(); + sm->uploadAll(rsc); } static uint32_t SC_loadU32(uint32_t bank, uint32_t offset) @@ -616,20 +619,20 @@ static void SC_vec2Rand(float *vec, float maxLen) static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va) { GET_TLS(); - rsi_ProgramFragmentBindTexture(rsc, - static_cast<ProgramFragment *>(vpf), - slot, - static_cast<Allocation *>(va)); + rsi_ProgramBindTexture(rsc, + static_cast<ProgramFragment *>(vpf), + slot, + static_cast<Allocation *>(va)); } static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs) { GET_TLS(); - rsi_ProgramFragmentBindSampler(rsc, - static_cast<ProgramFragment *>(vpf), - slot, - static_cast<Sampler *>(vs)); + rsi_ProgramBindSampler(rsc, + static_cast<ProgramFragment *>(vpf), + slot, + static_cast<Sampler *>(vs)); } @@ -683,13 +686,13 @@ static void SC_drawLine(float x1, float y1, float z1, rsc->setupCheck(); float vtx[] = { x1, y1, z1, x2, y2, z2 }; - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glEnableClientState(GL_VERTEX_ARRAY); - glVertexPointer(3, GL_FLOAT, 0, vtx); - - glDisableClientState(GL_NORMAL_ARRAY); - glDisableClientState(GL_COLOR_ARRAY); + VertexArray va; + va.addLegacy(GL_FLOAT, 3, 12, RS_KIND_POSITION, false, (uint32_t)vtx); + if (rsc->checkVersion2_0()) { + va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache); + } else { + va.setupGL(rsc, &rsc->mStateVertexArray); + } glDrawArrays(GL_LINES, 0, 2); } @@ -701,12 +704,13 @@ static void SC_drawPoint(float x, float y, float z) float vtx[] = { x, y, z }; - glBindBuffer(GL_ARRAY_BUFFER, 0); - glEnableClientState(GL_VERTEX_ARRAY); - glVertexPointer(3, GL_FLOAT, 0, vtx); - - glDisableClientState(GL_NORMAL_ARRAY); - glDisableClientState(GL_COLOR_ARRAY); + VertexArray va; + va.addLegacy(GL_FLOAT, 3, 12, RS_KIND_POSITION, false, (uint32_t)vtx); + if (rsc->checkVersion2_0()) { + va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache); + } else { + va.setupGL(rsc, &rsc->mStateVertexArray); + } glDrawArrays(GL_POINTS, 0, 1); } @@ -721,6 +725,7 @@ static void SC_drawQuadTexCoords(float x1, float y1, float z1, float u4, float v4) { GET_TLS(); + rsc->setupCheck(); //LOGE("Quad"); //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1); @@ -731,26 +736,15 @@ static void SC_drawQuadTexCoords(float x1, float y1, float z1, 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}; - rsc->setupCheck(); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]); - - glEnableClientState(GL_VERTEX_ARRAY); - glVertexPointer(3, GL_FLOAT, 0, vtx); - - glClientActiveTexture(GL_TEXTURE0); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glTexCoordPointer(2, GL_FLOAT, 0, tex); - glClientActiveTexture(GL_TEXTURE1); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glTexCoordPointer(2, GL_FLOAT, 0, tex); - glClientActiveTexture(GL_TEXTURE0); - - glDisableClientState(GL_NORMAL_ARRAY); - glDisableClientState(GL_COLOR_ARRAY); + VertexArray va; + va.addLegacy(GL_FLOAT, 3, 12, RS_KIND_POSITION, false, (uint32_t)vtx); + va.addLegacy(GL_FLOAT, 2, 8, RS_KIND_TEXTURE, false, (uint32_t)tex); + if (rsc->checkVersion2_0()) { + va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache); + } else { + va.setupGL(rsc, &rsc->mStateVertexArray); + } - //glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); } @@ -769,29 +763,46 @@ static void SC_drawQuad(float x1, float y1, float z1, static void SC_drawSpriteScreenspace(float x, float y, float z, float w, float h) { GET_TLS(); - rsc->setupCheck(); + ObjectBaseRef<const ProgramVertex> tmp(rsc->getVertex()); + rsc->setVertex(rsc->getDefaultProgramVertex()); + //rsc->setupCheck(); + + //GLint crop[4] = {0, h, w, -h}; + + float sh = rsc->getHeight(); - GLint crop[4] = {0, h, w, -h}; - glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop); - glDrawTexfOES(x, y, z, w, h); + SC_drawQuad(x, sh - y, z, + x+w, sh - y, z, + x+w, sh - (y+h), z, + x, sh - (y+h), z); + rsc->setVertex((ProgramVertex *)tmp.get()); } static void SC_drawSpriteScreenspaceCropped(float x, float y, float z, float w, float h, float cx0, float cy0, float cx1, float cy1) { GET_TLS(); - rsc->setupCheck(); + ObjectBaseRef<const ProgramVertex> tmp(rsc->getVertex()); + rsc->setVertex(rsc->getDefaultProgramVertex()); + + float tw = rsc->getFragment()->getTexture(0)->getType()->getDimY(); + float th = rsc->getFragment()->getTexture(0)->getType()->getDimX(); + float sh = rsc->getHeight(); + float u0 = cx0 / tw; + float u1 = (cx1 + cx0) / tw; + float v0 = cy0 / th; + float v1 = (cy1 + cy0) / th; - GLint crop[4] = {cx0, cy0, cx1, cy1}; - glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop); - glDrawTexfOES(x, y, z, w, h); + SC_drawQuadTexCoords(x, sh - y, z, u0, v0, + x+w, sh - y, z, u1, v0, + x+w, sh - (y+h), z, u1, v1, + x, sh - (y+h), z, u0, v1); + rsc->setVertex((ProgramVertex *)tmp.get()); } static void SC_drawSprite(float x, float y, float z, float w, float h) { GET_TLS(); - rsc->setupCheck(); - float vin[3] = {x, y, z}; float vout[4]; @@ -813,9 +824,8 @@ static void SC_drawSprite(float x, float y, float z, float w, float h) //LOGE("ds out2 %f %f %f", vout[0], vout[1], vout[2]); // U, V, W, H - GLint crop[4] = {0, h, w, -h}; - glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop); - glDrawTexiOES(vout[0], vout[1], 0/*vout[2]*/, w, h); + SC_drawSpriteScreenspace(vout[0], vout[1], z, h, w); + //rsc->setupCheck(); } @@ -833,7 +843,7 @@ static void SC_drawSimpleMesh(RsSimpleMesh vsm) GET_TLS(); SimpleMesh *sm = static_cast<SimpleMesh *>(vsm); rsc->setupCheck(); - sm->render(); + sm->render(rsc); } static void SC_drawSimpleMeshRange(RsSimpleMesh vsm, uint32_t start, uint32_t len) @@ -841,7 +851,7 @@ static void SC_drawSimpleMeshRange(RsSimpleMesh vsm, uint32_t start, uint32_t le GET_TLS(); SimpleMesh *sm = static_cast<SimpleMesh *>(vsm); rsc->setupCheck(); - sm->renderRange(start, len); + sm->renderRange(rsc, start, len); } @@ -851,7 +861,14 @@ static void SC_drawSimpleMeshRange(RsSimpleMesh vsm, uint32_t start, uint32_t le static void SC_color(float r, float g, float b, float a) { - glColor4f(r, g, b, a); + GET_TLS(); + rsc->mStateVertex.color[0] = r; + rsc->mStateVertex.color[1] = g; + rsc->mStateVertex.color[2] = b; + rsc->mStateVertex.color[3] = a; + if (!rsc->checkVersion2_0()) { + glColor4f(r, g, b, a); + } } static void SC_ambient(float r, float g, float b, float a) @@ -956,9 +973,14 @@ static int SC_hsbToAbgr(float h, float s, float b, float a) static void SC_hsb(float h, float s, float b, float a) { + GET_TLS(); float rgb[3]; SC_hsbToRgb(h, s, b, rgb); - glColor4f(rgb[0], rgb[1], rgb[2], a); + if (rsc->checkVersion2_0()) { + glVertexAttrib4f(1, rgb[0], rgb[1], rgb[2], a); + } else { + glColor4f(rgb[0], rgb[1], rgb[2], a); + } } static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel) @@ -973,6 +995,13 @@ static void SC_uploadToBufferObject(RsAllocation va) rsi_AllocationUploadToBufferObject(rsc, va); } +static void SC_syncToGL(RsAllocation va) +{ + GET_TLS(); + Allocation *a = static_cast<Allocation *>(va); + +} + static void SC_ClearColor(float r, float g, float b, float a) { //LOGE("c %f %f %f %f", r, g, b, a); @@ -1221,33 +1250,33 @@ ScriptCState::SymbolTable_t ScriptCState::gSyms[] = { // vec3 { "vec3Norm", (void *)&SC_vec3Norm, - "void", "(struct vec3_s *)" }, + "void", "(struct vecF32_3_s *)" }, { "vec3Length", (void *)&SC_vec3Length, - "float", "(struct vec3_s *)" }, + "float", "(struct vecF32_3_s *)" }, { "vec3Add", (void *)&SC_vec3Add, - "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" }, + "void", "(struct vecF32_3_s *dest, struct vecF32_3_s *lhs, struct vecF32_3_s *rhs)" }, { "vec3Sub", (void *)&SC_vec3Sub, - "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" }, + "void", "(struct vecF32_3_s *dest, struct vecF32_3_s *lhs, struct vecF32_3_s *rhs)" }, { "vec3Cross", (void *)&SC_vec3Cross, - "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" }, + "void", "(struct vecF32_3_s *dest, struct vecF32_3_s *lhs, struct vecF32_3_s *rhs)" }, { "vec3Dot", (void *)&SC_vec3Dot, - "float", "(struct vec3_s *lhs, struct vec3_s *rhs)" }, + "float", "(struct vecF32_3_s *lhs, struct vecF32_3_s *rhs)" }, { "vec3Scale", (void *)&SC_vec3Scale, - "void", "(struct vec3_s *lhs, float scale)" }, + "void", "(struct vecF32_3_s *lhs, float scale)" }, // vec4 { "vec4Norm", (void *)&SC_vec4Norm, - "void", "(struct vec4_s *)" }, + "void", "(struct vecF32_4_s *)" }, { "vec4Length", (void *)&SC_vec4Length, - "float", "(struct vec4_s *)" }, + "float", "(struct vecF32_4_s *)" }, { "vec4Add", (void *)&SC_vec4Add, - "void", "(struct vec4_s *dest, struct vec4_s *lhs, struct vec4_s *rhs)" }, + "void", "(struct vecF32_4_s *dest, struct vecF32_4_s *lhs, struct vecF32_4_s *rhs)" }, { "vec4Sub", (void *)&SC_vec4Sub, - "void", "(struct vec4_s *dest, struct vec4_s *lhs, struct vec4_s *rhs)" }, + "void", "(struct vecF32_4_s *dest, struct vecF32_4_s *lhs, struct vecF32_4_s *rhs)" }, { "vec4Dot", (void *)&SC_vec4Dot, - "float", "(struct vec4_s *lhs, struct vec4_s *rhs)" }, + "float", "(struct vecF32_4_s *lhs, struct vecF32_4_s *rhs)" }, { "vec4Scale", (void *)&SC_vec4Scale, - "void", "(struct vec4_s *lhs, float scale)" }, + "void", "(struct vecF32_4_s *lhs, float scale)" }, // context { "bindProgramFragment", (void *)&SC_bindProgramFragment, @@ -1323,6 +1352,9 @@ ScriptCState::SymbolTable_t ScriptCState::gSyms[] = { { "uploadToBufferObject", (void *)&SC_uploadToBufferObject, "void", "(int)" }, + { "syncToGL", (void *)&SC_syncToGL, + "void", "(int)" }, + { "colorFloatRGBAtoUNorm8", (void *)&SC_colorFloatRGBAtoUNorm8, "int", "(float, float, float, float)" }, { "colorFloatRGBto565", (void *)&SC_colorFloatRGBAto565, diff --git a/libs/rs/rsShaderCache.cpp b/libs/rs/rsShaderCache.cpp new file mode 100644 index 0000000..3a1f370 --- /dev/null +++ b/libs/rs/rsShaderCache.cpp @@ -0,0 +1,167 @@ +/* + * Copyright (C) 2009 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 "rsContext.h" + +#include <GLES/gl.h> +#include <GLES2/gl2.h> + +using namespace android; +using namespace android::renderscript; + + +ShaderCache::ShaderCache() +{ + mEntryCount = 0; + mEntryAllocationCount = 16; + mEntries = (entry_t *)calloc(mEntryAllocationCount, sizeof(entry_t)); +} + +ShaderCache::~ShaderCache() +{ + for (uint32_t ct=0; ct < mEntryCount; ct++) { + glDeleteProgram(mEntries[ct].program); + } + + mEntryCount = 0; + mEntryAllocationCount = 0; + free(mEntries); +} + +bool ShaderCache::lookup(Context *rsc, ProgramVertex *vtx, ProgramFragment *frag) +{ + if (!vtx->getShaderID()) { + vtx->loadShader(rsc); + } + if (!frag->getShaderID()) { + frag->loadShader(rsc); + } + //LOGV("ShaderCache lookup vtx %i, frag %i", vtx->getShaderID(), frag->getShaderID()); + + for (uint32_t ct=0; ct < mEntryCount; ct++) { + if ((mEntries[ct].vtx == vtx->getShaderID()) && + (mEntries[ct].frag == frag->getShaderID())) { + + //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)"); + return true; + } + } + // Not in cache, add it. + + if (mEntryAllocationCount == mEntryCount) { + // Out of space, make some. + mEntryAllocationCount *= 2; + entry_t *e = (entry_t *)calloc(mEntryAllocationCount, sizeof(entry_t)); + if (!e) { + LOGE("Out of memory for ShaderCache::lookup"); + return false; + } + memcpy(e, mEntries, sizeof(entry_t) * mEntryCount); + free(mEntries); + mEntries = e; + } + + //LOGV("ShaderCache miss, using %i", mEntryCount); + //LOGE("e0 %x", glGetError()); + + entry_t *e = &mEntries[mEntryCount]; + mCurrent = e; + e->vtx = vtx->getShaderID(); + e->frag = frag->getShaderID(); + e->program = glCreateProgram(); + e->mUserVertexProgram = vtx->isUserProgram(); + if (mEntries[mEntryCount].program) { + GLuint pgm = e->program; + glAttachShader(pgm, vtx->getShaderID()); + //LOGE("e1 %x", glGetError()); + glAttachShader(pgm, frag->getShaderID()); + + if (!vtx->isUserProgram()) { + glBindAttribLocation(pgm, 0, "ATTRIB_LegacyPosition"); + glBindAttribLocation(pgm, 1, "ATTRIB_LegacyColor"); + glBindAttribLocation(pgm, 2, "ATTRIB_LegacyNormal"); + glBindAttribLocation(pgm, 3, "ATTRIB_LegacyPointSize"); + glBindAttribLocation(pgm, 4, "ATTRIB_LegacyTexture"); + e->mVtxAttribSlots[RS_KIND_POSITION] = 0; + e->mVtxAttribSlots[RS_KIND_COLOR] = 1; + e->mVtxAttribSlots[RS_KIND_NORMAL] = 2; + e->mVtxAttribSlots[RS_KIND_POINT_SIZE] = 3; + e->mVtxAttribSlots[RS_KIND_TEXTURE] = 4; + } + + //LOGE("e2 %x", glGetError()); + glLinkProgram(pgm); + //LOGE("e3 %x", glGetError()); + GLint linkStatus = GL_FALSE; + glGetProgramiv(pgm, GL_LINK_STATUS, &linkStatus); + if (linkStatus != GL_TRUE) { + GLint bufLength = 0; + glGetProgramiv(pgm, GL_INFO_LOG_LENGTH, &bufLength); + if (bufLength) { + char* buf = (char*) malloc(bufLength); + if (buf) { + glGetProgramInfoLog(pgm, bufLength, NULL, buf); + LOGE("Could not link program:\n%s\n", buf); + free(buf); + } + } + glDeleteProgram(pgm); + } + if (vtx->isUserProgram()) { + for (uint32_t ct=0; ct < vtx->getAttribCount(); ct++) { + e->mVtxAttribSlots[ct] = glGetAttribLocation(pgm, vtx->getAttribName(ct)); + if (rsc->props.mLogShaders) { + LOGV("vtx A %i, %s = %d\n", ct, vtx->getAttribName(ct).string(), e->mVtxAttribSlots[ct]); + } + } + } + for (uint32_t ct=0; ct < vtx->getUniformCount(); ct++) { + e->mVtxUniformSlots[ct] = glGetUniformLocation(pgm, vtx->getUniformName(ct)); + if (rsc->props.mLogShaders) { + LOGV("vtx U, %s = %d\n", vtx->getUniformName(ct).string(), e->mVtxUniformSlots[ct]); + } + } + for (uint32_t ct=0; ct < frag->getUniformCount(); ct++) { + e->mFragUniformSlots[ct] = glGetUniformLocation(pgm, frag->getUniformName(ct)); + if (rsc->props.mLogShaders) { + LOGV("frag U, %s = %d\n", frag->getUniformName(ct).string(), e->mFragUniformSlots[ct]); + } + } + } + + //LOGV("SC made program %i", e->program); + glUseProgram(e->program); + mEntryCount++; + rsc->checkError("ShaderCache::lookup (miss)"); + return true; +} + +void ShaderCache::cleanupVertex(uint32_t id) +{ +} + +void ShaderCache::cleanupFragment(uint32_t id) +{ +} + +void ShaderCache::cleanupAll() +{ +} + diff --git a/libs/rs/rsShaderCache.h b/libs/rs/rsShaderCache.h new file mode 100644 index 0000000..7aa8183 --- /dev/null +++ b/libs/rs/rsShaderCache.h @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2009 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_SHADER_CACHE_H +#define ANDROID_SHADER_CACHE_H + + +#include "rsObjectBase.h" +#include "rsVertexArray.h" + +// --------------------------------------------------------------------------- +namespace android { +namespace renderscript { + + +// An element is a group of Components that occupies one cell in a structure. +class ShaderCache +{ +public: + ShaderCache(); + virtual ~ShaderCache(); + + bool lookup(Context *rsc, ProgramVertex *, ProgramFragment *); + + void cleanupVertex(uint32_t id); + void cleanupFragment(uint32_t id); + + void cleanupAll(); + + int32_t vtxAttribSlot(uint32_t a) const {return mCurrent->mVtxAttribSlots[a];} + int32_t vtxUniformSlot(uint32_t a) const {return mCurrent->mVtxUniformSlots[a];} + int32_t fragAttribSlot(uint32_t a) const {return mCurrent->mFragAttribSlots[a];} + int32_t fragUniformSlot(uint32_t a) const {return mCurrent->mFragUniformSlots[a];} + bool isUserVertexProgram() const {return mCurrent->mUserVertexProgram;} + +protected: + typedef struct { + uint32_t vtx; + uint32_t frag; + uint32_t program; + int32_t mVtxAttribSlots[Program::MAX_ATTRIBS]; + int32_t mVtxUniformSlots[Program::MAX_UNIFORMS]; + int32_t mFragAttribSlots[Program::MAX_ATTRIBS]; + int32_t mFragUniformSlots[Program::MAX_UNIFORMS]; + bool mUserVertexProgram; + } entry_t; + entry_t *mEntries; + entry_t *mCurrent; + + uint32_t mEntryCount; + uint32_t mEntryAllocationCount; + +}; + + + +} +} +#endif //ANDROID_SHADER_CACHE_H + + + + diff --git a/libs/rs/rsSimpleMesh.cpp b/libs/rs/rsSimpleMesh.cpp index b082fd7..53ce5cd 100644 --- a/libs/rs/rsSimpleMesh.cpp +++ b/libs/rs/rsSimpleMesh.cpp @@ -34,62 +34,71 @@ SimpleMesh::~SimpleMesh() delete[] mVertexBuffers; } -void SimpleMesh::render() const +void SimpleMesh::render(Context *rsc) const { if (mPrimitiveType.get()) { - renderRange(0, mPrimitiveType->getDimX()); + renderRange(rsc, 0, mPrimitiveType->getDimX()); return; } if (mIndexType.get()) { - renderRange(0, mIndexType->getDimX()); + renderRange(rsc, 0, mIndexType->getDimX()); return; } - renderRange(0, mVertexTypes[0]->getDimX()); + renderRange(rsc, 0, mVertexTypes[0]->getDimX()); } -void SimpleMesh::renderRange(uint32_t start, uint32_t len) const +void SimpleMesh::renderRange(Context *rsc, uint32_t start, uint32_t len) const { if (len < 1) { return; } - glDisableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_NORMAL_ARRAY); - glDisableClientState(GL_COLOR_ARRAY); - for (uint32_t ct=0; ct < RS_MAX_TEXTURE; ct++) { - glClientActiveTexture(GL_TEXTURE0 + ct); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - } - glClientActiveTexture(GL_TEXTURE0); - - for (uint32_t ct=0; ct < mVertexTypeCount; ct++) { - glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffers[ct]->getBufferObjectID()); - mVertexTypes[ct]->enableGLVertexBuffer(); + rsc->checkError("SimpleMesh::renderRange 1"); + VertexArray va; + if (rsc->checkVersion2_0()) { + for (uint32_t ct=0; ct < mVertexTypeCount; ct++) { + mVertexBuffers[ct]->uploadCheck(rsc); + va.setActiveBuffer(mVertexBuffers[ct]->getBufferObjectID()); + mVertexTypes[ct]->enableGLVertexBuffer2(&va); + } + va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache); + } else { + for (uint32_t ct=0; ct < mVertexTypeCount; ct++) { + mVertexBuffers[ct]->uploadCheck(rsc); + va.setActiveBuffer(mVertexBuffers[ct]->getBufferObjectID()); + mVertexTypes[ct]->enableGLVertexBuffer(&va); + } + va.setupGL(rsc, 0); } + rsc->checkError("SimpleMesh::renderRange 2"); if (mIndexType.get()) { + mIndexBuffer->uploadCheck(rsc); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer->getBufferObjectID()); glDrawElements(mGLPrimitive, len, GL_UNSIGNED_SHORT, (uint16_t *)(start * 2)); } else { glDrawArrays(mGLPrimitive, start, len); } + + rsc->checkError("SimpleMesh::renderRange"); } -void SimpleMesh::uploadAll() +void SimpleMesh::uploadAll(Context *rsc) { for (uint32_t ct=0; ct < mVertexTypeCount; ct++) { if (mVertexBuffers[ct].get()) { - mVertexBuffers[ct]->uploadToBufferObject(); + mVertexBuffers[ct]->deferedUploadToBufferObject(rsc); } } if (mIndexBuffer.get()) { - mIndexBuffer->uploadToBufferObject(); + mIndexBuffer->deferedUploadToBufferObject(rsc); } if (mPrimitiveBuffer.get()) { - mPrimitiveBuffer->uploadToBufferObject(); + mPrimitiveBuffer->deferedUploadToBufferObject(rsc); } + rsc->checkError("SimpleMesh::uploadAll"); } diff --git a/libs/rs/rsSimpleMesh.h b/libs/rs/rsSimpleMesh.h index 1e5c908..6defbda 100644 --- a/libs/rs/rsSimpleMesh.h +++ b/libs/rs/rsSimpleMesh.h @@ -45,9 +45,9 @@ public: uint32_t mGLPrimitive; - void render() const; - void renderRange(uint32_t start, uint32_t len) const; - void uploadAll(); + void render(Context *) const; + void renderRange(Context *, uint32_t start, uint32_t len) const; + void uploadAll(Context *); protected: diff --git a/libs/rs/rsType.cpp b/libs/rs/rsType.cpp index ddaa2f0..22a267a 100644 --- a/libs/rs/rsType.cpp +++ b/libs/rs/rsType.cpp @@ -26,7 +26,6 @@ Type::Type(Context *rsc) : ObjectBase(rsc) mAllocLine = __LINE__; mLODs = 0; mLODCount = 0; - memset(&mGL, 0, sizeof(mGL)); clear(); } @@ -93,9 +92,9 @@ void Type::compute() mLODs[lod].mZ = tz; mLODs[lod].mOffset = offset; offset += tx * rsMax(ty, 1u) * rsMax(tz, 1u) * mElement->getSizeBytes(); - tx = (tx + 1) >> 1; - ty = (ty + 1) >> 1; - tz = (tz + 1) >> 1; + if (tx > 1) tx >>= 1; + if (ty > 1) ty >>= 1; + if (tz > 1) tz >>= 1; } // At this point the offset is the size of a mipmap chain; @@ -133,110 +132,64 @@ uint32_t Type::getLODOffset(uint32_t lod, uint32_t x, uint32_t y, uint32_t z) co void Type::makeGLComponents() { - uint32_t texNum = 0; - memset(&mGL, 0, sizeof(mGL)); - - for (uint32_t ct=0; ct < getElement()->getComponentCount(); ct++) { - const Component *c = getElement()->getComponent(ct); + uint32_t userNum = 0; + + for (uint32_t ct=0; ct < getElement()->getFieldCount(); ct++) { + const Component &c = getElement()->getField(ct)->getComponent(); + + switch(c.getKind()) { + case RS_KIND_USER: + mGL.mUser[userNum].size = c.getVectorSize(); + mGL.mUser[userNum].offset = mElement->getFieldOffsetBytes(ct); + mGL.mUser[userNum].type = c.getGLType(); + mGL.mUser[userNum].normalized = c.getType() != RS_TYPE_FLOAT_32;//c.getIsNormalized(); + mGL.mUser[userNum].name.setTo(getElement()->getFieldName(ct)); + userNum ++; + break; - switch(c->getKind()) { - case Component::X: + case RS_KIND_POSITION: rsAssert(mGL.mVtx.size == 0); - mGL.mVtx.size = 1; - mGL.mVtx.offset = mElement->getComponentOffsetBytes(ct); - mGL.mVtx.type = c->getGLType(); - break; - case Component::Y: - rsAssert(mGL.mVtx.size == 1); - rsAssert(mGL.mVtx.type == c->getGLType()); - mGL.mVtx.size = 2; - break; - case Component::Z: - rsAssert(mGL.mVtx.size == 2); - rsAssert(mGL.mVtx.type == c->getGLType()); - mGL.mVtx.size = 3; + mGL.mVtx.size = c.getVectorSize(); + mGL.mVtx.offset = mElement->getFieldOffsetBytes(ct); + mGL.mVtx.type = c.getGLType(); + mGL.mVtx.normalized = false; + mGL.mVtx.name.setTo("Position"); break; - case Component::W: - rsAssert(mGL.mVtx.size == 4); - rsAssert(mGL.mVtx.type == c->getGLType()); - mGL.mVtx.size = 4; - break; - case Component::RED: + case RS_KIND_COLOR: rsAssert(mGL.mColor.size == 0); - mGL.mColor.size = 1; - mGL.mColor.offset = mElement->getComponentOffsetBytes(ct); - mGL.mColor.type = c->getGLType(); - break; - case Component::GREEN: - rsAssert(mGL.mColor.size == 1); - rsAssert(mGL.mColor.type == c->getGLType()); - mGL.mColor.size = 2; - break; - case Component::BLUE: - rsAssert(mGL.mColor.size == 2); - rsAssert(mGL.mColor.type == c->getGLType()); - mGL.mColor.size = 3; + mGL.mColor.size = c.getVectorSize(); + mGL.mColor.offset = mElement->getFieldOffsetBytes(ct); + mGL.mColor.type = c.getGLType(); + mGL.mColor.normalized = c.getType() != RS_TYPE_FLOAT_32; + mGL.mColor.name.setTo("Color"); break; - case Component::ALPHA: - // Can be RGBA or A at this point - if (mGL.mColor.size > 0) { - rsAssert(mGL.mColor.size == 3); - rsAssert(mGL.mColor.type == c->getGLType()); - mGL.mColor.size = 4; - } else { - mGL.mColor.size = 1; - mGL.mColor.offset = mElement->getComponentOffsetBytes(ct); - mGL.mColor.type = c->getGLType(); - } - break; - case Component::NX: + case RS_KIND_NORMAL: rsAssert(mGL.mNorm.size == 0); - mGL.mNorm.size = 1; - mGL.mNorm.offset = mElement->getComponentOffsetBytes(ct); - mGL.mNorm.type = c->getGLType(); - break; - case Component::NY: - rsAssert(mGL.mNorm.size == 1); - rsAssert(mGL.mNorm.type == c->getGLType()); - mGL.mNorm.size = 2; - break; - case Component::NZ: - rsAssert(mGL.mNorm.size == 2); - rsAssert(mGL.mNorm.type == c->getGLType()); - mGL.mNorm.size = 3; - break; + mGL.mNorm.size = c.getVectorSize(); + mGL.mNorm.offset = mElement->getFieldOffsetBytes(ct); + mGL.mNorm.type = c.getGLType(); + mGL.mNorm.normalized = false; + mGL.mNorm.name.setTo("Normal"); + break; - case Component::S: - if (mGL.mTex[texNum].size) { - texNum++; - } - mGL.mTex[texNum].size = 1; - mGL.mTex[texNum].offset = mElement->getComponentOffsetBytes(ct); - mGL.mTex[texNum].type = c->getGLType(); - break; - case Component::T: - rsAssert(mGL.mTex[texNum].size == 1); - rsAssert(mGL.mTex[texNum].type == c->getGLType()); - mGL.mTex[texNum].size = 2; - break; - case Component::R: - rsAssert(mGL.mTex[texNum].size == 2); - rsAssert(mGL.mTex[texNum].type == c->getGLType()); - mGL.mTex[texNum].size = 3; - break; - case Component::Q: - rsAssert(mGL.mTex[texNum].size == 3); - rsAssert(mGL.mTex[texNum].type == c->getGLType()); - mGL.mTex[texNum].size = 4; - break; + case RS_KIND_TEXTURE: + rsAssert(mGL.mTex.size == 0); + mGL.mTex.size = c.getVectorSize(); + mGL.mTex.offset = mElement->getFieldOffsetBytes(ct); + mGL.mTex.type = c.getGLType(); + mGL.mTex.normalized = false; + mGL.mTex.name.setTo("Texture"); + break; - case Component::POINT_SIZE: + case RS_KIND_POINT_SIZE: rsAssert(!mGL.mPointSize.size); - mGL.mPointSize.size = 1; - mGL.mPointSize.offset = mElement->getComponentOffsetBytes(ct); - mGL.mPointSize.type = c->getGLType(); + mGL.mPointSize.size = c.getVectorSize(); + mGL.mPointSize.offset = mElement->getFieldOffsetBytes(ct); + mGL.mPointSize.type = c.getGLType(); + mGL.mPointSize.normalized = false; + mGL.mPointSize.name.setTo("PointSize"); break; default: @@ -245,62 +198,75 @@ void Type::makeGLComponents() } } -void Type::enableGLVertexBuffer() const +void Type::enableGLVertexBuffer(VertexArray *va) const { // Note: We are only going to enable buffers and never disable them - // here. The reasonis more than one Allocation may be used as a vertex + // here. The reason is more than one Allocation may be used as a vertex // source. So we cannot disable arrays that may have been in use by // another allocation. uint32_t stride = mElement->getSizeBytes(); if (mGL.mVtx.size) { - //LOGE("va vtx %i %x, %i, %p", mGL.mVtx.size, mGL.mVtx.type, stride, (void *)mGL.mVtx.offset); - glEnableClientState(GL_VERTEX_ARRAY); - glVertexPointer(mGL.mVtx.size, - mGL.mVtx.type, - stride, - (void *)mGL.mVtx.offset); + va->addLegacy(mGL.mVtx.type, + mGL.mVtx.size, + stride, + RS_KIND_POSITION, + false, + mGL.mVtx.offset); } if (mGL.mNorm.size) { - //LOGE("va norm %i %x, %i, %p", mGL.mNorm.size, mGL.mNorm.type, stride, (void *)mGL.mNorm.offset); - glEnableClientState(GL_NORMAL_ARRAY); - rsAssert(mGL.mNorm.size == 3); - glNormalPointer(mGL.mNorm.type, - stride, - (void *)mGL.mNorm.offset); + va->addLegacy(mGL.mNorm.type, + 3, + stride, + RS_KIND_NORMAL, + false, + mGL.mNorm.offset); } if (mGL.mColor.size) { - glEnableClientState(GL_COLOR_ARRAY); - glColorPointer(mGL.mColor.size, - mGL.mColor.type, - stride, - (void *)mGL.mColor.offset); + va->addLegacy(mGL.mColor.type, + mGL.mColor.size, + stride, + RS_KIND_COLOR, + true, + mGL.mColor.offset); } - for (uint32_t ct=0; ct < RS_MAX_TEXTURE; ct++) { - if (mGL.mTex[ct].size) { - //LOGE("va tex%i %i %x, %i, %p", ct, mGL.mTex[ct].size, mGL.mTex[ct].type, stride, (void *)mGL.mTex[ct].offset); - glClientActiveTexture(GL_TEXTURE0 + ct); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glTexCoordPointer(mGL.mTex[ct].size, - mGL.mTex[ct].type, - stride, - (void *)mGL.mTex[ct].offset); - } + if (mGL.mTex.size) { + va->addLegacy(mGL.mTex.type, + mGL.mTex.size, + stride, + RS_KIND_TEXTURE, + false, + mGL.mTex.offset); } - glClientActiveTexture(GL_TEXTURE0); if (mGL.mPointSize.size) { - glEnableClientState(GL_POINT_SIZE_ARRAY_OES); - glPointSizePointerOES(mGL.mPointSize.type, - stride, - (void *)mGL.mPointSize.offset); + va->addLegacy(mGL.mPointSize.type, + 1, + stride, + RS_KIND_POINT_SIZE, + false, + mGL.mPointSize.offset); } } +void Type::enableGLVertexBuffer2(VertexArray *va) const +{ + // Do legacy buffers + enableGLVertexBuffer(va); + + uint32_t stride = mElement->getSizeBytes(); + for (uint32_t ct=0; ct < RS_MAX_ATTRIBS; ct++) { + if (mGL.mUser[ct].size) { + va->addUser(mGL.mUser[ct], stride); + } + } +} + + void Type::dumpLOGV(const char *prefix) const { diff --git a/libs/rs/rsType.h b/libs/rs/rsType.h index 2c43405..4fa4933 100644 --- a/libs/rs/rsType.h +++ b/libs/rs/rsType.h @@ -18,6 +18,7 @@ #define ANDROID_STRUCTURED_TYPE_H #include "rsElement.h" +#include "rsVertexArray.h" // --------------------------------------------------------------------------- namespace android { @@ -67,7 +68,8 @@ public: void clear(); void compute(); - void enableGLVertexBuffer() const; + void enableGLVertexBuffer(class VertexArray *) const; + void enableGLVertexBuffer2(class VertexArray *) const; void dumpLOGV(const char *prefix) const; @@ -108,18 +110,13 @@ protected: LOD *mLODs; uint32_t mLODCount; - struct VertexComponent_t { - uint32_t offset; - uint32_t type; - uint32_t size; - uint32_t stride; - }; struct GLState_t { - VertexComponent_t mVtx; - VertexComponent_t mNorm; - VertexComponent_t mColor; - VertexComponent_t mTex[RS_MAX_TEXTURE]; - VertexComponent_t mPointSize; + VertexArray::Attrib mUser[RS_MAX_ATTRIBS]; + VertexArray::Attrib mVtx; + VertexArray::Attrib mNorm; + VertexArray::Attrib mColor; + VertexArray::Attrib mTex; + VertexArray::Attrib mPointSize; }; GLState_t mGL; void makeGLComponents(); diff --git a/libs/rs/rsVertexArray.cpp b/libs/rs/rsVertexArray.cpp new file mode 100644 index 0000000..d0c0414 --- /dev/null +++ b/libs/rs/rsVertexArray.cpp @@ -0,0 +1,216 @@ +/* + * Copyright (C) 2009 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 "rsContext.h" + +#include <GLES/gl.h> +#include <GLES2/gl2.h> + +using namespace android; +using namespace android::renderscript; + + +VertexArray::VertexArray() +{ + clearAll(); +} + +VertexArray::~VertexArray() +{ +} + + +void VertexArray::clearAll() +{ + for (uint32_t ct=0; ct < RS_MAX_ATTRIBS; ct++) { + mAttribs[ct].clear(); + } + mActiveBuffer = 0; + mCount = 0; +} + +VertexArray::Attrib::Attrib() +{ + clear(); +} + +void VertexArray::Attrib::set(const Attrib &a) +{ + buffer = a.buffer; + offset = a.offset; + type = a.type; + size = a.size; + stride = a.stride; + normalized = a.normalized; + kind = RS_KIND_USER; + name.setTo(a.name); +} + +void VertexArray::Attrib::clear() +{ + buffer = 0; + offset = 0; + type = 0; + size = 0; + stride = 0; + normalized = false; + name.setTo(""); +} + +void VertexArray::clear(uint32_t n) +{ + mAttribs[n].clear(); +} + +void VertexArray::addUser(const Attrib &a, uint32_t stride) +{ + assert(mCount < RS_MAX_ATTRIBS); + mAttribs[mCount].set(a); + mAttribs[mCount].buffer = mActiveBuffer; + mAttribs[mCount].stride = stride; + mAttribs[mCount].kind = RS_KIND_USER; + mCount ++; +} + +void VertexArray::addLegacy(uint32_t type, uint32_t size, uint32_t stride, RsDataKind kind, bool normalized, uint32_t offset) +{ + assert(mCount < RS_MAX_ATTRIBS); + mAttribs[mCount].clear(); + mAttribs[mCount].type = type; + mAttribs[mCount].size = size; + mAttribs[mCount].offset = offset; + mAttribs[mCount].normalized = normalized; + mAttribs[mCount].buffer = mActiveBuffer; + mAttribs[mCount].stride = stride; + mAttribs[mCount].kind = kind; + mCount ++; +} + +void VertexArray::logAttrib(uint32_t idx, uint32_t slot) const { + LOGE("va %i: slot=%i name=%s buf=%i size=%i type=0x%x kind=%i stride=0x%x norm=%i offset=0x%x", idx, slot, + mAttribs[idx].name.string(), + mAttribs[idx].buffer, + mAttribs[idx].size, + mAttribs[idx].type, + mAttribs[idx].kind, + mAttribs[idx].stride, + mAttribs[idx].normalized, + mAttribs[idx].offset); +} + +void VertexArray::setupGL(const Context *rsc, class VertexArrayState *state) const +{ + glClientActiveTexture(GL_TEXTURE0); + glDisableClientState(GL_NORMAL_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_POINT_SIZE_ARRAY_OES); + + for (uint32_t ct=0; ct < mCount; ct++) { + switch(mAttribs[ct].kind) { + case RS_KIND_POSITION: + //logAttrib(POSITION); + glEnableClientState(GL_VERTEX_ARRAY); + glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); + glVertexPointer(mAttribs[ct].size, + mAttribs[ct].type, + mAttribs[ct].stride, + (void *)mAttribs[ct].offset); + break; + + case RS_KIND_NORMAL: + //logAttrib(NORMAL); + glEnableClientState(GL_NORMAL_ARRAY); + rsAssert(mAttribs[ct].size == 3); + glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); + glNormalPointer(mAttribs[ct].type, + mAttribs[ct].stride, + (void *)mAttribs[ct].offset); + break; + + case RS_KIND_COLOR: + //logAttrib(COLOR); + glEnableClientState(GL_COLOR_ARRAY); + glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); + glColorPointer(mAttribs[ct].size, + mAttribs[ct].type, + mAttribs[ct].stride, + (void *)mAttribs[ct].offset); + break; + + case RS_KIND_TEXTURE: + //logAttrib(TEXTURE); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); + glTexCoordPointer(mAttribs[ct].size, + mAttribs[ct].type, + mAttribs[ct].stride, + (void *)mAttribs[ct].offset); + break; + + case RS_KIND_POINT_SIZE: + //logAttrib(POINT_SIZE); + glEnableClientState(GL_POINT_SIZE_ARRAY_OES); + glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); + glPointSizePointerOES(mAttribs[ct].type, + mAttribs[ct].stride, + (void *)mAttribs[ct].offset); + break; + + default: + rsAssert(0); + } + } + + rsc->checkError("VertexArray::setupGL"); +} + +void VertexArray::setupGL2(const Context *rsc, class VertexArrayState *state, ShaderCache *sc) const +{ + rsc->checkError("VertexArray::setupGL2 start"); + for (uint32_t ct=1; ct <= state->mLastEnableCount; ct++) { + glDisableVertexAttribArray(ct); + } + + rsc->checkError("VertexArray::setupGL2 disabled"); + for (uint32_t ct=0; ct < mCount; ct++) { + uint32_t slot = 0; + if (sc->isUserVertexProgram()) { + slot = sc->vtxAttribSlot(ct); + } else { + slot = sc->vtxAttribSlot(mAttribs[ct].kind); + } + + //logAttrib(ct, slot); + glEnableVertexAttribArray(slot); + glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); + + glVertexAttribPointer(slot, + mAttribs[ct].size, + mAttribs[ct].type, + mAttribs[ct].normalized, + mAttribs[ct].stride, + (void *)mAttribs[ct].offset); + } + state->mLastEnableCount = mCount; + rsc->checkError("VertexArray::setupGL2 done"); +} +//////////////////////////////////////////// + +void VertexArrayState::init(Context *) { + mLastEnableCount = 0; +} + diff --git a/libs/rs/rsVertexArray.h b/libs/rs/rsVertexArray.h new file mode 100644 index 0000000..3904cb6 --- /dev/null +++ b/libs/rs/rsVertexArray.h @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2009 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_VERTEX_ARRAY_H +#define ANDROID_VERTEX_ARRAY_H + + +#include "rsObjectBase.h" + +// --------------------------------------------------------------------------- +namespace android { +namespace renderscript { + +class ShaderCache; + +// An element is a group of Components that occupies one cell in a structure. +class VertexArray +{ +public: + VertexArray(); + virtual ~VertexArray(); + + + class Attrib { + public: + uint32_t buffer; + uint32_t offset; + uint32_t type; + uint32_t size; + uint32_t stride; + bool normalized; + String8 name; + RsDataKind kind; + + Attrib(); + void set(const Attrib &); + void clear(); + }; + + + void clearAll(); + void setActiveBuffer(uint32_t id) {mActiveBuffer = id;} + void addUser(const Attrib &, uint32_t stride); + void addLegacy(uint32_t type, uint32_t size, uint32_t stride, RsDataKind kind, bool normalized, uint32_t offset); + + void setupGL(const Context *rsc, class VertexArrayState *) const; + void setupGL2(const Context *rsc, class VertexArrayState *, ShaderCache *) const; + void logAttrib(uint32_t idx, uint32_t slot) const; + +protected: + void clear(uint32_t index); + uint32_t mActiveBuffer; + uint32_t mCount; + + Attrib mAttribs[RS_MAX_ATTRIBS]; +}; + + +class VertexArrayState { +public: + void init(Context *); + + uint32_t mLastEnableCount; + //VertexArray::Attrib mAttribs[VertexArray::_LAST]; +}; + + +} +} +#endif //ANDROID_LIGHT_H + + + |