Пример #1
0
        public override void Transfer(GpuStateStruct gpuState)
        {
            Console.WriteLine("Transfer Not Implemented");
            //return;
            var textureTransferState = gpuState.TextureTransferState;

            if (
                (textureTransferState.DestinationAddress.Address == gpuState.DrawBufferState.Address) &&
                (textureTransferState.DestinationLineWidth == gpuState.DrawBufferState.Width) &&
                (textureTransferState.BytesPerPixel == gpuState.DrawBufferState.BytesPerPixel)
                )
            {
                //Console.Error.WriteLine("Writting to DrawBuffer");
                TransferToFrameBuffer(gpuState);
            }
            else
            {
                Console.Error.WriteLine("NOT Writting to DrawBuffer");
                TransferGeneric(gpuState);

                /*
                 * base.Transfer(GpuStateStruct);
                 * PrepareWrite(GpuStateStruct);
                 * {
                 *
                 * }
                 * PrepareRead(GpuStateStruct);
                 */
            }
        }
Пример #2
0
        private void PrepareState_Stencil(GpuStateStruct gpuState)
        {
            if (!GL.EnableDisable(GL.GL_STENCIL_TEST, gpuState.StencilState.Enabled))
            {
                return;
            }

            //Console.Error.WriteLine("aaaaaa!");

            //if (state.stencilFuncFunc == 2) { outputDepthAndStencil(); assert(0); }

#if false
            Console.Error.WriteLine(
                "{0}:{1}:{2} - {3}, {4}, {5}",
                StencilFunctionTranslate[(int)GpuState->StencilState.Function],
                GpuState->StencilState.FunctionRef,
                GpuState->StencilState.FunctionMask,
                StencilOperationTranslate[(int)GpuState->StencilState.OperationFail],
                StencilOperationTranslate[(int)GpuState->StencilState.OperationZFail],
                StencilOperationTranslate[(int)GpuState->StencilState.OperationZPass]
                );
#endif
            GL.glStencilFunc(
                OpenglGpuImplConversionTables.StencilFunctionTranslate[(int)gpuState.StencilState.Function],
                gpuState.StencilState.FunctionRef,
                gpuState.StencilState.FunctionMask
                );

            GL.glStencilOp(
                OpenglGpuImplConversionTables.StencilOperationTranslate[(int)gpuState.StencilState.OperationFail],
                OpenglGpuImplConversionTables.StencilOperationTranslate[(int)gpuState.StencilState.OperationZFail],
                OpenglGpuImplConversionTables.StencilOperationTranslate[(int)gpuState.StencilState.OperationZPass]
                );
        }
Пример #3
0
 private void TransferToFrameBuffer(GpuStateStruct gpuState)
 {
     Console.WriteLine("TransferToFrameBuffer Not Implemented");
     //var TextureTransferState = GpuState->TextureTransferState;
     //
     //var GlPixelFormat = GlPixelFormatList[(int)GpuState->DrawBufferState.Format];
     //
     //GL.PixelZoom(1, -1);
     //GL.WindowPos2(TextureTransferState.DestinationX, 272 - TextureTransferState.DestinationY);
     ////GL.PixelZoom(1, -1);
     ////GL.PixelZoom(1, 1);
     //GL.PixelStore(PixelStoreParameter.UnpackAlignment, TextureTransferState.BytesPerPixel);
     //GL.PixelStore(PixelStoreParameter.UnpackRowLength, TextureTransferState.SourceLineWidth);
     //GL.PixelStore(PixelStoreParameter.UnpackSkipPixels, TextureTransferState.SourceX);
     //GL.PixelStore(PixelStoreParameter.UnpackSkipRows, TextureTransferState.SourceY);
     //
     //{
     //	GL.DrawPixels(
     //		TextureTransferState.Width,
     //		TextureTransferState.Height,
     //		PixelFormat.Rgba,
     //		GlPixelFormat.OpenglPixelType,
     //		new IntPtr(Memory.PspAddressToPointerSafe(
     //			TextureTransferState.SourceAddress,
     //			TextureTransferState.Width * TextureTransferState.Height * 4
     //		))
     //	);
     //}
     //
     //GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
     //GL.PixelStore(PixelStoreParameter.UnpackRowLength, 0);
     //GL.PixelStore(PixelStoreParameter.UnpackSkipPixels, 0);
     //GL.PixelStore(PixelStoreParameter.UnpackSkipRows, 0);
 }
Пример #4
0
        public override void PrimStart(GlobalGpuState globalGpuState, GpuStateStruct gpuState,
                                       GuPrimitiveType primitiveType)
        {
            GpuState       = gpuState;
            _primitiveType = primitiveType;
            _doPrimStart   = true;
            ResetVertex();


            if (_shader != null)
            {
                _shader.GetUniform("lopEnabled").Set(gpuState.LogicalOperationState.Enabled);

                if (gpuState.LogicalOperationState.Enabled)
                {
                    if (_logicOpsRenderTarget == null)
                    {
                        _logicOpsRenderTarget = GLRenderTarget.Create(512, 272, RenderTargetLayers.Color);
                    }
                    GLRenderTarget.CopyFromTo(GLRenderTarget.Current, _logicOpsRenderTarget);
                    _shader.GetUniform("backtex").Set(GLTextureUnit.CreateAtIndex(1).SetFiltering(GLScaleFilter.Linear)
                                                      .SetWrap(GLWrap.ClampToEdge).SetTexture(_logicOpsRenderTarget.TextureColor));

                    _shader.GetUniform("lop").Set((int)gpuState.LogicalOperationState.Operation);

                    //new Bitmap(512, 272).SetChannelsDataInterleaved(LogicOpsRenderTarget.ReadPixels(), BitmapChannelList.RGBA).Save(@"c:\temp\test.png");
                }
            }
        }
