modules/graphics/src/main/java/com/sun/prism/d3d/D3DContext.java

Print this page




  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.prism.d3d;
  27 
  28 import com.sun.glass.ui.Screen;
  29 import com.sun.javafx.geom.Rectangle;
  30 import com.sun.javafx.geom.Vec3d;
  31 import com.sun.javafx.geom.transform.BaseTransform;
  32 import com.sun.javafx.geom.transform.GeneralTransform3D;
  33 import com.sun.javafx.sg.prism.NGCamera;
  34 import com.sun.javafx.sg.prism.NGDefaultCamera;
  35 import com.sun.prism.CompositeMode;

  36 import com.sun.prism.MeshView;
  37 import com.sun.prism.RTTexture;
  38 import com.sun.prism.RenderTarget;
  39 import com.sun.prism.Texture;
  40 import com.sun.prism.impl.PrismSettings;
  41 import com.sun.prism.impl.VertexBuffer;
  42 import com.sun.prism.impl.ps.BaseShaderContext;
  43 import com.sun.prism.ps.Shader;
  44 
  45 class D3DContext extends BaseShaderContext {
  46 
  47     public static final int D3DERR_DEVICENOTRESET   = 0x88760869;
  48     public static final int D3DERR_DEVICELOST       = 0x88760868;
  49     public static final int E_FAIL                  = 0x80004005;
  50     public static final int D3DERR_OUTOFVIDEOMEMORY = 0x8876017c;
  51     public static final int D3D_OK                  = 0x0;
  52 
  53     public static final int D3DCOMPMODE_CLEAR           = 0;
  54     public static final int D3DCOMPMODE_SRC             = 1;
  55     public static final int D3DCOMPMODE_SRCOVER         = 2;


  68     public final static int CULL_NONE                  = 112;
  69     /**
  70      * WIN32 COM bool FAILED(HRESULT hr) macro synonym
  71      * @param hr
  72      * @return
  73      */
  74     public static boolean FAILED(int hr) {
  75         return hr<0;
  76     }
  77 
  78     // Temp. variables (Not Thread Safe)
  79     private static GeneralTransform3D tempTx = new GeneralTransform3D();
  80     private static Vec3d tempVec3d = new Vec3d();
  81     private static double[] tempAdjustClipSpaceMat = new double[16];
  82 
  83     private State state;
  84     private boolean isLost = false;
  85 
  86     private final long pContext;
  87 
  88     NGCamera camera = null;
  89     private int targetWidth = 0, targetHeight = 0;
  90 
  91     private final D3DResourceFactory factory;
  92 
  93     public static final int NUM_QUADS = PrismSettings.superShader ? 4096 : 256;
  94 
  95     static VertexBuffer createVertexBuffer(long contextHandle) {
  96         return new D3DVertexBuffer(contextHandle, NUM_QUADS);
  97     }
  98 
  99     D3DContext(long pContext, Screen screen, D3DResourceFactory factory) {
 100         super(screen, factory, createVertexBuffer(pContext));
 101         this.pContext = pContext;
 102         this.factory = factory;
 103     }
 104 
 105     @Override
 106     public D3DResourceFactory getResourceFactory() {
 107         return factory;
 108     }


 198         m[9] = (m[9] + m[13])/2;
 199         m[10] = (m[10] + m[14])/2;
 200         m[11] = (m[11] + m[15])/2; 
 201         projViewTx.set(m);
 202         return projViewTx;
 203     }
 204 
 205     @Override
 206     protected State updateRenderTarget(RenderTarget target, NGCamera camera,
 207                                        boolean depthTest)  {
 208         long resourceHandle = ((D3DRenderTarget)target).getResourceHandle();
 209         int res = nSetRenderTarget(pContext, resourceHandle, depthTest, target.isMSAA());
 210         validate(res);
 211         // resetLastClip should be called only if render target was changed
 212         // return value is S_FALSE (success with negative result)
 213         // if render target wasn't changed
 214         if (res == D3D_OK) {
 215             resetLastClip(state);
 216         }
 217 
 218         this.camera = camera;
 219         targetWidth = target.getPhysicalWidth();
 220         targetHeight = target.getPhysicalHeight();
 221 
 222         // Need to validate the camera before getting its computed data.
 223         if (camera instanceof NGDefaultCamera) {
 224             ((NGDefaultCamera) camera).validate(targetWidth, targetHeight);
 225             tempTx = adjustClipSpace(camera.getProjViewTx(tempTx));
 226         } else {
 227             tempTx = adjustClipSpace(camera.getProjViewTx(tempTx));
 228             // TODO: verify that this is the right solution. There may be
 229             // other use-cases where rendering needs different viewport size.
 230             double vw = camera.getViewWidth();
 231             double vh = camera.getViewHeight();
 232             if (targetWidth != vw || targetHeight != vh) {
 233                 tempTx.scale(vw / targetWidth, vh / targetHeight, 1.0);
 234             }
 235         }
 236 
 237         // Set projection view matrix
 238         res = nSetProjViewMatrix(pContext, depthTest,
 239             tempTx.get(0),  tempTx.get(1),  tempTx.get(2),  tempTx.get(3),
 240             tempTx.get(4),  tempTx.get(5),  tempTx.get(6),  tempTx.get(7),
 241             tempTx.get(8),  tempTx.get(9),  tempTx.get(10), tempTx.get(11),
 242             tempTx.get(12), tempTx.get(13), tempTx.get(14), tempTx.get(15));
 243         validate(res);
 244 
 245         tempVec3d = camera.getPositionInWorld(tempVec3d);
 246 //        System.err.println("Camera position in world = " + tempVec3d);
 247         res = nSetCameraPosition(pContext, tempVec3d.x, tempVec3d.y, tempVec3d.z);
 248 
 249         return state;
 250     }
 251 
 252     @Override
 253     protected void updateTexture(int texUnit, Texture tex) {
 254         long texHandle;
 255         boolean linear;
 256         int wrapMode;
 257         if (tex != null) {
 258             D3DTexture d3dtex = (D3DTexture)tex;
 259             texHandle = d3dtex.getNativeSourceHandle();
 260             linear = tex.getLinearFiltering();
 261             switch (tex.getWrapMode()) {
 262                 case CLAMP_NOT_NEEDED:
 263                     wrapMode = D3DTADDRESS_NOP;
 264                     break;
 265                 case CLAMP_TO_EDGE:
 266                 case CLAMP_TO_EDGE_SIMULATED:
 267                 case CLAMP_TO_ZERO_SIMULATED:


 527         }
 528         nSetCullingMode(pContext, nativeMeshView, cm);
 529     }
 530 
 531     void setMaterial(long nativeMeshView, long nativePhongMaterial) {
 532         nSetMaterial(pContext, nativeMeshView, nativePhongMaterial);
 533     }
 534 
 535     void setWireframe(long nativeMeshView, boolean wireframe) {
 536          nSetWireframe(pContext, nativeMeshView, wireframe);
 537     }
 538 
 539     void setAmbientLight(long nativeMeshView, float r, float g, float b) {
 540         nSetAmbientLight(pContext, nativeMeshView, r, g, b);
 541     }
 542 
 543     void setPointLight(long nativeMeshView, int index, float x, float y, float z, float r, float g, float b, float w) {
 544         nSetPointLight(pContext, nativeMeshView, index, x, y, z, r, g, b, w);
 545     }
 546 
 547     void renderMeshView(long nativeMeshView, BaseTransform transformNoClone) {
 548         updateWorldTransform(transformNoClone);






 549         nRenderMeshView(pContext, nativeMeshView);
 550     }
 551 
 552     @Override
 553     public void blit(RTTexture srcRTT, RTTexture dstRTT,
 554                      int srcX0, int srcY0, int srcX1, int srcY1,
 555                      int dstX0, int dstY0, int dstX1, int dstY1) {
 556         long dstNativeHandle = dstRTT == null ? 0L : ((D3DTexture)dstRTT).getNativeSourceHandle();
 557         long srcNativeHandle = ((D3DTexture)srcRTT).getNativeSourceHandle();
 558         nBlit(pContext, srcNativeHandle, dstNativeHandle,
 559                           srcX0, srcY0, srcX1, srcY1,
 560                           dstX0, dstY0, dstX1, dstY1);
 561     }
 562 }


  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.prism.d3d;
  27 
  28 import com.sun.glass.ui.Screen;
  29 import com.sun.javafx.geom.Rectangle;
  30 import com.sun.javafx.geom.Vec3d;
  31 import com.sun.javafx.geom.transform.BaseTransform;
  32 import com.sun.javafx.geom.transform.GeneralTransform3D;
  33 import com.sun.javafx.sg.prism.NGCamera;
  34 import com.sun.javafx.sg.prism.NGDefaultCamera;
  35 import com.sun.prism.CompositeMode;
  36 import com.sun.prism.Graphics;
  37 import com.sun.prism.MeshView;
  38 import com.sun.prism.RTTexture;
  39 import com.sun.prism.RenderTarget;
  40 import com.sun.prism.Texture;
  41 import com.sun.prism.impl.PrismSettings;
  42 import com.sun.prism.impl.VertexBuffer;
  43 import com.sun.prism.impl.ps.BaseShaderContext;
  44 import com.sun.prism.ps.Shader;
  45 
  46 class D3DContext extends BaseShaderContext {
  47 
  48     public static final int D3DERR_DEVICENOTRESET   = 0x88760869;
  49     public static final int D3DERR_DEVICELOST       = 0x88760868;
  50     public static final int E_FAIL                  = 0x80004005;
  51     public static final int D3DERR_OUTOFVIDEOMEMORY = 0x8876017c;
  52     public static final int D3D_OK                  = 0x0;
  53 
  54     public static final int D3DCOMPMODE_CLEAR           = 0;
  55     public static final int D3DCOMPMODE_SRC             = 1;
  56     public static final int D3DCOMPMODE_SRCOVER         = 2;


  69     public final static int CULL_NONE                  = 112;
  70     /**
  71      * WIN32 COM bool FAILED(HRESULT hr) macro synonym
  72      * @param hr
  73      * @return
  74      */
  75     public static boolean FAILED(int hr) {
  76         return hr<0;
  77     }
  78 
  79     // Temp. variables (Not Thread Safe)
  80     private static GeneralTransform3D tempTx = new GeneralTransform3D();
  81     private static Vec3d tempVec3d = new Vec3d();
  82     private static double[] tempAdjustClipSpaceMat = new double[16];
  83 
  84     private State state;
  85     private boolean isLost = false;
  86 
  87     private final long pContext;
  88 
  89     private Vec3d cameraPos = new Vec3d();
  90     private int targetWidth = 0, targetHeight = 0;
  91 
  92     private final D3DResourceFactory factory;
  93 
  94     public static final int NUM_QUADS = PrismSettings.superShader ? 4096 : 256;
  95 
  96     static VertexBuffer createVertexBuffer(long contextHandle) {
  97         return new D3DVertexBuffer(contextHandle, NUM_QUADS);
  98     }
  99 
 100     D3DContext(long pContext, Screen screen, D3DResourceFactory factory) {
 101         super(screen, factory, createVertexBuffer(pContext));
 102         this.pContext = pContext;
 103         this.factory = factory;
 104     }
 105 
 106     @Override
 107     public D3DResourceFactory getResourceFactory() {
 108         return factory;
 109     }


 199         m[9] = (m[9] + m[13])/2;
 200         m[10] = (m[10] + m[14])/2;
 201         m[11] = (m[11] + m[15])/2; 
 202         projViewTx.set(m);
 203         return projViewTx;
 204     }
 205 
 206     @Override
 207     protected State updateRenderTarget(RenderTarget target, NGCamera camera,
 208                                        boolean depthTest)  {
 209         long resourceHandle = ((D3DRenderTarget)target).getResourceHandle();
 210         int res = nSetRenderTarget(pContext, resourceHandle, depthTest, target.isMSAA());
 211         validate(res);
 212         // resetLastClip should be called only if render target was changed
 213         // return value is S_FALSE (success with negative result)
 214         // if render target wasn't changed
 215         if (res == D3D_OK) {
 216             resetLastClip(state);
 217         }
 218 

 219         targetWidth = target.getPhysicalWidth();
 220         targetHeight = target.getPhysicalHeight();
 221 
 222         // Need to validate the camera before getting its computed data.
 223         if (camera instanceof NGDefaultCamera) {
 224             ((NGDefaultCamera) camera).validate(targetWidth, targetHeight);
 225             tempTx = adjustClipSpace(camera.getProjViewTx(tempTx));
 226         } else {
 227             tempTx = adjustClipSpace(camera.getProjViewTx(tempTx));
 228             // TODO: verify that this is the right solution. There may be
 229             // other use-cases where rendering needs different viewport size.
 230             double vw = camera.getViewWidth();
 231             double vh = camera.getViewHeight();
 232             if (targetWidth != vw || targetHeight != vh) {
 233                 tempTx.scale(vw / targetWidth, vh / targetHeight, 1.0);
 234             }
 235         }
 236 
 237         // Set projection view matrix
 238         res = nSetProjViewMatrix(pContext, depthTest,
 239             tempTx.get(0),  tempTx.get(1),  tempTx.get(2),  tempTx.get(3),
 240             tempTx.get(4),  tempTx.get(5),  tempTx.get(6),  tempTx.get(7),
 241             tempTx.get(8),  tempTx.get(9),  tempTx.get(10), tempTx.get(11),
 242             tempTx.get(12), tempTx.get(13), tempTx.get(14), tempTx.get(15));
 243         validate(res);
 244 
 245         // update camera position; this will be uploaded to the shader
 246         // when we switch to 3D state       
 247         cameraPos = camera.getPositionInWorld(cameraPos);
 248 
 249         return state;
 250     }
 251 
 252     @Override
 253     protected void updateTexture(int texUnit, Texture tex) {
 254         long texHandle;
 255         boolean linear;
 256         int wrapMode;
 257         if (tex != null) {
 258             D3DTexture d3dtex = (D3DTexture)tex;
 259             texHandle = d3dtex.getNativeSourceHandle();
 260             linear = tex.getLinearFiltering();
 261             switch (tex.getWrapMode()) {
 262                 case CLAMP_NOT_NEEDED:
 263                     wrapMode = D3DTADDRESS_NOP;
 264                     break;
 265                 case CLAMP_TO_EDGE:
 266                 case CLAMP_TO_EDGE_SIMULATED:
 267                 case CLAMP_TO_ZERO_SIMULATED:


 527         }
 528         nSetCullingMode(pContext, nativeMeshView, cm);
 529     }
 530 
 531     void setMaterial(long nativeMeshView, long nativePhongMaterial) {
 532         nSetMaterial(pContext, nativeMeshView, nativePhongMaterial);
 533     }
 534 
 535     void setWireframe(long nativeMeshView, boolean wireframe) {
 536          nSetWireframe(pContext, nativeMeshView, wireframe);
 537     }
 538 
 539     void setAmbientLight(long nativeMeshView, float r, float g, float b) {
 540         nSetAmbientLight(pContext, nativeMeshView, r, g, b);
 541     }
 542 
 543     void setPointLight(long nativeMeshView, int index, float x, float y, float z, float r, float g, float b, float w) {
 544         nSetPointLight(pContext, nativeMeshView, index, x, y, z, r, g, b, w);
 545     }
 546 
 547     void renderMeshView(long nativeMeshView, Graphics g) {
 548         updateWorldTransform(g.getTransformNoClone());
 549         float pixelScaleFactor = g.getPixelScaleFactor();
 550         int res = nSetCameraPosition(pContext,
 551                 cameraPos.x * pixelScaleFactor,
 552                 cameraPos.y * pixelScaleFactor,
 553                 cameraPos.z * pixelScaleFactor);
 554         validate(res);
 555         nRenderMeshView(pContext, nativeMeshView);
 556     }
 557 
 558     @Override
 559     public void blit(RTTexture srcRTT, RTTexture dstRTT,
 560                      int srcX0, int srcY0, int srcX1, int srcY1,
 561                      int dstX0, int dstY0, int dstX1, int dstY1) {
 562         long dstNativeHandle = dstRTT == null ? 0L : ((D3DTexture)dstRTT).getNativeSourceHandle();
 563         long srcNativeHandle = ((D3DTexture)srcRTT).getNativeSourceHandle();
 564         nBlit(pContext, srcNativeHandle, dstNativeHandle,
 565                           srcX0, srcY0, srcX1, srcY1,
 566                           dstX0, dstY0, dstX1, dstY1);
 567     }
 568 }