Пример #5
0
        private void PrepareStateDraw(GpuStateStruct gpuState)
        {
            GL.glColorMask(true, true, true, true);

#if ENABLE_TEXTURES
            PrepareState_Texture_Common(gpuState);
#endif
            PrepareState_Blend(gpuState);
            PrepareState_Clip(gpuState);

            if (gpuState.VertexState.Type.Transform2D)
            {
                PrepareState_Colors_2D(gpuState);
                GL.glDisable(GL.GL_STENCIL_TEST);
                GL.glDisable(GL.GL_CULL_FACE);
                GL.DepthRange(0, 1);
                GL.glDisable(GL.GL_DEPTH_TEST);
                //GL.glDisable(EnableCap.Lighting);
            }
            else
            {
                PrepareState_Colors_3D(gpuState);
                PrepareState_CullFace(gpuState);
                PrepareState_Lighting(gpuState);
                PrepareState_Depth(gpuState);
                PrepareState_DepthTest(gpuState);
                PrepareState_Stencil(gpuState);
            }
            //GL.ShadeModel((GpuState->ShadeModel == ShadingModelEnum.Flat) ? ShadingModel.Flat : ShadingModel.Smooth);
            PrepareState_AlphaTest(gpuState);
        }
Пример #6
0
        private void PrepareState_Texture_Common(GpuStateStruct gpuState)
        {
            var textureMappingState = gpuState.TextureMappingState;
            //var ClutState = &TextureMappingState->ClutState;
            var textureState = textureMappingState.TextureState;

            if (!GL.EnableDisable(GL.GL_TEXTURE_2D, textureMappingState.Enabled))
            {
                return;
            }

            if (VertexType.Transform2D)
            {
                PrepareState_Texture_2D(gpuState);
            }
            else
            {
                PrepareState_Texture_3D(gpuState);
            }

            //GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
            //glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

            RenderbufferManager.TextureCacheGetAndBind(gpuState);
            //CurrentTexture.Save("test.png");

            //GL.glTexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)TextureEnvModeTranslate[(int)TextureState->Effect]);
        }
Пример #7
0
        internal static void PrepareStateClear(GpuStateStruct gpuState)
        {
            bool ccolorMask = false, calphaMask = false;

            //return;

            GL.glDisable(GL.GL_BLEND);
            //GL.glDisable(GL.GL_LIGHTING);
            GL.glDisable(GL.GL_TEXTURE_2D);
            //GL.glDisable(GL.GL_ALPHA_TEST);
            GL.glDisable(GL.GL_DEPTH_TEST);
            GL.glDisable(GL.GL_STENCIL_TEST);
            //GL.glDisable(GL.GL_FOG);
            //GL.glDisable(GL.GL_LOGIC_OP);
            GL.glDisable(GL.GL_CULL_FACE);
            GL.glDepthMask(false);

            if (gpuState.ClearFlags.HasFlag(ClearBufferSet.ColorBuffer))
            {
                ccolorMask = true;
            }

            if (GL.EnableDisable(GL.GL_STENCIL_TEST, gpuState.ClearFlags.HasFlag(ClearBufferSet.StencilBuffer)))
            {
                calphaMask = true;
                // Sets to 0x00 the stencil.
                // @TODO @FIXME! : Color should be extracted from the color! (as alpha component)
                GL.glStencilFunc(GL.GL_ALWAYS, 0x00, 0xFF);
                GL.glStencilOp(GL.GL_REPLACE, GL.GL_REPLACE, GL.GL_REPLACE);
                //Console.Error.WriteLine("Stencil!");
                //GL.Enable(EnableCap.DepthTest);
            }

            //int i; glGetIntegerv(GL_STENCIL_BITS, &i); writefln("GL_STENCIL_BITS: %d", i);

            if (gpuState.ClearFlags.HasFlag(ClearBufferSet.DepthBuffer))
            {
                GL.glEnable(GL.GL_DEPTH_TEST);
                GL.glDepthFunc(GL.GL_ALWAYS);
                GL.glDepthMask(true);

                GL.DepthRange(0, 0);

                //glDepthRange(0.0, 1.0); // Original value
            }

            GL.glColorMask(ccolorMask, ccolorMask, ccolorMask, calphaMask);

            //glClearDepth(0.0); glClear(GL_COLOR_BUFFER_BIT);

            //if (state.clearFlags & ClearBufferMask.GU_COLOR_BUFFER_BIT) glClear(GL_DEPTH_BUFFER_BIT);
            //GL.Clear(ClearBufferMask.StencilBufferBit);
        }
Пример #8
0
 private void PrepareState_AlphaTest(GpuStateStruct gpuState)
 {
     //if (!GL.EnableDisable(EnableCap.AlphaTest, GpuState->AlphaTestState.Enabled))
     //{
     //	return;
     //}
     //
     //GL.glAlphaFunc(
     //	(AlphaFunction)DepthFunctionTranslate[(int)GpuState->AlphaTestState.Function],
     //	GpuState->AlphaTestState.Value
     //);
 }
Пример #9
0
        public static void PrepareStateCommon(GpuStateStruct gpuState, int scaleViewport)
        {
            // ReSharper disable once UnusedVariable
            var viewport = gpuState.Viewport;
            //ViewportStruct(
            //  Position=Vector3f(X=2048,Y=2048,Z=0.9999847),
            //  Scale=Vector3f(X=480,Y=-272,Z=-32768),
            //  RegionTopLeft=PointS(X=0,Y=0),
            //  RegionBottomRight=PointS(X=479,Y=271)
            //)
            //ViewportStruct(
            //  RegionSize=PointS(X=384,Y=240),
            //  Position=Vector3f(X=2048,Y=2048,Z=0),
            //  Scale=Vector3f(X=480,Y=-272,Z=0),
            //  RegionTopLeft=PointS(X=0,Y=0),
            //  RegionBottomRight=PointS(X=383,Y=239)
            //)
            //Console.Error.WriteLine(Viewport.ToString());

            //GL.Hint(HintTarget.PolygonSmoothHint, HintMode.Fastest);
            //GL.Hint(HintTarget.LineSmoothHint, HintMode.Fastest);
            //GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Fastest);
            //GL.Hint(HintTarget.PointSmoothHint, HintMode.Fastest);

            /*
             *              int halfHeight = Math.abs(context.viewport_height);
             *              int halfWidth = Math.abs(context.viewport_width);
             *              int viewportX = context.viewport_cx - halfWidth - context.offset_x;
             *              int viewportY = context.viewport_cy - halfHeight - context.offset_y;
             *              int viewportWidth = 2 * halfWidth;
             *              int viewportHeight = 2 * halfHeight;
             *
             *              // For OpenGL, translate the viewportY from the upper left corner
             *              // to the lower left corner.
             *              viewportY = Screen.height - viewportY - viewportHeight;
             *
             *              re.setViewport(viewportX, viewportY, viewportWidth, viewportHeight);
             */

            //int ScreenWidth = 480;
            //int ScreenHeight = 272;
            //
            //int ScaledWidth = (int)(((double)ScreenWidth / (double)Viewport.RegionSize.X) * (double)ScreenWidth);
            //int ScaledHeight = (int)(((double)ScreenHeight / (double)Viewport.RegionSize.Y) * (double)ScreenHeight);
            //
            //GL.glViewport(
            //	(int)Viewport.RegionTopLeft.X * ScaleViewport,
            //	(int)Viewport.RegionTopLeft.Y * ScaleViewport,
            //	ScaledWidth * ScaleViewport,
            //	ScaledHeight * ScaleViewport
            //);
        }
Пример #10
0
        private void PrepareState_CullFace(GpuStateStruct gpuState)
        {
            if (!GL.EnableDisable(GL.GL_CULL_FACE, gpuState.BackfaceCullingState.Enabled))
            {
                return;
            }

            //GL.EnableDisable(EnableCap.CullFace, false);

            GL.glCullFace((gpuState.BackfaceCullingState.FrontFaceDirection == FrontFaceDirectionEnum.ClockWise)
                ? GL.GL_FRONT
                : GL.GL_BACK);
        }
Пример #11
0
 private void PrepareState_DepthTest(GpuStateStruct gpuState)
 {
     if (gpuState.DepthTestState.Mask != 0 && gpuState.DepthTestState.Mask != 1)
     {
         Console.Error.WriteLine("WARNING! DepthTestState.Mask: {0}", gpuState.DepthTestState.Mask);
     }
     GL.glDepthMask(gpuState.DepthTestState.Mask == 0);
     if (!GL.EnableDisable(GL.GL_DEPTH_TEST, gpuState.DepthTestState.Enabled))
     {
         return;
     }
     GL.glDepthFunc(
         OpenglGpuImplConversionTables.DepthFunctionTranslate[(int)gpuState.DepthTestState.Function]);
 }
Пример #12
0
 private void PrepareState_Lighting(GpuStateStruct gpuState)
 {
     //var LightingState = &GpuState->LightingState;
     //
     //if (!GL.EnableDisable(EnableCap.Lighting, LightingState->Enabled))
     //{
     //	return;
     //}
     //
     //GL.LightModel(
     //	LightModelParameter.LightModelColorControl,
     //	(int)((LightingState->LightModel == LightModelEnum.SeparateSpecularColor) ? LightModelColorControl.SeparateSpecularColor : LightModelColorControl.SingleColor)
     //);
     //GL.LightModel(LightModelParameter.LightModelAmbient, &LightingState->AmbientLightColor.Red);
     //
     //for (int n = 0; n < 4; n++)
     //{
     //	var LightState = &(&LightingState->Light0)[n];
     //	LightName LightName = (LightName)(LightName.Light0 + n);
     //
     //	if (!GL.EnableDisable((EnableCap)(EnableCap.Light0 + n), LightState->Enabled))
     //	{
     //		continue;
     //	}
     //
     //	GL.Light(LightName, LightParameter.Specular, &LightState->SpecularColor.Red);
     //	GL.Light(LightName, LightParameter.Ambient, &LightState->AmbientColor.Red);
     //	GL.Light(LightName, LightParameter.Diffuse, &LightState->DiffuseColor.Red);
     //
     //	LightState->Position.W = 1.0f;
     //	GL.Light(LightName, LightParameter.Position, &LightState->Position.X);
     //
     //	GL.Light(LightName, LightParameter.ConstantAttenuation, &LightState->Attenuation.Constant);
     //	GL.Light(LightName, LightParameter.LinearAttenuation, &LightState->Attenuation.Linear);
     //	GL.Light(LightName, LightParameter.QuadraticAttenuation, &LightState->Attenuation.Quadratic);
     //
     //	if (LightState->Type == LightTypeEnum.SpotLight)
     //	{
     //		GL.Light(LightName, LightParameter.SpotDirection, &LightState->SpotDirection.X);
     //		GL.Light(LightName, LightParameter.SpotExponent, &LightState->SpotExponent);
     //		GL.Light(LightName, LightParameter.SpotCutoff, &LightState->SpotCutoff);
     //	}
     //	else
     //	{
     //		GL.Light(LightName, LightParameter.SpotExponent, 0);
     //		GL.Light(LightName, LightParameter.SpotCutoff, 180);
     //	}
     //}
 }
Пример #13
0
        private void PrepareState_Clip(GpuStateStruct gpuState)
        {
            if (!GL.EnableDisable(GL.GL_SCISSOR_TEST, gpuState.ClipPlaneState.Enabled))
            {
                return;
            }
            var scissor = gpuState.ClipPlaneState.Scissor;

            GL.glScissor(
                scissor.Left * ScaleViewport,
                scissor.Top * ScaleViewport,
                scissor.Width * ScaleViewport,
                scissor.Height * ScaleViewport
                );
        }
Пример #14
0
 internal static void PrepareStateMatrix(GpuStateStruct gpuState, out Matrix4x4 worldViewProjectionMatrix)
 {
     // DRAW BEGIN COMMON
     {
         if (gpuState.VertexState.Type.Transform2D)
         //if (true)
         {
             worldViewProjectionMatrix = Matrix4x4.CreateOrthographic(512, 272, 0, -0xFFFF);
             //WorldViewProjectionMatrix = Matrix4f.Ortho(0, 480, 272, 0, 0, -0xFFFF);
         }
         else
         {
             worldViewProjectionMatrix =
                 gpuState.VertexState.WorldMatrix * gpuState.VertexState.ViewMatrix *
                 gpuState.VertexState.ProjectionMatrix;
         }
     }
 }
Пример #15
0
        public void BindCurrentDrawBufferTexture(GpuStateStruct gpuState)
        {
            if (_cachedBindAddress != gpuState.DrawBufferState.Address)
            {
                GL.glFlush();
                GL.glFinish();

                _cachedBindAddress = gpuState.DrawBufferState.Address;
                var key = new DrawBufferKey()
                {
                    Address = gpuState.DrawBufferState.Address,
                    //Width = (int)GpuState->DrawBufferState.Width,
                    //Height = (int)272,
                };
                CurrentDrawBuffer?.Unbind();
                CurrentDrawBuffer = GetOrCreateDrawBufferTexture(key);
                CurrentDrawBuffer.Bind();
            }
        }
Пример #16
0
        private void PrepareState_Texture_3D(GpuStateStruct gpuState)
        {
            var textureMappingState = gpuState.TextureMappingState;
            var textureState        = textureMappingState.TextureState;

            if (textureMappingState.Enabled)
            {
                _textureMatrix = Matrix4x4.Identity;

                switch (textureMappingState.TextureMapMode)
                {
                case TextureMapMode.GuTextureCoords:

                    _textureMatrix = _textureMatrix *
                                     Matrix4x4.CreateTranslation(textureState.OffsetU, textureState.OffsetV, 0) *
                                     Matrix4x4.CreateScale(textureState.ScaleU, textureState.ScaleV, 1);
                    break;

                case TextureMapMode.GuTextureMatrix:
                    switch (gpuState.TextureMappingState.TextureProjectionMapMode)
                    {
                    default:
                        Console.Error.WriteLine("NotImplemented: GU_TEXTURE_MATRIX: {0}",
                                                gpuState.TextureMappingState.TextureProjectionMapMode);
                        break;
                    }
                    break;

                case TextureMapMode.GuEnvironmentMap:
                    Console.Error.WriteLine("NotImplemented: GU_ENVIRONMENT_MAP");
                    break;

                default:
                    Console.Error.WriteLine("NotImplemented TextureMappingState->TextureMapMode: " +
                                            textureMappingState.TextureMapMode);
                    break;
                }
            }
        }
Пример #17
0
        public GLTexture TextureCacheGetAndBind(GpuStateStruct gpuState)
        {
            if (DynarecConfig.EnableRenderTarget)
            {
                var textureMappingState = gpuState.TextureMappingState;
                var clutState           = textureMappingState.ClutState;
                var textureState        = textureMappingState.TextureState;
                var key = new DrawBufferKey()
                {
                    Address = textureState.Mipmap0.Address,
                };

                if (_drawBufferTextures.ContainsKey(key))
                {
                    return(GetOrCreateDrawBufferTexture(key).RenderTarget.TextureColor.Bind());
                }
            }

            var currentTexture = OpenglGpuImpl.TextureCache.Get(gpuState);

            currentTexture.Bind();
            return(currentTexture.Texture);
        }
Пример #18
0
        private void PrepareState_Texture_2D(GpuStateStruct gpuState)
        {
            var textureMappingState = gpuState.TextureMappingState;
            var mipmap0             = textureMappingState.TextureState.Mipmap0;

            if (textureMappingState.Enabled)
            {
                _textureMatrix = Matrix4x4.CreateScale(
                    1.0f / mipmap0.BufferWidth,
                    1.0f / mipmap0.TextureHeight,
                    1.0f
                    )
                ;
                //GL.ActiveTexture(TextureUnit.Texture0);
                //GL.MatrixMode(MatrixMode.Texture);
                //GL.LoadIdentity();
                //
                //GL.Scale(
                //	1.0f / Mipmap0->BufferWidth,
                //	1.0f / Mipmap0->TextureHeight,
                //	1.0f
                //);
            }
        }
Пример #19
0
 public override void TextureSync(GpuStateStruct gpuState)
 {
 }
Пример #20
0
 public override void TextureFlush(GpuStateStruct gpuState)
 {
     TextureCache.RecheckAll();
 }
Пример #21
0
 public override void Sync(GpuStateStruct gpuState)
 {
 }
Пример #22
0
 public override void End(GpuStateStruct gpuState)
 {
     //PrepareWrite(GpuState);
 }
Пример #23
0
 public override void Finish(GpuStateStruct gpuState)
 {
 }
Пример #24
0
 public override void BeforeDraw(GpuStateStruct gpuState)
 {
     RenderbufferManager.BindCurrentDrawBufferTexture(gpuState);
 }
Пример #25
0
        private void TransferGeneric(GpuStateStruct gpuState)
        {
            Console.WriteLine("TransferGeneric Not Implemented");
            var textureTransferState = gpuState.TextureTransferState;

            var sourceX       = textureTransferState.SourceX;
            var sourceY       = textureTransferState.SourceY;
            var destinationX  = textureTransferState.DestinationX;
            var destinationY  = textureTransferState.DestinationY;
            var bytesPerPixel = textureTransferState.BytesPerPixel;

            var sourceTotalBytes      = textureTransferState.SourceLineWidth * textureTransferState.Height * bytesPerPixel;
            var destinationTotalBytes =
                textureTransferState.DestinationLineWidth * textureTransferState.Height * bytesPerPixel;

            var sourcePointer =
                (byte *)Memory.PspAddressToPointerSafe(textureTransferState.SourceAddress.Address, sourceTotalBytes);
            var destinationPointer =
                (byte *)Memory.PspAddressToPointerSafe(textureTransferState.DestinationAddress.Address,
                                                       destinationTotalBytes);

            for (uint y = 0; y < textureTransferState.Height; y++)
            {
                var rowSourceOffset = (uint)(
                    (textureTransferState.SourceLineWidth * (y + sourceY)) + sourceX
                    );
                var rowDestinationOffset = (uint)(
                    (textureTransferState.DestinationLineWidth * (y + destinationY)) + destinationX
                    );
                PointerUtils.Memcpy(
                    destinationPointer + rowDestinationOffset * bytesPerPixel,
                    sourcePointer + rowSourceOffset * bytesPerPixel,
                    textureTransferState.Width * bytesPerPixel
                    );
            }

            /*
             * // Generic implementation.
             * with (gpu.state.textureTransfer) {
             *  auto srcAddressHost = cast(ubyte*)gpu.memory.getPointer(srcAddress);
             *  auto dstAddressHost = cast(ubyte*)gpu.memory.getPointer(dstAddress);
             *
             *  if (gpu.state.drawBuffer.isAnyAddressInBuffer([srcAddress, dstAddress])) {
             *      gpu.performBufferOp(BufferOperation.STORE, BufferType.COLOR);
             *  }
             *
             *  for (int n = 0; n < height; n++) {
             *      int srcOffset = ((n + srcY) * srcLineWidth + srcX) * bpp;
             *      int dstOffset = ((n + dstY) * dstLineWidth + dstX) * bpp;
             *      (dstAddressHost + dstOffset)[0.. width * bpp] = (srcAddressHost + srcOffset)[0.. width * bpp];
             *      //writefln("%08X <- %08X :: [%d]", dstOffset, srcOffset, width * bpp);
             *  }
             *  //std.file.write("buffer", dstAddressHost[0..512 * 272 * 4]);
             *
             *  if (gpu.state.drawBuffer.isAnyAddressInBuffer([dstAddress])) {
             *      //gpu.impl.test();
             *      //gpu.impl.test("trxkick");
             *      gpu.markBufferOp(BufferOperation.LOAD, BufferType.COLOR);
             *  }
             *  //gpu.impl.test();
             * }
             */
        }
Пример #26
0
        public TTexture Get(GpuStateStruct GpuState)
        {
            var TextureMappingState = GpuState.TextureMappingState;
            var ClutState           = TextureMappingState.ClutState;
            var TextureState        = TextureMappingState.TextureState;

            TTexture Texture;
            //GC.Collect();
            bool Swizzled       = TextureState.Swizzled;
            uint TextureAddress = TextureState.Mipmap0.Address;
            uint ClutAddress    = ClutState.Address;
            var  ClutFormat     = ClutState.PixelFormat;
            var  ClutStart      = ClutState.Start;
            var  ClutDataStart  = PixelFormatDecoder.GetPixelsSize(ClutFormat, ClutStart);

            ulong Hash1   = TextureAddress | (ulong)((ClutAddress + ClutDataStart) << 32);
            bool  Recheck = false;

            if (Cache.TryGetValue(Hash1, out Texture))
            {
                if (Texture.RecheckTimestamp != RecheckTimestamp)
                {
                    Recheck = true;
                }
            }
            else
            {
                Recheck = true;
            }

            if (Recheck)
            {
                //Console.Write(".");

                //Console.WriteLine("{0:X}", ClutAddress);

                var TextureFormat = TextureState.PixelFormat;
                //var Width = TextureState->Mipmap0.TextureWidth;

                int BufferWidth = TextureState.Mipmap0.BufferWidth;

                // FAKE!
                //BufferWidth = TextureState->Mipmap0.TextureWidth;

                var Height          = TextureState.Mipmap0.TextureHeight;
                var TextureDataSize = PixelFormatDecoder.GetPixelsSize(TextureFormat, BufferWidth * Height);
                if (ClutState.NumberOfColors > 256)
                {
                    ClutState.NumberOfColors = 256;
                }
                var ClutDataSize = PixelFormatDecoder.GetPixelsSize(ClutFormat, ClutState.NumberOfColors);
                var ClutCount    = ClutState.NumberOfColors;
                var ClutShift    = ClutState.Shift;
                var ClutMask     = ClutState.Mask;

                //Console.WriteLine(TextureFormat);

                // INVALID TEXTURE
                if (!PspMemory.IsRangeValid(TextureAddress, TextureDataSize) || TextureDataSize > 2048 * 2048 * 4)
                {
                    Console.Error.WriteLineColored(ConsoleColor.DarkRed,
                                                   "UPDATE_TEXTURE(TEX={0},CLUT={1}:{2}:{3}:{4}:0x{5:X},SIZE={6}x{7},{8},Swizzled={9})",
                                                   TextureFormat, ClutFormat, ClutCount, ClutStart, ClutShift, ClutMask, BufferWidth, Height,
                                                   BufferWidth, Swizzled);
                    Console.Error.WriteLineColored(ConsoleColor.DarkRed,
                                                   "Invalid TEXTURE! TextureAddress=0x{0:X}, TextureDataSize={1}", TextureAddress,
                                                   TextureDataSize);
                    if (InvalidTexture == null)
                    {
                        InvalidTexture = new TTexture();
                        InvalidTexture.Init(GpuImpl);

                        int InvalidTextureWidth = 2, InvalidTextureHeight = 2;
                        int InvalidTextureSize = InvalidTextureWidth * InvalidTextureHeight;
                        var Data = new OutputPixel[InvalidTextureSize];
                        fixed(OutputPixel *DataPtr = Data)
                        {
                            var Color1 = OutputPixel.FromRgba(0xFF, 0x00, 0x00, 0xFF);
                            var Color2 = OutputPixel.FromRgba(0x00, 0x00, 0xFF, 0xFF);

                            for (int n = 0; n < InvalidTextureSize; n++)
                            {
                                DataPtr[n] = (n & 1) != 0 ? Color1 : Color2;
                            }
                            InvalidTexture.SetData(Data, InvalidTextureWidth, InvalidTextureHeight);
                        }
                    }
                    return(InvalidTexture);
                }

                //Console.WriteLine("TextureAddress=0x{0:X}, TextureDataSize=0x{1:X}", TextureAddress, TextureDataSize);

                byte *TexturePointer = null;
                byte *ClutPointer    = null;

                try
                {
                    TexturePointer = (byte *)PspMemory.PspAddressToPointerSafe(TextureAddress);
                    ClutPointer    = (byte *)PspMemory.PspAddressToPointerSafe(ClutAddress);
                }
                catch (PspMemory.InvalidAddressException InvalidAddressException)
                {
                    throw InvalidAddressException;
                }

                TextureCacheKey TextureCacheKey = new TextureCacheKey()
                {
                    TextureAddress = TextureAddress,
                    TextureFormat  = TextureFormat,
                    TextureHash    = FastHash(TexturePointer, TextureDataSize),

                    ClutHash    = FastHash(&ClutPointer[ClutDataStart], ClutDataSize),
                    ClutAddress = ClutAddress,
                    ClutFormat  = ClutFormat,
                    ClutStart   = ClutStart,
                    ClutShift   = ClutShift,
                    ClutMask    = ClutMask,
                    Swizzled    = Swizzled,

                    ColorTestEnabled  = GpuState.ColorTestState.Enabled,
                    ColorTestRef      = GpuState.ColorTestState.Ref,
                    ColorTestMask     = GpuState.ColorTestState.Mask,
                    ColorTestFunction = GpuState.ColorTestState.Function,
                };

                if (Texture == null || !Texture.TextureCacheKey.Equals(TextureCacheKey))
                {
                    string TextureName = "texture_" + TextureCacheKey.TextureHash + "_" + TextureCacheKey.ClutHash +
                                         "_" + TextureFormat + "_" + ClutFormat + "_" + BufferWidth + "x" + Height +
                                         "_" + Swizzled;
#if DEBUG_TEXTURE_CACHE
                    Console.Error.WriteLine("UPDATE_TEXTURE(TEX={0},CLUT={1}:{2}:{3}:{4}:0x{5:X},SIZE={6}x{7},{8},Swizzled={9})", TextureFormat, ClutFormat, ClutCount, ClutStart, ClutShift, ClutMask, BufferWidth, Height, BufferWidth, Swizzled);
#endif
                    Texture = new TTexture();
                    Texture.Init(GpuImpl);
                    Texture.TextureCacheKey = TextureCacheKey;
                    //Texture.Hash = Hash1;

                    {
                        //int TextureWidth = Math.Max(BufferWidth, Height);
                        //int TextureHeight = Math.Max(BufferWidth, Height);
                        int TextureWidth       = BufferWidth;
                        int TextureHeight      = Height;
                        int TextureWidthHeight = TextureWidth * TextureHeight;

                        fixed(OutputPixel *TexturePixelsPointer = DecodedTextureBuffer)
                        {
                            if (Swizzled)
                            {
                                fixed(byte *SwizzlingBufferPointer = SwizzlingBuffer)
                                {
                                    PointerUtils.Memcpy(SwizzlingBuffer, TexturePointer, TextureDataSize);
                                    PixelFormatDecoder.UnswizzleInline(TextureFormat, (void *)SwizzlingBufferPointer,
                                                                       BufferWidth, Height);
                                    PixelFormatDecoder.Decode(
                                        TextureFormat, (void *)SwizzlingBufferPointer, TexturePixelsPointer,
                                        BufferWidth, Height,
                                        ClutPointer, ClutFormat, ClutCount, ClutStart, ClutShift, ClutMask,
                                        strideWidth: PixelFormatDecoder.GetPixelsSize(TextureFormat, TextureWidth)
                                        );
                                }
                            }
                            else
                            {
                                PixelFormatDecoder.Decode(
                                    TextureFormat, (void *)TexturePointer, TexturePixelsPointer, BufferWidth, Height,
                                    ClutPointer, ClutFormat, ClutCount, ClutStart, ClutShift, ClutMask,
                                    strideWidth: PixelFormatDecoder.GetPixelsSize(TextureFormat, TextureWidth)
                                    );
                            }

                            if (TextureCacheKey.ColorTestEnabled)
                            {
                                byte EqualValue, NotEqualValue;

                                switch (TextureCacheKey.ColorTestFunction)
                                {
                                case ColorTestFunctionEnum.GuAlways:
                                    EqualValue    = 0xFF;
                                    NotEqualValue = 0xFF;
                                    break;

                                case ColorTestFunctionEnum.GuNever:
                                    EqualValue    = 0x00;
                                    NotEqualValue = 0x00;
                                    break;

                                case ColorTestFunctionEnum.GuEqual:
                                    EqualValue    = 0xFF;
                                    NotEqualValue = 0x00;
                                    break;

                                case ColorTestFunctionEnum.GuNotequal:
                                    EqualValue    = 0x00;
                                    NotEqualValue = 0xFF;
                                    break;

                                default: throw new NotImplementedException();
                                }

                                ConsoleUtils.SaveRestoreConsoleState(() =>
                                {
                                    Console.BackgroundColor = ConsoleColor.Red;
                                    Console.ForegroundColor = ConsoleColor.Yellow;
                                    Console.Error.WriteLine("{0} : {1}, {2} : ref:{3} : mask:{4}",
                                                            TextureCacheKey.ColorTestFunction, EqualValue, NotEqualValue,
                                                            TextureCacheKey.ColorTestRef, TextureCacheKey.ColorTestMask);
                                });

                                for (int n = 0; n < TextureWidthHeight; n++)
                                {
                                    if ((TexturePixelsPointer[n] & TextureCacheKey.ColorTestMask).Equals(
                                            TextureCacheKey.ColorTestRef & TextureCacheKey.ColorTestMask))
                                    {
                                        TexturePixelsPointer[n].A = EqualValue;
                                    }
                                    else
                                    {
                                        TexturePixelsPointer[n].A = NotEqualValue;
                                    }
                                    if (TexturePixelsPointer[n].A == 0)
                                    {
                                        //Console.Write("yup!");
                                    }
                                }
                            }

                            var TextureInfo = new TextureHookInfo()
                            {
                                TextureCacheKey = TextureCacheKey,
                                Data            = DecodedTextureBuffer,
                                Width           = TextureWidth,
                                Height          = TextureHeight
                            };

                            MessageBus.Dispatch(TextureInfo);

                            var Result = Texture.SetData(TextureInfo.Data, TextureInfo.Width, TextureInfo.Height);
                        }
                    }
                    if (Cache.ContainsKey(Hash1))
                    {
                        Cache[Hash1].Dispose();
                    }
                    Cache[Hash1] = Texture;
                }
            }

            Texture.RecheckTimestamp = RecheckTimestamp;

            return(Texture);
        }
Пример #27
0
 private void PrepareState_Colors_2D(GpuStateStruct gpuState)
 {
     PrepareState_Colors_3D(gpuState);
 }
Пример #28
0
 private void PrepareState_Depth(GpuStateStruct gpuState)
 {
     GL.DepthRange(gpuState.DepthTestState.RangeNear, gpuState.DepthTestState.RangeFar);
 }
Пример #29
0
 private void PrepareState_Colors_3D(GpuStateStruct gpuState)
 {
     //GL.EnableDisable(EnableCap.ColorMaterial, VertexType.Color != VertexTypeStruct.ColorEnum.Void);
     //
     //var Color = GpuState->LightingState.AmbientModelColor;
     //var LightingState = &GpuState->LightingState;
     //GL.Color4(&Color.Red);
     //
     //if (VertexType.Color != VertexTypeStruct.ColorEnum.Void && LightingState->Enabled)
     //{
     //	var Flags = (ColorMaterialParameter)0;
     //	/*
     //	glMaterialfv(faces, GL_AMBIENT , [0.0f, 0.0f, 0.0f, 0.0f].ptr);
     //	glMaterialfv(faces, GL_DIFFUSE , [0.0f, 0.0f, 0.0f, 0.0f].ptr);
     //	glMaterialfv(faces, GL_SPECULAR, [0.0f, 0.0f, 0.0f, 0.0f].ptr);
     //	*/
     //
     //	var MaterialColorComponents = LightingState->MaterialColorComponents;
     //
     //	if (MaterialColorComponents.HasFlag(LightComponentsSet.Ambient))
     //	{
     //		GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Ambient, &LightingState->AmbientModelColor.Red);
     //	}
     //
     //	if (MaterialColorComponents.HasFlag(LightComponentsSet.Diffuse))
     //	{
     //		GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Diffuse, &LightingState->DiffuseModelColor.Red);
     //	}
     //
     //	if (MaterialColorComponents.HasFlag(LightComponentsSet.Specular))
     //	{
     //		GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Specular, &LightingState->SpecularModelColor.Red);
     //	}
     //
     //	if (MaterialColorComponents.HasFlag(LightComponentsSet.AmbientAndDiffuse))
     //	{
     //		Flags = ColorMaterialParameter.AmbientAndDiffuse;
     //	}
     //	else if (MaterialColorComponents.HasFlag(LightComponentsSet.Ambient))
     //	{
     //		Flags = ColorMaterialParameter.Ambient;
     //	}
     //	else if (MaterialColorComponents.HasFlag(LightComponentsSet.Diffuse))
     //	{
     //		Flags = ColorMaterialParameter.Diffuse;
     //	}
     //	else if (MaterialColorComponents.HasFlag(LightComponentsSet.Specular))
     //	{
     //		Flags = ColorMaterialParameter.Specular;
     //	}
     //	else
     //	{
     //		//throw (new NotImplementedException("Error! : " + MaterialColorComponents));
     //	}
     //	//flags = GL_SPECULAR;
     //	if (Flags != 0)
     //	{
     //		GL.ColorMaterial(MaterialFace.FrontAndBack, Flags);
     //	}
     //	//glEnable(GL_COLOR_MATERIAL);
     //}
     //
     //GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Emission, &GpuState->LightingState.EmissiveModelColor.Red);
 }
Пример #30
0
        private void PrepareState_Blend(GpuStateStruct gpuState)
        {
            var blendingState = gpuState.BlendingState;

            if (!GL.EnableDisable(GL.GL_BLEND, blendingState.Enabled))
            {
                return;
            }

            //Console.WriteLine("Blend!");

            var openglFunctionSource =
                OpenglGpuImplConversionTables.BlendFuncSrcTranslate[(int)blendingState.FunctionSource];
            //var OpenglFunctionDestination = BlendFuncDstTranslate[(int)BlendingState->FunctionDestination];
            var openglFunctionDestination =
                OpenglGpuImplConversionTables.BlendFuncSrcTranslate[(int)blendingState.FunctionDestination];

            Func <ColorfStruct, int> getBlendFix = (color) =>
            {
                if (color.IsColorf(0, 0, 0))
                {
                    return(GL.GL_ZERO);
                }
                if (color.IsColorf(1, 1, 1))
                {
                    return(GL.GL_ONE);
                }
                return(GL.GL_CONSTANT_COLOR);
            };

            if (blendingState.FunctionSource == GuBlendingFactorSource.GuFix)
            {
                openglFunctionSource = getBlendFix(blendingState.FixColorSource);
            }

            if (blendingState.FunctionDestination == GuBlendingFactorDestination.GuFix)
            {
                if (((int)openglFunctionSource == GL.GL_CONSTANT_COLOR) &&
                    (blendingState.FixColorSource + blendingState.FixColorDestination).IsColorf(1, 1, 1))
                {
                    openglFunctionDestination = GL.GL_ONE_MINUS_CONSTANT_COLOR;
                }
                else
                {
                    openglFunctionDestination = getBlendFix(blendingState.FixColorDestination);
                }
            }
            //Console.WriteLine("{0}, {1}", OpenglFunctionSource, OpenglFunctionDestination);

            var openglBlendEquation =
                OpenglGpuImplConversionTables.BlendEquationTranslate[(int)blendingState.Equation];

            /*
             * Console.WriteLine(
             *  "{0} : {1} -> {2}",
             *  OpenglBlendEquation, OpenglFunctionSource, OpenglFunctionDestination
             * );
             */

            GL.glBlendEquation(openglBlendEquation);
            GL.glBlendFunc(openglFunctionSource, openglFunctionDestination);

            GL.glBlendColor(
                blendingState.FixColorDestination.Red,
                blendingState.FixColorDestination.Green,
                blendingState.FixColorDestination.Blue,
                blendingState.FixColorDestination.Alpha
                );
        }